具有多个规则的Makefile来制作单个文件

时间:2018-12-12 20:43:30

标签: makefile

我想用多种方式创建Makefile来生成文件:

.PHONY: app bin tgt

app file: app-dependency ; touch file

bin file: bin-dependency ; touch file

tgt: file ; touch tgt

我可以运行make tgt,并根据是否存在文件app-dependencybin-dependency来决定运行哪个目标。

当我写类似上面的内容时,会收到以下警告:

  

警告:覆盖目标“文件”的配方
  警告:忽略目标“文件”的旧配方

2 个答案:

答案 0 :(得分:1)

您的规范未完全完成100%,因此让我们发明一下缺失的内容:如果存在app-dependency,则使用app-rule;否则,如果存在bin-dependency,则使用bin-rule,否则引发错误。如果这不是您想要的,请编辑您的问题,然后尝试添加缺少的规范。

使条件句成为您要执行的操作的一种方法:

.PHONY: app bin tgt

ifeq ($(wildcard app-dependency),app-dependency)

app file: app-dependency
    app-rule

else ifeq ($(wildcard bin-dependency),bin-dependency)

bin file: bin-dependency
    bin-rule

else

app bin file:
    $(error app-dependency and bin-dependency not found)

endif

tgt: file
    tgt-rule

答案 1 :(得分:0)

由于app,bin和tgt是伪造的,因此无论如何都不会使用依赖项,除非在规则丢失时阻止构建。

tgt无法知道要构建哪个版本的文件。如果应用程序和bin创建的文件相同,则将其排除在外:

file: ; touch file
app: file ; ...
bin: file ; ...
tgt: file ; ...

如果app和bin构建不同版本的文件,则无需将其作为目标。反正Tgt也不知道要建造哪一个:

app: ; touch file
bin: ; touch file
tgt: file ; ...

如果目的是在文件丢失时tgt同时构建app和bin(它充当“或”依赖项),则可以将其排除在外:

app: ; touch file
bin: ; touch file
file: app bin
tgt: file ; ...