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

继续翻译

6.2 The Two Flavors of Variables

================================



There are two ways that a variable in GNU `make' can have a value; we

call them the two "flavors" of variables.  The two flavors are

distinguished in how they are defined and in what they do when expanded.



   The first flavor of variable is a "recursively expanded" variable.

Variables of this sort are defined by lines using `=' (*note Setting

Variables: Setting.) or by the `define' directive (*note Defining

Multi-Line Variables: Multi-Line.).  The value you specify is installed

verbatim; if it contains references to other variables, these

references are expanded whenever this variable is substituted (in the

course of expanding some other string).  When this happens, it is

called "recursive expansion".



   For example,



     foo = $(bar)

     bar = $(ugh)

     ugh = Huh?



     all:;echo $(foo)



will echo `Huh?': `$(foo)' expands to `$(bar)' which expands to

`$(ugh)' which finally expands to `Huh?'.



   This flavor of variable is the only sort supported by other versions

of `make'.  It has its advantages and its disadvantages.  An advantage

(most would say) is that:



     CFLAGS = $(include_dirs) -O

     include_dirs = -Ifoo -Ibar



will do what was intended: when `CFLAGS' is expanded in a recipe, it

will expand to `-Ifoo -Ibar -O'.  A major disadvantage is that you

cannot append something on the end of a variable, as in



     CFLAGS = $(CFLAGS) -O



because it will cause an infinite loop in the variable expansion.

(Actually `make' detects the infinite loop and reports an error.)  



   Another disadvantage is that any functions (*note Functions for

Transforming Text: Functions.)  referenced in the definition will be

executed every time the variable is expanded.  This makes `make' run

slower; worse, it causes the `wildcard' and `shell' functions to give

unpredictable results because you cannot easily control when they are

called, or even how many times.



   To avoid all the problems and inconveniences of recursively expanded

