How to make Variadic macros using $ (call ...) in GNU Make

I created a macro for use in make files line by line:

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 if there is anything offering more explicit support for variable arrays.

+4
source share
3 answers

Here's a remix for the beta solution:

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

If there was a function $ (identity ...) for Make, I would use it; $ (strip ...) was the closest I could find.

+5
source

Will it do it?

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

As I mentioned in the comments, the answers of @Beta and @claytontstanley are good enough and personally I will use one of my solutions.

If you prefer a general way of getting variable arguments, the following may help you.

Warning: a lot 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, eg $ (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:

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

Output:

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

All Articles