|
1 | 1 | # 目录
|
| 2 | + |
| 3 | +## Makefile内建函数 |
| 4 | + |
| 5 | +### 文本处理和分析函数 |
| 6 | + |
| 7 | +#### 替换 |
| 8 | +```makefile |
| 9 | +$(subst from,to,text) |
| 10 | +``` |
| 11 | +#### 模式替换 |
| 12 | +> 可用%(只用第一个%有用),如 `$(patsubst %.c,%.o,x.c.c bar.c)`,结果 `‘x.c.o bar.o’` |
| 13 | +```makefile |
| 14 | +$(patsubst pattern,replacement,text) |
| 15 | +``` |
| 16 | +#### 去掉文本两端空格,以及把2个和2个以上的空格换成一个 |
| 17 | +```makefile |
| 18 | +$(strip string) |
| 19 | +``` |
| 20 | +#### 查找 |
| 21 | +```makefile |
| 22 | +$(findstring find,in) |
| 23 | +``` |
| 24 | +#### 过滤 |
| 25 | +> 只保留pattern部分 |
| 26 | +```makefile |
| 27 | +$(filter pattern…,text) |
| 28 | +``` |
| 29 | +#### 过滤掉 |
| 30 | +> 不保留pattern部分 |
| 31 | +```makefile |
| 32 | +$(filter-out pattern…,text) |
| 33 | +``` |
| 34 | +#### 排序 |
| 35 | +```makefile |
| 36 | +$(sort list) |
| 37 | +``` |
| 38 | +#### 取字符串 |
| 39 | +```makefile |
| 40 | +$(word n,text) |
| 41 | +``` |
| 42 | +#### 取字符串列表 |
| 43 | +> 第s(start)个到第e(end)个 |
| 44 | +```makefile |
| 45 | +$(wordlist s,e,text) |
| 46 | +# $(words text) Returns the number of words in text. Thus, the last word of text is $(word $(words text),text). |
| 47 | +``` |
| 48 | +#### 取第一个 |
| 49 | +```makefile |
| 50 | +$(firstword names…) |
| 51 | +``` |
| 52 | +#### 取最后一个 |
| 53 | +```makefile |
| 54 | +$(lastword names…) |
| 55 | +``` |
| 56 | +### 文件名处理函数 |
| 57 | +#### 取目录 |
| 58 | +```makefile |
| 59 | +$(dir names…) |
| 60 | +``` |
| 61 | +#### 取文件 |
| 62 | +> 但并不完全正确,注意观察,因为这个原理是已斜杠“/”为标识符的,如果文件名中包含斜杠,则返回的文件名就有误 |
| 63 | +```makefile |
| 64 | +$(notdir names…) |
| 65 | +``` |
| 66 | +#### 取文件后缀 |
| 67 | +```makefile |
| 68 | +$(suffix names…) |
| 69 | +``` |
| 70 | +#### 取文件名 |
| 71 | +> 包括前面的目录部分,如`$(basename src/foo.c src-1.0/bar hacks)`, 结果为`src/foo src-1.0/bar hacks` |
| 72 | +```makefile |
| 73 | +$(basename names…) |
| 74 | +``` |
| 75 | +#### 添加后缀 |
| 76 | +> example :`$(addprefix src/,foo bar)`,produces the result `‘src/foo src/bar’` |
| 77 | +```makefile |
| 78 | +$(addsuffix suffix,names…) |
| 79 | +``` |
| 80 | +#### 连接函数 |
| 81 | +> example: ‘$(join a b,.c .o)’ produces ‘a.c b.o’. |
| 82 | +```makefile |
| 83 | +$(join list1,list2) |
| 84 | +``` |
| 85 | +#### 通配符函数 |
| 86 | +> 表示可以使用正则表达式的符号。The argument pattern is a file name pattern, typically containing wildcard characters (as in shell file name patterns). The result of wildcard is a space-separated list of the names of existing files that match the pattern |
| 87 | +```makefile |
| 88 | +$(wildcard pattern) |
| 89 | +``` |
| 90 | +#### 真实路径 |
| 91 | +```makefile |
| 92 | +$(realpath names…) |
| 93 | +``` |
| 94 | +#### 绝对路径 |
| 95 | +```makefile |
| 96 | +$(abspath names…) |
| 97 | +``` |
| 98 | + |
| 99 | +### foreach函数 |
| 100 | +`$(foreach var,list,text)`相当于for循环函数,不过最终这里返回的是text的值,这个值是循环得到的一个list,如 |
| 101 | +```makefile |
| 102 | +find_files = $(wildcard $(dir)/*) #“=”等号是延时加载(deferred) |
| 103 | +dirs := a b c d |
| 104 | +files := $(foreach dir,$(dirs),$(find_files)) |
| 105 | +``` |
| 106 | +即 |
| 107 | +```makefile |
| 108 | +files := $(wildcard a/* b/* c/* d/*) |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | +### if函数 |
| 113 | +```makefile |
| 114 | +ifeq (arg1, arg2) |
| 115 | +ifneq (arg1, arg2) |
| 116 | +ifdef variable-name |
| 117 | +ifndef variable-name |
| 118 | +``` |
| 119 | +### call函数 |
| 120 | +```makefile |
| 121 | +$(call VARIABLE,PARAM,PARAM,...) |
| 122 | +``` |
| 123 | +“call”函数是唯一一个可以创建定制化参数函数的引用函数。使用这个函数可以 实现对用户自己定义函数引用。我们可以将一个变量定义为一个复杂的表达式,用“call” 函数根据不同的参数对它进行展开来获得不同的结果。 |
| 124 | + |
| 125 | +如:`reverse = $(2) $(1) foo = $(call reverse,a,b)` foo will contain ‘b a |
| 126 | +
|
| 127 | +### value函数 |
| 128 | +```makefile |
| 129 | +$(value variable) |
| 130 | +``` |
| 131 | +The result of this function is a string containing the value of variable, without any expansion occurring. For example, in this makefile: |
| 132 | +```makefile |
| 133 | +FOO = $PATH |
| 134 | +all: |
| 135 | + @echo $(FOO) |
| 136 | + @echo $(value FOO) |
| 137 | +``` |
| 138 | +The first output line would be ATH, since the `$P` would be expanded as a make variable, while the second output line would be the current value of your $PATH environment variable, since the value function avoided the expansion. |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +### origin函数 |
| 143 | +```makefile |
| 144 | +$(origin variable) |
| 145 | +``` |
| 146 | +获取变量的属性值,如下几个 |
| 147 | +1. undefined 变量“VARIABLE”没有被定义。 |
| 148 | +2. default 变量“VARIABLE”是一个默认定义(内嵌变量)。如“CC”、“MAKE”、“RM”等变 量。如果在 Makefile 中重新定义这些变量,函数返回值将相应发生变化 |
| 149 | +3. environment 变量“VARIABLE”是一个系统环境变量,并且 make 没有使用命令行选项“-e” (Makefile 中不存在同名的变量定义,此变量没有被替代)。 |
| 150 | +4. environment override 变量“VARIABLE”是一个系统环境变量,并且 make 使用了命令行选项“-e”。 Makefile 中存在一个同名的变量定义,使用“make -e”时环境变量值替代了文 件中的变量定义。 |
| 151 | +5. file 变量“VARIABLE”在某一个 makefile 文件中定义。 |
| 152 | +6. command line 变量“VARIABLE”在命令行中定义。 |
| 153 | +7. override 变量“VARIABLE”在 makefile 文件中定义并使用“override”指示符声明。 |
| 154 | +8. automatic 变量“VARIABLE”是自动化变量。 |
| 155 | +### shell函数 |
| 156 | +```makefile |
| 157 | +contents := $(shell cat foo) |
| 158 | +files := $(shell echo *.c) |
| 159 | +``` |
| 160 | +### make LOG以及控制函数 |
| 161 | +```makefile |
| 162 | +$(info text) #打印log |
| 163 | +$(warning text) #和 error 一样,但是 产生致命错误退出 |
| 164 | +$(error text) #产生致命错误,并提示“text”信息给用户,并退出 make 的执行 |
| 165 | +``` |
0 commit comments