Makefile内嵌函数

subst字符串替换函数

$(subst <from>, <to>, <text>),把<text>中的<from>字符串替换成<to>,返回被替换过的字符串

SRC:=/mnt/hgfs/share/123
DES:=$(subst /mnt,/root,$(SRC))	#ATTENTION:是,/root不是, /root
all:
	@echo  $(SRC)		
	@echo  $(DES)


/mnt/hgfs/share/123
/root/hgfs/share/123

patsubst模式字符串替换函数

$(patsubst <pattern>, <replacement>, <text>)
,查找<text>中的单词(单词以 空格,Tab,回车 分隔)是否符合模式<pattern>,符合的话,则以<replacement>替换,这里,<pattern>可以包括通配符 % 表示任意长度的字符串,如果<replacement>中也包含 %, 则 <replacement>中的这个 % 将是 <pattern>中的那个 % 所代表的字符串

SRC:=/mnt/hgfs/share/123
DES:=$(patsubst %/123,%/456,$(SRC))
all:
	@echo  $(SRC)							#Note:shell可不支持@
	@echo  $(DES)


/mnt/hgfs/share/123
/mnt/hgfs/share/456

strip

$(strip a b c)把字符串去掉开头和结尾的空格,结果是 “a b c”

DES:=$(strip       12,43  )					#参数字符串是从1开始的,能去掉最后一个空格
DES:=$(strip "       12,43  ")				#参数字符串是从空格开始的,能去掉最后一个空格

all:
	@echo  $(DES)


/mnt/hgfs/share/123
 12,43 

findstring查找字符串函数

$(findstring <find>, <in>),在字符串<in>中查找<find>,如果找到,就返回<find>,否则返回空

SRC:=/mnt/hgfs/share/123
DES:=$(findstring /mnt,$(SRC))
all:
	@echo  $(SRC)	
	@echo  $(DES)


/mnt/hgfs/share/123
/mnt

filter过滤函数

$(filter <pattern…>, <text>),以<pattern>模式过滤<text>字符串的单词,保留<pattern模式的单词,可以有多个<pattern>

SRC:= 123.c 789.c 456.s eee.j
#DES:=$(filter %.c,%.s,$(SRC))			#WRONG!!!
DES:=$(filter %.c %.s,$(SRC))

all:
	@echo  $(SRC)					
	@echo  $(DES)


123.c 789.c 456.s eee.j
123.c 789.c 456.s

filter-out反过滤函数

$(filter-out <pattern…> ,<text>)
,以<pattern>模式过滤<text>字符串的单词,去除<pattern>模式的单词,可以有多个<pattern>

SRC:= 123.c 789.c 456.s eee.j
DES:=$(filter-out %.c,$(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
456.s eee.j

sort排序函数

$(sort <list>),给字符串<list>中的单词(不是字符)按升序排序

SRC:= 123.c 789.c 456.s eee.j
DES:=$(sort $(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
123.c 456.s 789.c eee.j

word

$(word <n>,<text>)取单词函数,取字符串<text>中的第<n>个单词,返回字符串<text>中的第n个单词,如果n比<text>中的单词要大,那么返回空字符串

SRC:= 123.c 789.c 456.s eee.j
DES:=$(word 2,$(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
789.c

wordlist取单词串函数

$(wordlist <s>, <e>, <text>)
,从字符串<text>中取从<s>开始到<e>的单词串,<s><e>是一个数字,返回取出的单词串,如果s大于<text>,那么返回空,如果e大于<text>,则返回从s开始到<text>结尾的字符串

SRC:= 123.c 789.c 456.s eee.j
DES:=$(wordlist 2,4,$(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
789.c 456.s eee.j

words单词个数统计函数

$(words <text>),返回单词数

SRC:= 123.c 789.c 456.s eee.j
DES:=$(words $(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
4

firstword首单词函数

$(firstword <text>),返回<text>的第一个单词

SRC:= 123.c 789.c 456.s eee.j
DES:=$(firstword $(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
123.c

dir取目录函数

$(dir <names…>),从每个name中取出目录部分,返回文件所在目录,如果没有/,则返回./

SRC:=/mnt/hgfs/share/123
DES:=$(dir $(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


/mnt/hgfs/share/123
/mnt/hgfs/share/

notdir取文件函数

$(notdir <names…>),从每个name中取出文件部分,返回文件名,如果没有文件,返回空。这里的name必须是变量名,不能是变量的内容,否则什么都不会返回

SRC:=/mnt/hgfs/share/123
DES:=$(notdir $(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


/mnt/hgfs/share/123
123

suffix取后缀函数

$(suffix <names…>),从文件名序列中取出各个文件的后缀,返回后缀,如果没有后缀,返回空

SRC:= 123.c 789.c 456.s eee.j
DES:=$(suffix $(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
.c .c .s .j

basename取前缀函数

$(basename <names…>),从文件名序列中取出各个文件的前缀,返回前缀,如果没有前缀,返回空

SRC:= 123.c 789.c 456.s eee.j
DES:=$(basename $(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
123 789 456 eee

addsuffix

$(addsuffix <suffix>, <names…>)加后缀函数
,将后缀<suffix>加到<names>中每个单词的后面,返回加过后缀的文件名序列

SRC:= 123.c 789.c 456.s eee.j
DES:=$(addsuffix _suf,$(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
123.c_suf 789.c_suf 456.s_suf eee.j_suf

addprefix

$(addprefix <prefix>, <names…>)加前缀函数
,将前缀<suffix>加到<names>中每个单词的后面,返回加过前缀的文件名序列

SRC:= 123.c 789.c 456.s eee.j
DES:=$(addprefix prf_,$(SRC))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
prf_123.c prf_789.c prf_456.s prf_eee.j

join

$(join <list1> , <list2>)连接函数
,把<list2>中的单词对应的加到<list1>的单词的后面,如果<list1>中的单词更多,则多出来的单词保持原样,如果<list2>的单词个数更多,那么多出来的被复制到<list2>

SRC:= 123.c 789.c 456.s eee.j
SRCJOIN:= q e r t y u
DES:=$(join $(SRC),$(SRCJOIN))
all:
	@echo  $(SRC)		
	@echo  $(DES)


123.c 789.c 456.s eee.j
123.cq 789.ce 456.sr eee.jt y u

foreach

$(foreach <var>, <list>, <text>)
<list>中的单词逐一取出放到参数<var>所制定的变量中,然后再执行<text>所包含的表达式,循环过程中,<text>所返回的每个字符串会以空格分隔,最后当整个循环结束时,<text>所返回的每个字符串所组成的整个字符串(以空格分隔)将会是foreach函数的返回值

if

$(if <condition>, <then-part>)
$(if <condition>, <then-part>,<else-part>)
<condition>返回非0即为真

call创建函数的函数

$(call <expression>, <param1>, <param2>, <param3>…)

origin确定变量的出身

$(origin <variable>)
,返回值有 “undefined”, “default”, “environment”, “file”, “command line”, “override” , “automatic”

shell

$(shell <shell-command>)$(<shell-command>)
参数是一个shell命令

error

$(error <text…>)
错误生成函数,<text…>是错误信息

原文地址:https://www.cnblogs.com/xiaojiang1025/p/5955085.html