Skip to contentSkip to conversation

build: Replace `which` with `command -v`

PATCH Superseded Anthony PERARD 3 participants 4 messages Feb 14, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 build: Replace `which` with `command -v`

The which command is not standard, may not exist on the build host, or may not behave as expected. It is recommanded to use command -v to find out if a command exist and have it's path, and it's part of a POSIX shell standard.

---
 xen/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/Makefile +2 -2
index 21832d6402..767e47d6c7 100644
@@ -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
Signature · 1 line
Anthony PERARD
#1 lore raw
Quoted 15 lines · Anthony PERARD

The which command is not standard, may not exist on the build host, or may not behave as expected. It is recommanded to use command -v to find out if a command exist and have it's path, and it's part of a POSIX shell standard.

---
 xen/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/Makefile +0 -0
index 21832d6402..767e47d6c7 100644

xen/Makefile export XEN_BUILD_HOST := $(shell hostname) · line 30

Quoted 8 lines
xen/Makefile +2 -2
@@ -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)

Shouldn't we go farther with this and also switch the use in xen/build.mk (which looks to be the only other one I can find under xen/ and config/)?

Jan

#2 lore raw
In reply to Anthony PERARD
Quoted 4 lines · Anthony PERARD

The which command is not standard, may not exist on the build host, or may not behave as expected. It is recommanded to use command -v to find out if a command exist and have it's path, and it's part of a POSIX shell standard.

Just to mention it: It hasn't always been. In IEEE Std 1003.1-2004 it was still an optional thing. First mention of it being non-optional looks to be in 1003.1-2008, which is slightly newer than the oldest gcc/binutils we presently support. Still even on the oldest systems I'm still building Xen on every once in a while, the command form is supported. So not an objection, merely a request to be a little more precise in the description of v2.

Jan

Tested-by on v1 1/1
#3 lore raw
In reply to Anthony PERARD
Quoted 6 lines · Anthony PERARD

The which command is not standard, may not exist on the build host, or may not behave as expected. It is recommanded to use command -v to find out if a command exist and have it's path, and it's part of a POSIX shell standard.

This fixes build on fedora 39:

Quoted 23 lines
---
 xen/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/Makefile +2 -2
index 21832d6402..767e47d6c7 100644
@@ -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
Signature · 1 line
Anthony PERARD
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab