【问题标题】:Makefile multiple commands in single recipe在单个配方中生成多个命令
【发布时间】:2021-03-26 20:42:28
【问题描述】:

我有一个运行 Python 脚本的 Makefile 配方。但是,在脚本之前和之后,我想在屏幕上写一些信息来描述正在做什么。我可以将这些打印语句放入 Python 脚本中,但这是一种解决方法,我想了解为什么这不起作用。我的 Makefile 看起来像:

/data/interim/opt_smoothing.csv: $(shell find /data/raw/evi_data -type f) src/data/determine_optimal_smoothing.py
        $(info Determining optimal smoothing) && python src/data/determine_optimal_smoothing.py && $(info Optimal smoothing calculation complete)

我的印象是,将这些 && 放在一起会将这些命令链接在一起并让它们一个接一个地执行,但这似乎不起作用。当我尝试制作此文件时,出现错误:

root@61276deb5c1a:/code# make /data/interim/opt_smoothing.csv 
Determining optimal smoothing
Optimal smoothing calculation complete
&& python src/data/determine_optimal_smoothing.py && 
/bin/sh: 1: Syntax error: "&&" unexpected
make: *** [Makefile:10: /data/interim/opt_smoothing.csv] Error 2

当我在单独的行中包含这 3 件事时,它可以工作,除了在脚本完成之前出现计算完成消息。将这些东西链接在一起以便它们在同一个 shell 中按顺序执行的正确方法是什么?

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    您不能为此使用 make 的 info 函数。 Make 函数由 ma​​ke 运行,而不是由 shell 运行,作为脚本扩展的一部分,准备将其发送到 shell。因此,它们在调用 shell 之前运行。其次,它们扩展为空字符串。

    所以对于配方行:

        $(info foo) && python bar && $(info baz)
    

    make 将展开导致打印foobaz 的行,然后它将调用shell 中的结果字符串,其中包含&&,如下所示:

    foo
    baz
    /bin/sh -c '&& python bar &&`
    

    这显然是无效的。

    如果你想让 shell 打印你必须使用 shell 命令来完成它,例如echo,而不是 make 函数。

    【讨论】:

    • 谢谢,这很有意义。我正在处理的示例将 3 个 shell 命令链接在一起,但我没有意识到 info 函数根本不同。
    • 配方中任何以$ 开头的东西,除非它被转义为$$,在它被发送到shell 之前总是被make 扩展并且shell 永远不会看到它(它只看到任何东西它扩展到)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多