【问题标题】:How can I simplify this Makefile to make it less repetitive?如何简化此 Makefile 以减少重复性?
【发布时间】:2015-01-05 11:19:51
【问题描述】:

Make 是我在是否理解它之间来回切换的技术之一。

这当然是我知道我做错了什么的一个例子,因为 Make 的开发是为了减少这些任务的重复性。

all: 24.1 24.2 24.3

24.1:
    evm install emacs-24.1-bin || true
    emacs --version
    emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit
24.2:
    evm install emacs-24.2-bin || true
    emacs --version
    emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit
24.3:
    evm install emacs-24.3-bin || true
    emacs --version
    emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit

如何编辑此 Makefile 以仅布置一次测试序列但能够针对多个版本进行测试?

【问题讨论】:

    标签: makefile


    【解决方案1】:

    试试这个:

    VERSIONS = 24.1 24.2 24.3
    
    all :: $(VERSIONS)
    
    $(VERSIONS) ::
        evm install emacs-$@-bin || true
        emacs --version
        emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit
    

    :: 是一种特殊的规则,它将目标视为虚假的(并且还具有其他属性)。

    【讨论】:

    • 这似乎是最干净的解决方案,并且允许我在 Makefile 中拥有其他目标,并为 VERSIONS only 指定此配方。好答案 :) 你有没有描述:: 用法的手册页的链接?
    • 这是 GNU Make 手册中的一个链接:gnu.org/software/make/manual/make.html#Double_002dColon - 但它不是 GNU 特定的,它是“传统”语法。
    • 我不知道 :: 代表虚假目标是什么意思,但我读到该声明并不能说明它是正确的 :-)。双冒号规则不会创建虚假目标,尽管双冒号规则通常也被声明为虚假(单独)。
    【解决方案2】:

    怎么样:

    all: 24.1 24.2 24.3
    
    %:
            evm install emacs-$@-bin || true
            emacs --version
            emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit
    

    【讨论】:

    【解决方案3】:

    我不得不承认,转向“最后的手段”策略总是让我感到不安:感觉好像违背了工具的本质。另一方面,BSD make 允许显式循环构造,因此摆脱重复规则很简单:

    VERSIONS = 24.1 24.2 24.3
    all: ${VERSIONS}
    
    .for VERSION in ${VERSIONS}
    ${VERSION}:
        evm install emacs-${VERSION}-bin || true
        emacs --version
        emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit
    .endfor
    

    我很清楚,这个解决方案几乎肯定对您毫无帮助;切换 make 实现几乎肯定是不可能的。虽然 BSD make 的代表性非常低,所以我认为记录替代方法可能对其他人有用。

    正如 MadScientist 正确指出的那样,GNU make 不支持任何像 .for 这样的“点结构”,它们是 BSD make 特有的。然而,这个问题提出了一些其他可能适用于 GNU make 的循环技术:How to write loop in a Makefile?

    【讨论】:

    • 哦,那太棒了 :) GNU Make 可能 支持这个(Emacs 突出显示结构,但这并不意味着什么),但它会在 @ 处抛出 Makefile:23: *** missing separator. Stop. 987654325@线。
    • GNU make 绝对不支持这种语法。事实上,GNU make 中没有任何 BSD“点命令”(如 .for)可用。
    • 是的,我在 GNU make 上也遇到了缺少分隔符的错误。
    猜你喜欢
    • 2022-01-22
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2018-12-28
    • 2019-12-14
    • 2021-03-19
    相关资源
    最近更新 更多