Question

I created a macro for use in makefiles along the lines of:

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

I can get around it with something like the following:

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

... but I'm curious whether there is anything offering more explicit support for variadic macros.

Was it helpful?

Solution

Here is a remix on Beta's solution:

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

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

If there was an $(identity ...) function for Make, I'd use that; $(strip ...) was the closest I could find.

OTHER TIPS

Will this do?

comma := ,

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

As I have mentioned in comments, @Beta's and @claytontstanley's answers are good enough and personally I would use one of their solutions.

If you prefer a generic way of getting variadic arguments, then the following could help you.

Warning: lots of magic and dirty hacks below.

# 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

Testing it:

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))

The output is:

Makefile:49: Func foo called with args [a1,a2,a3,a4]
Makefile:50: Func func called with args [ar,baz,boo]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top