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

问题描述

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

但是我看到的所有示例每个“conditional-directive-n”都在测试与第一个 conditional-directive-one 中相同的 $var。实际上,它就像一个 case 语句针对多个可能值测试一个变量。像这样:

ifeq ($(option), 1)
    CC=gcc
else ifeq ($(option), 2)
    CC=clang
else
    CC=mipsel-linux-gcc
endif

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

ifeq ($(option), 1)
    CC=gcc
else ifeq ($(myhostname), "JohnsLaptop")
    CC=johnsCompiler
else
    CC=
endif

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

我知道我可以这样做:

ifeq ($(option), 1)
    CC=gcc
else
ifeq ($(myhostname), "johnsLaptop")
    CC=johnsCompiler
else
    CC=mipsel-linux-gcc
endif
endif

但那是丑陋的。如果不需要,我宁愿不嵌套 if/else 而不缩进。

标签: makefilegnu-make

解决方案


我在我的非标准版 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

推荐阅读