- 论坛徽章:
- 3
|
摘自info make
The `call' Function
===================
The `call' function is unique in that it can be used to create new
parameterized functions. You can write a complex expression as the
value of a variable, then use `call' to expand it with different values.
The syntax of the `call' function is:
$(call VARIABLE,PARAM,PARAM,...)
When `make' expands this function, it assigns each PARAM to
temporary variables `$(1)', `$(2)', etc. The variable `$(0)' will
contain VARIABLE. There is no maximum number of parameter arguments.
There is no minimum, either, but it doesn't make sense to use `call'
with no parameters.
Then VARIABLE is expanded as a `make' variable in the context of
these temporary assignments. Thus, any reference to `$(1)' in the
value of VARIABLE will resolve to the first PARAM in the invocation of
`call'.
Note that VARIABLE is the _name_ of a variable, not a _reference_ to
that variable. Therefore you would not normally use a `$' or
parentheses when writing it. (You can, however, use a variable
reference in the name if you want the name not to be a constant.)
If VARIABLE is the name of a builtin function, the builtin function
is always invoked (even if a `make' variable by that name also exists).
The `call' function expands the PARAM arguments before assigning
them to temporary variables. This means that VARIABLE values
containing references to builtin functions that have special expansion
rules, like `foreach' or `if', may not work as you expect.
Some examples may make this clearer.
This macro simply reverses its arguments:
reverse = $(2) $(1)
foo = $(call reverse,a,b)
Here FOO will contain `b a'.
This one is slightly more interesting: it defines a macro to search
for the first instance of a program in `PATH':
pathsearch = $(firstword $(wildcard $(addsufix /$(1),$(subst :, ,$(PATH)))))
LS := $(call pathsearch,ls)
Now the variable LS contains `/bin/ls' or similar.
The `call' function can be nested. Each recursive invocation gets
its own local values for `$(1)', etc. that mask the values of
higher-level `call'. For example, here is an implementation of a "map"
function:
map = $(foreach a,$(2),$(call $(1),$(a)))
Now you can MAP a function that normally takes only one argument,
such as `origin', to multiple values in one step:
o = $(call map,origin,o map MAKE)
and end up with O containing something like `file file default'.
A final caution: be careful when adding whitespace to the arguments
to `call'. As with other functions, any whitespace contained in the
second and subsequent arguments is kept; this can cause strange
effects. It's generally safest to remove all extraneous whitespace when
providing parameters to `call'.
摘自《跟我一起写Makefile》
六、call函数
call函数是唯一一个可以用来创建新的参数化的函数。你可以写一个非常复杂的表达式,这个表达式中,你可以定义许多参数,然后你可以用call函数来向这个表达式传递参数。其语法是:
$(call <expression>,<parm1>,<parm2>,<parm3>...)
当make执行这个函数时,<expression>参数中的变量,如$(1),$(2),$(3)等,会被参数<parm1>,<parm2>,<parm3>依次取代。而<expression>的返回值就是call函数的返回值。例如:
reverse = $(1) $(2)
foo = $(call reverse,a,b)
那么,foo的值就是“a b”。当然,参数的次序是可以自定义的,不一定是顺序的,如:
reverse = $(2) $(1)
foo = $(call reverse,a,b)
此时的foo的值就是“b a”。 |
|