makefile 函数全解
字符串处理函数
subst 函数—字符串替换
把字符串<text>中所有的<from>字符串替换成<to>
#$(subst <from>,<to>,<text>)
res = $(subst oo,OO,good food)
show:@echo $(res)
#输出
gOOd fOOdpatsubst 函数 — 模式字符串替换
查找<text>中的单词末尾是否符合模式<pattern>,如果匹配的话,则以<replacement>替换。
#$(patsubst <pattern>,<replacement>,<text>)
res = $(patsubst %ood,%OOd,good food)
show:@echo $(res)
#输出
gOOd fOOdstrip 函数 — 去空格
去掉<string>字串中开头和结尾的空字符
#$(strip <string>)
res = $(strip good food time show get set)
show:@echo $(res)
#输出
good food time show get setfindstring 函数 — 查找字符串
在字串<in>中查找<find>字串,如果找到就返回字符串,没找到就返回空
#$(findstring <find>,<in>)
res = $(findstring foo,good food)
show:@echo $(res)#输出
foofilter 函数 — 过滤器
以<pattern>模式过滤<text>字符串中的单词,保留符合模式<pattern>的单词。可以有多个模式,用空格分隔。
#$(filter <pattern...>,<text>)
res = $(filter %d %c %ab,good food bab come luc)
show:@echo $(res)
#输出
good food bab lucfilter-out 函数 — 过滤器
功能和上面相反,去除符合匹配pattern的。以<pattern>模式过滤<text>字符串中的单词,去除符合模式<pattern>的单词。可以有多个模式。
#$(filter-out <pattern...>,<text>)
res = $(filter-out %d %c %ab,good food bab come luc)
show:@echo $(res)
#输出
comesort 函数 — 排序
给字符串<list>中的单词排序(升序)。
#$(sort <list>)
res = $(sort a c z d w y b e)
show:@echo $(res)
#输出
a b c d e w y zword 函数 — 取单词
取字符串<text>中第<n>个单词。(从一开始)
#$(word <n>,<text>)
res = $(word 2,good food time)
show:@echo $(res)
#输出
foodwordlist函数 — 取一串字符串
从字符串<text>中取从<s>开始到<e>的单词串。<s>和<e>是一个数字。
#$(wordlist <s>,<e>,<text>)
res = $(wordlist 2,3,good food time)
show:@echo $(res)
#输出
food timewords 函数 — 计算单词数
统计<text>中字符串中的单词个数
#$(words <text>)
res = $(words good food time)
show:@echo $(res)
#输出
3firstword 函数 — 返回第一个单词
取字符串<text>中的第一个单词。
#$(firstword <text>)
res = $(firstword good food time)
show:@echo $(res)
#输出
goodlastword 函数 — 返回最后一个单词
返回字符串<text>的第一个单词
#$(lastword <text>)
res = $(lastword good food time)
show:@echo $(res)
#输出
time文件名操作函数
dir 函数 — 取文件夹部分
从文件名序列<names>中取出目录部分。目录部分是指最后一个反斜杠(“/”)之前的部分。如果没有反斜杠,那么返回“./”。
#$(dir <names...>)
res = $(dir main.c subdir/subfile.c)
show:@echo $(res)
#输出
./ subdir/notdir 函数 — 取非文件夹部分
从文件名序列<names>中取出非目录部分。非目录部分是指最后一个反斜杠(“ /”)之后的部分。
#$(notdir <names...>)
res = $(dir main.c subdir/subfile.c)
show:@echo $(res)
#输出
main.c subfile.csuffix 函数 — 取出各个文件名的后缀
从文件名序列<names>中取出各个文件名的后缀
#$(suffix <names...>)
res = $(suffix main.c subdir/subfile.c)
show:@echo $(res)
#输出
.c .cbasename 函数 — 取出各个文件名的前缀部
从文件名序列<names>中取出各个文件名的前缀部分。
#$(basename <names...>)
res = $(basename main.c subdir/subfile.c)
show:@echo $(res)
#输出
main subdir/subfileaddsuffix 函数 — 添加后缀
把后缀<suffix>加到<names>中的每个单词后面
#$(addsuffix <suffix>,<names...>)
res = $(addsuffix .c,main subdir/subfile)
show:@echo $(res)
#输出
main.c subdir/subfile.caddprefix 函数 — 添加前缀
把前缀<prefix>加到<names>中的每个单词前面。
#$(addprefix <prefix>,<names...>)
res = $(addprefix ./,main.c subdir/subfile.c)
show:@echo $(res)
#输出
./main.c ./subdir/subfile.cjoin 函数 — 各个单词进行连接
把 <list2> 中的单词对应地加到<list1>的单词后面。如果<list1>的单词个数要比的多,那么,中的多出来的单词将保持原样。如果<list2>的单词个数要比<list1>多,那么,<list2>多出来的单词将被复制到<list2>中。
#$(join <list1>,<list2>)
res = $(join good food time,show look find read get put)
show:@echo $(res)
#输出
goodshow foodlook timefind read get putforeach 函数 — 依次处理字符串
把参数<list>中的单词逐一取出放到参数<var>所指定的变量中,然后再执行<text>所包含的表达式。每一次<text>会返回一个字符串,循环过程中,<text>的所返回的每个字符串会以空格分隔,最后当整个循环结束时,<text>所返回的每个字符串所组成的整个字符串(以空格分隔)将会是 foreach 函数的返回值。
所以,<var>最好是一个变量名,<list>可以是一个表达式,而<text>中一般会使用<var>这个参数来依次枚举<list>中的单词。<var>参数是一个临时的局部变量,foreach 函数执行完后,参数<var>的变量将不在作用。
#$(foreach <var>,<list>,<text>)
names := a b c d
files := $(foreach n,$(names),$(n).o)
show:@echo $(files)
#输出
a.o b.o c.o d.oif 函数 — 分支判断
类似于关键字 ifeq
#$(if <condition>,<then-part>) 或则 $(if <condition>,<then-part>,<else-part>)
num=4
res := $(if num<5,smaller,bigger)
show:@echo $(res) #输出
smallercall 函数 — 调用其他变量
<expression>参数中的变量,如$(1),$(2),$(3)等,会被参数<parm1>,<parm2>,<parm3>依次取代。而<expression>的返回值就是 call 函数的返回值。
#$(call <expression>,<parm1>,<parm2>,<parm3>...)
first = Donald
last = Trump
Donald_Trump = "I'M BACK!"
func = $(1)_$(2)
fullname = $(call func,$(first),$(last))
res := $($(call func,$(first),$(last)))
show:@echo $(fullname)@echo $(res)
#输出
Donald_Trump
I'M BACK!origin 函数 — 变量来源
获取变量来源,<variable> 是变量名,不需要加 $。
undefined :如果
<variable>从来没有定义过,origin 函数返回这个值“undefined”default : 如果
<variable>是一个默认的定义,比如“CC”这个变量file : 如果
<variable>这个变量被定义在 Makefile 中command line : 如果
<variable>这个变量是被命令行定义的override : 如果
<variable>是被 override 指示符重新定义的automatic : 如果
<variable>是一个命令运行中的自动化变量
#$(origin <variable>)shell 函数 — 执行shell命令
用于执行shell 命令
#$(shell <shell command>)
num = 10
res = $(shell echo $(num))
show:@echo $(res)
# 输出
10日志输出函数
用于控制日志输出,以及make执行
$(info 'here is info.')
$(warning 'here is warning.')
$(error 'here is error.')
#输出
'here is info.'
Makefile:3: 'here is warning.'
Makefile:4: *** 'here is error.'. Stop.