Makefile:将PHONY目标声明为recipe prerequiste

时间:2018-05-31 19:57:50

标签: makefile

我的Makefile正在捆绑配方以并行处理大量文件(make <recipe> -j8等)。但在处理文件之前,需要找到find。这是一个耗时的操作,所以我只想为用户调用的确切配方运行它。我可以这样做:

test: FILES=$(shell find "$(SEARCHDIR)/" -mindepth 3 -maxdepth 3 -type f ! -regex $(SOMEREGEX))
test: $(FILES)
$(FILES):
    echo "do thing with file $@ here"

问题在于,由于文件本身已经存在,因此需要将它们声明为.PHONY才能运行配方,如下所示:

.PHONY: $(FILES)

但为了使其工作,FILES变量需要存在并填充,这需要我运行find命令。这违背了我的目标,即在调用FILES之前不执行搜索以查找test。我需要的是这样的事情:

test: FILES=$(shell find "$(SEARCHDIR)/" -mindepth 3 -maxdepth 3 -type f ! -regex $(SOMEREGEX))
test: .PHONY: $(FILES)
test: $(FILES)
$(FILES):
    echo "do thing with file $@ here"

但是test: .PHONY: $(FILES)语法无效且不起作用。

1 个答案:

答案 0 :(得分:2)

即使没有.PHONY内容,您的makefile也无法正常工作;只是:

test: FILES=$(shell find "$(SEARCHDIR)/" -mindepth 3 -maxdepth 3 -type f ! -regex $(SOMEREGEX))

$(FILES):
        echo "do thing with file $@ here"

失败,因为在$(FILES)解析makefile之前,规则目标中的make会扩展为空字符串,然后才开始运行test目标。

我建议你在这里使用recursive make;像这样写你的makefile:

FIND_FILES :=
FILES :=
ifneq ($(FIND_FILES),)
FILES := $(shell find "$(SEARCHDIR)/" -mindepth 3 -maxdepth 3 -type f ! -regex $(SOMEREGEX))
endif

test:
        $(MAKE) test-recurse FIND_FILES=1

test-recurse: $(FILES)
$(FILES):
        echo "do thing with file $@ here"
.PHONY: test test-recurse $(FILES)
相关问题