variables, there is another flavor: simply expanded variables.



   "Simply expanded variables" are defined by lines using `:=' (*note

Setting Variables: Setting.).  The value of a simply expanded variable

is scanned once and for all, expanding any references to other

variables and functions, when the variable is defined.  The actual

value of the simply expanded variable is the result of expanding the

text that you write.  It does not contain any references to other

variables; it contains their values _as of the time this variable was

defined_.  Therefore,



     x := foo

     y := $(x) bar

     x := later



is equivalent to



     y := foo bar

     x := later



   When a simply expanded variable is referenced, its value is

substituted verbatim.



   Here is a somewhat more complicated example, illustrating the use of

`:=' in conjunction with the `shell' function.  (*Note The `shell'

Function: Shell Function.)  This example also shows use of the variable

`MAKELEVEL', which is changed when it is passed down from level to

level.  (*Note Communicating Variables to a Sub-`make':

Variables/Recursion, for information about `MAKELEVEL'.)



     ifeq (0,${MAKELEVEL})

     whoami    := $(shell whoami)

     host-type := $(shell arch)

     MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}

     endif



An advantage of this use of `:=' is that a typical `descend into a

directory' recipe then looks like this:



     ${subdirs}:

             ${MAKE} -C $@ all



   Simply expanded variables generally make complicated makefile

programming more predictable because they work like variables in most

programming languages.  They allow you to redefine a variable using its

own value (or its value processed in some way by one of the expansion

functions) and to use the expansion functions much more efficiently

(*note Functions for Transforming Text: Functions.).



   You can also use them to introduce controlled leading whitespace into

variable values.  Leading whitespace characters are discarded from your

input before substitution of variable references and function calls;

this means you can include leading spaces in a variable value by

protecting them with variable references, like this:



     nullstring :=

     space := $(nullstring) # end of the line



Here the value of the variable `space' is precisely one space.  The

comment `# end of the line' is included here just for clarity.  Since

trailing space characters are _not_ stripped from variable values, just

a space at the end of the line would have the same effect (but be

rather hard to read).  If you put whitespace at the end of a variable

value, it is a good idea to put a comment like that at the end of the

line to make your intent clear.  Conversely, if you do _not_ want any

whitespace characters at the end of your variable value, you must

remember not to put a random comment on the end of the line after some

whitespace, such as this:



     dir := /foo/bar    # directory to put the frobs in



Here the value of the variable `dir' is `/foo/bar    ' (with four

trailing spaces), which was probably not the intention.  (Imagine

something like `$(dir)/file' with this definition!)



   There is another assignment operator for variables, `?='.  This is

called a conditional variable assignment operator, because it only has

an effect if the variable is not yet defined.  This statement:



     FOO ?= bar



is exactly equivalent to this (*note The `origin' Function: Origin

Function.):



     ifeq ($(origin FOO), undefined)

       FOO = bar

     endif



   Note that a variable set to an empty value is still defined, so `?='

will not set that variable.

6.2 变量的两个风格
================================

有两种方法可以使得GNU make中的一个变量拥有一个值;我们成其为变量的两个风格。这两个趣味由他们如何定义和如何扩展而区分开来。

第一个变量的趣味来自递归扩展变量。此类变量用 =来定义(*note Setting Variables: Setting),或者用 define 指令(*note Defining Multi-Line Varaibles: Multi-Line)

你所定义的值会被逐字翻译;如果它包含了对其他变量的参照。这些参照会在此变量被替换的时候进行扩展(在对其他字符串的扩展时)。当它发生的时候,可以称为递归扩展。

例如,

foo = $(bar)
bar = $(ugh)
ugh = Huh?

all:;echo $(foo)

会显示`Huh?': `$(foo)' 扩展为 `$(bar)',它再继续扩展为`$(ugh)' ,最终扩展为`Huh?'。

This flavor of variable is the only sort supported by other versions
of `make'. It has its advantages and its disadvantages. An advantage
(most would say) is that:

这种风格的变量是唯一被其他版本make所支持的。它有优点和缺点。一个优点(大多数情况下)是:

CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar

将会执行所期待的: 当 CFLAGS 在片段中被扩展,它会扩展为  -Ifoo -Ibar -O。一个主要的确定是你无法在变量后面最加一些东西,就像

CFLAGS = $(CFLAGS) -O

因为它将导致一个变量扩展的无限的循环(实际上make会检测到这个无限循环报告一个错误)。

另一个坏处是,在定义中所参照的任何函数(*note Functiosn for Transforming Text: Functions.)在每次变量扩展的时候都要执行。这导致make 运行缓慢。更糟糕的是,它导致通配符和shell 函数给出不确定的结果,因为你无法简单地控制它们何时以何种方式被调用多少次。

为了防止上述的所有问题和不方便之处,这里有另外一个风格:简单扩展变量。

"简单扩展变量"由:=来定义(*note Setting Variables: Setting.),简单扩展变量的值只被扫描一次,在任何其他的对此变量进行参照的变量和函数中扩展。简单扩展变量的实际值就是你所写的文本扩展结果。它不包含任何对其他变量的参照;它包含此变量被定义时的值。因此,

x := foo
y := $(x) bar
x := later

等价于:

y := foo bar

x := later

当一个简单扩展变量被参照的时候,它的值会被逐字逐句替换。

This example also shows use of the variable
`MAKELEVEL', which is changed when it is passed down from level to
level. (*Note Communicating Variables to a Sub-`make':
Variables/Recursion, for information about `MAKELEVEL'.)

这里有一个更加复杂的例子,显示了和shell 函数结合,对:=的利用。(*Note the 'shell' Function:Shell Function.)

ifeq (0,${MAKELEVEL})
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif

使用这种 :=的一个好处就是 典型的进入目录的片段会看起来像这样: 

${subdirs}:
${MAKE} -C $@ all

简单扩展变量通常会使得复杂的makefile编程更精确,因为它们就像大多数的编程语言一样进行工作。它们允许你用变量的值来重新定义自己(或者用某种方式在扩展函数中处理),可以更加高效地运行扩展函数(*note Functions for Transforming Text: Functions.)

你也可以用它们来在变量值中引入前导空格。在替换到变量参照和函数调用之前,前导空格会被忽略;这种方式下你可以通过利用变量参照来保护前导空格,像下面这样:

nullstring :=
space := $(nullstring) # end of the line

这里变量space 的值是精确的一个空格。注释'# end of the line' 在这里只是为了更加清晰地说明问题。因为末尾的空格字符不会从变量值中剥除,在行末的一个空格也可以达到同样的效果(但是读取来很困难)。

如果你在一个变量值得最后面附加一个空格,像上述那样在此行后面附加一个注释是一个好主意。相反地,如果你不想任何空格出现在你的变量末尾,你必须记住不要在行的末尾,在若干空格后面加一个注释。就像这样:

dir := /foo/bar # directory to put the frobs in

此时,dir 变量的值是 '/foo/bar '(末尾有四个空格),这可能不符合我们的预期。(想象比如 $(dir)/file这样的东西)

这一由另外一个对变量的复制操作符号 ?=。这被常委一个条件变量夫之妇好,一位只有在一个变量尚未确定时它才有效果。下面的句子:

FOO ?= bar

等价于 (*note The `origin' Function: Origin
Function.):

ifeq ($(origin FOO), undefined)
FOO = bar
endif

请注意一个便来能够设置为空值也相当于定义了,所以`?='不会设定给那个变量。
后文待续

你可能感兴趣的:(Make)