【问题标题】:makefile match-anything as a prerequisitemakefile 匹配任何东西作为先决条件
【发布时间】:2013-08-08 23:15:56
【问题描述】:

我有一个包含四个目标的生成文件。其中两个目标可以复制,因此可能有许多目标需要作为先决条件运行(它们是double-colon targets)。其中两个目标是match-anything rules。不幸的是,它根本不像我期望的那样工作。我从这个开始:

.PHONY:
build_%: pre_build post_build _internal_%
        @echo "4. $@: $^"

.PHONY: pre_build
pre_build::
        @echo "1. $@: $^"

.PHONY: _internal_%
_internal_%:
        @echo "2. $@: $^"

.PHONY: post_build
post_build::
        @echo "3. $@: $^"

这给了我这个输出:

make build_foo
1. pre_build: 
3. post_build: 
2. _internal_foo: 
4. build_foo: pre_build _internal_foo post_build

如果我对先决条件很明确,像这样:

.PHONY:
build_%: pre_build post_build _internal_%
        @echo "4. $@: $^"

.PHONY: pre_build
pre_build::
        @echo "1. $@: $^"

.PHONY: _internal_%
_internal_%: pre_build
        @echo "2. $@: $^"

.PHONY: post_build
post_build:: _internal_%
        @echo "3. $@: $^"

相反,我得到了这个:

make build_foo
1. pre_build: 
make: *** No rule to make target `pre_build'.  Stop.

我需要做什么才能使依赖项像这样工作?我正在使用 GNU Make 3.81。

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    这似乎是 GNU Make 3.81 中的一个真正的错误。

    我建议这种解决方法:

    define prebuild
    @echo "1. pre_build: $^"
    endef
    
    define postbuild
    @echo "3. post_build: $^"
    endef
    
    .PHONY:
    build_%: _internal_%
            @echo "4. $@: $^"
    
    .PHONY: _internal_%
    _internal_%:
            $(prebuild)
            @echo "2. $@: $^"
            $(postbuild)
    
    # pre_build and post_build are not used here, but may still be used elsewhere
    
    .PHONY: pre_build
    pre_build::
        $(prebuild)
    
    .PHONY: post_build
    post_build::
            $(postbuild)
    

    【讨论】:

    • 这真的非常接近我正在寻找的东西,因为现在事情已经按顺序执行了。我更新了问题以表明期望的目标是能够运行多个 pre_buildpost_build 目标。
    • @paullckby:你不能把他们的命令放在prebuildpostbuild吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多