且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-15 22:13:45

继续翻译

GNU make manual 翻译( 一百四十四)
5.7.2 Communicating Variables to a Sub-`make'
---------------------------------------------

Variable values of the top-level `make' can be passed to the sub-`make'
through the environment by explicit request.  These variables are
defined in the sub-`make' as defaults, but do not override what is
specified in the makefile used by the sub-`make' makefile unless you
use the `-e' switch (*note Summary of Options: Options Summary.).

   To pass down, or "export", a variable, `make' adds the variable and
its value to the environment for running each line of the recipe.  The
sub-`make', in turn, uses the environment to initialize its table of
variable values.  *Note Variables from the Environment: Environment.

   Except by explicit request, `make' exports a variable only if it is
either defined in the environment initially or set on the command line,
and if its name consists only of letters, numbers, and underscores.
Some shells cannot cope with environment variable names consisting of
characters other than letters, numbers, and underscores.

   The value of the `make' variable `SHELL' is not exported.  Instead,
the value of the `SHELL' variable from the invoking environment is
passed to the sub-`make'.  You can force `make' to export its value for
`SHELL' by using the `export' directive, described below.  *Note
Choosing the Shell::.

   The special variable `MAKEFLAGS' is always exported (unless you
unexport it).  `MAKEFILES' is exported if you set it to anything.

   `make' automatically passes down variable values that were defined
on the command line, by putting them in the `MAKEFLAGS' variable.
*Note Options/Recursion::.

   Variables are _not_ normally passed down if they were created by
default by `make' (*note Variables Used by Implicit Rules: Implicit
Variables.).  The sub-`make' will define these for itself.

   If you want to export specific variables to a sub-`make', use the
`export' directive, like this:

     export VARIABLE ...

If you want to _prevent_ a variable from being exported, use the
`unexport' directive, like this:

     unexport VARIABLE ...

In both of these forms, the arguments to `export' and `unexport' are
expanded, and so could be variables or functions which expand to a
(list of) variable names to be (un)exported.

   As a convenience, you can define a variable and export it at the same
time by doing:

     export VARIABLE = value

has the same result as:

     VARIABLE = value
     export VARIABLE

and

     export VARIABLE := value

has the same result as:

     VARIABLE := value
     export VARIABLE

   Likewise,

     export VARIABLE += value

is just like:

     VARIABLE += value
     export VARIABLE

*Note Appending More Text to Variables: Appending.
GNU make manual 翻译( 一百四十四)

5.7.2 向子make 通讯变量
---------------------------------------------

通过显式的需求,***make的变量可以被传递给子make,但是不会覆盖子make的值,除非你使用了 -e选项(*note Summary of Options: Options Summary.)。

为了下传,或者导出一个变量,make 追加此变量和它的值到片段运行的每一行的环境中。子make,依照顺序,使用环境来初始化它自己的变量表。*Note Variables from the Environment: Environment.

除了被显式地请求,make仅仅当一个变量或者定义在环境里或者被设置在命令行里的时候,才会导出此变量,并且它的名字只能由 字母,数字和下划线组成。有些shell 不能 处理变量名含有其他字符的情况。

make 变量 SHELL 的值不会导出。反而,SHELL 变量的值从激活shell的环境中传递到子make, 你可通过export 指令,强迫 make 为SHELL变量导出值,下面会有讲述。*Note Choosing the Shell::

特殊变量 MAKEFLAGS 总是要导出的(除非你特意不导出),如果你设置它为任何值,它都要导出。

make 通过把变量值放入 MAKEFLAGS 里,自动地传递那些命令行中定义的变量值。*Note Options/Recursion::

如果变量是由make缺省地创建的,那么通常不会传递下去(*note Variables Used by Implicit Rules:Implicit Variables),子make将会自己定义这些值。

如果你想要导出特定的变量到一个子make, 使用 export 指令,像如下这样:

export VARIABLE ...

如果你想防止一个变量被导出,使用unexport 指令,像这样:

unexport VARIABLE ...

在上述的试样中,需要导出和限制导出的参数被扩展,这样变量或者函数可以被导出。

作为一个方便的方法,你可以在将变量定义和导出放在一起:

export VARIABLE = value

和如下的有同样的效果:

VARIABLE = value
export VARIABLE

或者

export VARIABLE := value

和如下的有同样的效果:

VARIABLE := value
export VARIABLE

与此类似,

export VARIABLE += value

就像是:

VARIABLE += value
export VARIABLE

*Note Appending More Text to Variables: Appending.

后文待续





本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/10/01/2709739.html,如需转载请自行联系原作者