From patchwork Thu Sep 16 07:11:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Xinglu Chen X-Patchwork-Id: 32889 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id DDF3F27BBE3; Thu, 16 Sep 2021 08:13:17 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,T_DKIM_INVALID, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 428CD27BBE1 for ; Thu, 16 Sep 2021 08:13:17 +0100 (BST) Received: from localhost ([::1]:59284 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mQla8-0001dC-9n for patchwork@mira.cbaines.net; Thu, 16 Sep 2021 03:13:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60140) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQlYw-0000QF-Qi for guix-patches@gnu.org; Thu, 16 Sep 2021 03:12:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:42113) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mQlYw-000309-Iw for guix-patches@gnu.org; Thu, 16 Sep 2021 03:12:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mQlYw-00009G-CK for guix-patches@gnu.org; Thu, 16 Sep 2021 03:12:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#50614] [PATCH core-updates] build: utils: Add =?utf-8?b?4oCY?= =?utf-8?b?b3B0aW9uYWzigJk=?= macro. Resent-From: Xinglu Chen Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 16 Sep 2021 07:12:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 50614 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 50614@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.1631776304540 (code B ref -1); Thu, 16 Sep 2021 07:12:02 +0000 Received: (at submit) by debbugs.gnu.org; 16 Sep 2021 07:11:44 +0000 Received: from localhost ([127.0.0.1]:53659 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mQlYd-00008e-R5 for submit@debbugs.gnu.org; Thu, 16 Sep 2021 03:11:44 -0400 Received: from lists.gnu.org ([209.51.188.17]:60654) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mQlYZ-00008V-Ry for submit@debbugs.gnu.org; Thu, 16 Sep 2021 03:11:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60072) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQlYZ-0000Ez-My for guix-patches@gnu.org; Thu, 16 Sep 2021 03:11:39 -0400 Received: from h87-96-130-155.cust.a3fiber.se ([87.96.130.155]:45988 helo=mail.yoctocell.xyz) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQlYU-0002VZ-QY for guix-patches@gnu.org; Thu, 16 Sep 2021 03:11:37 -0400 From: Xinglu Chen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=yoctocell.xyz; s=mail; t=1631776283; bh=ImuW6s/X9UPv4WZ0b/Wdqf+cCK2oZ25rpyb3j7kzl/Y=; h=From:To:Subject:Date; b=MCPSruK1CNLZlcZuk0NSAtUpap77TBgCIB7SXqcS9Yf87Zxa7PwHp1caSgs3yzH3G HRVSvwFUefnA8Ttsc9/c3FldEXfQ/GT9h4sN6Be/ftxhfTsA2hAxCCCnOHSGcIypb3 5bTgvQSxTAJe4u90QYmtgCG8Qzjcu7DXSeaSEpu4= Message-Id: Date: Thu, 16 Sep 2021 09:11:22 +0200 MIME-Version: 1.0 Received-SPF: pass client-ip=87.96.130.155; envelope-from=public@yoctocell.xyz; helo=mail.yoctocell.xyz X-Spam_score_int: 34 X-Spam_score: 3.4 X-Spam_bar: +++ X-Spam_report: (3.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FROM_SUSPICIOUS_NTLD=0.5, FROM_SUSPICIOUS_NTLD_FP=1.999, PDS_OTHER_BAD_TLD=1.999, RDNS_DYNAMIC=0.982, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * 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(-) base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d 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 ;;; Copyright © 2020, 2021 Maxim Cournoyer ;;; Copyright © 2021 Maxime Devos +;;; Copyright © 2021 Xinglu Chen ;;; ;;; 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 ;;; Copyright © 2021 Maxim Cournoyer ;;; Copyright © 2021 Maxime Devos +;;; Copyright © 2021 Xinglu Chen ;;; ;;; 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)