make 提供了很多函数(包括内置函数和自定义的函数)。这些函数为我们处理变量、文本内容、文件名、命令提供了方便。我们在需要的地方调用函数来处理指定的文本(参数)。函数在调用的地方被替换成它的处理结果。
函数的调用语法
make 内置函数的调用语法(function call syntax)类似于变量的引用(variable reference),它能出现在变量引用可以出现的地方。make 使用和变量引用相同的规则对函数进行展开。
语法格式如下:
$(function arguments)
${function arguments}
1 语法格式中的 function 是函数名。函数名可以是 make 内置函数名,也可以是使用 call 内建函数建立的自定义的函数名。函数名称区分大小写,make 的内置函数都是小写的。
2 语法格式中的 arguments 是函数的参数列表。
3 参数和函数名之间使用若干空白( spaces or tabs 空格或者 Tab 字符,建议使用空格)分隔;如果存在多个参数,参数之间使用逗号(commas ,)分隔。这些空白和逗号(white-space and commas)不是参数列表的一部分,仅仅表示分隔符的作用。
示例 1
Makefile 文件的内容如下
null=
space = $(null) $(null)
string1 ="Functions for Transforming Text"
print:
@echo $(subst $(space),-, $(string1))
测试结果如下
malihou@ubuntu:~$ make
-Functions-for-Transforming-Text
malihou@ubuntu:~$
示例 2
Makefile 文件的内容如下
null=
space = $(null) $(null)
comma =,
lparenthes =(
rparenthes =)
lbrace ={
rbrace =}
# string1 = \(a\) \{b\} c
string1 = $(space)\$(lparenthes)a\$(rparenthes) \$(lbrace)b\$(rbrace) c
print:
@echo $(subst $(space),$(comma),$(string1))
@echo $(subst $(space),$(comma), $(string1))
@echo $(subst $(lparenthes),*,$(string1))
@echo $(subst $(rparenthes),*,$(string1))
@echo $(subst $(lbrace),*,$(string1))
@echo $(subst $(rbrace),*,$(string1))
测试结果如下
malihou@ubuntu:~$ make
,(a),{b},c
,,(a),{b},c
*a){b} c
(a*{b} c
(a)*b} c
(a){b* c
malihou@ubuntu:~$
staticstruct function_table_entry function_table_init[]=
{
/* Name MIN MAX EXP? Function */
FT_ENTRY ("abspath",0,1,1, func_abspath),
FT_ENTRY ("addprefix",2,2,1, func_addsuffix_addprefix),
FT_ENTRY ("addsuffix",2,2,1, func_addsuffix_addprefix),
FT_ENTRY ("basename",0,1,1, func_basename_dir),
FT_ENTRY ("dir",0,1,1, func_basename_dir),
FT_ENTRY ("notdir",0,1,1, func_notdir_suffix),
FT_ENTRY ("subst",3,3,1, func_subst),
FT_ENTRY ("suffix",0,1,1, func_notdir_suffix),
FT_ENTRY ("filter",2,2,1, func_filter_filterout),
FT_ENTRY ("filter-out",2,2,1, func_filter_filterout),
FT_ENTRY ("findstring",2,2,1, func_findstring),
FT_ENTRY ("firstword",0,1,1, func_firstword),
FT_ENTRY ("flavor",0,1,1, func_flavor),
FT_ENTRY ("join",2,2,1, func_join),
FT_ENTRY ("lastword",0,1,1, func_lastword),
FT_ENTRY ("patsubst",3,3,1, func_patsubst),
FT_ENTRY ("realpath",0,1,1, func_realpath),
FT_ENTRY ("shell",0,1,1, func_shell),
FT_ENTRY ("sort",0,1,1, func_sort),
FT_ENTRY ("strip",0,1,1, func_strip),
FT_ENTRY ("wildcard",0,1,1, func_wildcard),
FT_ENTRY ("word",2,2,1, func_word),
FT_ENTRY ("wordlist",3,3,1, func_wordlist),
FT_ENTRY ("words",0,1,1, func_words),
FT_ENTRY ("origin",0,1,1, func_origin),
FT_ENTRY ("foreach",3,3,0, func_foreach),
FT_ENTRY ("call",1,0,1, func_call),
FT_ENTRY ("info",0,1,1, func_error),
FT_ENTRY ("error",0,1,1, func_error),
FT_ENTRY ("warning",0,1,1, func_error),
FT_ENTRY ("if",2,3,0, func_if),
FT_ENTRY ("or",1,0,0, func_or),
FT_ENTRY ("and",1,0,0, func_and),
FT_ENTRY ("value",0,1,1, func_value),
FT_ENTRY ("eval",0,1,1, func_eval),
FT_ENTRY ("file",1,2,1, func_file),
#ifdef EXPERIMENTAL
FT_ENTRY ("eq",2,2,1, func_eq),
FT_ENTRY ("not",0,1,1, func_not),
#endif
};
函数分类 | 个数 |
字符串替换和分析函数(Functions for String Substitution and Analysis) | 12 |
文件名函数(Functions for File Names) | 10 |
条件语句函数(Functions for Conditionals) | 3 |
foreach 函数(The foreach Function) | 1 |
file 函数(The file Function) | 1 |
call 函数(The call Function) | 1 |
value 函数(The value Function) | 1 |
eval 函数(The eval Function) | 1 |
origin 函数(The origin Function) | 1 |
flavor 函数(The flavor Function) | 1 |
make 控制函数(Functions That Control Make) | 3 |
shell 函数(The shell Function) | 1 |
make 的字符串替换和分析函数( Functions for String Substitution and Analysis)是处理字符串的,实现替换、搜索、过滤、排序、取子字符串、取单词等操作。
函数语法 | 函数功能描述 |
$(subst from,to,text) | 字符串替换 |
$(patsubst pattern,replacement,text) | 模式字符串替换 |
$(strip string) | 去空白 |
$(findstring find,in) | 字符串查找 |
$(filter pattern...,text) | 过滤 |
$(filter-out pattern...,text) | 反过滤 |
$(sort list) | 排序 |
$(word n,text) | 取第 n 个位置单词 |
$(wordlist s,e,text) | 取单词列表 |
$(words text) | 单词个数 |
$(firstword names...) | 取首单词 |
$(lastword names...) | 取末尾单词 |
$(subst from,to,text)
1 subst 函数对 text 执行文本替换(textual replacement)操作,使用 to 替换 text 中所有的 from。
示例 3
string="feet on the street"
print:
@echo $(subst ee,EE,$(string))
测试结果如下
malihou@ubuntu:~$ make
fEEt on the strEEt
malihou@ubuntu:~$
patsubst 函数
$(patsubst pattern,replacement,text)
示例 4
Makefile 文件的内容如下
sources =1.c2.c test.c
objects = $(patsubst %.c,%.o,$(sources))
file1 =%1.%.tmp 1%1.%45.%
file2 = $(patsubst \%%.%,%.bak,$(file1))
file3 = $(patsubst %.%,%.bak,$(file1))
print:
@echo"sources=$(sources)"
@echo"objects=$(objects)"
@echo"file1=$(file1)"
@echo"file2=$(file2)"
@echo"file3=$(file3)"
测试结果如下
malihou@ubuntu:~$ make
sources=1.c2.c test.c
objects=1.o2.o test.o
file1=%1.%.tmp 1%1.%45.%
file2=%1.%.tmp 1%1.%45.%
file3=%1.%.tmp 1%1.bak45.bak
malihou@ubuntu:~$
变量的替换引用(substitution references)是 patsubst 函数的简化实现。
$(var:pattern=replacement)
$(var:suffix=replacement)
等同于
$(patsubst pattern,replacement,$(var))
$(patsubst %suffix,%replacement,$(var))
Makefile 文件的内容如下
cpps = foo.cpp bar.cpp baz.cpp
obj1 = $(cpps:cpp=o)
obj2 = $(cpps:.cpp=.o)
obj3 = $(patsubst %cpp,%o,$(cpps))
obj4 = $(patsubst %.cpp,%.o,$(cpps))
print:
@echo obj1=$(obj1)
@echo obj2=$(obj2)
@echo obj3=$(obj3)
@echo obj4=$(obj4)
malihou@ubuntu:~$ make
obj1=foo.o bar.o baz.o
obj2=foo.o bar.o baz.o
obj3=foo.o bar.o baz.o
obj4=foo.o bar.o baz.o
malihou@ubuntu:~$
strip 函数
$(strip string)
示例 6
Makefile 文件的内容如下
string= hello world
ifeq ($(string),hello world)
output_str1 = equal
else
output_str1 =not equal
endif
ifeq ($(strip $(string)),hello world)
output_str2 = equal
else
output_str2 =not equal
endif
print:
@echo"$(string)"
@echo"$(strip $(string))"
@echo"$(output_str1)"
@echo"$(output_str2)"
测试结果如下
malihou@ubuntu:~$ make
hello world
hello world
not equal
equal
malihou@ubuntu:~$
findstring 函数
$(findstring find,in)
示例 7
Makefile 文件的内容如下
string= hello world
ifeq '$(findstring world,$(string))'''
output_str1 = search fail
else
output_str1 = search success
endif
ifeq '$(findstring World,$(string))'''
output_str2 = search fail
else
output_str2 = search success
endif
print:
@echo $(output_str1)
@echo $(output_str2)
测试结果如下
malihou@ubuntu:~$ make
search success
search fail
malihou@ubuntu:~$
filter 函数
$(filter pattern...,text)
filter-out 函数
$(filter-out pattern...,text)
示例 8
Makefile 文件的内容如下
lists =1.c phone.c Camera.o computer.obj 0.mak phone.c
print:
@echo filter=$(filter %.c %.mak,$(lists))
@echo filter_out=$(filter-out%.c %.mak,$(lists))
测试结果如下
malihou@ubuntu:~$ make
filter=1.c phone.c 0.mak phone.c
filter_out=Camera.o computer.obj
malihou@ubuntu:~$
示例 9
Makefile 文件的内容如下
lists =1 phone Camera computer 0 phone
print:
@echo"$(sort $(lists))"
测试结果如下
malihou@ubuntu:~$ make
01Camera computer phone
malihou@ubuntu:~$
VPATH="src:../headers"
print:
@echo CFLAGS=$(patsubst %,-I%,$(subst :,,$(VPATH)))
malihou@ubuntu:~$ make
CFLAGS=-Isrc-I../headers
malihou@ubuntu:~$
函数语法 | 函数功能描述 |
$(dir names...) | 取文件的目录部分 |
$(notdir names...) | 取文件的非目录部分 |
$(suffix names...) | 取文件的后缀 |
$(basename names...) | 取文件的文件名 |
$(addsuffix suffix,names...) | 给文件附加后缀 |
$(addprefix prefix,names...) | 给文件附加前缀 |
$(join list1,list2) | 列表单词连接 |
$(wildcard pattern) | 文件通配 |
$(realpath names...) | 取真实路径 |
$(abspath names...) | 取绝对路径 |