(address . guix-patches@gnu.org)
The procedure ‘which’ from (guix build utils)
is used for two different purposes:
1. for finding the absolute file name of a binary
that we need to run during the build process
2. for finding the absolute file name of a binary,
for the target system (as in --target=TARGET),
e.g. for substituting sh->/gnu/store/.../bin/sh,
python->/gnu/store/.../bin/python
When compiling natively (SYSTEM=TARGET modulo nix/autotools differences),
this is perfectly fine.
However, when cross-compiling, we have a problem.
"which" looks in $PATH for binaries. That's good for purpose (1),
but incorrect for (2), as the $PATH contains binaries from native-inputs
instead of inputs.
Admittedly, ‘which’ is simply very convenient (although incorrect
when cross-compiling), and we don't have the right tool ...
until now, that is (see patch)!
This patch adds an optional 'inputs' argument. When it is present,
'which' will look in the bin and sbin subdirectories of the directories
in the 'inputs' alist.
Use it like this:
(package [...]
(arguments
`(#:modules (guix build utils)
#:phases
(modify-phases %standard-phases
(delete 'configure)
(delete 'check)
(add-after 'install 'wrap
(lambda* (#:key outputs inputs #:allow-other-keys)
(let ((growpart (string-append (assoc-ref outputs "out")
"/bin/growpart")))
(wrap-program growpart
`("PATH" ":" prefix (,(dirname (which "sfdisk" inputs))
,(dirname (which "readlink" inputs)))))))))))
(Examples comes from the "cloud-utils" package)
The only change is adding adding the 'inputs' argument. Isn't that easy?
Alternative methods I've seen:
* (string-append (assoc-ref inputs "coreutils") "/bin/readlink")
* (let ((coreutils (assoc-ref inputs "coreutils")))
(setenv "PATH" (string-append coreutils "/bin"))
[code using (which "readlink")])
* (which "readlink") ; possibly incorrect when cross-compiling
I've tested this with "cloud-utils", though admittedly I didn't try to cross-compile
yet, and I've placed my adjusted "which" in a separate module to avoid having to rebuild
everything. (The attached patch just modifies (guix build utils).) I've written
a few tests, which pass. I also documented the new functionality in the manual.
Currently incorrect uses of "which" can be fixed in a follow-up patch.
Thoughts?
Maxime.
From 6561c943fa134ca1e2a17e43f8f5498fca9b1560 Mon Sep 17 00:00:00 2001
From: Maxime Devos <maximedevos@telenet.be>
Date: Sun, 18 Apr 2021 13:15:08 +0200
Subject: [PATCH 2/2] =?UTF-8?q?doc:=20Document=20new=20functionality=20of?=
=?UTF-8?q?=20=E2=80=98which=E2=80=99.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* doc/guix.texi (File Search)[which]: Document the optional
'inputs' argument, and give an example on how to use the
procedure.
---
doc/guix.texi | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
Toggle diff (42 lines)
diff --git a/doc/guix.texi b/doc/guix.texi
index 6464fa32cb..365cb13604 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -87,6 +87,7 @@ Copyright @copyright{} 2020 Daniel Brooks@*
Copyright @copyright{} 2020 John Soo@*
Copyright @copyright{} 2020 Jonathan Brielmaier@*
Copyright @copyright{} 2020 Edgar Vincent@*
+Copyright @copyright{} 2021 Maxime Devos@*
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
@@ -8598,11 +8599,25 @@ the root of the Guix source tree:
@result{} ("./libformat.a" "./libstore.a" @dots{})
@end lisp
-@deffn {Scheme Procedure} which @var{program}
+@deffn {Scheme Procedure} which @var{program} [@var{inputs}=#false]
Return the complete file name for @var{program} as found in
-@code{$PATH}, or @code{#f} if @var{program} could not be found.
+@code{$PATH}, or @code{#false} if @var{program} could not be found.
+If @var{INPUTS} is not @code{#false}, instead look in the
+@file{/bin} and @file{/sbin} subdirectories of @var{INPUTS}.
+@var{inputs} is an alist; its keys are ignored."
@end deffn
+Here is an example using the @code{which} procedure in a build phase:
+
+@lisp
+(lambda* (#:key outputs inputs #:allow-other-keys)
+ (let ((growpart (string-append (assoc-ref outputs "out")
+ "/bin/growpart")))
+ (wrap-program growpart
+ `("PATH" ":" prefix (,(dirname (which "sfdisk" inputs))
+ ,(dirname (which "readlink" inputs)))))))
+@end lisp
+
@subsection Build Phases
@cindex build phases
--
2.31.1
-----BEGIN PGP SIGNATURE-----
iI0EABYKADUWIQTB8z7iDFKP233XAR9J4+4iGRcl7gUCYHwV+xccbWF4aW1lZGV2
b3NAdGVsZW5ldC5iZQAKCRBJ4+4iGRcl7r2/AP4hU2XQPn+le3JfU7/cCobGnlXJ
npfGlgydOpvNVuhJeAEAti0f3F1y/3SMnZnZbYl8q4Ub7FexwiYUGEs3UedKPgs=
=hpVX
-----END PGP SIGNATURE-----