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.
Sometimes you need a Makefile to be able to run on different target OS's and you want the build to fail early if a required executable is not in
PATH
rather than to run for a possibly long time before failing.The excellent solution provided by engineerchuan requires making a target. However, if you have many executables to test and your Makefile has many independent targets, each of which requires the tests, then each target requires the test target as a dependency. That makes for a lot of extra typing as well as processing time when you make more than one target at a time.
The solution provided by 0xf can test for an executable without making a target. That saves a lot of typing and execution time when there are multiple targets that can be built either separately or together.
My improvement to the latter solution is to use the
which
executable (where
in Windows), rather than to rely on there being a--version
option in each executable, directly in the GNU Makeifeq
directive, rather than to define a new variable, and to use the GNU Makeerror
function to stop the build if a required executable is not in${PATH}
. For example, to test for thelzop
executable:If you have several executables to check, then you might want to use a
foreach
function with thewhich
executable:Note the use of the
:=
assignment operator that is required in order to force immediate evaluation of the RHS expression. If your Makefile changes thePATH
, then instead of the last line above you will need:This should give you output similar to:
For me all above answers are based on linux and are not working with windows. I'm new to make so my approach may not be ideal. But complete example that works for me on both linux and windows is this:
optionally when I need to detect more tools I can use:
Solved by compiling a special little program in another makefile target, whose sole purpose is to check for whatever runtime stuff I was looking for.
Then, I called this program in yet another makefile target.
It was something like this if I recall correctly:
I mixed the solutions from @kenorb and @0xF and got this:
It works beautifully because "command -v" doesn't print anything if the executable is not available, so the variable DOT never gets defined and you can just check it whenever you want in your code. In this example I'm throwing an error, but you could do something more useful if you wanted.
If the variable is available, "command -v" performs the inexpensive operation of printing the command path, defining the DOT variable.