Check if a program exists from a Makefile

后端 未结 12 1100
时光取名叫无心
时光取名叫无心 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:47

    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.

提交回复
热议问题