gnu makefile:如果条件在多行分配中

gnu makefile: if condition inside multirow assignment

是否可以if条件变量的多行赋值

以下代码触发错误:

MY_FILES=                      \
  Path/To/IncludeFolder_1      \
ifeq ($(USE_ALTERNATIVE),1)    \
  Path/To/IncludeFolder_2a     \
else                           \
  Path/To/IncludeFolder_2b     \
endif                          \
  Path/To/IncludeFolder_3      \

结果:

/bin/sh: -c: line 0: syntax error near unexpected token `('
make: *** [target] Error 2

但是下面的代码也会报错:

MY_FILES=                      \
  Path/To/IncludeFolder_1      \
ifeq ($(USE_ALTERNATIVE),1)
  Path/To/IncludeFolder_2a     \
else
  Path/To/IncludeFolder_2b     \
endif
  Path/To/IncludeFolder_3      \

结果:

Makefile:4: *** missing separator.  Stop.
"make" terminated with exit code 2. Build might be incomplete.

如果不可能,是否有其他简单的替代方法可以实现此目的?用例是一长串包含文件夹,在某些定义的功能上可能因地而异。

您可以自己制作一个相对容易的字符串比较函数,这样您就可以使用 $(if ) 内置函数:

###### $(call str-eq,_string1_,_string2_)
## Compare two strings on equality. Strings are allowed to have blanks.
## Return non-empty if string  and  are identical, empty string otherwise.
## - `$(call str-eq,yes,no)` --> ` ` (empty string)
## - `$(call str-eq,yes ,yes)` --> ` ` (empty string)
## - `$(call str-eq,yes ,yes )`  --> `t`
str-eq = $(if $(subst x,,x),,t)

MY_FILES=                      \
  Path/To/IncludeFolder_1      \
$(if $(call str-eq,$(USE_ALTERNATIVE),1)    \
  Path/To/IncludeFolder_2a,     \
  Path/To/IncludeFolder_2b     \
 )                          \
  Path/To/IncludeFolder_3

str-eq 和更多实用函数可以在 https://github.com/markpiffer/gmtt 找到。

你把事情搞得太复杂了。

MY_FILES :=  Path/To/IncludeFolder_1
ifeq ($(USE_ALTERNATIVE),1)
 MY_FILES += Path/To/IncludeFolder_2a
else                           
 MY_FILES += Path/To/IncludeFolder_2b
endif
MY_FILES += Path/To/IncludeFolder_3