I\'m trying to execute a simple if else statement in a Makefile:
check:
if [ -z \"$(APP_NAME)\" ]; then \\
echo \"Empty\" \\
else \\
echo \"Not e
Other answers already pointed out that the problem is combination of makefile design and shell syntax. The design of Makefiles make it really cumbersome to write complex recipes. Often it is better to rethink the process and either rewrite parts of the makefile or put the complexity in a shell script.
Here is example of your recipe put in a shell script:
check:
sh check.sh "$(APP_NAME)"
and the script:
if [ -z "$1" ]; then
echo "Empty"
else
echo "Not empty"
fi
advantage: you have all the power and flexibility of a shell script without any of the makefile awkwardness. You just need to pass the right arguments.
disadvantage: you have aditional files for your build process and your makefile recipes is spread across multiple files.
If the condition is "simple" you might use the conditional construct from make itself. In your case I would argue that it is just barely simple enough to tolerate, but any more complexity and it will go in a shell script.
Here is how to write conditional recipes using makefile features:
check:
ifdef APP_NAME
echo "Empty"
else
echo "Not empty"
endif
again with annotation
check: # target name
ifdef APP_NAME # makefile conditional syntax
echo "Empty" # recipe if condition true
else # makefile conditional syntax
echo "Not empty" # recipe if condition false
endif # makefile conditional syntax
For example if APP_NAME is defined the rule will effectively look like this during execution:
check:
echo "Empty"
This specific example is probably semantically equivalent to your makefile. I cannot say for sure because I did not test thoroughly.
It is important to know that this conditional is evaluated before the recipe is executed. That means the value of variables that get computed values might be different.
advantage: all build commands in one place.
disadvantage: headaches trying to figure out when makefile does variable assignment and evaluation if the conditional does not behave like you expected.
read here for more info:
see also