pushd 在 makefile 中不起作用

问题描述 投票:0回答:3

我在 makefile 中有以下规则:

ninja:
    git clone git://github.com/martine/ninja.git
    pushd ninja
    pwd
    git checkout release
    ./configure.py --bootstrap
    popd

这个想法是自动下载并构建 ninja 作为项目依赖项。请注意,

pwd
命令只是为了确保目录已推送。这是它生成的输出:

git clone git://github.com/martine/ninja.git
Cloning into 'ninja'...
remote: Counting objects: 8646, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 8646 (delta 0), reused 0 (delta 0), pack-reused 8642
Receiving objects: 100% (8646/8646), 1.88 MiB | 427.00 KiB/s, done.
Resolving deltas: 100% (6114/6114), done.
Checking connectivity... done.
pushd ninja
~/Desktop/core/ninja ~/Desktop/core
pwd
/Users/fratelli/Desktop/core
git checkout release
error: pathspec 'release' did not match any file(s) known to git.
make: *** [ninja] Error 1

正如您所看到的,目录确实被推入堆栈,但是

pwd
没有返回正确的目录。这也是
checkout
之后失败的原因。有什么想法可以解决这个问题吗?

shell makefile
3个回答
27
投票

makefile 目标配方中的每一行都在其自己的 shell 会话中运行。这不会影响大多数配方,因为它们默认在所需的目录中操作。当他们不这样做并且您需要使用

cd
pushd
时,您需要将所有命令写在同一行上,或者告诉 make 这些行是继续的。

请参阅拆分配方行了解更多详细信息和示例。


0
投票

解决方法:

  • \ 在 Makefile 的行尾
  • make SHELL='bash -ex'

0
投票

另一种解决方法:

do_something:
    cd path/to/dir ; \
    ...            ; \
    cd $(CURDIR)   ; \
    ...
© www.soinside.com 2019 - 2024. All rights reserved.