I want to set a variable if it is empty. I tried in this way:
.... TEST := $(something) ... TEST ?= $(something else)
The first $(something)
may return an empty string, however the conditional assignment ?= works only if the previous variable is not set, not if empty.
Any elegant solution to set the variable if empty?
EDIT I found this solution:
.... TEST := $(something) ... TEST += $(something else) TEST := $(word 1, $(TEST))
but I think that there will be one more elegant.
Check if variable is defined in a Makefilecheck_defined = \ $(strip $(foreach 1,$1, \ $(call __check_defined,$1,$(strip $(value 2))))) __check_defined = \ $(if $(value $1),, \ $(error Undefined $1$(if $2, ($2)))) install: $(call check_defined, var1) $(call check_defined, var2) # do stuff here..
Expanded assignment = defines a recursively-expanded variable. := defines a simply-expanded variable.
The ifeq directive begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared.
A variable is a name defined in a makefile to represent a string of text, called the variable's value. These values are substituted by explicit request into targets, prerequisites, commands, and other parts of the makefile. (In some other versions of make , variables are called macros .)
Now that we understand the basics of using a variable in a Makefile, we’ll be more comfortable understanding the ?= operator, which is used for conditional variable assignment in a Makefile. To that end, let’s extend our Makefile by allowing the user to specify the username to be mentioned in the greeting instead of using the login name.
These values are substituted by explicit request into targets, prerequisites, commands, and other parts of the makefile. (In some other versions of make , variables are called macros .)
Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value. But an explicit assignment in the makefile, or with a command argument, overrides the environment. (If the `-e' flag is specified, then values from the environment override assignments in the makefile.
Here's another alternative that I personally find quite elegant, because it's a one-liner and doesn't need the redundant else-branch:
TEST := $(or $(TEST),$(something else))
Any elegant solution to set the variable if empty?
GNU make is hardly known for elegant solutions. Unless you find trapdoors and minefields to be elegant. I know only of the two ways to accomplish what you want:
The standard ifeq
/endif
solution:
ifeq ($(TEST),) TEST := $(something else) endif
Use the $(if)
function:
TEST := $(if $(TEST),$(TEST),$(something else))
One can try to package that construct into a function too, but that is inadvisable. The function would have the hidden pitfall of occasionally breaking the $(something else)
if it contains the ,
(for which there are only wayward workarounds). (The built-in functions like $(if)
are immune to the ,
bug.)
Elegance test is up to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With