How can I check if a program is callable from a Makefile?
(That is, the program should exist in the path or otherwise be callable.)
It could be used to check for which compiler is installed, for instance.
E.g. something like this question, but without assuming the underlying shell is POSIX compatible.
is this what you did?
credit to my coworker.
Assume you have different targets and builders, each requires another set of tools. Set a list of such tools and consider them as target to force checking their availability
For example:
Use the
shell
function to call your program in a way that it prints something to standard output. For example, pass--version
.GNU Make ignores the exit status of the command passed to
shell
. To avoid the potential "command not found" message, redirect standard error to/dev/null
.Then you may check the result using
ifdef
,ifndef
,$(if)
etc.As a bonus, the output (such as program version) might be useful in other parts of your Makefile.
You can use bash built commands such as
type foo
orcommand -v foo
, as below:Where
foo
is your program/command. Redirect to> /dev/null
if you want it silent.Cleaned up some of the existing solutions here...
The
$(info ...)
you can exclude if you want this to be quieter.This will fail fast. No target required.
My solution involves a little helper script1 that places a flag file if all required commands exist. This comes with the advantage that the check for the required commands is only done once and not on every
make
invocation.check_cmds.sh
Makefile
1 More about the
command -v
technique can be found here.