如何在GNU Make中修改PATH并成功使用$shell ...执行命令?

问题描述

背景:我有一个定制的工具链,该文件应由Makefile使用。工具链被检出(通过svn:externals)到项目的工作副本中。 Makefile必须适应PATH变量。否则,找不到工具链二进制文件。不能在“ .bashrc”或“ .profile”中修改“ PATH”,因为有多个项目使用不同版本的工具链。

请参见这个最小的Makefile文件,该文件演示了对交叉编译器“ cc”的调用,该交叉编译器位于“ / home / edeviser / bin”中的其他几个工具旁边:

export PATH:=/home/edeviser/bin:$(PATH)$
$(info Compiler used: $(shell which cc))

all:
    @echo "Compiler used: $(shell which cc)"
    @echo -n "Compiler used: "
    @which cc

调用make后的输出

Compiler used: /usr/bin/cc
Compiler used: /usr/bin/cc
Compiler used: /home/edeviser/bin/cc

我的期望是:

Compiler used: /home/edeviser/bin/cc
Compiler used: /home/edeviser/bin/cc
Compiler used: /home/edeviser/bin/cc

如何在GNU Make中成功修改PATH并成功使用$(shell ...)执行命令?

解决方法

您必须像这样编写shell调用:

$(info Compiler used: $(shell PATH='$(PATH)' which cc))

在shell函数中设置PATH。