Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional variable define in Makefile with ifeq

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 ?

like image 532
user255607 Avatar asked Jun 29 '11 16:06

user255607


People also ask

How do I use IFEQ in Makefile?

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.

What is := in Makefile?

Expanded assignment = defines a recursively-expanded variable. := defines a simply-expanded variable.

How do I check if a variable is empty in Makefile?

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


1 Answers

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.)

like image 194
Beta Avatar answered Sep 18 '22 14:09

Beta