Check if a program exists from a Makefile

makefile check if file exists
makefile if
bash check if command exists
makefile test if directory exists
makefile if variable defined
makefile run command if
if not exist makefile
makefile detect shell

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 Make ifeq directive, rather than to define a new variable, and to use the GNU Make error function to stop the build if a required executable is not in ${PATH}. For example, to test for the lzop executable:

 ifeq (, $(shell which lzop))
 $(error "No lzop in $(PATH), consider doing apt-get install lzop")
 endif

If you have several executables to check, then you might want to use a foreach function with the which executable:

EXECUTABLES = ls dd dudu lxop
K := $(foreach exec,$(EXECUTABLES),\
        $(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH")))

Note the use of the := assignment operator that is required in order to force immediate evaluation of the RHS expression. If your Makefile changes the PATH, then instead of the last line above you will need:

        $(if $(shell PATH=$(PATH) which $(exec)),some string,$(error "No $(exec) in PATH")))

This should give you output similar to:

ads$ make
Makefile:5: *** "No dudu in PATH.  Stop.

how can a makefile detect whether a command is available in the , How can I check if a program is callable from a Makefile? (That is, the program should exist in the path or otherwise. 2.1 Running ’make check’ To run tests from an existing collection, first use configure as usual to set up the build directory. Then type make check. If the check target exists, it usually saves you some trouble. For instance, it can set up any auxiliary programs or other files needed by the tests.

I mixed the solutions from @kenorb and @0xF and got this:

DOT := $(shell command -v dot 2> /dev/null)

all:
ifndef DOT
    $(error "dot is not available please install graphviz")
endif
    dot -Tpdf -o pres.pdf pres.dot 

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.

Check If a Command/Executable Exists from Shell Script A Scripter's , Use the type built-in to see if a command is available. sum= for of a hack, but you could test whether each exists and execute them if they do: check if directory exists Hi, I need to prompt for a response from a user to enter a path read dest_dir?"Please Enter Directory :" How do I do this until a valid directory is entered by the user. I can use to check the existence of the directory.

is this what you did?

check: PYTHON-exists
PYTHON-exists: ; @which python > /dev/null
mytarget: check
.PHONY: check PYTHON-exists

credit to my coworker.

gnu make - Check if a program exists from a Makefile, Shell script snippets to check if you have an executable or binary installed in PATH . Table of Contents. Bash Shell; Tcsh Shell. I often need to  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.

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.

YOUR_PROGRAM_VERSION := $(shell your_program --version 2>/dev/null)

all:
ifdef YOUR_PROGRAM_VERSION
    @echo "Found version $(YOUR_PROGRAM_VERSION)"
else
    @echo Not found
endif

As a bonus, the output (such as program version) might be useful in other parts of your Makefile.

[PDF] GNU Make, 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  It's a more trustworthy check that the program runs than merely looking at PATH directories and file permissions. Plus you can get some useful result from your program, such as its version. Of course the drawbacks are that some programs can be heavy to start and some don't have a --version option to immediately (and successfully) exit.

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

#!/bin/bash

NEEDED_COMMANDS="jlex byaccj ant javac"

for cmd in ${NEEDED_COMMANDS} ; do
    if ! command -v ${cmd} &> /dev/null ; then
        echo Please install ${cmd}!
        exit 1
    fi
done

touch .cmd_ok

Makefile

.cmd_ok:
    ./check_cmds.sh

build: .cmd_ok target1 target2

1 More about the command -v technique can be found here.

GNU make, rule is not processed, unless you tell make to do so (with a command If you want make to simply ignore a makefile which does not exist or  parfois, vous avez besoin d'un Makefile pour pouvoir exécuter sur des OS cibles différents et vous voulez que la compilation échoue tôt si un exécutable requis n'est pas dans PATH plutôt que de s'exécuter pendant peut-être longtemps avant d'échouer.

How to Check / Test if a File or Directory Exists in Bash, Shell Function, Substitute the output of a shell command. You need a file called a makefile to tell make what to do. If you want make to simply ignore a makefile which does not exist or cannot be remade, with no error  To check directory existence we will again use stat structure. sys/stat.h header file defines a macro S_ISDIR(), used to check directory existence. The macro accepts stat.st_mode param and returns a non-zero integer if given file is a directory, otherwise zero. Program to check directory existence. /** * C program to check whether a directory

Check if a program exists from a Fish script - shell - php, Bash is a shell that interprets commands. You can use a bash command to check if a file or directory exists. This guide walks you through several  Conditionals affect which lines of the makefile make uses. If the condition is true, make reads the lines of the text-if-true as part of the makefile; if the condition is false, make ignores those lines completely. It follows that syntactic units of the makefile, such as rules, may safely be split across the beginning or the end of the conditional.

Dependency Checking: make vs. Shell Scripts, How can I check if a program exists within a fish script? I know that Even using SHELL := /bin/bash at beginning of the Makefile did not solve the problem. I am trying to instruct GNU Make 3.81 to not stop if a command fails (so I prefix the command with -) but I also want to check the exit status on the next command and print a more informative message. However my Makefile below fails:

Comments
  • Can you invoke a POSIX-compatible shell?
  • Probably not, I guess I could demand one to be there, but it would be much easier if I didn't have to.
  • In the meantime, I solved it by adding a program to the project, which is built first, and whose sole purpose is to check for that other program... :-)
  • The traditional workaround is to have an automake script which checks various prerequisites and writes out a suitable Makefile.
  • If you make EXECUTABLES all variables, (i.e. LS CC LD), and use $($(exec)), you can pass them to make seamlessly from the environment or other makefiles. Useful when cross-compiling.
  • My make chokes at the "," when running "ifeq (, $(shell which lzop))" =/
  • On Windows, use where my_exe 2>NUL instead of which.
  • Beware of TABS indentations with ifeq which is a rule syntax! it must be WITHOUT TAB. Had a hard time with that. stackoverflow.com/a/21226973/2118777
  • If you're using Bash, there's no need to make an external call to which. Use the built-in command -v instead. More info.
  • At least in some systems (i.e. Ubuntu 14.04), $(shell command -v dot) fails with make: command: Command not found. To fix this, redirect stderr output to /dev/null: $(shell command -v dot 2> /dev/null). Explanation
  • command is a bash builtin, for more portability, consider using which ?
  • @JulienPalard I believe you're mistaken: the command utility is required by posix (including the -v option) On the other hand which has no standardized bahaviour (that I know of) and is sometimes outright unusable