chakrit
chakrit

Reputation: 61558

Makefile conditional not respecting target-specific variables

Is there a way to use the native Makefile if-else conditional and also have it respects target-specific variables re-assignments?

Example Makefile:

#!/usr/bin/make

CONFIG = Debug

.PHONY: test printme

test: override CONFIG=Release
test: printme
    @echo "Done."

printme:
ifeq "$(CONFIG)" "Debug"
    @echo "should be DEBUG -> $(CONFIG)"
else
    @echo "should be RELEASE -> $(CONFIG)"
endif

Running make test prints the following output:

should be DEBUG -> Release
Done.

The output I'm looking for is should be RELEASE -> Release how can I achieve that? Do I need to use shell conditionals instead?

Upvotes: 4

Views: 811

Answers (1)

Didier Trosset
Didier Trosset

Reputation: 37477

This behavior seems logical to me: At the time of parsing the Makefile, CONFIG is defined as Debug. The ifeq conditional uses the value of CONFIG it knows at that time. Therefore it chooses the ifeq branch that outputs "Should be DEBUG".

The target-specific variable is only defined as Release with the target test. Its prerequisite printme also knows the target-specific variable and ouputs Release.


May I suggest that you set variable to make on the command line for the purpose you want. It's not many more characters to write when invoking make but brings all you seem to be willing.

make CONFIG=Release

Upvotes: 3

Related Questions