diff mbox series

[XEN,v2] build: Replace `which` with `command -v`

Message ID 20240214180039.57527-1-anthony.perard@citrix.com (mailing list archive)
State New
Headers show
Series [XEN,v2] build: Replace `which` with `command -v` | expand

Commit Message

Anthony PERARD Feb. 14, 2024, 6 p.m. UTC
The `which` command is not standard, may not exist on the build host,
or may not behave as expected by the build system. It is recommended
to use `command -v` to find out if a command exist and have its path,
and it's part of a POSIX shell standard (at least, it seems to be
mandatory since IEEE Std 1003.1-2008, but was optional before).

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
---

Notes:
    v2:
    - also replace it in xen/build.mk
    - mention that it's a mandatory feature since edition 2008.
    - some other rework of the commit message.

 xen/Makefile | 4 ++--
 xen/build.mk | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

Comments

Andrew Cooper Feb. 14, 2024, 6:06 p.m. UTC | #1
On 14/02/2024 6:00 pm, Anthony PERARD wrote:
> The `which` command is not standard, may not exist on the build host,
> or may not behave as expected by the build system. It is recommended
> to use `command -v` to find out if a command exist and have its path,
> and it's part of a POSIX shell standard (at least, it seems to be
> mandatory since IEEE Std 1003.1-2008, but was optional before).
>
> Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
> Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>

Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>

I expect we want fixes tags for c8a8645f1ef (python) and 3b47bcdb6d383
(figlet).  Both my fault it turns out...

Happy to fix on commit.

~Andrew
Jan Beulich Feb. 15, 2024, 9:32 a.m. UTC | #2
On 14.02.2024 19:00, Anthony PERARD wrote:
> The `which` command is not standard, may not exist on the build host,
> or may not behave as expected by the build system. It is recommended
> to use `command -v` to find out if a command exist and have its path,
> and it's part of a POSIX shell standard (at least, it seems to be
> mandatory since IEEE Std 1003.1-2008, but was optional before).
> 
> Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
> Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>

Reviewed-by: Jan Beulich <jbeulich@suse.com>
Anthony PERARD Feb. 16, 2024, 10:37 a.m. UTC | #3
On Wed, Feb 14, 2024 at 06:06:38PM +0000, Andrew Cooper wrote:
> On 14/02/2024 6:00 pm, Anthony PERARD wrote:
> > The `which` command is not standard, may not exist on the build host,
> > or may not behave as expected by the build system. It is recommended
> > to use `command -v` to find out if a command exist and have its path,
> > and it's part of a POSIX shell standard (at least, it seems to be
> > mandatory since IEEE Std 1003.1-2008, but was optional before).
> >
> > Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
> > Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> 
> Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
> 
> I expect we want fixes tags for c8a8645f1ef (python) and 3b47bcdb6d383
> (figlet).  Both my fault it turns out...

Sounds good. I saw this patch as an improvement rather than a bug fix so
I didn't think about fixes tags. It's one less build dependency, so I
guess it's nice to have and backport.

It's common to use `which` to find a path to a binary, and unfortunately
not well known that `command -v` is a more portable alternative. So not
exactly your fault.

It's looks like there's a nice write up about this here:
https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then/85250#85250

Cheers,
diff mbox series

Patch

diff --git a/xen/Makefile b/xen/Makefile
index 21832d6402..767e47d6c7 100644
--- a/xen/Makefile
+++ b/xen/Makefile
@@ -25,8 +25,8 @@  export XEN_BUILD_HOST	:= $(shell hostname)
 endif
 
 # Best effort attempt to find a python interpreter, defaulting to Python 3 if
-# available.  Fall back to just `python` if `which` is nowhere to be found.
-PYTHON_INTERPRETER	:= $(word 1,$(shell which python3 python python2 2>/dev/null) python)
+# available.  Fall back to just `python`.
+PYTHON_INTERPRETER	:= $(word 1,$(shell command -v python3 || command -v python || command -v python2) python)
 export PYTHON		?= $(PYTHON_INTERPRETER)
 
 export CHECKPOLICY	?= checkpolicy
diff --git a/xen/build.mk b/xen/build.mk
index 26dd5a8e87..0f490ca71b 100644
--- a/xen/build.mk
+++ b/xen/build.mk
@@ -1,6 +1,6 @@ 
 quiet_cmd_banner = BANNER  $@
 define cmd_banner
-    if which figlet >/dev/null 2>&1 ; then \
+    if command -v figlet >/dev/null 2>&1 ; then \
 	echo " Xen $(XEN_FULLVERSION)" | figlet -f $< > $@.tmp; \
     else \
 	echo " Xen $(XEN_FULLVERSION)" > $@.tmp; \