如何在GNU Make中使用$(call ...)进行可变参数宏

时间:2012-08-28 23:22:10

标签: macros gnu-make variadic-macros

我创建了一个用于makefile的宏:

TODO_MSG = $(warning TODO: $(1))
$(call TODO_MSG, This part of the msg displays fine, but this part does not)

我可以使用以下内容解决问题:

BLAH := $(shell perl -e 'print join( " ", 2..200 )'
COMMA := ,
TODO_MSG = $(warning TODO:$(1)$(strip $(foreach x,${BLAH},$(if $(${x}),${COMMA}$(${x}))))

...但我很好奇是否有任何事情可以为变量宏提供更明确的支持。

3 个答案:

答案 0 :(得分:6)

以下是Beta解决方案的混音:

TODO_MSG = $(warning TODO: $(1))

test:
        $(call TODO_MSG, $(strip This part displays fine, and this does too))

如果Make有$(identity ...)函数,我会使用它; $(strip ...)是我能找到的最接近的。

答案 1 :(得分:3)

这会吗?

comma := ,

#define TODO_MSG = $(warning TODO: $(1))
TODO_MSG = $(warning TODO: $(1))
$(call TODO_MSG, This part displays fine$(comma) and so does this)

答案 2 :(得分:1)

正如我在评论中提到的那样,@Beta's@claytontstanley's的答案已经足够好了,我个人会使用他们的解决方案。

如果您更喜欢获取可变参数的通用方法,那么以下内容可以帮助您。

警告:下面有很多魔法和肮脏的黑客。

# Expands to comma-separated list of numeric arguments appearing in the
# current call context.
args = \
    $(eval __arg_tmp := \
        $(call __args_strip_tail,$(foreach __a,.1,$(__args_list))))$(__arg_tmp)

# The list of non-empty argument references, e.g. $(1),,$(3),,,
# Context: __a - encoded number of an argument being currently processed.
__args_list = \
    $(if $(__arg_value),$(__arg_ref))$(foreach __a,$(call inc,$(__a)) \
        ,$(if $(__arg_simple),$(comma)$(eval __arg_tmp := \
            $(value __args_list))$(__arg_tmp)))

__arg_value  = $(value $(call to_dec,$(__a)))
__arg_ref    = $${$(call to_dec,$(__a))}
__arg_simple = $(findstring simple,$(flavor $(call to_dec,$(__a))))

# Used as a workaround for GNU Make bugs in 'call'.
# ${1},,${3},,, -> ${1},,${3}
__args_strip_tail = $(subst } ,},$(call nolastword,$(subst },} ,$1,)))

# Some useful stuff...
comma := ,
nolastword = $(wordlist 2,$(words $1),x $1)

# Decodes .4.2 into 42
to_dec = $(subst .,,$1)

# Increments the given numeber.
#   1. Decimal with periods between digits: 42 is .4.2
inc = \
    $(call __inc$(suffix .0$1),$(basename $1))

__inc.0 = $1.1
__inc.1 = $1.2
__inc.2 = $1.3
__inc.3 = $1.4
__inc.4 = $1.5
__inc.5 = $1.6
__inc.6 = $1.7
__inc.7 = $1.8
__inc.8 = $1.9
__inc.9 = $(call inc,$1).0

测试它:

func = Func $0 called with args [$(args)]

foo = $(func)
bar = $(call func,ar,baz,boo)

$(warning $(call foo,a1,a2,a3,a4))
$(warning $(call bar,a1,a2,a3,a4))

输出结果为:

Makefile:49: Func foo called with args [a1,a2,a3,a4]
Makefile:50: Func func called with args [ar,baz,boo]