最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

gnu make - How to unconditionnally re-assign a variable in a makefile (with submakefile)? - Stack Overflow

programmeradmin0浏览0评论

I am trying to re-assign a makefile variable (depending on its initial value), while keeping the new value in a submakefile. Here is the layout:

Makefile
subdir/
    - Makefile-sub

Main makefile:

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)

If I run make, I get the expected output:

In main makefile, ARCH=good_value
In sub makefile, ARCH=good_value

But if I try to override the variable from the CLI (which I need to be able to do), it doesn't work anymore: make ARCH=bad_value yields

In main makefile, ARCH=bad_value
In sub makefile, ARCH=bad_value

And make -E ARCH=bad_value or make -E "ARCH=bad_value" or make -E "ARCH:=bad_value" or make -E "export ARCH=bad_value" or make -E "export ARCH:=bad_value" yields

In main makefile, ARCH=good_value
In sub makefile, ARCH=bad_value

I noticed that calling the makefile as @make --no-print-directory -C subdir ARCH=$(ARCH) reassigns the value correctly, but I reckon I shouldn't need to do it, and I'd rather avoid it since I have a LOT of variables to reassign, and sub-sub-makefiles. Is there another solution to my problem ?

I am trying to re-assign a makefile variable (depending on its initial value), while keeping the new value in a submakefile. Here is the layout:

Makefile
subdir/
    - Makefile-sub

Main makefile:

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)

If I run make, I get the expected output:

In main makefile, ARCH=good_value
In sub makefile, ARCH=good_value

But if I try to override the variable from the CLI (which I need to be able to do), it doesn't work anymore: make ARCH=bad_value yields

In main makefile, ARCH=bad_value
In sub makefile, ARCH=bad_value

And make -E ARCH=bad_value or make -E "ARCH=bad_value" or make -E "ARCH:=bad_value" or make -E "export ARCH=bad_value" or make -E "export ARCH:=bad_value" yields

In main makefile, ARCH=good_value
In sub makefile, ARCH=bad_value

I noticed that calling the makefile as @make --no-print-directory -C subdir ARCH=$(ARCH) reassigns the value correctly, but I reckon I shouldn't need to do it, and I'd rather avoid it since I have a LOT of variables to reassign, and sub-sub-makefiles. Is there another solution to my problem ?

Share Improve this question asked Feb 1 at 0:03 Magyar_57Magyar_57 571 silver badge8 bronze badges 6
  • Recursive make is considered harmful, but have you considered factoring out the variables and having the sub-makefiles include ../vars.make? – o11c Commented Feb 1 at 1:11
  • That's exactly what I'm doing already, and I'm re-assigning the variable in a Config.mk file. Are you suggesting to include this file AGAIN in the sub-makefile ? – Magyar_57 Commented Feb 1 at 11:00
  • It does work, but implies that I need to include the Config.mk in all submakefiles. Isn't the point of the "export VAR" to have the variable accessible in subprocess ? – Magyar_57 Commented Feb 1 at 11:05
  • You can include extra makefiles from the make command using multiple -f arguments, like make -f config.mk -f makefile – Andreas Commented Feb 1 at 13:37
  • By process of elimination, I guess that the result you want to achieve in the override case is bad_value from the main make and good_value from the recursive make. Please edit the question to make it explicit about that, or if I'm mistaken then to specify whatever it is you do want. – John Bollinger Commented Feb 1 at 14:28
 |  Show 1 more comment

1 Answer 1

Reset to default 1

You could use 3 sets of variables:

  • DEFAULT_VAR to define the default value of variable VAR,
  • TOP_VAR to be used in the top Makefile instead of VAR,
  • VAR to be used in sub-Makefiles.

This would imply modifications of your top Makefile but only to this one (replace all uses of ARCH with TOP_ARCH):

$ 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

If you have many such variables and your make is GNU make you can automate most of this:

$ 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:$(foreach v,$(VARIABLES), TOP_$v=$(TOP_$v))
    $(MAKE) --no-print-directory -C subdir

$ cat subdir/Makefile
all:
    @echo In sub makefile:$(foreach v,ARCH BIN COMP, $v=$($v))

$ 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
发布评论

评论列表(0)

  1. 暂无评论