Makefile set if variable is empty

前端 未结 6 469
暖寄归人
暖寄归人 2021-01-31 01:37

I want to set a variable if it is empty. I tried in this way:

....
TEST := $(something)
...
TEST ?= $(something else)

The first $(somethi

6条回答
  •  鱼传尺愫
    2021-01-31 01:54

    Just in case anyone stumbled upon putting the condition in the rule itself. below how I did it, thought it might help others.

    In Makefile, suppose we have the following rule with check target and we need to check whether var was passed.

    check:
        @[ "${var}" ] && echo "all good" || ( echo "var is not set"; exit 1 )
    

    To test this out, run the following commands

    $ make check 
      var is not set
      make: *** [check] Error 1  
    
    $ make check var=test
      all good 
    

    So, Now we can pass the variable value or a default value in case it was not passed to a bash script that will be responsible to do the logic. something like the following:

    @[ "${var}" ] && ./b.sh ${var} || ./b.sh 'ss'
    

    Here's below what b.sh might look like, though you can add more logic to it.

    #!/bin/sh
    echo $1
    

提交回复
热议问题