如何在makefile中无条件重新分配一个变量(带有subsakefile)? 我正在尝试重新分配一个makefile变量(取决于其初始值),同时将新值保留在亚基文件中。 这是布局: makefile subdir/ - makefile-sub 主要makefile: ...

问题描述 投票:0回答:1

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
makefile gnu-make
1个回答
0
投票
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


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.