diff mbox series

[bug#50614] build: utils: Add ‘list-when’ macro.

Message ID 08db559a4e24a409d332b3552d6a176de6353166.1631867018.git.public@yoctocell.xyz
State New
Headers show
Series [bug#50614] build: utils: Add ‘list-when’ macro. | expand

Checks

Context Check Description
cbaines/applying patch fail View Laminar job
cbaines/issue success View issue

Commit Message

Xinglu Chen Sept. 17, 2021, 8:26 a.m. UTC
* 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(-)


base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d

Comments

Liliana Marie Prikler Sept. 17, 2021, 5:55 p.m. UTC | #1
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.
Sarah Morgensen Sept. 17, 2021, 7:15 p.m. UTC | #2
Hi,

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

> 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
Sarah Morgensen Sept. 17, 2021, 7:19 p.m. UTC | #3
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.

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
Liliana Marie Prikler Sept. 17, 2021, 7:31 p.m. UTC | #4
Am Freitag, den 17.09.2021, 12:15 -0700 schrieb Sarah Morgensen:
> 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 Sept. 18, 2021, 2:41 p.m. UTC | #5
Liliana Marie Prikler schreef op vr 17-09-2021 om 19:55 [+0200]:
> 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.
Xinglu Chen Sept. 19, 2021, 1:38 p.m. UTC | #6
On Fri, Sep 17 2021, Liliana Marie Prikler wrote:

> 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.
Xinglu Chen Sept. 19, 2021, 1:41 p.m. UTC | #7
On Fri, Sep 17 2021, Sarah Morgensen wrote:

> 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.

> 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.

> (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.  :-)
diff mbox series

Patch

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)