在Mac OS X上的Makefile中设置PATH(但它适用于Linux)

时间:2014-08-26 12:39:01

标签: macos makefile gnu-make

我可以在Linux上的Makefile中设置PATH但不能在Mac OS X中设置PATH。使用OS X时,PATH会被设置但不会被使用。这是一个示范:

在CentOS 6上使用bash 4.1.2(1)-release和GNU Make 3.81

$ pwd
/tmp/experiment
$ find * -type f -ls
132133    4 -rw-rw-r--   1 tlim tlim      113 Aug 26 11:01 Makefile
132132    4 -rwxrwxr-x   1 tlim tlim       28 Aug 26 10:59 bin/myprog
$ cat Makefile 
export PATH := $(shell /bin/pwd)/bin:$(PATH)

what:
    @echo PATH=$$PATH
    @echo PATH=$(PATH)
    which myprog
    myprog
$ cat bin/myprog 
#!/bin/bash
echo HERE I AM.
$ make what
PATH=/tmp/experiment/bin:/usr/stack/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
PATH=/tmp/experiment/bin:/usr/stack/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
which myprog
/tmp/experiment/bin/myprog
myprog
HERE I AM.

这里使用GNU Make 3.81在Mac OS X 10.9.4上运行完全相同的文件:

$ pwd
/tmp/experiment
$ find * -type f -ls
51838886        8 -rw-rw-r--    1 tal              wheel                 113 Aug 26 07:01 Makefile
51838887        8 -rwxrwxr-x    1 tal              wheel                  28 Aug 26 06:59 bin/myprog
$ cat Makefile 
export PATH := $(shell /bin/pwd)/bin:$(PATH)

what:
    @echo PATH=$$PATH
    @echo PATH=$(PATH)
    which myprog
    myprog
$ cat bin/myprog 
#!/bin/bash
echo HERE I AM.
$ make what
PATH=/tmp/experiment/bin:/Users/tal/bin:/opt/local/sbin:/opt/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
PATH=/tmp/experiment/bin:/Users/tal/bin:/opt/local/sbin:/opt/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
which myprog
/tmp/experiment/bin/myprog
myprog
make: myprog: No such file or directory
make: *** [what] Error 1

如您所见,PATH正在设置并导出到环境中。两个" echo $ PATH"和#34; myprog"显示路径已设置。但是,当运行" myprog" shell无法找到它。

更新:如果我将这两行添加到Makefile中的what配方:

@echo SHELL=$$SHELL
@echo SHELL=$(SHELL)

Linux和Mac输出:

SHELL=/bin/bash
SHELL=/bin/sh

在运行SHELL之前,两者都在/bin/bash等于make。这让我觉得在Makefile中设置SHELL只是神奇的,并且是GNU制作手册总是设置SHELL的众多原因之一。

1 个答案:

答案 0 :(得分:5)

事实证明,PATH变量是特殊的,除非"SHELL" is also set,否则不会导出。换句话说,如果我将这一行添加到Makefile中,它可以在Mac和Linux上运行:

SHELL=/bin/bash

这当然让我想知道为什么Mac会有所不同。希望通过记录这一点,其他人看到这个问题也将受益。另外,我想知道为什么Mac会有所不同,如果确实如此。