Skip to content

Commit

Permalink
Stop using 'which' command in build scripts
Browse files Browse the repository at this point in the history
These days, it becomes normal where "which" command is not found
in minimal container images such as fedora and archlinux.

Since POSIX 2008, a portable and efficient way of testing command
availability is:

    command -v <command_name>

Use it in build scripts (via make and shell variable WHICH) instead.

Link: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html
Signed-off-by: Akira Yokosawa <[email protected]>
  • Loading branch information
akiyks committed Mar 10, 2023
1 parent 77d9fbc commit fbcd7ed
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
15 changes: 8 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SHELL = /bin/bash
LATEX ?= pdflatex
WHICH = command -v

GITREFSTAGS := $(shell ls -d .git/refs/tags 2>/dev/null)

Expand Down Expand Up @@ -101,16 +102,16 @@ PDFTARGETS_OF_EPSOTHER := $(filter-out $(PDFTARGETS_OF_EPSORIG) $(PDFTARGETS_OF_
BIBSOURCES := bib/*.bib alphapf.bst

# required commands
LATEX_CMD := $(shell which $(LATEX) 2>/dev/null)
DOT := $(shell which dot 2>/dev/null)
FIG2EPS := $(shell which fig2eps 2>/dev/null)
FIG2DEV := $(shell which fig2dev 2>/dev/null)
INKSCAPE := $(shell which inkscape 2>/dev/null)
LATEX_CMD := $(shell $(WHICH) $(LATEX) 2>/dev/null)
DOT := $(shell $(WHICH) dot 2>/dev/null)
FIG2EPS := $(shell $(WHICH) fig2eps 2>/dev/null)
FIG2DEV := $(shell $(WHICH) fig2dev 2>/dev/null)
INKSCAPE := $(shell $(WHICH) inkscape 2>/dev/null)
ifdef INKSCAPE
INKSCAPE_ONE := $(shell inkscape --version 2>/dev/null | grep -c "Inkscape 1")
endif
LATEXPAND := $(shell which latexpand 2>/dev/null)
QPDF := $(shell which qpdf 2>/dev/null)
LATEXPAND := $(shell $(WHICH) latexpand 2>/dev/null)
QPDF := $(shell $(WHICH) qpdf 2>/dev/null)

# required fonts
STEELFONT := $(shell fc-list | grep -c -i steel)
Expand Down
2 changes: 1 addition & 1 deletion a2ping-rule.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# Authors: Akira Yokosawa <[email protected]>

A2PING := $(shell which a2ping 2>/dev/null)
A2PING := $(shell $(WHICH) a2ping 2>/dev/null)

ifdef A2PING
GS_950_OR_LATER := $(shell gs --version | grep -c -E "^(9\.[5-9]|10\.[0-9]).?")
Expand Down
2 changes: 1 addition & 1 deletion epstopdf-rule.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# Authors: Akira Yokosawa <[email protected]>

EPSTOPDF := $(shell which epstopdf 2>/dev/null)
EPSTOPDF := $(shell $(WHICH) epstopdf 2>/dev/null)
GS_953_OR_LATER := $(shell gs --version | grep -c -E "^(9\.5[3-9]|10\.[0-9]).?")
GS_OPT=--gsopt=-dPDFSETTINGS=/ebook

Expand Down
3 changes: 2 additions & 1 deletion utilities/cleverefcheck.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh

: ${GREP:=grep}
: ${WHICH:=command -v}
CREFPTN='\\[Cc](ln)?ref[{][^}]+}\s*[{][^}]+}'

tex_sources_all=`find . -name "*.tex" -print`
Expand Down Expand Up @@ -41,6 +42,6 @@ do
fi
done
if [ $grep_z_opt -eq 0 ] ; then
echo "`which $GREP` doesn't know the -z option."
echo "`$WHICH $GREP` doesn't know the -z option."
echo "Skipping \\crefrange checks."
fi
4 changes: 3 additions & 1 deletion utilities/mpostcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
# follow the instruction given there.
#-------------------------------------------------------------------

: ${WHICH:=command -v}

dogrep() {
texsrc=`find . -name "*.tex" -print`
bibsrc=`find . -name "*.bib" -print`
Expand All @@ -130,7 +132,7 @@ dogrep() {
fi
}

if which kpsewhich >/dev/null
if $WHICH kpsewhich >/dev/null
then
command_list_orig=`kpsewhich -var-value=shell_escape_commands`
command_list=`echo $command_list_orig | sed -E "s/r-u?p?mpost,//g"`
Expand Down
5 changes: 3 additions & 2 deletions utilities/precheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LC_TIME=C
: ${SED:=sed}
: ${DATE:=date}
: ${VERBOSE:=}
: ${WHICH:=command -v}

fatal=""

Expand Down Expand Up @@ -68,7 +69,7 @@ if [ "$sed_result" != "OK" -o "$VERBOSE" != "" ] ; then
if [ "$sed_result" = "OK" ] ; then
echo "OK."
else
echo "$SED (at `which $SED`) failed the test!"
echo "$SED (at `$WHICH $SED`) failed the test!"
fi
fi
if [ "$date_result" != "OK" -o "$VERBOSE" != "" ] ; then
Expand All @@ -79,7 +80,7 @@ if [ "$date_result" != "OK" -o "$VERBOSE" != "" ] ; then
echo -n "$date_flavor ... "
if [ "$date_flavor" = "Unknown" ] ; then
echo
echo "Unknown date command found at `which $DATE`."
echo "Unknown date command found at `$WHICH $DATE`."
else
if [ "$month" = "January" ] ; then
echo "OK."
Expand Down

0 comments on commit fbcd7ed

Please sign in to comment.