2017-10-19 138 views
0

如何在GNU Makefile中创建非依赖假目标?如何在GNU Makefile中创建非依赖假目标?

要求:

  1. 来检查一个特定的设置的规则 - 可以比下面的例子更加复杂。
  2. 如果此规则失败,则建立特定目标应失败。
  3. 如果这个规则成功了,只有修改了它们的源代码,才能建立特定的目标。
  4. 规则不可能是全球性的,因为该规则甚至不应该对非特异性指标

如果规则是每一个具体目标的编译规则之前插入这是可以实现的执行。 因为它们可能有很多特定的目标,所以我更愿意为此规则创建一个假目标,并将此假目标指定为所有这些特定目标的依赖项。 这有一个不需要的副作用,即当假目标成功时,即使它们的源未被修改,也会重建那些特定目标。

换句话说,我如何指定一个假目标来强制从属目标被重建,如果它们的来源是最新的。

$ 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 
#------------------------------------------------------------ 
#... 

回答

0

我会去了解它有一点不同:

base=try 
ARCH := $(shell test `uname -i` = "x86_64"; echo $$?) 

ifeq ($(ARCH),0) 
define PLATFORM_64 = 
out3 
endef 
endif 

define ALL = 
out1 
out2 
$(PLATFORM_64) 
endef 

all: $(addprefix $(base)., $(ALL)) 

,我会完全失去platform_ok

$(base).out3: $(base).c 
    touch $(base).out3 #Dummy compile 
+0

感谢您的替代解决方案,但它确实没有解决第四个要求(规则不应该针对非特定目标执行)。在你的答案中总是计算ARCH。只是fyi,我的实际问题有一个更复杂的检查,我想避免为其他(非特定)目标执行.. –

2

只需使用假目标作为订单唯一的先决条件。请如果在依赖关系图中发现总会执行这个规则,但如果他们的正常先决条件是最近这取决于它的目标才会被执行:

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 
#------------------------------------------------------------ 

$(base).out1: $(base).c | platform_ok 
    touch $(base).out1 #Dummy compile 

$(base).out2: $(base).c | platform_ok 
    touch $(base).out2 #Dummy compile 

$(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 
+0

谢谢,手册页的make没有这个。添加仅限订单的先决条件的链接,以提供有关此主题的其他信息,以及可能遇到类似问题的其他人。 https://www.gnu.org/software/make/manual/make.html#Prerequisite-Types –