Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if variables are defined in a makefile

Tags:

I have a GNU Makefile (version 3.81) that looks like the following:

.PHONY: SPOneDot

SPOneDot:
    ifndef X
    X=0.05
    $$(info X undefined, changed to $X)
    endif
    ifndef Y
    Y=0.05
    $$(info Y undefined, changed to $Y)
    endif
    python ./Submit3DSP.py -f OneDot.qdt -x $(X) -y $(Y)

I execute with the following command line: make X=0.1 Y=0.1 SPOneDot but I get the following result:

ifndef X
make: ifndef: Command not found
make: *** [SPOneDot] Error 127

I've looked in the makefile documentation and seen others use it. Any help is appreciated, it's likely something foolish.

like image 312
Troy Rockwood Avatar asked May 23 '13 19:05

Troy Rockwood


People also ask

How do you check if a variable is defined 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 Ifdef in makefile?

The ifdef directive begins the conditional, and specifies the condition. It contains single argument. If the given argument is true then condition becomes true. The ifndef directive begins the conditional, and specifies the condition.

Can we use variables 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.)

Are makefile variables environment variables?

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value. However, an explicit assignment in the makefile, or with a command argument, overrides the environment.


2 Answers

Most likely your make directives must not be tab indented but start in the first column. I also suspect you want .if(...) or similar, not plain ifdef. It's hard to tell without knowing what make implementation you use.

In GNU make, conditional parts are used e.g. like this

ifeq ($(CC),gcc)
        $(CC) -o foo $(objects) $(libs_for_gcc)
else
        $(CC) -o foo $(objects) $(normal_libs)
endif

The GNU make manual has all the details.

If you really mean to test an environment variable (as opposed to a make variable), then simply do so in the commands:

SPOneDot:
    if test -z "$$X"; then X=0.05; echo "X undefined, changed to $$X"; fi; \
    if test -z "$$Y"; then Y=0.05; echo "Y undefined, changed to $$Y"; fi; \
    python ./Submit3DSP.py -f OneDot.qdt -x $$X -y $$Y

Note that $$ is passed to the shell as a single $ and everything must be a single command for the shell, hence the semicolons and backslash/newlines.

like image 112
Jens Avatar answered Oct 22 '22 00:10

Jens


If the line begins with a tab, it will be considered part of a recipe for a rule. Extra spaces are allowed and ignored at the beginning of the conditional directive line, but a tab is not allowed.

like image 30
frido Avatar answered Oct 21 '22 23:10

frido