I am trying to define variables in a Makefile, according to conditions. As ifeq can be run only in rules, I have added an additional rule (def_rule) I refer to for each rule.
Example:
def_rule:
ifeq ($(TARGET), android)
CC=arm-linux-androideabi-gcc
else
echo "native build"
endf
all: def_rule tp xi_eid_chipset.o
Unfortunately, invoking make all returns this:
ifeq (linux, android)
/bin/sh: Syntax error: word unexpected (expecting ")")
make: *** [def_rule] Error 2
I cannot figure out why. I have just followed examples in GNU Make documentation.
Do you know how to do conditional defines in Makefiles ?
The ifeq directive begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared.
Expanded assignment = defines a recursively-expanded variable. := defines a simply-expanded variable.
Check if variable is defined in a Makefilecheck_defined = \ $(strip $(foreach 1,$1, \ $(call __check_defined,$1,$(strip $(value 2))))) __check_defined = \ $(if $(value $1),, \ $(error Undefined $1$(if $2, ($2)))) install: $(call check_defined, var1) $(call check_defined, var2) # do stuff here..
Conditionals can be outside of rules:
ifeq ($(TARGET), android)
$(info Android)
CC=arm-linux-androideabi-gcc
else
$(info native build)
CC=something else
endif
(Note that I've tossed in a few leading spaces, just to make it easier to read-- they are neither necessary nor harmful.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With