Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally set Makefile variable to something if it is empty?

Tags:

makefile

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.

like image 561
ocirocir Avatar asked Aug 06 '16 07:08

ocirocir


People also ask

How do I check if a variable is empty in Makefile?

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..

What is := in Makefile?

Expanded assignment = defines a recursively-expanded variable. := defines a simply-expanded variable.

What is IFEQ in Makefile?

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.

What is a variable in makefile?

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 .)

What is the “=” operator in makefile?

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.

How do you substitute variables in a make file?

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 .)

What is the difference between environment variables and make variables?

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.


Video Answer


2 Answers

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))

like image 40
Tomi Aarnio Avatar answered Sep 16 '22 18:09

Tomi Aarnio


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:

  1. The standard ifeq/endif solution:

    ifeq ($(TEST),) TEST := $(something else) endif 
  2. 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.

like image 184
Dummy00001 Avatar answered Sep 17 '22 18:09

Dummy00001