Exporting environment variables to Makefile shell

makefileshell

I want to do immediate expansion of a shell command within a Makefile, but I want the shell command to have access to the environment variables within the Makefile. If I use the $(shell …), it expands immediately, but there is no access to the variables. If I use the backquotes, the expansion is not immediate, and it causes problems for me later in the Makefile. I'm wondering if there is any way to make the backquotes expand immediately, or to pass the current environment to a $(shell) command.

For example, the following makefile:

SOME_VAR := some_val
export SOME_VAR

VAR1 := `echo $$SOME_VAR`
export VAR1
VAR2 := `echo $$VAR1`

all:
      @echo VAR1=$(VAR1)
      @echo VAR2=$(VAR2)

Will output:

~/tmp/t2> make
VAR1=some_val
VAR2=`echo $SOME_VAR`

Where I want it to print "VAR2=some_val". The real example is a bit more complicated (environment variables are inherited from parent makefiles, and I'm trying to use a perl script to edit the variables), but the principle is the same.

Any help is appreciated.

Best Solution

Is this what you want?

VAR2 := $(shell VAR1="$(VAR1)" script_that_uses_var1)