GNU make manual 翻译( 一百八十五)

继续翻译

8 Functions for Transforming Text
*********************************

Functions allow you to do text processing in the makefile to compute
the files to operate on or the commands to use in recipes.  You use a
function in a "function call", where you give the name of the function
and some text (the "arguments") for the function to operate on.  The
result of the function's processing is substituted into the makefile at
the point of the call, just as a variable might be substituted.

8.1 Function Call Syntax
========================

A function call resembles a variable reference.  It looks like this:

     $(FUNCTION ARGUMENTS)

or like this:

     ${FUNCTION ARGUMENTS}

   Here FUNCTION is a function name; one of a short list of names that
are part of `make'.  You can also essentially create your own functions
by using the `call' builtin function.

   The ARGUMENTS are the arguments of the function.  They are separated
from the function name by one or more spaces or tabs, and if there is
more than one argument, then they are separated by commas.  Such
whitespace and commas are not part of an argument's value.  The
delimiters which you use to surround the function call, whether
parentheses or braces, can appear in an argument only in matching pairs;
the other kind of delimiters may appear singly.  If the arguments
themselves contain other function calls or variable references, it is
wisest to use the same kind of delimiters for all the references; write
`$(subst a,b,$(x))', not `$(subst a,b,${x})'.  This is because it is
clearer, and because only one type of delimiter is matched to find the
end of the reference.

   The text written for each argument is processed by substitution of
variables and function calls to produce the argument value, which is
the text on which the function acts.  The substitution is done in the
order in which the arguments appear.

   Commas and unmatched parentheses or braces cannot appear in the text
of an argument as written; leading spaces cannot appear in the text of
the first argument as written.  These characters can be put into the
argument value by variable substitution.  First define variables
`comma' and `space' whose values are isolated comma and space
characters, then substitute these variables where such characters are
wanted, like this:

     comma:= ,
     empty:=
     space:= $(empty) $(empty)
     foo:= a b c
     bar:= $(subst $(space),$(comma),$(foo))
     # bar is now `a,b,c'.

Here the `subst' function replaces each space with a comma, through the
value of `foo', and substitutes the result.

8 转换文本的函数
*********************************

The
result of the function's processing is substituted into the makefile at
the point of the call, just as a variable might be substituted.

函数允许你在makefile中进行文本处理来计算要操作的文件或者在片段中操作的命令。你在一个函数调用中使用函数,此处你要给出函数的名字和一些文本(参数)来使得函数可以操作。

8.1 Function Call Syntax
========================

一个函数调用类似于变量参照。它看上去像这样:

$(FUNCTION ARGUMENTS)

或者像这样:

${FUNCTION ARGUMENTS}

这里的 FUNCTION 是一个函数的名字;makefile中的一部分,是一个名字短列表。你也可以从根本上用call 内建函数来创建你自己的函数。

ARGUMENTS 是函数的参数。它们被从函数名称中用一个或多个空格或tab符号分隔,如果有多于一个的参数,它们会被用逗号分隔。空格和逗号不是参数值的部分。你所用的包绕函数调用的间隔符号,无论是括号还是花括号,可以出现在一个参数里,但是要配对;其他的间隔符号可以单独出现。

如果参数自身包含了对其他函数调的调用或者变量参照,最好对所有的参照使用同一种区隔符号;如写 $(subst a, b, $(x)),而不是 $(subst, a , b , ${x})。这是因为这样更加清晰,而且只有一种类型的区隔符号需要匹配。

为每一个参数所写的文本被变量替换或者函数调用替换而产生的参数值所处理,此值来自于函数的动作。替换是按照参数出现的顺序而完成。

在参数中,逗号或不匹配的括号或者花括号不能出现在文本里;前导的空格不能出现在第一个参数里。这些字符可以通过变量替换被放入参数中。首先定义变量 comma 和 space,令其值映射到逗号和空格。然后在需要逗号和空格的地方,替换这些变量,像如下这样:

comma:= ,
empty:=
space:= $(empty) $(empty)
foo:= a b c
bar:= $(subst $(space),$(comma),$(foo))
# bar is now `a,b,c'.

这里的subst函数把foo里面每一个空格替换成为逗号。

后文待续

posted @ 2012-10-09 13:56  健哥的数据花园  阅读(247)  评论(0编辑  收藏  举报