Check if a program exists from a Makefile

后端 未结 12 1088
时光取名叫无心
时光取名叫无心 2020-12-12 20:20

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 chec

12条回答
  •  既然无缘
    2020-12-12 20:36

    The solutions checking for STDERR output of --version does not work for programs which print their version to STDOUT instead of STDERR. Instead of checking their output to STDERR or STDOUT, check for the program return code. If the program does not exist, its exit code will always be non zero.

    #!/usr/bin/make -f
    # https://stackoverflow.com/questions/7123241/makefile-as-an-executable-script-with-shebang
    ECHOCMD:=/bin/echo -e
    SHELL := /bin/bash
    
    RESULT := $(shell python --version >/dev/null 2>&1 || (echo "Your command failed with $$?"))
    
    ifeq (,${RESULT})
        EXISTS := true
    else
        EXISTS := false
    endif
    
    all:
        echo EXISTS: ${EXISTS}
        echo RESULT: ${RESULT}
    

提交回复
热议问题