Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile variable initialization and export

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?

like image 265
Pablo Avatar asked May 15 '10 02:05

Pablo


People also ask

How do I export a variable in makefile?

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).

How can we pass variables from top level makefile to sub makefiles?

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).

What does ?= Mean in makefile?

?= 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)

How do I set an environment variable in makefile?

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.


1 Answers

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"' 
like image 57
Beta Avatar answered Oct 05 '22 09:10

Beta