在sub-make中使变量不“重置”

时间:2016-10-13 21:25:17

标签: makefile

我看到一些有趣的行为,传递给Sub-make的变量。我已经制作了一个具有以下布局的玩具示例项目:

./make_test
| -- foo
|   \ -- Makefile
\ -- Makefile

make_test/Makefile

.PHONY: deps
deps:
        $(MAKE) -C foo

.PHONY: deps2
deps2: export SOME_VAR=something
deps2:
        $(MAKE) -C foo

make_test/foo/Makefile

SOME_VAR ?= something
$(info 1 - SOME_VAR=$(SOME_VAR))

SOME_VAR += else
$(info 2 - SOME_VAR=$(SOME_VAR))

all: deps
        @$(MAKE) app

.PHONY: deps
deps:
        $(info Getting deps)

app: foo_a foo_b
        touch app

foo_a:
        touch foo_a

foo_b:
        touch foo_b

运行make deps

的结果
/Library/Developer/CommandLineTools/usr/bin/make -C foo
1 - SOME_VAR=something
2 - SOME_VAR=something else
Getting deps
1 - SOME_VAR=something
2 - SOME_VAR=something else
make[2]: `app' is up to date.

运行make deps2

的结果
/Library/Developer/CommandLineTools/usr/bin/make -C foo
1 - SOME_VAR=something
2 - SOME_VAR=something else
Getting deps
1 - SOME_VAR=something else
2 - SOME_VAR=something else else
make[2]: `app' is up to date.

我理解SOME_VAR被评估两次。但是,对于deps,最终结果为something else,而deps2会产生something else else

我的问题是,在调用SOME_VAR而不是deps时,为什么deps2的值会在第二次评估之前“重置”。为什么将初始值传递给sub-make会影响这个?

1 个答案:

答案 0 :(得分:0)

如果您export变量,该变量将以递归方式export编辑,除非您稍后unexport

当您在$(MAKE) foo/Makefile export SOME_VAR中调用something else作为export时,因为您在根文件中告诉它 compile "org.igniterealtime.smack:smack-android-extensions:4.2.0-alpha3" compile "org.igniterealtime.smack:smack-tcp:4.2.0-alpha3"