Conditional judgment in makefile

Conditional statements can control make execution based on the value of a variable or ignore specific parts of the Makefile; conditional statements can be a comparison of two different variables, or a variable and a constant value. It should be noted that the conditional statement can only be used to control the part of the makefile actually executed by make, and it cannot control the execution process of the regular shell command;

There are mainly the following condition judgments in the makefile:

1. Judging whether it is equal to ifeq

ifeq 条件语句
    条件成立时要做的事情
endif
或者
ifeq 条件语句
    条件成立时要做的事情
else
    条件不成立时要做的事情
endif

2. Judging whether it is not equal ifneq

ifneq 条件语句
    条件成立时要做的事情
endif
或者
ifneq 条件语句
    条件成立时要做的事情
else
    条件不成立时要做的事情
endif

The make function can also be used in the conditional statement, for example, use the "strip" function to remove the characters in the variable, and then determine whether to return empty:

ifeq ($(strip $(foo)),)
    #TODO
endif

See the following example:

v1 = 5
v2 = 6
ifeq ($(v1),$(v2))# ifeq 和条件语句之间的空格必须存在!!
	var3 = 1
else
	var3 = 0	
endif
all:
	@$(list)
condition:
	#ifeq($(v1),$(v2))
	#	echo "var1 is equal var2"
	#else
	#	echo "var1 is not equal var2"#if语句不能写在命令行的位置
	#endif
	@echo $(var3)

3. Determine whether the variable has been defined

ifdef VARIABLE-NAME
    #TODO
else
    #TODO
endif

或者:
ifndef VARIABLE-NAME
    #TODO
else
    #TODO
endif

What needs to be explained about "ifdef" is : ifdef only tests whether a variable has a value, and does not perform replacement and expansion on the variable to determine whether the value of the variable is empty . For the variable "VARIABLE-NAME", except for the case of "VARIABLE-NAME=", any other way of defining it will make "ifdef" return true. That is, even if we assign a null value to it by other means (for example, define its value to refer to other variables), "ifdef" will return true; see the following example:

例1:
bar =
foo = $(bar)
ifdef foo
    frobozz = yes
else
    frobozz = no
endif

例 2:
foo =
ifdef foo
    frobozz = yes
else
    frobozz = no
endif

Example 1. frobozz = yes, but the result of example 2 is no, the reason is that in 1, the variable foo is defined through the variable bar, even if the value of bar is empty, the result of ifdef is true; so when we need When judging whether the value of a variable is empty, you need to use ifeq or ifneq instead of ifdef;
 

 

Guess you like

Origin blog.csdn.net/bleauchat/article/details/124225775