Makefile匹配规则的通配符先决条件

时间:2018-09-25 06:07:37

标签: makefile

我想将一些配方传递给makefile,例如comp1.mt comp2.mt comp3.mt,然后让makefile将它们汇总到一个MT列表中(也可以是mt.compN),以批量运行它们。

%.mt:
    @echo $* >> list_of_mts.txt

mt: %.mt
    @cat list_of_mts.txt

我想以任何顺序将这些配方传递给makefile,所以不希望调用make comp1.mt comp2.mt mt的特定情况。

1 个答案:

答案 0 :(得分:1)

我不明白为什么需要在makefile中实现此功能,但是您在这里:

MT_TARGETS:=$(filter %.mt,$(MAKECMDGOALS))

.PHONY: $(MT_TARGETS)
$(MT_TARGETS):
        @echo $@ | sed -e "s:.mt$$::" >> list_of_mts.txt

mt: $(MT_TARGETS)
        @cat list_of_mts.txt

测试:

$ make mt comp1.mt comp2.mt comp3.mt
comp1
comp2
comp3
make: `comp1.mt' is up to date.
make: `comp2.mt' is up to date.
make: `comp3.mt' is up to date.

这是使用特殊变量MAKECMDGOALS

注意:list_of_mts.txt将不断增长...

注意2:在并行执行中写入list_of_mts.txt是不安全的(list_of_mts.txt文件可能已损坏)。