在 OS X 上使用 Bash 命令生成文件 ifeq

mfe*_*ner 7 macos bash makefile gnu-make

我正在尝试编写一个 Makefile 来评估 Bash 命令的结果,例如uname.

生成文件:

OS1 = $(uname)
OS2 = Darwin

all:
    @echo $(value OS1)

ifeq ($(uname),Darwin)
    @echo "OK"
else
    @echo "Fail"
endif

ifeq ($(OS1),Darwin)
    @echo "OK"
else
    @echo "Fail"
endif

ifeq ($(OS2),Darwin)
    @echo "OK"
else
    @echo "Fail"
endif
Run Code Online (Sandbox Code Playgroud)

输出:

Darwin
Fail
Fail
OK
Run Code Online (Sandbox Code Playgroud)

如何将变量OS1或命令$(uname)Darwinan 中的文字进行比较ifeq?从我读过的内容来看ifeq,我的 Makefile 中的第二个应该可以工作,但它没有。

我在 OS X 10.9.3 上为 i386-apple-darwin11.3.0 使用 GNU Make 3.81。

mfe*_*ner 10

关于 Makefile、变量和 shell 命令有很多不同的问题和答案。但事实证明,查看手册有时比搜索 Stackoverflow 更可靠。

首先,我不知道可以在 GNU make 中分配变量的不同方式:https : //www.gnu.org/software/make/manual/make.html#Reading-Makefiles

其次,shell在这种情况下需要的函数只能与:=(立即)运算符结合使用:https : //www.gnu.org/software/make/manual/make.html#Shell-Function

因此正确的 Makefile 如下所示:

OS := $(shell uname)

all:
    @echo $(OS)

ifeq ($(shell uname),Darwin)
    @echo "OK"
else
    @echo "Fail"
endif

ifeq ($(OS),Darwin)
    @echo "OK"
else
    @echo "Fail"
endif
Run Code Online (Sandbox Code Playgroud)

  • 关于 `shell` 是正确的,但是 `OS = $(shell uname)` 很好。 (2认同)