Subject: [bug#62551] Updated patch - with changes, documentation and tests.

From: Sarthak Shah <[email-masked]>
Date: Tue, 25 Apr 2023 17:52:10 +0530

* doc/guix.texi (--with-configure-flag): Added documentation for
--with-configure-flag
* guix/transformations.scm (transform-package-configure-flag): New
function, changes to %transformations and
show-transformation-options-help/detailed
* tests/transformations.scm (test-equal "options-transformation,
with-configure-flag"): Added a test for --with-configure-flag
---
 doc/guix.texi             | 19 ++++++++++++++
 guix/transformations.scm  | 53 +++++++++++++++++++++++++++++++++++++++
 tests/transformations.scm | 10 +++++++-
 3 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index c49e51b72e..627a468b62 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -12859,6 +12859,25 @@ guix build coreutils
--with-patch=glibc=./glibc-frob.patch
 In this example, glibc itself as well as everything that leads to
 Coreutils in the dependency graph is rebuilt.

+@item --with-configure-flag=@var{package}=@var{configure-flag}
+Add @var{configure-flag} to the list of configure-flags applied to
+the arguments of @var{package}, where @var{package} is a spec such as
+@code{guile@@3.1} or @code{glibc}. The build system of @var{package}
+must support the argument @code{#:configure-flags}.
+
+For example, the command below builds GNU Hello with the
+configure-flag @code{--disable-nls}:
+
+@example
+guix build hello --with-configure-flag=hello=--disable-nls
+@end example
+
+@quotation Warning
+Currently, there is a primitive check for whether the build system
+supports the argument @code{#:configure-flags} or not, however
+users should not rely on it.
+@end quotation
+
 @cindex upstream, latest version
 @item --with-latest=@var{package}
 @itemx --with-version=@var{package}=@var{version}
diff --git a/guix/transformations.scm b/guix/transformations.scm
index 8ff472ad21..27fb0cb646 100644
--- a/guix/transformations.scm
+++ b/guix/transformations.scm
@@ -676,6 +676,53 @@ (define rewrite
         (rewrite obj)
         obj)))

+(define (transform-package-configure-flag specs)
+  "Return a procedure that, when passed a package and a flag, adds the
flag to #:configure-flags in the package's
+'arguments' field."
+  (define (package-with-configure-flag p extra-flag)
+    (package/inherit p
+      (arguments
+       (substitute-keyword-arguments (package-arguments p)
+         ((#:configure-flags list-of-flags (quote '()))
+          #~(cons* #$extra-flag #$list-of-flags))))))
+
+
+  (define %BUILD-SYSTEMS-WITHOUT-CONFIGURE-FLAGS
+    ;; These build systems do not have a #:configure-flags parameter
+'(android-ndk asdf/sbcl asdf/ecl asdf/source cargo channel chicken clojure
copy dub dune elm emacs go guile julia linux-module maven minetest-mod
minify node perl rakudo rebar ruby scons texlive tree-sitter trivial))
+
+  (define (build-system-supports-flags? spec)
+    ;; XXX: a more sophisticated approach could be added that checks the
given build system for a configure-flags option
+    ;; if a new build system is added, it needs to be added to the
%BUILD-SYSTEMS-WITHOUT-CONFIGURE-FLAGS list manually
+    (not (member (build-system-name (package-build-system spec))
+                 %BUILD-SYSTEMS-WITHOUT-CONFIGURE-FLAGS)))
+
+  (define cflags
+    ;; Spec/flag alist.
+     (map (lambda (spec)
+            (match (string-tokenize spec %not-equal)
+              ((spec flag)
+               (cons spec flag))
+              (_
+               (raise (formatted-message
+                       (G_ "~a: invalid package configure-flags
specification")
+                       spec)))))
+          specs))
+
+  (define rewrite
+    (package-input-rewriting/spec
+     (map (match-lambda
+            ((spec . flags)
+             (cons spec (cut package-with-configure-flag <> flags))))
+          cflags)))
+
+  (lambda (obj)
+    (if (and
+         (package? obj)
+         (build-system-supports-flags? obj))
+        (rewrite obj)
+        obj)))
+
 (define (patched-source name source patches)
   "Return a file-like object with the given NAME that applies PATCHES to
 SOURCE.  SOURCE must itself be a file-like object of any type, including
@@ -845,6 +892,7 @@ (define %transformations
     (tune . ,transform-package-tuning)
     (with-debug-info . ,transform-package-with-debug-info)
     (without-tests . ,transform-package-tests)
+    (with-configure-flag . ,transform-package-configure-flag)
     (with-patch  . ,transform-package-patches)
     (with-latest . ,transform-package-latest)
     (with-version . ,transform-package-version)))
@@ -915,6 +963,8 @@ (define micro-architecture
                   (parser 'with-debug-info))
           (option '("without-tests") #t #f
                   (parser 'without-tests))
+          (option '("with-configure-flag") #t #f
+                  (parser 'with-configure-flag))
           (option '("with-patch") #t #f
                   (parser 'with-patch))
           (option '("with-latest") #t #f
@@ -952,6 +1002,9 @@ (define (show-transformation-options-help/detailed)
   (display (G_ "
       --with-patch=PACKAGE=FILE
                          add FILE to the list of patches of PACKAGE"))
+  (display (G_ "
+      --with-configure-flag=PACKAGE=FLAG
+                         add FLAG to the list of #:configure-flags of
PACKAGE"))
   (display (G_ "
       --with-latest=PACKAGE
                          use the latest upstream release of PACKAGE"))
diff --git a/tests/transformations.scm b/tests/transformations.scm
index 1fa2c0bba8..31fd042d31 100644
--- a/tests/transformations.scm
+++ b/tests/transformations.scm
@@ -33,7 +33,7 @@ (define-module (test-transformations)
   #:use-module ((guix gexp)
                 #:select (local-file? local-file-file
                           computed-file? computed-file-gexp
-                          gexp-input-thing))
+                          gexp-input-thing gexp->approximate-sexp))
   #:use-module (guix ui)
   #:use-module (guix utils)
   #:use-module (guix git)
@@ -408,6 +408,14 @@ (define (package-name* obj)
                         (package-full-name grep))
               (package-arguments (package-replacement dep0))))))))

+(test-equal "options->transformation, with-configure-flag"
+  '(cons* "--flag" '())
+  (let* ((p   (dummy-package "foo"
+                (build-system gnu-build-system)))
+         (t   (options->transformation '((with-configure-flag .
"foo=--flag")))))
+    (let ((new (t p)))
+      (gexp->approximate-sexp (cadr (memq #:configure-flags
(package-arguments new)))))))
+
 (test-assert "options->transformation, without-tests"
   (let* ((dep (dummy-package "dep"))
          (p   (dummy-package "foo"
-- 
2.40.0