Makefile variable as prerequisite

后端 未结 8 614
清歌不尽
清歌不尽 2020-12-07 09:45

In a Makefile, a deploy recipe needs a environment variable ENV to be set to properly execute itself, whereas others don\'t care, e.g.:



        
8条回答
  •  粉色の甜心
    2020-12-07 10:05

    You can create an implicit guard target, that checks that the variable in the stem is defined, like this:

    guard-%:
        @ if [ "${${*}}" = "" ]; then \
            echo "Environment variable $* not set"; \
            exit 1; \
        fi
    

    You then add a guard-ENVVAR target anywhere you want to assert that a variable is defined, like this:

    change-hostname: guard-HOSTNAME
            ./changeHostname.sh ${HOSTNAME}
    

    If you call make change-hostname, without adding HOSTNAME=somehostname in the call, then you'll get an error, and the build will fail.

提交回复
热议问题