仅运行第一个列出的依赖项

问题描述 投票:0回答:2

我正在编写一个接受三个目标的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是输出。

bash makefile
2个回答
0
投票

我没有注意到您将make build指定为命令行。

在这种情况下,请参见以下答案:makefile: second dependency not execute


0
投票

这是因为build-consulbuild-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
© www.soinside.com 2019 - 2024. All rights reserved.