From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id OCWcKYfuQmElAQEAgWs5BA (envelope-from ) for ; Thu, 16 Sep 2021 09:13:11 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id WABfJYfuQmEEBQAA1q6Kng (envelope-from ) for ; Thu, 16 Sep 2021 07:13:11 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 16DA318032 for ; Thu, 16 Sep 2021 09:13:11 +0200 (CEST) Received: from localhost ([::1]:59284 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mQla2-0001dC-52 for larch@yhetil.org; Thu, 16 Sep 2021 03:13:10 -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?Q?=E2=80=98optional=E2=80=99?= 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 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1631776391; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=RQgtBOj6RpoAkdnv0gsIZ9lHOtrO+tHIP3lKWfgi0wk=; b=NxvUdXESDjntX3O3CbiYs+MG/akcOCbCc2LtZImZKCVsQ+zMfXw431bPta0/gcrdmoHzd2 SW57ADDLDySRKVEIMawi2z260V6pbp8NwHV38s8Y9XYo1ERBL2N6tYa6aQMpA3RpnE4z6T edn6pyjwlOo2iRBI2t2iQLAmO1Q6jSUo0QmXZ0GCmVFoBYukGDsi+bay2BwDXiXvC6H6KW eP/VNBBulLEY2HoEz8bexTflo7rHwnI8zM0YfblTiWPTaCyO2ePLScCQt8YU7y01RrVuMm 8PIE8kKP7W1Q9uQdosdHApnWgp+Nu5MnWnzMGfU0UPxuPqf5HcXPXEjqInlkWQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1631776391; a=rsa-sha256; cv=none; b=lbI7/1Kzm+dxRr5MAUH7Ge9CO4fORAfwHLqSVu1k/1AgheDeMKgrWJnO563ebsU1xuHrNY GxLprcn147zj1R6SKRL86NvvRHwqVJriqEX4txUr33It74K10RkRqsOZXDjSCIGJ8ZjCQH 3bDHSLwNjRybG5/r0cyGlnI+GgsqlzHK6WwwkPRMWYEyOA3uCqTO6mYpfZEAth2M65Oczn /vfoeUPk8VRvMa7uasYnNYveCs1FP4s/s+E09E8lFBm7o3b3WtZfV8o9I6QhoQu7fBlpIH Ldk4rxYd4etSSF2Keiec6B0Nzlhct05y65F7CYoJhznlWjUcrtc31zjmDdieqw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=yoctocell.xyz header.s=mail header.b=MCPSruK1; dmarc=fail reason="SPF not aligned (relaxed)" header.from=yoctocell.xyz (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: -0.30 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=yoctocell.xyz header.s=mail header.b=MCPSruK1; dmarc=fail reason="SPF not aligned (relaxed)" header.from=yoctocell.xyz (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: 16DA318032 X-Spam-Score: -0.30 X-Migadu-Scanner: scn0.migadu.com X-TUID: 8AvP5xVRFggn * 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 =E2=80=98optional=E2=80=99 macro, one can just write (optional EXPR1 EXPR2) I am not sure if =E2=80=98optional=E2=80=99 is the best name (it was inspir= ed by =E2=80=98lib.optional=E2=80=99 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} packa= ge: `("PATH" ":" prefix ,(list coreutils)))))) @end lisp =20 +@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=20=20=20=20=20=20=20=20=20=20 + +It can be useful when certain targets require an additional configure +flags, e.g., + +@lisp +(arguments + `(#:configure-flags (list "--localstatedir=3D/var" + "--sysconfdir=3D/etc" + ,@@(optional (hurd-target?) '("--with-courage")= ))) +@end lisp + @subsection Build Phases =20 @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 =C2=A9 2020 Efraim Flashner ;;; Copyright =C2=A9 2020, 2021 Maxim Cournoyer ;;; Copyright =C2=A9 2021 Maxime Devos +;;; Copyright =C2=A9 2021 Xinglu Chen ;;; ;;; This file is part of GNU Guix. ;;; @@ -123,7 +124,9 @@ =20 make-desktop-entry-file =20 - locale-category->string)) + locale-category->string + + optional)) =20 ;;; @@ -1613,6 +1616,18 @@ returned." LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE LC_TIME))) =20 + +;;; +;;; 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 =C2=A9 2019 Ricardo Wurmus ;;; Copyright =C2=A9 2021 Maxim Cournoyer ;;; Copyright =C2=A9 2021 Maxime Devos +;;; Copyright =C2=A9 2021 Xinglu Chen ;;; ;;; This file is part of GNU Guix. ;;; @@ -287,5 +288,12 @@ print('hello world')")) ("guile/bin" . ,(dirname (which "guile")))) "guile")))) =20 +(test-equal "optional: expr1 is non-#f" + 'bar + (optional 'foo 'bar)) + +(test-equal "optional: expr1 is #f" + '() + (optional #f 'bar)) =20 (test-end) base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d --=20 2.33.0