From f598c0168bfcb75f718cc8edf990b7a560334405 Mon Sep 17 00:00:00 2001 From: Maxime Devos Date: Mon, 31 May 2021 18:36:09 +0200 Subject: [PATCH 02/18] =?UTF-8?q?build:=20Define=20=E2=80=98search-input-f?= =?UTF-8?q?ile=E2=80=99=20procedure.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The procedure ‘which’ from (guix build utils) is used for two different purposes: 1. for finding the absolute file name of a binary that needs 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 (target=#f in Guix parlance), this is perfectly fine. However, when cross-compiling, there is 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. This commit defines a ‘search-input-file’ procedure. It functions like 'which', but instead of searching in $PATH, it searches in the 'inputs' of the build phase, which must be passed to ‘search-input-file’ as an argument. Also, the file name must include "bin/" or "sbin/" as appropriate. * guix/build/utils.scm (search-input-file): New procedure. * tests/build-utils.scm ("search-input-file: exception if not found") ("search-input-file: can find if existent"): Test it. * doc/guix.texi (File Search): Document it. Partially-Fixes: --- doc/guix.texi | 13 +++++++++++++ guix/build/utils.scm | 9 +++++++++ tests/build-utils.scm | 11 +++++++++++ 3 files changed, 33 insertions(+) diff --git a/doc/guix.texi b/doc/guix.texi index 535e7614fd..f9d2322ea7 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -8661,6 +8661,19 @@ Return the complete file name for @var{program} as found in @code{$PATH}, or @code{#f} if @var{program} could not be found. @end deffn +@deffn {Scheme Procedure} search-input-file @var{inputs} @var{name} +Return the complete file name for @var{name} as found in @var{inputs}. +If @var{name} could not be found, an exception is raised instead. +Here, @var{inputs} is an association list like @var{inputs} and +@var{native-inputs} as available to build phases. + +This procedure can be used for telling @code{wrap-script} and +@code{wrap-program} (currently undocumented) where the Guile +binary or shell binary are located. In fact, that's the +purpose for which @code{search-input-file} has been created +in the first place. +@end deffn + @subsection Build Phases @cindex build phases diff --git a/guix/build/utils.scm b/guix/build/utils.scm index c6731b37ae..2ae8478ef7 100644 --- a/guix/build/utils.scm +++ b/guix/build/utils.scm @@ -80,6 +80,7 @@ search-path-as-string->list list->search-path-as-string which + search-input-file every* alist-cons-before @@ -614,6 +615,14 @@ PROGRAM could not be found." (search-path (search-path-as-string->list (getenv "PATH")) program)) +(define (search-input-file inputs file) + "Find a file named FILE among the INPUTS and return its absolute file name. + +FILE must be a string like \"bin/sh\". If FILE is not found, an exception is +raised." + (or (search-path (map cdr inputs) file) + (error "could not find ~a among the inputs" file))) + ;;; ;;; Phases. diff --git a/tests/build-utils.scm b/tests/build-utils.scm index 31be7ff80f..33685c6468 100644 --- a/tests/build-utils.scm +++ b/tests/build-utils.scm @@ -2,6 +2,7 @@ ;;; Copyright © 2012, 2015, 2016, 2019, 2020 Ludovic Courtès ;;; Copyright © 2019 Ricardo Wurmus ;;; Copyright © 2021 Maxim Cournoyer +;;; Copyright © 2021 Maxime Devos ;;; ;;; This file is part of GNU Guix. ;;; @@ -263,4 +264,14 @@ print('hello world')")) (lambda _ (get-string-all (current-input-port)))))))) +(test-assert "search-input-file: exception if not found" + (not (false-if-exception + (search-input-file '() "does-not-exist")))) + +(test-equal "search-input-file: can find if existent" + (which "guile") + (search-input-file + `(("guile/bin" . ,(dirname (which "guile")))) + "guile")) + (test-end) -- 2.31.1