* [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro.
@ 2021-09-16 7:11 Xinglu Chen
2021-09-16 16:21 ` Liliana Marie Prikler
` (2 more replies)
0 siblings, 3 replies; 14+ messages in thread
From: Xinglu Chen @ 2021-09-16 7:11 UTC (permalink / raw)
To: 50614
* 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))
\f
;;;
@@ -1613,6 +1616,18 @@ returned."
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 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
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro.
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
2 siblings, 0 replies; 14+ messages in thread
From: Liliana Marie Prikler @ 2021-09-16 16:21 UTC (permalink / raw)
To: Xinglu Chen, 50614
Hi,
Am Donnerstag, den 16.09.2021, 09:11 +0200 schrieb Xinglu Chen:
> * 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?
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro.
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
2 siblings, 0 replies; 14+ messages in thread
From: Sarah Morgensen @ 2021-09-17 0:37 UTC (permalink / raw)
To: Xinglu Chen; +Cc: 50614
Hi,
Xinglu Chen <public@yoctocell.xyz> writes:
> * 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?
> +@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"
> +@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"
> +flags, e.g.,
> +
> +@lisp
> +(arguments
> + `(#:configure-flags (list "--localstatedir=/var"
> + "--sysconfdir=/etc"
> + ,@@(optional (hurd-target?) '("--with-courage"))))
^ extra "@"?
--
Sarah
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
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 ` Xinglu Chen
2021-09-17 17:55 ` Liliana Marie Prikler
` (2 more replies)
2 siblings, 3 replies; 14+ messages in thread
From: Xinglu Chen @ 2021-09-17 8:26 UTC (permalink / raw)
To: 50614; +Cc: Sarah Morgensen, Liliana Marie Prikler
* 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(-)
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))
\f
;;;
@@ -1613,6 +1616,18 @@ returned."
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 @@ 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
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
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
` (2 more replies)
2021-09-17 19:19 ` Sarah Morgensen
2021-09-19 13:50 ` [bug#50614] [PATCH core-updates v2] " Xinglu Chen
2 siblings, 3 replies; 14+ messages in thread
From: Liliana Marie Prikler @ 2021-09-17 17:55 UTC (permalink / raw)
To: Xinglu Chen, 50614; +Cc: Sarah Morgensen
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.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
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
2 siblings, 1 reply; 14+ messages in thread
From: Sarah Morgensen @ 2021-09-17 19:15 UTC (permalink / raw)
To: Liliana Marie Prikler; +Cc: 50614, Xinglu Chen
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
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
2021-09-17 19:15 ` Sarah Morgensen
@ 2021-09-17 19:31 ` Liliana Marie Prikler
0 siblings, 0 replies; 14+ messages in thread
From: Liliana Marie Prikler @ 2021-09-17 19:31 UTC (permalink / raw)
To: Sarah Morgensen; +Cc: 50614, Xinglu Chen
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?
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
2021-09-17 17:55 ` Liliana Marie Prikler
2021-09-17 19:15 ` Sarah Morgensen
@ 2021-09-18 14:41 ` Maxime Devos
2021-09-19 13:38 ` Xinglu Chen
2 siblings, 0 replies; 14+ messages in thread
From: Maxime Devos @ 2021-09-18 14:41 UTC (permalink / raw)
To: Liliana Marie Prikler, Xinglu Chen, 50614; +Cc: Sarah Morgensen
[-- Attachment #1: Type: text/plain, Size: 753 bytes --]
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.
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
2021-09-17 17:55 ` Liliana Marie Prikler
2021-09-17 19:15 ` Sarah Morgensen
2021-09-18 14:41 ` Maxime Devos
@ 2021-09-19 13:38 ` Xinglu Chen
2 siblings, 0 replies; 14+ messages in thread
From: Xinglu Chen @ 2021-09-19 13:38 UTC (permalink / raw)
To: Liliana Marie Prikler, 50614; +Cc: Sarah Morgensen
[-- Attachment #1: Type: text/plain, Size: 783 bytes --]
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.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 861 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
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:19 ` Sarah Morgensen
2021-09-19 13:41 ` Xinglu Chen
2021-09-19 13:50 ` [bug#50614] [PATCH core-updates v2] " Xinglu Chen
2 siblings, 1 reply; 14+ messages in thread
From: Sarah Morgensen @ 2021-09-17 19:19 UTC (permalink / raw)
To: Xinglu Chen; +Cc: 50614, Liliana Marie Prikler
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
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
2021-09-17 19:19 ` Sarah Morgensen
@ 2021-09-19 13:41 ` Xinglu Chen
0 siblings, 0 replies; 14+ messages in thread
From: Xinglu Chen @ 2021-09-19 13:41 UTC (permalink / raw)
To: Sarah Morgensen; +Cc: 50614, Liliana Marie Prikler
[-- Attachment #1: Type: text/plain, Size: 841 bytes --]
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. :-)
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 861 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH core-updates v2] build: utils: Add ‘list-when’ macro.
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:19 ` Sarah Morgensen
@ 2021-09-19 13:50 ` Xinglu Chen
2021-09-19 14:35 ` Maxime Devos
2021-09-20 11:03 ` [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro zimoun
2 siblings, 2 replies; 14+ messages in thread
From: Xinglu Chen @ 2021-09-19 13:50 UTC (permalink / raw)
To: 50614; +Cc: Sarah Morgensen, Liliana Marie Prikler
* 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
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH core-updates v2] build: utils: Add ‘list-when’ macro.
2021-09-19 13:50 ` [bug#50614] [PATCH core-updates v2] " Xinglu Chen
@ 2021-09-19 14:35 ` Maxime Devos
2021-09-20 11:03 ` [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro zimoun
1 sibling, 0 replies; 14+ messages in thread
From: Maxime Devos @ 2021-09-19 14:35 UTC (permalink / raw)
To: Xinglu Chen, 50614; +Cc: Sarah Morgensen, Liliana Marie Prikler
[-- Attachment #1: Type: text/plain, Size: 1151 bytes --]
Xinglu Chen schreef op zo 19-09-2021 om 15:50 [+0200]:
> * 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.
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro.
2021-09-19 13:50 ` [bug#50614] [PATCH core-updates v2] " Xinglu Chen
2021-09-19 14:35 ` Maxime Devos
@ 2021-09-20 11:03 ` zimoun
1 sibling, 0 replies; 14+ messages in thread
From: zimoun @ 2021-09-20 11:03 UTC (permalink / raw)
To: Xinglu Chen; +Cc: 50614, Liliana Marie Prikler, Sarah Morgensen
Hi,
On Sun, 19 Sep 2021 at 15:50, Xinglu Chen <public@yoctocell.xyz> wrote:
> +@lisp
> +(if @var{test}
> + (list @var{consequent} @dots{})
> + '())
> +@end lisp
[...]
> +@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:
--8<---------------cut here---------------start------------->8---
gnu/packages/guile.scm:162: ,@(if (target-mingw?) '() `(("bash" ,bash-minimal)))))
--8<---------------cut here---------------end--------------->8---
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
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2021-09-20 11:04 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [bug#50614] [PATCH core-updates v2] " Xinglu Chen
2021-09-19 14:35 ` Maxime Devos
2021-09-20 11:03 ` [bug#50614] [PATCH core-updates] build: utils: Add ‘optional’ macro zimoun
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.