export ARCH:=good_value
all:
@echo In main makefile, ARCH=$(ARCH)
@make --no-print-directory -C subdir
SUB-MAKEFILE:
all:
@echo In sub makefile, ARCH=$(ARCH)
如果我运行
make
,我会得到预期的输出:
In main makefile, ARCH=good_value
In sub makefile, ARCH=good_value
但是,如果我尝试覆盖CLI的变量(我需要能够做到),它将不再起作用:
make ARCH=bad_value
屈服
In main makefile, ARCH=bad_value
In sub makefile, ARCH=bad_value
,
make -E ARCH=bad_value
或make -E "ARCH=bad_value"
或
make -E "ARCH:=bad_value"
或
make -E "export ARCH=bad_value"
或make -E "export ARCH:=bad_value"
屈服In main makefile, ARCH=good_value
In sub makefile, ARCH=bad_value
我注意到,将makefile称为@make --no-print-directory -C subdir ARCH=$(ARCH)
正确地重新分配该值,但是我认为我不需要这样做,我宁愿避免使用它,因为我有很多变量可以重新分配,并且makefiles。
还有我的问题解决方案吗?
您可以使用3组变量:
DEFAULT_VAR
VAR
,
TOP_VAR
用于在顶部file而不是
VAR
,,
VAR
要在子摄影files中使用。
这意味着您的顶级file修改,但只有一个:$ cat Makefile
TOP_ARCH := good_value
export ARCH := good_value
all:
@echo In main makefile, TOP_ARCH=$(TOP_ARCH)
$(MAKE) --no-print-directory -C subdir
$cat subdir/Makefile
all:
@echo In sub makefile, ARCH=$(ARCH)
$ make
In main makefile, TOP_ARCH=good_value
make --no-print-directory -C subdir
In sub makefile, ARCH=good_value
$ make TOP_ARCH=bad_value
In main makefile, TOP_ARCH=bad_value
make --no-print-directory -C subdir
In sub makefile, ARCH=good_value
如果您有许多这样的变量,并且您的成品是GNU使您可以自动化大部分:$ cat Makefile
VARIABLES := ARCH BIN COMP
DEFAULT_ARCH := good_value
DEFAULT_BIN := good_value
DEFAULT_COMP := good_value
$(foreach v,$(VARIABLES),$(eval TOP_$v=$(DEFAULT_$v)$(eval export $v=$(DEFAULT_$v))))
all:
@echo In main makefile, TOP_ARCH=$(TOP_ARCH), TOP_BIN=$(TOP_BIN), TOP_COMP=$(TOP_COMP)
$(MAKE) --no-print-directory -C subdir
$ cat subdir/Makefile
all:
@echo In sub makefile, ARCH=$(ARCH), BIN=$(BIN), COMP=$(COMP)
$ make TOP_ARCH=bad_value TOP_COMP=bad_value
In main makefile, TOP_ARCH=bad_value, TOP_BIN=good_value, TOP_COMP=bad_value
make --no-print-directory -C subdir
In sub makefile, ARCH=good_value, BIN=good_value, COMP=good_value