【问题标题】:md5sum fails to find file after changing dir in a Makefile instruction在 Makefile 指令中更改目录后,md5sum 找不到文件
【发布时间】:2018-03-26 14:25:43
【问题描述】:

我在使用 Makefiles 生成包含文件的 md5sum 的包时遇到问题。

我确实找到了解决方法,但我对此并不满意。

这是我的 makefile 工作方式的一个示例,它可用于重现我的 file1 问题,以及我使用 file2 的解决方法。

    VERSION:=1
    DIR:=tmp

    file: #rule to build the file with current version tag
            touch $(DIR)/file-$(VERSION)

    $(DIR)/file1.tar:file #rule that fails to create the md5 file
            cd $(DIR)
            md5sum -b \
                    file-$(VERSION) \
                    >> file-$(VERSION).md5
            tar -cf $@ \
                    file-$(VERSION) \
                    file-$(VERSION).md5
            cd -

    $(DIR)/file2.tar:file #workaround that fails to create the md5 file
            md5sum -b \
                    $(DIR)/file-$(VERSION) \
                    >> $(DIR)/file-$(VERSION).md5
            tar -cf $@ -C $(DIR) \
                    file-$(VERSION) \
                    file-$(VERSION).md5

    file1: $(DIR) $(DIR)/file1.tar

    file2: $(DIR) $(DIR)/file2.tar

    $(DIR):
            mkdir -p $(DIR)

运行file1,构建失败,我得到以下输出:

:~/tmp$ make file1
mkdir -p tmp
touch tmp/file-1
cd tmp
md5sum -b \
    file-1 \
    >> file-1.md5
md5sum: file-1: No such file or directory
Makefile:8: recipe for target 'tmp/file1.tar' failed
make: *** [tmp/file1.tar] Error 1

运行file2,文件构建成功:

:~/tmp$ make file2
touch tmp/file-1
md5sum -b \
    tmp/file-1 \
    >> tmp/file-1.md5
tar -cf tmp/file2.tar -C tmp \
    file-1 \
    file-1.md5

我的问题是,为什么当md5sum 工具用作Makefile 指令时,在调用cd dir 后,它无法在其运行的同一目录中找到文件?或者,我错过了什么?

【问题讨论】:

  • 配方中的每一行都由单独的 shell 调用执行。因此,您的 cd $(DIR) 行由 shell 执行,并且对由另一个 shell 执行的下一行 (md5sum...) 没有影响。
  • @RenaudPacalet,谢谢。我习惯于看到cd 和cd - 总是在同一行,但从没想过这是一个要求。刚刚添加了`; ` 在每条指令的末尾被解释为一行,它现在正在工作。你能回答这个问题以便我接受吗?
  • 问题已回答。请注意,根据您希望在命令失败时发生的情况,您还可以使用&& 或|| 链接您的命令。

标签: makefile md5sum


【解决方案1】:

配方中的每一行都由单独的 shell 调用执行。因此,您的 cd $(DIR) 行由 shell 执行,并且对由另一个 shell 执行的下一行 (md5sum...) 没有影响。在您的情况下,一个简单的解决方案包括链接所有命令,以便它们被 make 视为单行并由同一个 shell 执行:

target: prerequisites
    cd here; \
    do that; \
    ...

或:

target: prerequisites
    cd here && \
    do that && \
    ...

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 2021-12-02
    • 2021-10-03
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多