make: Call Function
1
1 8.7 The 'call' Function
1 =======================
1
1 The 'call' function is unique in that it can be used to create new
1 parameterized functions. You can write a complex expression as the
1 value of a variable, then use 'call' to expand it with different values.
1
1 The syntax of the 'call' function is:
1
1 $(call VARIABLE,PARAM,PARAM,...)
1
1 When 'make' expands this function, it assigns each PARAM to temporary
1 variables '$(1)', '$(2)', etc. The variable '$(0)' will contain
1 VARIABLE. There is no maximum number of parameter arguments. There is
1 no minimum, either, but it doesn't make sense to use 'call' with no
1 parameters.
1
1 Then VARIABLE is expanded as a 'make' variable in the context of
1 these temporary assignments. Thus, any reference to '$(1)' in the value
1 of VARIABLE will resolve to the first PARAM in the invocation of 'call'.
1
1 Note that VARIABLE is the _name_ of a variable, not a _reference_ to
1 that variable. Therefore you would not normally use a '$' or
1 parentheses when writing it. (You can, however, use a variable
1 reference in the name if you want the name not to be a constant.)
1
1 If VARIABLE is the name of a built-in function, the built-in function
1 is always invoked (even if a 'make' variable by that name also exists).
1
1 The 'call' function expands the PARAM arguments before assigning them
1 to temporary variables. This means that VARIABLE values containing
1 references to built-in functions that have special expansion rules, like
1 'foreach' or 'if', may not work as you expect.
1
1 Some examples may make this clearer.
1
1 This macro simply reverses its arguments:
1
1 reverse = $(2) $(1)
1
1 foo = $(call reverse,a,b)
1
1 Here FOO will contain 'b a'.
1
1 This one is slightly more interesting: it defines a macro to search
1 for the first instance of a program in 'PATH':
1
1 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
1
1 LS := $(call pathsearch,ls)
1
1 Now the variable LS contains '/bin/ls' or similar.
1
1 The 'call' function can be nested. Each recursive invocation gets
1 its own local values for '$(1)', etc. that mask the values of
1 higher-level 'call'. For example, here is an implementation of a "map"
1 function:
1
1 map = $(foreach a,$(2),$(call $(1),$(a)))
1
1 Now you can MAP a function that normally takes only one argument,
1 such as 'origin', to multiple values in one step:
1
1 o = $(call map,origin,o map MAKE)
1
1 and end up with O containing something like 'file file default'.
1
1 A final caution: be careful when adding whitespace to the arguments
1 to 'call'. As with other functions, any whitespace contained in the
1 second and subsequent arguments is kept; this can cause strange effects.
1 It's generally safest to remove all extraneous whitespace when providing
1 parameters to 'call'.
1