Make
概述
基本语法
条件语句
ifeq ($(CC),gcc)
libs=$(libs_for_gcc)
else
libs=$(normal_libs)
endif
循环语句
LIST = one two three
all:
for i in $(LIST); do \
echo $$i; \
done
变量
内置变量
详见:https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
常用的,
自动变量
- $@
$@指代当前目标,就是Make命令当前构建的那个目标。比如,make foo的 $@ 就指代 foo。
a.txt b.txt: touch $@
等同于下面的写法。
> ```bash
a.txt:
touch a.txt
b.txt:
touch b.txt
(2)$<
a.txt: b.txt c.txt cp $< $@
等同于下面的写法。
> ```bash
a.txt: b.txt c.txt
cp b.txt a.txt
(3)$?
指代比目标更新的所有前置条件,之间以空格分隔。比如,规则为 t: p1 p2,其中 p2 的时间戳比 t 新,
(4)$^
指代所有前置条件,之间以空格分隔。比如,规则为 t: p1 p2,那么
(5)$*
指代匹配符 % 匹配的部分, 比如 % 匹配 f1.txt 中的 f1 ,$*就表示 f1。
(6)$(@D) 和 $(@F)
$(@D) 和 $(@F) 分别指向
(7)$(<D) 和 $(<F)
$(<D) 和 $(<F) 分别指向 $< 的目录名和文件名。
所有的自动变量清单,请看手册。下面是自动变量的一个例子。
dest/%.txt: src/%.txt @[ -d dest ] || mkdir dest cp $< $@
上面代码将 src 目录下的 txt 文件,拷贝到 dest 目录下。首先判断 dest 目录是否存在,如果不存在就新建,然后,$< 指代前置文件(src/%.txt), $@ 指代目标文件(dest/%.txt)。
## 函数
### 语法
$(function arguments)
或者
${function arguments}
### 内置函数
1. shell
files = $(shell echo "xxx")
> 要想实现执行shell命令,也可以这样实现 files != echo "xxx"
1. wildcard
files = $(wildcard src/*.txt)
1. 替换函数(subst、patsubst)
更多内容可以参考https://www.gnu.org/software/make/manual/html_node/Functions.html