给定一个 Makefile:
all: build/a build/b build/c # need to change this to all: build/*
build/a:
...
build/b:
...
build/c:
...
我想更改 all
目标以自动构建所有匹配 build/*
的目标。 This question看起来非常相似,除了它打印结果而不是对其进行操作。另外,我不确定这个答案是否适用于 Linux 和 Mac。
最佳答案
Make 没有内置这样的功能。而且,事实上,手动更新目标列表比任何替代解决方案都更简洁、更容易。就个人而言,我可能会从这样的事情开始:
# second expansion is needed to get the value of
# $(targets) after the whole file was preprocessed
.SECONDEXPANSION:
all: $$(targets)
targets += build/a
build/a:
...
targets += build/b
build/b:
...
targets += build/c
build/c:
...
我认为为每个目标添加一行 (targets+=xxx
) 的要求不会让您感到厌烦。
但是,这是我们自己“预处理”Makefile 的方式:
# assume all targets are explicitly defined in Makefile
targets != grep -o '^ *build/\w* *:' Makefile | sed 's/^ *//;s/ *:$$//'
all: $(targets)
build/a:
...
build/b:
...
build/c:
...
当然,如果目标在包含的文件中或包含替换等,这将失败。但是,它适用于“简单情况”。
关于makefile - 如何执行所有匹配通配符的 makefile 目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55349222/