在Makefile指令中更改dir后,md5sum无法找到文件 [英] md5sum fails to find file after changing dir in a Makefile instruction

查看:140
本文介绍了在Makefile指令中更改dir后,md5sum无法找到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having trouble generating a package with a md5sum of the contained files using Makefiles.

我确实找到了一种解决方法,但对此不满意.

I do have found an workaround, but I am not satisfied with it.

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

This is a example of how my makefile works, it can be used to reproduce my problem with file1, and my workaround with 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,构建失败,我得到以下输出:

Running file1, the build fails and I get the following output:

:~/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指令时,为什么md5sum工具在调用cd dir后无法在与运行时相同的目录中找到文件?还是我想念的东西?

My question is, why md5sum tool fails to find the file in the same dir as it is running after calling cd dir when it is used as a Makefile instruction? Or, what I am missing?

推荐答案

配方中的每一行都由单独的shell调用执行.因此,您的cd $(DIR)行由一个外壳程序执行,而对由另一个外壳程序执行的下一行(md5sum...)无效.在您的情况下,一个简单的解决方案是将所有命令链接起来,这样它们就可以被make视为一行并由同一shell执行:

Each line in a recipe is executed by a separate shell invocation. So your cd $(DIR) line is executed by a shell and has no effect on the next line (md5sum...) that gets executed by another shell. In your case a simple solution consists in chaining all commands such that they are considered as a single line by make and executed by the same shell:

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

或:

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

这篇关于在Makefile指令中更改dir后,md5sum无法找到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