The syntax of thecall
function is:$(call variable,param,param,...)make
執行這道命時,會賦予param...一個暫時的變數名稱$(1)
,$(2)
, etc. 變數$(0)
則是 variable.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 ofcall
.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).examples
This macro simply reverses its arguments:
reverse = $(2) $(1)foo = $(call reverse,a,b)foo 會是 `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
.
沒有留言:
張貼留言