Makefile is set if the variable is empty

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

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

The first $(something) can return an empty string, however conditional assignment? = only works if the previous variable is not set, and not empty.

Any elegant solution to set a variable if empty?


EDIT I found this solution:

 .... TEST := $(something) ... TEST += $(something else) TEST := $(word 1, $(TEST)) 

but I think there will be another elegant one.

+31
makefile
source share
6 answers

Any elegant solution to set a variable if empty?

GNU make is hardly known for elegant solutions. If you do not find elegant curtains and minefields. I know only two ways to accomplish what you want:

  • Standard ifeq / endif solution:

     ifeq ($(TEST),) TEST := $(something else) endif 
  • Use the $(if) function:

     TEST := $(if $(TEST),$(TEST),$(something else)) 

    You can also try to compose this construction into a function, but this is not practical. A function would have a hidden trap, occasionally breaking $(something else) if it contains (for which there are only collapsing workarounds). (Built-in functions like $(if) are immune to error,.)

The elegance test is up to you.

+63
source share

From GNU make , chapter 7.2, Conditional Expression Syntax:

β€œOften you want to check if a variable has a non-empty value. When the value is the result of a complex extension of variables and functions, extensions that you consider empty may actually contain white space characters and therefore not be considered empty. However, you can use the strip function, to avoid interpreting the space as a non-empty value , for example:

 ifeq ($(strip $(foo)),) text-if-empty endif 

will appreciate text-if-empty even if the extension $ (foo) contains whitespace. "

+9
source share

Here is another alternative that I personally find pretty elegant, because it is single-line and does not need an excess else branch:

TEST := $(or $(TEST),$(something else))

+5
source share

In case you need to determine if a variable is undefined or just has an empty value, use the function $ (origin VARNAME) :

 ifeq ($(origin VARNAME),undefined) VARNAME := "now it finally defined" endif 

Note that VARNAME is not surrounded by $() - you are literally giving the variable name.

+1
source share

According to gnu make docs :

If youd like a variable that will only be set to a value if it is not already set, can you use the shorthand operator '? = Instead of '=

0
source share

Just in case, someone accidentally came across a condition in the rule itself. below, as I did, I thought that this could help others.

Suppose in the Makefile we have the following rule with check target, and we need to check if var was passed.

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

To verify this, 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 value of the variable or the default value if it has not been passed to the bash script, which will be responsible for the execution of the logic. something like the following:

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

Below b.sh may look b.sh , although you can add more logic to it.

 #!/bin/sh echo $1 
0
source share

All Articles