Makefile 在宏内部使用if

Makefile 在宏内部使用if,makefile,gnu-make,Makefile,Gnu Make,我想写一个函数,根据参数返回不同的值。 我写过: define function ifeq($(1),a) aaaa else bbbb endif endef $(info MSG: $(call function,a)) 我得到输出: MSG: ifeq(a,a) aaaa else bbbb endif 这是整个功能的主体,而不是aaaa 我应该如何重写函数或更改调用它的方式,以便仅从匹配的if分支获取文本 更新: 此外,我还尝试在eval中包含函数调用,但发现缺少分隔符错误。我想出

我想写一个函数,根据参数返回不同的值。 我写过:

define function
ifeq($(1),a)
aaaa
else
bbbb
endif
endef

$(info MSG: $(call function,a))
我得到输出:

MSG: ifeq(a,a)
aaaa
else
bbbb
endif
这是整个功能的主体,而不是aaaa

我应该如何重写函数或更改调用它的方式,以便仅从匹配的if分支获取文本

更新:
此外,我还尝试在eval中包含函数调用,但发现缺少分隔符错误。

我想出了这样一个决定。也许不是很优雅,但它仍然满足了我的需要:

txtcmp = $(and $(filter $(1),$(2)),$(filter $(2),$(1)))

define function
$(if $(call txtcmp,$(1),a),aaaa,bbbb)
endef

$(info MSG: $(call function,b))

我想出了这样一个决定。也许不是很优雅,但它仍然满足了我的需要:

txtcmp = $(and $(filter $(1),$(2)),$(filter $(2),$(1)))

define function
$(if $(call txtcmp,$(1),a),aaaa,bbbb)
endef

$(info MSG: $(call function,b))

考虑到
txtcmp
的实现,您需要一个函数来测试两个参数是否相等。如果是,则返回一个值,否则返回另一个值

您的解决方案看起来不错,但是可以稍作改进:

function = $(if $(call txtcmp,$(1),a),aaaa,bbbb)
注意,还有另一个文本搜索功能-
findstring
。使用它而不是
过滤器将产生略微不同的结果:

txtcmp = $(and $(filter $(1),$(2)),$(filter $(2),$(1)))

function = $(if $(call txtcmp,$(1),a),aaaa,bbbb)

$(info MSG: $(call function,b))
$(info MSG: $(call function,a))
$(info MSG: $(call function, a))

txtcmp = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))

$(info MSG: $(call function,b))
$(info MSG: $(call function,a))
$(info MSG: $(call function, a))
运行上述makefile将输出:

MSG: bbbb
MSG: aaaa
MSG: aaaa
MSG: bbbb
MSG: aaaa
MSG: bbbb
make: *** No targets.  Stop.

考虑到
txtcmp
的实现,您需要一个函数来测试两个参数是否相等。如果是,则返回一个值,否则返回另一个值

您的解决方案看起来不错,但是可以稍作改进:

function = $(if $(call txtcmp,$(1),a),aaaa,bbbb)
注意,还有另一个文本搜索功能-
findstring
。使用它而不是
过滤器将产生略微不同的结果:

txtcmp = $(and $(filter $(1),$(2)),$(filter $(2),$(1)))

function = $(if $(call txtcmp,$(1),a),aaaa,bbbb)

$(info MSG: $(call function,b))
$(info MSG: $(call function,a))
$(info MSG: $(call function, a))

txtcmp = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))

$(info MSG: $(call function,b))
$(info MSG: $(call function,a))
$(info MSG: $(call function, a))
运行上述makefile将输出:

MSG: bbbb
MSG: aaaa
MSG: aaaa
MSG: bbbb
MSG: aaaa
MSG: bbbb
make: *** No targets.  Stop.

使用
过滤
比两次使用
过滤
更容易:

$(if $(filter-out $(1),$(2)),bbbb,aaaa)

(注意,我必须反转条件的分支,因为当它们不匹配时,
过滤掉
将给出输出(因此,
如果
,则为“true”)。

使用
过滤掉
,比使用两次
过滤更容易:

$(if $(filter-out $(1),$(2)),bbbb,aaaa)
(注意,我必须反转条件的分支,因为当它们不匹配时,
过滤掉
将给出输出(因此,
如果
,则为“true”)