UlfR
UlfR

Reputation: 4395

Using conditionals in make

I want to assign a var in the recipe to check for it in another recipe. Can that be done and in that case how?

According to the gmake manual you should be able to use the construct:

ifeq ($(strip $(foo)),)

This example Makefile is producing one OK-line and one ERR-line:

rule: my_file1
ifeq ($(strip $(MY_VAR1)),)
    @echo "ERR: MY_VAR1 is set to $(MY_VAR1) so this line should NOT be shown"
else
    @echo "OK: MY_VAR1 in set to $(MY_VAR1)"
endif
ifeq ($(strip $(MY_VAR2)),)
    @echo "OK: MY_VAR2 is not set so this line should be shown"
else
    @echo "ERR: MY_VAR2 in not set so this line should NOT be shown"
endif

my_file1:
    @echo "Building the file $@"
    # Some complex stuff
    # ...
    # Then assign a var to check in "rule"
    $(eval MY_VAR1+=$@)

The output when running make:

$ make
Building the file my_file1
# Some complex stuff
# ...
# Then assign a var to check in "rule"
ERR: MY_VAR1 is set to my_file1 so this line should NOT be shown
OK: MY_VAR2 is not set so this line should be shown

How do I fix the ifeq-lines so that both of the OK-lines are shown and no ERR-line?

I run gmake 3.81.

Upvotes: 0

Views: 45

Answers (1)

MadScientist
MadScientist

Reputation: 100781

You cannot use ifeq, because ifeq is like a preprocessor statement: it happens while the makefile is being parsed. As a rule of thumb, anything that is outside of a recipe (not indented with TAB) is parsed while the makefile is being read in. Only after all makefiles are parsed, are the recipes run and only when the recipes are run are the contents of the recipe evaluated.

If you want to test something inside a recipe that was the result of running another recipe, you have to either use functions like $(if ...) or else use shell if-statements, not make ifeq etc.

Upvotes: 2

Related Questions