Makefile

发布时间 2023-11-06 15:38:51作者: 皓然123

0 背景

在工作中,经常会与makefile打交道,但是有很多的时候,不明白其中的语法意思,这里主要记录一下常遇到的一些语法。

一、测试模板

很多时候,我们在理解一个知识的时候,有点难理解,我们可以自己写个测试文件来测试下,看看效果,加深理解。

$(warning warning:'this is a log info')

TARGET_BUILD_VARIANT ?= userdebug user

ifneq ($(filter user,$(TARGET_BUILD_VARIANT)),)
$(warning warning:'this is user')
else
$(warning warning:'this is not user')
endif

all:test

test:
        $(warning this is a test)

.PHONY:clean

clean:
        $(warning this is a clean)

将上面的部分,保存为makefile,这样我们就可以使用make 来进行测试了,其结果如下:

make:

makefile:1: warning:'this is a log info'
makefile:8: warning:'this is not user'
makefile:14: this is a test
make: Nothing to be done for 'all'.

#### build completed successfully  ####

make clean:

makefile:1: warning:'this is a log info'
makefile:8: warning:'this is not user'
makefile:19: this is a clean
make: Nothing to be done for 'clean'.

#### build completed successfully  ####

 

二、关键字strip

$(strip STR)
函数名称:strip。
函数功能:去掉字符串中多余的空格符(若干单词,使用若干空字符分割) “STR”开头和结尾的
空字符,并将其中多个连续空字符合并为一个空字符。
返回值:无前导和结尾空字符、使用单一空格分割的多单词字符串。
函数说明:空字符包括空格、[Tab]等不可显示字符。

示例:
STR = a b c
LOSTR = $(strip $(STR))

结果是:
a b c

“strip”函数经常用在条件判断语句的表达式中,确保表达式比较的可靠和健壮!

三、= 、:= 、?= 、+= 的区别

= 是最基本的赋值
:= 是覆盖之前的值
?= 是如果没有被赋值过就赋予等号后面的值
+= 是添加等号后面的值

而 = 与 := 的区别在于,= 会在makefile 展开后再决定变量的值,即最后被指定的值

示例:
x = foo
y = $(x) bar
x = xyz
在上例中,y的值将会是 xyz bar ,而不是 foo bar 。

而:= 表示变量的值决定于它在makefile中的位置,而不是整个makefile展开后的最终值。

在上例中,y的值将会是 foo bar ,而不是 xyz bar