使导出只是多字值的第一个单词

时间:2015-08-26 02:17:39

标签: makefile gnu-make

给定一个makefile,例如:

ifeq "$(MAKELEVEL)" "0"



# EDIT: I added the following line to address Etan Reisner post below. Thanks!
export foo

# A pattern-specific defintion, that matches both 'all' and 'dep'
% : foo += bar

all : dep ;

dep ::
    @echo '[makelevel="$(MAKELEVEL)"] foo="$(foo)"'
    @$(MAKE)

.PHONY : all



else



all ::
    @echo '[makelevel="$(MAKELEVEL)"] foo="$(foo)"'



endif




执行,我们得到:

# Overrides the Makefile-level-variable 'foo', with a command-line assignment.
$ make foo='cmd'
[makelevel="0"] foo="cmd cmd cmd"
make[1]: Entering directory '/home/myname'
[makelevel="1"] foo="cmd"
make[1]: Leaving directory '/home/myname'

# EDIT! I've added the following lines, again, to address good pointers from Etan Reisner, below!
# Please, be sure to try it out in versions 4.0 and up.
$ make foo='cmd' -e
[makelevel="0"] foo="cmd cmd cmd"
make[1]: Entering directory '/home/myname'
[makelevel="1"] foo="cmd cmd cmd"
make[1]: Leaving directory '/home/myname'



现在,我的问题不可能更明显了!

对于变量foo

,这里显然有2个值
  1. cmd cmd cmd。 ( root -make的值,对于以[MAKELEVEL=0]开头的行显而易见)
  2. cmd。 ( sub -make的值,对于以[MAKELEVEL=1]开头的行显而易见)
  3. 现在,假设变量foo在root-make中定义(即不是sub-make),而 仅定义 (在makefile级别,或者来自root - make的命令行,因此 sub -make“看到”foo的唯一值是从 root -make导出的值。同意?

    那么,假设那么,那么这两个进程( root -make和 sub -make)是否具有 两个相同变量的值非常不同 foo

1 个答案:

答案 0 :(得分:1)

因为cmd cmd cmd版本是特定于模式的变量,并且那些不是传递给子品牌的。全局变量确实如此。

这就是你得到这个的原因:

$ make -rR
[makelevel="0"] foo="bar bar"
make[1]: Entering directory `/home/myname'
[makelevel="1"] foo=""
make[1]: Leaving directory `/home/myname'

更新问题的答案是变量优先级之一。

在食谱中使用@echo '[makelevel="$(MAKELEVEL)"] foo="$(foo) $$(origin foo)=$(origin foo)"',您会得到以下结果:

$ make
[makelevel="0"] foo="bar bar $(origin foo)=file"
make[1]: Entering directory `/home/myname'
[makelevel="1"] foo="bar bar $(origin foo)=environment"
make[1]: Leaving directory `/home/myname'
$ make foo=cmd
[makelevel="0"] foo="cmd cmd cmd $(origin foo)=command line"
make[1]: Entering directory `/home/myname'
[makelevel="1"] foo="cmd $(origin foo)=command line"
make[1]: Leaving directory `/home/myname'

这清楚地说明了为什么你得到每种情况下得到的结果。由于附加值被命令行值再次覆盖(并且由于$(MAKELEVEL)拆分而未被重新附加)。