Makefile: ifeq directive compares a special variable with a constant, does not work

I have a little problem with my Makefile. I want to do to change the commands regarding the working directory. I added a conditional directive to the rule that checks the current target directory ($ (* D)).

The fact is that make always goes to the second branch of my test, even when my file is in mySpecialDirectory, and echo really prints "mySpecialDirectory".

.co .cpp.o .cc.o: ifeq ($(*D),mySpecialDirectory) @echo "I'm in mySpecialDirectory! \o/" $(CC) $(DEBUG_FLAGS) $(MYSPECIALFLAGS) -c $< -o $@ else @echo "Failed! I'm in $(*D)" $(CC) $(DEBUG_FLAGS) $(NOTTHATSPECIALFLAGS) -c $< -o $@ endif 
+4
source share
1 answer

This is the expected behavior.

Conditional expressions

All instances of conditional syntax are parsed immediately, completely; this includes the forms ifdef, ifeq, ifndef and ifneq. Of course, this means that automatic variables cannot be used in conditional statements, since automatic variables are not set until the script command for this rule is called . If you need to use automatic variables in a conditional expression, you must use the conditional syntax of the shell in your script proper command, do not execute conventions for these tests.

$(*D) is an automatic variable.

Instead, consider doing:

 .co .cpp.o .cc.o: $(CC) $(DEBUG_FLAGS) $(if $(subst mySpecialDirectory,,$(*D)),$(NOTTHATSPECIALFLAGS),$(MYSPECIALFLAGS)) -c $< -o $@ 

The idea is to abuse $(subst) in some equality testing, replacing mySpecialDirectory an empty string. Then, if the extension $(*D) is equal to mySpecialDirectory , it is completely replaced by an empty string, and the $(if) else part gets an estimate:

$(if condition,then-part[,else-part])

The if function provides support for conditional extension in a functional context (unlike GNU make makefile conditions such as ifeq (see Conditional Expression Syntax).

The first argument, the condition, first has all previous and trailing spaces, and then expanded. If it expands to any non-empty string, then the condition is considered true. If it expands to an empty string, the condition is considered false.

Note the flip in this hack between then-part and else-part .

Hope this helps!

+7
source

All Articles