views:

791

answers:

1

Hi're,

I have a little issue with my Makefile. I want make to change commands regarding the working directory. I added a conditionnal directive to the rule testing the current target's directory ($(*D)).

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

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

Thanks for your help!

+4  A: 

This is the expected behavior.

Conditional Statements

All instances of conditional syntax are parsed immediately, in their entirety; this includes the ifdef, ifeq, ifndef, and ifneq forms. Of course this means that automatic variables cannot be used in conditional statements, as automatic variables are not set until the command script for that rule is invoked. If you need to use automatic variables in a conditional you must use shell conditional syntax, in your command script proper, for these tests, not make conditionals.

$(*D) is an automatic variable.

Instead, consider doing:

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

The idea is to abuse $(subst) into some equality testing by replacing mySpecialDirectory with the empty string. Then, if $(*D) expansion equals to mySpecialDirectory, it is fully replaced by the empty string and the else-part of $(if) gets evaluated as per:

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

The if function provides support for conditional expansion in a functional context (as opposed to the GNU make makefile conditionals such as ifeq (see Syntax of Conditionals).

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

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

Hope this helps!

Gregory Pakosz
Thanks a lot Gregory, this is exactly what I wanted to do. Great explanation, I clearly wouldn't have thought of using if and subst to do the trick, this is quite clever ;)
Kyone