GNU Make.范围限制为单个Makefile的变量

Ale*_*tko 10 makefile gnu-make

我尝试在我当前的项目中实现一个非递归的make构建系统.我挣扎的是变量范围.目标特定变量不符合我的需要,因为通常变量定义目标而不是先决条件.我需要的是:

Makefile1:

SOMEVAR := original_value
include Makefile2
$(warning $(SOMEVAR))
Run Code Online (Sandbox Code Playgroud)

Makefile2:

#some magic here to do what I want and make me happy
SOMEVAR := included_value
#and maybe here
Run Code Online (Sandbox Code Playgroud)

我想要的输出是'original_value'.

有什么策略可以让它成真吗?

编辑:我目前唯一的解决方案是强制和组织自己将所有inlcudes放在每个特定Makefile的末尾并使用立即变量赋值:=

Joh*_*all 6

一种策略是变量名称冲突的老式解决方案,当你拥有全局变量时:以穷人命名空间的形式为变量名添加前缀.

Makefile1:

Makefile1_SOMEVAR := original_value
include Makefile2
$(warning $(Makefile1_SOMEVAR))
Run Code Online (Sandbox Code Playgroud)

Makefile2:

# no magic needed
Makefile2_SOMEVAR := included_value
# rest of Makefile2 uses $(Makefile2_SOMEVAR) of course
Run Code Online (Sandbox Code Playgroud)

嘿presto,有这样的约定,好像每个makefile都有自己的局部变量(或者,至少,它自己的变量在命名空间中不与任何其他makefile冲突).