我正在编写一个接受三个目标的makefile:
make build-consul
make build-envoy
make build # (builds both)
build-consul
/ build-envoy
任务可以按预期工作。但是,build
任务仅运行第一个依赖项。考虑以下Makefile:
%consul: APP=consul
%envoy: APP=envoy
build-consul: _build
build-envoy: _build
build: build-consul build-envoy
_build:
echo building $(APP)
运行build
目标的输出是:
$ make build
echo working on consul
working on consul
为什么只运行第一个依赖项build-consul
,而忽略第二个build-envoy
依赖项?似乎首先列出build
任务的依赖项始终会运行(如果我交换它们,那么envoy
是输出。
我没有注意到您将make build
指定为命令行。
在这种情况下,请参见以下答案:makefile: second dependency not execute
这是因为build-consul
和build-envoy
都不做任何事情。它们都依赖于一个单独的目标_build
,该目标将始终执行一次。当make
访问build-consul
时,它会重新制作_build
目标(因为该文件可能不存在),而到了build-envoy
时,它就不会重新制作_build
目标,因为这已经完成了。自己看看:
$ make build -dr
Considering target file 'build'.
File 'build' does not exist.
Looking for an implicit rule for 'build'.
No implicit rule found for 'build'.
Considering target file 'build-consul'.
File 'build-consul' does not exist.
Looking for an implicit rule for 'build-consul'.
No implicit rule found for 'build-consul'.
Considering target file '_build'.
File '_build' does not exist.
Finished prerequisites of target file '_build'.
Must remake target '_build'.
echo building consul
Putting child 0x55931292b870 (_build) PID 8627 on the chain.
Live child 0x55931292b870 (_build) PID 8627
building consul
Reaping winning child 0x55931292b870 PID 8627
Removing child 0x55931292b870 PID 8627 from chain.
Successfully remade target file '_build'.
Finished prerequisites of target file 'build-consul'.
Must remake target 'build-consul'.
Successfully remade target file 'build-consul'.
Considering target file 'build-envoy'.
File 'build-envoy' does not exist.
Looking for an implicit rule for 'build-envoy'.
No implicit rule found for 'build-envoy'.
Pruning file '_build'. <--- Already built, not making again
Finished prerequisites of target file 'build-envoy'.
Must remake target 'build-envoy'.
Successfully remade target file 'build-envoy'.
Finished prerequisites of target file 'build'.
Must remake target 'build'.
Successfully remade target file 'build'.
您可以在没有其他目标的情况下使其正常工作,例如:
$ cat Makefile
%consul: APP=consul
%envoy: APP=envoy
.PHONY: build
build: build-consul build-envoy
.PHONY: build-consul build-envoy
build-consul build-envoy:
echo building $(APP)
[.PHONY
是为了确保即使存在相同名称的文件,也要构建目标(良好实践)。
输出:
$ make build
echo building consul
building consul
echo building envoy
building envoy