somevar := apple export somevar update := $(shell echo "v=$$somevar") all: @echo $(update)
I was hoping to apple as output of command, however it's empty, which makes me think export and :=
variable expansion taking place on different phases. how to overcome this?
To pass down, or export, a variable, make adds the variable and its value to the environment for running each command. The sub-make, in turn, uses the environment to initialize its table of variable values. The special variables SHELL and MAKEFLAGS are always exported (unless you unexport them).
Variable values of the top-level make can be passed to the sub- make through the environment by explicit request. These variables are defined in the sub- make as defaults, but they do not override variables defined in the makefile used by the sub- make unless you use the ' -e ' switch (see Summary of Options).
?= indicates to set the KDIR variable only if it's not set/doesn't have a value. For example: KDIR ?= "foo" KDIR ?= "bar" test: echo $(KDIR)
Re: Setting Environment variable in MakefileTo get the shell to see one "$", you must use "$$" in the Makefile. 2.) Environment variables can only be inherited from parent to child processes, not vice versa. In this case, the parent process is the 'make' that is processing the Makefile.
The problem is that export
exports the variable to the subshells used by the commands; it is not available for expansion in other assignments. So don't try to get it from the environment outside a rule.
somevar := apple export somevar update1 := $(shell perl -e 'print "method 1 $$ENV{somevar}\n"') # Make runs the shell command, the shell does not know somevar, so update1 is "method 1 ". update2 := perl -e 'print "method 2 $$ENV{somevar}\n"' # Now update2 is perl -e 'print "method 2 $$ENV{somevar}\n"' # Lest we forget: update3 := method 3 $(somevar) all: echo $(update1) $(update2) echo $(update3) perl -e 'print method 4 "$$ENV{somevar}\n"'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With