Abort makefile if variable not set

后端 未结 5 1226
栀梦
栀梦 2020-12-22 16:11

How could I abort a make/makefile execution based on a makefile\'s variable not being set/valued?

I came up with this, but works only if caller doesn\'t explicitly r

5条回答
  •  一个人的身影
    2020-12-22 16:45

    Use the shell error handling for unset variables (note the double $):

    $ cat Makefile
    foo:
            echo "something is set to $${something:?}"
    
    $ make foo
    echo "something is set to ${something:?}"
    /bin/sh: something: parameter null or not set
    make: *** [foo] Error 127
    
    
    $ make foo something=x
    echo "something is set to ${something:?}"
    something is set to x
    

    If you need a custom error message, add it after the ?:

    $ cat Makefile
    hello:
            echo "hello $${name:?please tell me who you are via \$$name}"
    
    $ make hello
    echo "hello ${name:?please tell me who you are via \$name}"
    /bin/sh: name: please tell me who you are via $name
    make: *** [hello] Error 127
    
    $ make hello name=jesus
    echo "hello ${name:?please tell me who you are via \$name}"
    hello jesus
    

提交回复
热议问题