[PATCH core-updates] build: utils: Add ‘optional’ macro.

OpenSubmitted by Xinglu Chen.
Details
5 participants
  • Sarah Morgensen
  • Liliana Marie Prikler
  • Maxime Devos
  • Xinglu Chen
  • zimoun
Owner
unassigned
Severity
normal
X
X
Xinglu Chen wrote on 16 Sep 2021 09:11
[PATCH core-updates] build: utils: Add ‘optional ’ macro.
(address . guix-patches@gnu.org)
b7ee7798dbf384021126a2cd4366b604730181b8.1631776133.git.public@yoctocell.xyz
* guix/build/utils.scm (optional): New syntax
* tests/build-utils.scm ("optional: expr1 is non-#f", optional: expr1 is #f"):
Test it.
* guix.texi (Build Utilities): Document it.
---
A common idiom I have seen is

(if EXPR1
EXPR2
'())

with the ‘optional’ macro, one can just write

(optional EXPR1
EXPR2)

I am not sure if ‘optional’ is the best name (it was inspired by
‘lib.optional’ in Nixpkgs), feedback welcome!

doc/guix.texi | 35 +++++++++++++++++++++++++++++++++++
guix/build/utils.scm | 17 ++++++++++++++++-
tests/build-utils.scm | 8 ++++++++
3 files changed, 59 insertions(+), 1 deletion(-)

Toggle diff (117 lines)
diff --git a/doc/guix.texi b/doc/guix.texi
index 9a3e8ae12c..1bb9ddb397 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -8801,6 +8801,41 @@ in a build phase of the @code{wireguard-tools} package:
         `("PATH" ":" prefix ,(list coreutils))))))
 @end lisp
 
+@subsection Miscellaneous build utilities
+
+@cindex miscellaneous build utilities
+This section documents some miscellaneous utilities that are useful to
+have.
+
+@deffn {Scheme Syntax} optional @var{test} @var{consequent}
+Like @code{when} (@pxref{Conditionals,,, guile, GNU Guile Reference
+Manual}), but if @var{test} evaluates to false, return the empty list.
+This is replaces the following idiom:
+@end deffn
+
+@lisp
+(if @var{test}
+    @var{consequent}
+    '())
+@end lisp
+
+with this:
+
+@lisp
+(optional @var{test}
+          @var{consequent})
+@end lisp          
+
+It can be useful when certain targets require an additional configure
+flags, e.g.,
+
+@lisp
+(arguments
+ `(#:configure-flags (list "--localstatedir=/var"
+                           "--sysconfdir=/etc"
+                           ,@@(optional (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..ecf834461f 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 @@
 
             make-desktop-entry-file
 
-            locale-category->string))
+            locale-category->string
+
+            optional))
 
 
 ;;;
@@ -1613,6 +1616,18 @@ returned."
              LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE
              LC_TIME)))
 
+
+;;;
+;;; Misc.
+;;;
+
+;; If EXPR1 evaluates to a non-#f value, return EXPR2.  Otherwise, return an
+;; empty list.
+(define-syntax optional
+  (syntax-rules ()
+    ((_ expr1 expr2)
+     (if expr1 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..44ad9bafc0 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 @@ print('hello world')"))
           ("guile/bin" . ,(dirname (which "guile"))))
         "guile"))))
 
