如何在GNU Makefile中创建非依赖假目标?如何在GNU Makefile中创建非依赖假目标?
要求:
- 来检查一个特定的设置的规则 - 可以比下面的例子更加复杂。
- 如果此规则失败,则建立特定目标应失败。
- 如果这个规则成功了,只有修改了它们的源代码,才能建立特定的目标。
- 规则不可能是全球性的,因为该规则甚至不应该对非特异性指标
如果规则是每一个具体目标的编译规则之前插入这是可以实现的执行。 因为它们可能有很多特定的目标,所以我更愿意为此规则创建一个假目标,并将此假目标指定为所有这些特定目标的依赖项。 这有一个不需要的副作用,即当假目标成功时,即使它们的源未被修改,也会重建那些特定目标。
换句话说,我如何指定一个假目标来强制从属目标被重建,如果它们的来源是最新的。
$ make --version | grep Make
GNU Make 3.82
$ make -f try.mk clean setup
rm -f try.out?
touch try.C#Dummy create source
$ make -f try.mk
touch try.out1 #Dummy compile
touch try.out2 #Dummy compile
touch try.out3 #Dummy compile
$ make -f try.mk
touch try.out3 #Dummy compile
try.out3应该不已经得到上面/最后的化妆编译。
$ cat try.mk
#try.mk
base=try
all: $(base).out1 $(base).out2 $(base).out3 #...
clean:
rm -f $(base).out?
setup:
touch $(base).C#Dummy create source
.PHONY: all clean platform_ok
#------------------------------------------------------------
#Specific targets
#------------------------------------------------------------
#Attempt 1: works, but platform check is a rule, and hence needs to be inserted wherever it is needed.
$(base).out1: $(base.c)
@if [ $(shell uname -i) == x86_64 ]; then exit 0; else exit 1; fi
touch $(base).out1 #Dummy compile
#Attempt 2: works, but platform check is global, which gets executed even when building Non-OS specific targets
$(eval platform_check:=$(shell (if [ $(shell uname -i) == x86_64 ]; then echo 0; else echo 1; fi)))
$(base).out2: $(base).c
@exit $(platform_check)
touch $(base).out2 #Dummy compile
#Attempt 3: works partially when platform check is a phony target, but target gets rebuilt even if source is uptodate.
$(base).out3: $(base).c platform_ok
touch $(base).out3 #Dummy compile
platform_ok:
@if [ $(shell uname -i) == x86_64 ]; then exit 0; else exit 1; fi
#------------------------------------------------------------
#Non-Specific targets
#------------------------------------------------------------
#...
感谢您的替代解决方案,但它确实没有解决第四个要求(规则不应该针对非特定目标执行)。在你的答案中总是计算ARCH。只是fyi,我的实际问题有一个更复杂的检查,我想避免为其他(非特定)目标执行.. –