Mac 上带有 PATH 环境变量的 Makefile

Makefile with PATH environment variable on Mac

我正在尝试在 Makefile 编译中执行 qemu-system-arm。我已经编辑了:

它在 shell 命令 $qemu-system-arm 中工作正常,但我的 Makefile 不工作。 这是我的示例 Makefile:

qemu:
     qemu-system-arm -machine help

我用命令 $make qemu 得到这个错误:

qemu-system-arm -machine help
make: qemu-system-arm: No such file or directory
make: *** [qemu] Error 1

顺便说一句,我的qemu不是brew安装的,因为我需要另一个平台比如stm32-p103。所以在构建我自己的 qemu 之后,我必须相应地设置 $PATH。

这是我的 os 版本:

macOS Mojave v10.14.1

当 make 执行规则的配方时,它会将其传递给 shell。因此,为了使用 qemu-system-arm,shell 必须知道在哪里可以找到它。这取决于 shell make 使用的是什么(shbash、其他?)以及这个 shell 在启动时读取的配置文件(~/.profile~/.bashrc, 其他?).

您可以在 makefile 中使用简单的测试规则进行调查:

$ cat Makefile
...
.PHONY: test
test:
    echo 'SHELL=$(SHELL)'
...
$ make test
SHELL=...

在你的例子中,shell 可能不是 bourne shell (sh) 因为 ~/.profilesh 读取,所以它可能工作。因此,make 使用的 shell 可能是 bash:因为 make 生成的 shell 不是登录 shell 它不会读取 ~/.profile~/.bash_profile。您必须将 PATH 定义添加到您的 ~/.bashrc:

 echo 'export PATH=$PATH:/path/to/qemu-system-arm' >> ~/.bashrc

或者,如果您不想更改所有 bash 进程,您可以将此定义添加到您的 Makefile(环境变量成为 make 上下文中的 make 变量):

$ cat Makefile
PATH := $(PATH):/path/to/qemu-system-arm
...

请注意,您也可以强制 make 使用您想要的 shell(但考虑到它会对您的 makefile 的可移植性产生负面影响):

 SHELL := /usr/local/bin/my-preferred-shell