+(test-equal "optional: expr1 is non-#f"
+  'bar
+  (optional 'foo 'bar))
+
+(test-equal "optional: expr1 is #f"
+  '()
+  (optional #f 'bar))
 
 (test-end)

base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d
-- 
2.33.0
L
L
Liliana Marie Prikler wrote on 16 Sep 2021 18:21
Re: [PATCH core-updates] build: utils: Add ‘optional’ macro.
d4b9d957809ed02dd7c1f85d6519f719ad016abe.camel@gmail.com
Hi,

Am Donnerstag, den 16.09.2021, 09:11 +0200 schrieb Xinglu Chen:
Toggle quote (149 lines)
> * guix/build/utils.scm (optional): New syntax
> * tests/build-utils.scm ("optional: expr1 is non-#f", optional: expr1
> is #f"):
> Test it.
> * guix.texi (Build Utilities): Document it.
> ---
> A common idiom I have seen is
>
> (if EXPR1
> EXPR2
> '())
>
> with the ‘optional’ macro, one can just write
>
> (optional EXPR1
> EXPR2)
>
> I am not sure if ‘optional’ is the best name (it was inspired by
> ‘lib.optional’ in Nixpkgs), feedback welcome!
>
> doc/guix.texi | 35 +++++++++++++++++++++++++++++++++++
> guix/build/utils.scm | 17 ++++++++++++++++-
> tests/build-utils.scm | 8 ++++++++
> 3 files changed, 59 insertions(+), 1 deletion(-)
>
> diff --git a/doc/guix.texi b/doc/guix.texi
> index 9a3e8ae12c..1bb9ddb397 100644
> --- a/doc/guix.texi
> +++ b/doc/guix.texi
> @@ -8801,6 +8801,41 @@ in a build phase of the @code{wireguard-tools}
> package:
> `("PATH" ":" prefix ,(list coreutils))))))
> @end lisp
>
> +@subsection Miscellaneous build utilities
> +
> +@cindex miscellaneous build utilities
> +This section documents some miscellaneous utilities that are useful
> to +have.
> +
> +@deffn {Scheme Syntax} optional @var{test} @var{consequent}
> +Like @code{when} (@pxref{Conditionals,,, guile, GNU Guile Reference
> +Manual}), but if @var{test} evaluates to false, return the empty
> list.
> +This is replaces the following idiom:
> +@end deffn
> +
> +@lisp
> +(if @var{test}
> + @var{consequent}
> + '())
> +@end lisp
> +
> +with this:
> +
> +@lisp
> +(optional @var{test}
> + @var{consequent})
> +@end lisp
> +
> +It can be useful when certain targets require an additional
> configure
> +flags, e.g.,
> +
> +@lisp
> +(arguments
> + `(#:configure-flags (list "--localstatedir=/var"
> + "--sysconfdir=/etc"
> + ,@@(optional (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..ecf834461f 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 @@
>
> make-desktop-entry-file
>
> - locale-category->string))
> + locale-category->string
> +
> + optional))
>
>
> ;;;
> @@ -1613,6 +1616,18 @@ returned."
> LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE
> LC_TIME)))
>
> +
> +;;;
> +;;; Misc.
> +;;;
> +
> +;; If EXPR1 evaluates to a non-#f value, return EXPR2. Otherwise,
> return an
> +;; empty list.
> +(define-syntax optional
> + (syntax-rules ()
> + ((_ expr1 expr2)
> + (if expr1 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..44ad9bafc0 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 @@ print('hello world')"))
> ("guile/bin" . ,(dirname (which "guile"))))
> "guile"))))
>
> +(test-equal "optional: expr1 is non-#f"
> + 'bar
> + (optional 'foo 'bar))
> +
> +(test-equal "optional: expr1 is #f"
> + '()
> + (optional #f 'bar))
>
> (test-end)
>
> base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d

Naming choice aside, I don't think I like the asymmetry behind having a
plain symbol on the one side vs. an empty list. What about
(list-if condition . rest), which evaluates to rest if condition is
true and otherwise nil?
S
S
Sarah Morgensen wrote on 17 Sep 2021 02:37
Re: [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro.
(name . Xinglu Chen)(address . public@yoctocell.xyz)(address . 50614@debbugs.gnu.org)
864kakhwaj.fsf@mgsn.dev
Hi,

Xinglu Chen <public@yoctocell.xyz> writes:

Toggle quote (19 lines)
> * guix/build/utils.scm (optional): New syntax
> * tests/build-utils.scm ("optional: expr1 is non-#f", optional: expr1 is #f"):
> Test it.
> * guix.texi (Build Utilities): Document it.
> ---
> A common idiom I have seen is
>
> (if EXPR1
> EXPR2
> '())
>
> with the ‘optional’ macro, one can just write
>
> (optional EXPR1
> EXPR2)
>
> I am not sure if ‘optional’ is the best name (it was inspired by
> ‘lib.optional’ in Nixpkgs), feedback welcome!

I like the idea! But perhaps if we're getting rid of boilerplate, we
should go all the way and make it so that instead of

(if EXPR1
`(EXPR2 EXPR3)
'())

we can write

(optional EXPR1 EXPR2 EXPR3)

What do you think?

Also, maybe 'list-when' for the name?

Toggle quote (4 lines)
> +@deffn {Scheme Syntax} optional @var{test} @var{consequent}
> +Like @code{when} (@pxref{Conditionals,,, guile, GNU Guile Reference
> +Manual}), but if @var{test} evaluates to false, return the empty list.
> +This is replaces the following idiom:
^ no "is"

Toggle quote (16 lines)
> +@end deffn
> +
> +@lisp
> +(if @var{test}
> + @var{consequent}
> + '())
> +@end lisp
> +
> +with this:
> +
> +@lisp
> +(optional @var{test}
> + @var{consequent})
> +@end lisp
> +
> +It can be useful when certain targets require an additional configure
^ no "an"

Toggle quote (7 lines)
> +flags, e.g.,
> +
> +@lisp
> +(arguments
> + `(#:configure-flags (list "--localstatedir=/var"
> + "--sysconfdir=/etc"
> + ,@@(optional (hurd-target?) '("--with-courage"))))
^ extra "@"?

--
Sarah
X
X
Xinglu Chen wrote on 17 Sep 2021 10:26
[PATCH] build: utils: Add ‘list-when ’ macro.
(address . 50614@debbugs.gnu.org)
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.
---
Changes since v1:

* Rename ‘optional’ to ‘list-when’.

* Make

(list-when test consequent ...)

equivalent to

(if tests
(list consequent ...)
'())

doc/guix.texi | 35 +++++++++++++++++++++++++++++++++++
guix/build/utils.scm | 17 ++++++++++++++++-
tests/build-utils.scm | 8 ++++++++
3 files changed, 59 insertions(+), 1 deletion(-)

Toggle diff (117 lines)
diff --git a/doc/guix.texi b/doc/guix.texi
index 9a3e8ae12c..3b469c04c9 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -8801,6 +8801,41 @@ in a build phase of the @code{wireguard-tools} package:
         `("PATH" ":" prefix ,(list coreutils))))))
 @end lisp
 
+@subsection Miscellaneous
+
+@cindex miscellaneous build utilities
+This section documents some miscellaneous utilities that are useful to
+have.
+
+@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 is 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 an 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 @@
 
             make-desktop-entry-file
 
-            locale-category->string))
+            locale-category->string
+
+            list-when))
 
 
 ;;;
@@ -1613,6 +1616,18 @@ returned."
              LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE
              LC_TIME)))
 
+
+;;;
+;;; 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 @@ print('hello world')"))
           ("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
L
L
Liliana Marie Prikler wrote on 17 Sep 2021 19:55
Re: [PATCH] build: utils: Add ‘list-when’ macro.
(name . Sarah Morgensen)(address . iskarian@mgsn.dev)
eb9d11aa546425b6012feba70a2967b1f9b0b1f4.camel@gmail.com
Am Freitag, den 17.09.2021, 10:26 +0200 schrieb Xinglu Chen:
Toggle quote (19 lines)
> * 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.
> ---
> Changes since v1:
>
> * Rename ‘optional’ to ‘list-when’.
>
> * Make
>
> (list-when test consequent ...)
>
> equivalent to
>
> (if tests
> (list consequent ...)
> '())
LGTM, but let's wait on more opinions. IIRC, changing (guix build)
causes a world rebuild, no? So this patch might go to core-updates
first.
S
S
Sarah Morgensen wrote on 17 Sep 2021 21:15
Re: [bug#50614] [PATCH] build: utils: Add ‘list-w hen’ macro.
(name . Liliana Marie Prikler)(address . liliana.prikler@gmail.com)
86v92zggjs.fsf@mgsn.dev
Hi,

Liliana Marie Prikler <liliana.prikler@gmail.com> writes:

Toggle quote (24 lines)
> Am Freitag, den 17.09.2021, 10:26 +0200 schrieb Xinglu Chen:
>> * 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.
>> ---
>> Changes since v1:
>>
>> * Rename ‘optional’ to ‘list-when’.
>>
>> * Make
>>
>> (list-when test consequent ...)
>>
>> equivalent to
>>
>> (if tests
>> (list consequent ...)
>> '())
> LGTM, but let's wait on more opinions. IIRC, changing (guix build)
> causes a world rebuild, no? So this patch might go to core-updates
> first.

Do we actually use this idiom anywhere build-side? (If not, does it
belong in (guix utils)? Or should we stick it in (guix build utils)
just to be safe?)

--
Sarah
S
S
Sarah Morgensen wrote on 17 Sep 2021 21:19
(name . Xinglu Chen)(address . public@yoctocell.xyz)
86tuijggcr.fsf@mgsn.dev
Hi,

Looking at this again I have a couple more comments.

Xinglu Chen <public@yoctocell.xyz> writes:
Toggle quote (4 lines)
> +@cindex miscellaneous build utilities
> +This section documents some miscellaneous utilities that are useful to
> +have.

"that are useful to have" is implied by its presence in the manual, so
it's unnecessary to write it.

Also, you might consider adding a rule to .dir-locals.el:

(eval . (put list-when 'scheme-indent-function 1))

which would cause it to be indented like this:

(list-when expr1
expr2
expr3)

(I pointed out a couple other minor grammar corrections in the last
review--did you see those?)

--
Sarah
L
L
Liliana Marie Prikler wrote on 17 Sep 2021 21:31
Re: [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
(name . Sarah Morgensen)(address . iskarian@mgsn.dev)
7a8f3586ef49a5a4e5ec66b92581f70262262e27.camel@gmail.com
Am Freitag, den 17.09.2021, 12:15 -0700 schrieb Sarah Morgensen:
Toggle quote (3 lines)
> Do we actually use this idiom anywhere build-side? (If not, does it
> belong in (guix utils)? Or should we stick it in (guix build utils)
> just to be safe?)
I'm pretty sure that this would mostly be used in quasi-quoted contexts
outside of the actual build to build #:configure-flags, #:make-flags
and inputs. However, I can't really say there'd be no use of it inside
builds, even if builds typically don't need to check for conditionals
other than tests?
M
M
Maxime Devos wrote on 18 Sep 2021 16:41
(name . Sarah Morgensen)(address . iskarian@mgsn.dev)
9b662947afe2da5d5243bfa09fa85a1caf9ab2cc.camel@telenet.be
Liliana Marie Prikler schreef op vr 17-09-2021 om 19:55 [+0200]:
Toggle quote (22 lines)
> Am Freitag, den 17.09.2021, 10:26 +0200 schrieb Xinglu Chen:
> > * 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.
> > ---
> > Changes since v1:
> >
> > * Rename ‘optional’ to ‘list-when’.
> >
> > * Make
> >
> > (list-when test consequent ...)
> >
> > equivalent to
> >
> > (if tests
> > (list consequent ...)
> > '())
> LGTM, but let's wait on more opinions.

It looks nice to me, though you might want an opinion from someone who
is new to Scheme.

Greetings,
Maxime.
-----BEGIN PGP SIGNATURE-----

iI0EABYKADUWIQTB8z7iDFKP233XAR9J4+4iGRcl7gUCYUX6pxccbWF4aW1lZGV2
b3NAdGVsZW5ldC5iZQAKCRBJ4+4iGRcl7hIUAPsGwfhTLPq6OgTLxssjGXoewgdE
sqqNd2VLDvfmXypKDAD+LaBJies1pB2XMyqHxOR/kd4wrl2lwo9Xk/FNooMLTAg=
=A59r
-----END PGP SIGNATURE-----


X
X
Xinglu Chen wrote on 19 Sep 2021 15:38
Re: [bug#50614] [PATCH] build: utils: Add ‘list-w hen’ macro.
(name . Sarah Morgensen)(address . iskarian@mgsn.dev)
87y27sofcd.fsf@yoctocell.xyz
On Fri, Sep 17 2021, Liliana Marie Prikler wrote:

Toggle quote (24 lines)
> Am Freitag, den 17.09.2021, 10:26 +0200 schrieb Xinglu Chen:
>> * 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.
>> ---
>> Changes since v1:
>>
>> * Rename ‘optional’ to ‘list-when’.
>>
>> * Make
>>
>> (list-when test consequent ...)
>>
>> equivalent to
>>
>> (if tests
>> (list consequent ...)
>> '())
> LGTM, but let's wait on more opinions. IIRC, changing (guix build)
> causes a world rebuild, no? So this patch might go to core-updates
> first.

Sorry, I forgot to add ‘core-updates’ to the subject prefix.
-----BEGIN PGP SIGNATURE-----

iQJJBAEBCAAzFiEEAVhh4yyK5+SEykIzrPUJmaL7XHkFAmFHPVIVHHB1YmxpY0B5
b2N0b2NlbGwueHl6AAoJEKz1CZmi+1x5q0QQAL38nYOe7WgGzSYl3XvkpzdicOVR
S40Rhc+rh7mdvm9yf+Y7DIG6mtVR7Ea1+lKa8bs63OGgHoM9J2TfZkoZiZwVDwLa
oGHO2zECTG3ESIaJN/1DCo2c5OuG1cOf66vAzEahfKd77SdVzQXKawQF7155958W
NF++n1RK38Zp83pnxqattiNyONXvEoJoYK3o++o0TGUA8hiLKHWIineink3Hu0r/
FHapErErhHC7CB8lLcvManWeVPBU5u6dd0zGtSzB67dPMq8HSMEvUjXM3emJgjUa
wigHyq1WPJS0ac0rqY1jfFoPFvZw2FUiJOGvHRQ5UqEpy9JkhaSENr0v/NadmuUv
YCoe4IO5YtlcDXNHkHJm6TQ2k5JXkXnlooGrznY5uAynqDOvF5UNZn0LSSxGLmk8
OkZDLnOM9KcmAogBENuXzFvH0C14iCKt6v63w6LJtWKjsH71S/5Teup0TR69kFuj
4B+bzMyd+oJJ0fXp8kTa7+ipin1dp7vgHshTbM8gsrpWOieyiyZqxG8OVFB0kirD
OIfyJvpENyJ1gftxn/fXBmUC1TZ8UI7VD9ubx8pGVCFz1lTE0OY/EfIaYh2IsR5g
/oq6kzakaibE+vyKNUg62c3injj47Mmkae2Nj0GvRMXtSeAo0id2RMpYuYL4HsTe
+xVqRcE+uaY1TQp/
=ja/C
-----END PGP SIGNATURE-----

X
X
Xinglu Chen wrote on 19 Sep 2021 15:41
(name . Sarah Morgensen)(address . iskarian@mgsn.dev)
87v92wof7d.fsf@yoctocell.xyz
On Fri, Sep 17 2021, Sarah Morgensen wrote:

Toggle quote (12 lines)
> Hi,
>
> Looking at this again I have a couple more comments.
>
> Xinglu Chen <public@yoctocell.xyz> writes:
>> +@cindex miscellaneous build utilities
>> +This section documents some miscellaneous utilities that are useful to
>> +have.
>
> "that are useful to have" is implied by its presence in the manual, so
> it's unnecessary to write it.

Good point.

Toggle quote (10 lines)
> Also, you might consider adding a rule to .dir-locals.el:
>
> (eval . (put list-when 'scheme-indent-function 1))
>
> which would cause it to be indented like this:
>
> (list-when expr1
> expr2
> expr3)

Yes, that would be a good idea.

Toggle quote (3 lines)
> (I pointed out a couple other minor grammar corrections in the last
> review--did you see those?)

Oops, looks like I missed those. I will send a v2 with the suggestions
applied. :-)
-----BEGIN PGP SIGNATURE-----

iQJJBAEBCAAzFiEEAVhh4yyK5+SEykIzrPUJmaL7XHkFAmFHPgcVHHB1YmxpY0B5
b2N0b2NlbGwueHl6AAoJEKz1CZmi+1x5ZpQP/0/p2m+XQWnl0m7pXDJtBTuGZr5f
01V38qdlazS0Yn7gD7pHbpuMDn7874nDVDndilxfp9bIddwNqLr1lKsFSELi/gsA
ZuvkUO2VfKD2Xw/XmuZLcfG6vwRm22SHIcmjgOZs9QOkb05FRzhe9n8OWVTZypPC
QsvUHZJoiiUX0Qn2eAm+rdAYidY2UNp0H/KbbIF9G8sCM9W43mhytpREpWAq5toY
doohKSeor5lM6fuJeolwvH85VXJ6PoAR98cZ3eWVKvbqTlmWzWFoQtBZIej6VELn
ipukqH4IxE8XIDUXpnuhtNq29CspQ6BM/I/axUdeDBCneqTvZx12UUN3y5NoAW1L
aQNUXZxk1BsUil56UAINCr5B66qjajLmDBTThHoX+W+2EZtq6h5kOLe2gcuSKf7/
IMVE7FXGuLWNQYv6R9smAU0/CBikg8alQuAkaPCv+NwbfYc8gqzbhwiKlmDoo5W3
nPMuc4A+EbyPZV26jeJaavhZr03NP8RnyBWuR9pp68243e2vAnjqQSyqiUCgXX2s
bb/eIBfCI3Y5xyN2ERdD5bgK224kiZkIi/wLOH5DGJGrYuIK6kYevn8l8nb1V9Oi
1HEhk6Byy/DircVXevmXOOn1UlPvcxCTdFvR4VwbwpmyOTJjDdG2Xcm7ZmVQvNSk
FjpvyzTakUHdep2n
=mO9A
-----END PGP SIGNATURE-----

X
X
Xinglu Chen wrote on 19 Sep 2021 15:50
[PATCH core-updates v2] build: utils: Add ‘list-w hen’ macro.
(address . 50614@debbugs.gnu.org)
ccbe07b719e498438a8f610cffa9ba274f6d608d.1632059166.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(-)

Toggle diff (128 lines)
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))
 
 
 ;;;
@@ -1613,6 +1616,18 @@ (define (locale-category->string category)
              LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE
              LC_TIME)))
 
+
+;;;
+;;; 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
M
M
Maxime Devos wrote on 19 Sep 2021 16:35
Re: [bug#50614] [PATCH core-updates v2] build: utils: Add ‘list-when’ macro.
e9e6d5e3d379398e0b113bfd864ae04b7072ced3.camel@telenet.be
Xinglu Chen schreef op zo 19-09-2021 om 15:50 [+0200]:
Toggle quote (7 lines)
> * 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’.
> ---

If you will be doing this in (guix build utils), leading to a world-rebuild,
you light want to define list-unless as well. E.g., 'boost' has

(native-inputs
`(("perl" ,perl)
,@(if (%current-target-system)
'()
`(("python" ,python-wrapper)))
("tcsh" ,tcsh)))

with 'list-when', this could be rewritten to

(native-inputs
`(("perl" ,perl)
,@(list-when (not (%current-target-system))
`("python" ,python-wrapper))
("tcsh" ,tcsh)))

but '(when (not ...) EXP ...)' is simply '(unless EXP ...)',
so it would be nice to be able to do

(native-inputs
`(("perl" ,perl)
,@(list-unless (%current-target-system)
`("python" ,python-wrapper))
("tcsh" ,tcsh)))

Greetings,
Maxime.
-----BEGIN PGP SIGNATURE-----

iI0EABYKADUWIQTB8z7iDFKP233XAR9J4+4iGRcl7gUCYUdKtRccbWF4aW1lZGV2
b3NAdGVsZW5ldC5iZQAKCRBJ4+4iGRcl7oeqAQCisuejRuC9mQo0jx+nCkv847vv
eDaw96SW/LJnWnyIOwD/Q4G1ICKxjEcqYvSrZseNvcsxPd+j23w4TRETuGzsewE=
=wKgS
-----END PGP SIGNATURE-----


Z
Z
zimoun wrote on 20 Sep 2021 13:03
Re: bug#50614: [PATCH core-updates] build: utils: Add ‘optional’ macro.
(name . Xinglu Chen)(address . public@yoctocell.xyz)
871r5jh5kp.fsf_-_@gmail.com
Hi,

On Sun, 19 Sep 2021 at 15:50, Xinglu Chen <public@yoctocell.xyz> wrote:

Toggle quote (6 lines)
> +@lisp
> +(if @var{test}
> + (list @var{consequent} @dots{})
> + '())
> +@end lisp

[...]

Toggle quote (7 lines)
> +@lisp
> +(arguments
> + `(#:configure-flags (list "--localstatedir=/var"
> + "--sysconfdir=/etc"
> + ,@@(list-when (hurd-target?) "--with-courage"))))
> +@end lisp

Personally, I am not convinced it helps the readibility. But that’s a
matter of taste. :-)

My concern is about the coherence. First, ’list-unless’ is also
required by a similar pattern, see for instance:

Toggle snippet (3 lines)
gnu/packages/guile.scm:162: ,@(if (target-mingw?) '() `(("bash" ,bash-minimal)))))

And second, these 2 patterns ’list-when’ and ’list-unless’ are used in
by many files, see guix/build-system, guix/import, guix/describe,
gnu/packages, gnu/system, gnu/services files. For the oneline pattern:
“ag --scheme '@\(if' | grep '()'”. All should be replaced; which
implies a world-rebuild I guess.

Well, all in all, I am not convinced that all this work is worth for a
small debatable readibility improvement. :-)

All the best,
simon
?