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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23711087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:28:26  来源:igfitidea点击:

Makefile ifeq with Bash commands on OS X

macosbashmakefilegnu-make

提问by mfellner

I am trying to write a Makefile that evaluates results from Bash commands, e.g., uname.

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

Makefile:

生成文件:

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

Output:

输出:

Darwin
Fail
Fail
OK

How can I compare the variable OS1or the command $(uname)to the literal Darwininside an ifeq? From what I have read, the second ifeqin my Makefile should work, but it doesn't.

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

I am using GNU Make 3.81 for i386-apple-darwin11.3.0 on OS X 10.9.3.

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

回答by mfellner

There are a lot of different questions and answers about Makefiles, variables and shell commands. But as it turns out, looking into the manual is sometimes more reliable than searching Stackoverflow.

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

First, I didn't know the different ways that variables can be assigned in GNU make: https://www.gnu.org/software/make/manual/make.html#Reading-Makefiles

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

Second, the shellfunction, which is required in this case, only works in combination with the :=(immediate) operator: https://www.gnu.org/software/make/manual/make.html#Shell-Function

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

Thus the correct Makefile looks like this:

因此正确的 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