From: Xinglu Chen <public@yoctocell.xyz>
To: 50614@debbugs.gnu.org
Cc: Sarah Morgensen <iskarian@mgsn.dev>,
Liliana Marie Prikler <liliana.prikler@gmail.com>
Subject: [bug#50614] [PATCH core-updates v2] build: utils: Add ‘list-when’ macro.
Date: Sun, 19 Sep 2021 15:50:41 +0200 [thread overview]
Message-ID: <ccbe07b719e498438a8f610cffa9ba274f6d608d.1632059166.git.public@yoctocell.xyz> (raw)
In-Reply-To: <08db559a4e24a409d332b3552d6a176de6353166.1631867018.git.public@yoctocell.xyz>
* guix/build/utils.scm (list-when): New syntax
* tests/build-utils.scm ("list-when: expr1 is non-#f", list-when: expr1 is #f"):
Test it.
* guix.texi (Build Utilities): Document it.
* .dir-locals.el: Set ‘scheme-indent-function’ to 1 for ‘list-when’.
---
Changes since v1:
* Add an entry for ‘list-when’ in .dir-locals.el.
* Some fixes to the documentation.
.dir-locals.el | 1 +
doc/guix.texi | 34 ++++++++++++++++++++++++++++++++++
guix/build/utils.scm | 17 ++++++++++++++++-
tests/build-utils.scm | 8 ++++++++
4 files changed, 59 insertions(+), 1 deletion(-)
diff --git a/.dir-locals.el b/.dir-locals.el
index 919ed1d1c4..554044ee8c 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -56,6 +56,7 @@ ((nil
(eval . (put 'lambda* 'scheme-indent-function 1))
(eval . (put 'substitute* 'scheme-indent-function 1))
(eval . (put 'match-record 'scheme-indent-function 2))
+ (eval . (put 'list-when 'scheme-indent-function 1))
;; 'modify-inputs' and its keywords.
(eval . (put 'modify-inputs 'scheme-indent-function 1))
diff --git a/doc/guix.texi b/doc/guix.texi
index 9a3e8ae12c..4bea366b8e 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -8801,6 +8801,40 @@
`("PATH" ":" prefix ,(list coreutils))))))
@end lisp
+@subsection Miscellaneous
+
+@cindex miscellaneous build utilities
+This section documents some miscellaneous utilities.
+
+@deffn {Scheme Syntax} list-when @var{test} @var{consequent} @dots{}
+Like @code{when} (@pxref{Conditionals,,, guile, GNU Guile Reference
+Manual}), but if @var{test} evaluates to true, return @code{(list
+@var{consequent} @dots{})}, and if @var{test} evaluates to false, return
+the empty list. This replaces the following idiom:
+@end deffn
+
+@lisp
+(if @var{test}
+ (list @var{consequent} @dots{})
+ '())
+@end lisp
+
+with this:
+
+@lisp
+(list-when @var{test} @var{consequent} @dots{})
+@end lisp
+
+It can be useful when certain targets require additional configure
+flags, e.g.,
+
+@lisp
+(arguments
+ `(#:configure-flags (list "--localstatedir=/var"
+ "--sysconfdir=/etc"
+ ,@@(list-when (hurd-target?) "--with-courage"))))
+@end lisp
+
@subsection Build Phases
@cindex build phases
diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index 3beb7da67a..d3fb207ee5 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -8,6 +8,7 @@
;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
+;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -123,7 +124,9 @@ (define-module (guix build utils)
make-desktop-entry-file
- locale-category->string))
+ locale-category->string
+
+ list-when))
\f
;;;
@@ -1613,6 +1616,18 @@ (define (locale-category->string category)
LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE
LC_TIME)))
+\f
+;;;
+;;; Misc.
+;;;
+
+;; If EXPR1 evaluates to a non-#f value, return '(EXPR2 ...). Otherwise,
+;; return an empty list.
+(define-syntax list-when
+ (syntax-rules ()
+ ((_ expr1 expr2 ...)
+ (if expr1 (list expr2 ...) '()))))
+
;;; Local Variables:
;;; eval: (put 'call-with-output-file/atomic 'scheme-indent-function 1)
;;; eval: (put 'call-with-ascii-input-file 'scheme-indent-function 1)
diff --git a/tests/build-utils.scm b/tests/build-utils.scm
index 6b131c0af8..b558feb47d 100644
--- a/tests/build-utils.scm
+++ b/tests/build-utils.scm
@@ -3,6 +3,7 @@
;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
+;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -287,5 +288,12 @@ (define-module (test build-utils)
("guile/bin" . ,(dirname (which "guile"))))
"guile"))))
+(test-equal "list-when: expr1 is non-#f"
+ (list 3 'bar)
+ (list-when 'foo (+ 1 2) 'bar))
+
+(test-equal "list-when: expr1 is #f"
+ '()
+ (list-when #f (+ 2 3) 'bar))
(test-end)
base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d
--
2.33.0
next prev parent reply other threads:[~2021-09-19 13:51 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-16 7:11 [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro Xinglu Chen
2021-09-16 16:21 ` Liliana Marie Prikler
2021-09-17 0:37 ` Sarah Morgensen
2021-09-17 8:26 ` [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro Xinglu Chen
2021-09-17 17:55 ` Liliana Marie Prikler
2021-09-17 19:15 ` Sarah Morgensen
2021-09-17 19:31 ` Liliana Marie Prikler
2021-09-18 14:41 ` Maxime Devos
2021-09-19 13:38 ` Xinglu Chen
2021-09-17 19:19 ` Sarah Morgensen
2021-09-19 13:41 ` Xinglu Chen
2021-09-19 13:50 ` Xinglu Chen [this message]
2021-09-19 14:35 ` [bug#50614] [PATCH core-updates v2] " Maxime Devos
2021-09-20 11:03 ` [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro zimoun
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ccbe07b719e498438a8f610cffa9ba274f6d608d.1632059166.git.public@yoctocell.xyz \
--to=public@yoctocell.xyz \
--cc=50614@debbugs.gnu.org \
--cc=iskarian@mgsn.dev \
--cc=liliana.prikler@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/guix.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.