我可以在任何条件中使用 ifeq / else if eq / else 语法吗?或者我必须只针对多个值测试一个变量。(就像一个案例。)

Liv*_*ust 4 makefile gnu-make

make 文档说复杂条件的语法如下:

conditional-directive-one
text-if-one-is-true
else conditional-directive-two
text-if-two-is-true
else
text-if-one-and-two-are-false
endif
Run Code Online (Sandbox Code Playgroud)

但我见过的所有示例中的每个“条件指令 n”都在测试与第一个条件指令中相同的 $var 。实际上,它就像一个 case 语句,针对多个可能值测试一个变量。像这样:

ifeq ($(option), 1)
    CC=gcc
else ifeq ($(option), 2)
    CC=clang
else
    CC=mipsel-linux-gcc
endif
Run Code Online (Sandbox Code Playgroud)

我的问题是,这是一个要求吗?或者我可以有完全不同的条件指令,如下所示:

ifeq ($(option), 1)
    CC=gcc
else ifeq ($(myhostname), "JohnsLaptop")
    CC=johnsCompiler
else
    CC=
endif
Run Code Online (Sandbox Code Playgroud)

那么它真的只是一个 case 语句的时髦语法,还是每个“else ifeq”都是独立的。

我知道我可以这样做:

ifeq ($(option), 1)
    CC=gcc
else
ifeq ($(myhostname), "johnsLaptop")
    CC=johnsCompiler
else
    CC=mipsel-linux-gcc
endif
endif
Run Code Online (Sandbox Code Playgroud)

但那很丑。如果不需要的话,我宁愿不嵌套没有缩进的 if/else。

Liv*_*ust 6

我在我的非标准版本的 make 上对此进行了测试,答案是每个条件指令都是独立的。感谢madScientist的评论,似乎证实这也是标准和 gnumake 的正确答案。\

这段代码的工作原理:

ifneq ($(findstring mbist_rtl,$@),)
    RTL_OPT = mixed findSrc_targets_memwrap.ini
else ifeq ($(DFT), 1)
    RTL_OPT = dft
else ifeq ($(BB), 1)
    RTL_OPT = ndft
else
    RTL_OPT = syn
endif
Run Code Online (Sandbox Code Playgroud)