Make的规则依赖执行顺序
GNU Make的规则依赖有两种类型:普通依赖(normal prerequisites )和顺序依赖(order-only prerequisites )。对于普通依赖,其出现的顺序指定了依赖被更新的顺序。这一特性可以在一定程度上(非并行调用Make)隐含各依赖项之间的依赖关系,但不具备稳定性。笔者编写了简单的测试脚本:
.PHONY: all foo bar clean
ifeq ($(REORDER),yes)
all: bar foo | ordered
else
all: foo bar | ordered
endif
@echo "prerequisites for $@: $^"
foo bar :
@echo "try to make $@"
ordered:
@echo "update $@..." ; touch $@
clean:
rm -rf ordered
以不用的参数,分别执行可得到以下结果:
# touch ordered ; make
try to make foo
try to make bar
prerequisites for all: foo bar
# touch ordered ; make REORDER=yes
try to make bar
try to make foo
prerequisites for all: bar foo
根据命行行参数REORDER=yes 的有无,选择规则依赖项foo 和bar 出现的顺序,可以看到二者先后被执行更新的顺序不同。这说明了普通依赖的出现顺序可以指定其被更新的顺序(未启用并行执行Make)。
顺序依赖(order-only prerequisites )在之前的文章中提到过,它在一定程度上打破了Make对依赖更新的规则。其主要的功能特点并未在上面的Makefile脚本中体现。不过,在删除ordered 文件的情况下,可以看到order-only 依赖更新在foo 和bar 依赖更新之后:
# rm -rf ordered ; make -f order-only.mk
try to make foo
try to make bar
update ordered...
prerequisites for all: foo bar
但这是否意味着,order-only 依赖仍然会在普通依赖之后更新?还有,order-only 依赖之间是否如同普通依赖那样,其间也存在更新的先后顺序?笔者编写了简单Makefile脚本以探究竟:
.PHONY: all foo bar clean
ifeq ($(REORDER),yes)
all: | order1 order0
all: foo bar
else
all: foo bar | order0 order1
endif
@echo "prerequisites for $@: $^"
foo bar :
@echo "try to make $@"
order0 order1:
@echo "update $@..." ; touch $@
clean:
rm -rf order0 order1
以REORDER=yes 参数的有无,分别执行可得到以下结果:
# rm -rf order0 order1 ; make
try to make foo
try to make bar
update order0...
update order1...
prerequisites for all: foo bar
# rm -rf order0 order1 ; make REORDER=yes
try to make foo
try to make bar
update order1...
update order0...
prerequisites for all: foo bar
二者结果对比可知:
-
order-only 依赖总是在普通依赖之后更新; -
order-only 依赖项的出现顺序也指定了其被更新的顺序。
双冒号规则
在之前的一篇文章出提到了双冒号规则,这一规则一定程度上也打破了Makefile的目标更新规则。假设根据当前系统时间来更新某个文件:仅当当前时间epoch 是奇数时才更新;当该文件被更新后,依赖该文件的规则才会被更新。笔者编写的Makefile脚本如下:
MAKEFLAGS += -r -R
.PHONY: clean
all: dc_target
@echo "updating file $@..." ; touch $@
dc_target::
@echo "checking if $@ already exists..."
@NOWT="`date '+%s'`" ; THEN="`expr $${NOWT} % 2`" ; \
if [ $${THEN} -eq 1 ] ; then echo "Epoch is $${NOWT}" ; \
touch $@ ; fi ; exit 0
clean:
rm -rf all dc_target
如上,all 目标(注意,不是伪目标)依赖文件dc_target ,而dc_target 仅会在Epoch (脚本中为NOWT )为奇数时才会被更新。这个脚本需要多行执行测试,才能确定其是否可用:
# rm -rf dc_target ; make
checking if dc_target already exists...
Epoch is 1629639669
updating file all...
# make
make: 'all' is up to date.
# make
make: 'all' is up to date.
# make
make: 'all' is up to date.
# make
make: 'all' is up to date.
可见,当dc_target 文件存在时,不会执行其规则的更新操作。此情况下,双冒号规则就可以解决这个问题。修改上面的简单Makefile脚本,将dc_target 更改为双冒号规则:
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ MAKEFLAGS += -r -R
all: dc_target
@echo "updating file $@..." ; touch $@
-dc_target:
+dc_target::
@echo "checking if $@ already exists..."
@NOWT="`date '+%s'`" ; THEN="`expr $${NOWT} % 2`" ; \
if [ $${THEN} -eq 1 ] ; then echo "Epoch is $${NOWT}" ; \
至此,dc_target 规则在执行make all 时,每次都会尝试更新:
# make
checking if dc_target already exists...
Epoch is 1629640293
updating file all...
# make
checking if dc_target already exists...
Epoch is 1629640293
updating file all...
# make
checking if dc_target already exists...
# make
checking if dc_target already exists...
Epoch is 1629640295
updating file all...
# make
checking if dc_target already exists...
如以上多次执行的结果;当Epoch 为奇数时,dc_target 和all 都会被更新;当Epoch 不为奇数时,虽然dc_target 的操作被执行了(以尝试更新dc_target ),但未被更新,因而默认的all 目标不会被更新。
|