Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting environment variables to Makefile shell

Tags:

shell

makefile

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.

like image 901
John Avatar asked Mar 07 '12 17:03

John


People also ask

Are makefile variables environment variables?

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value. However, an explicit assignment in the makefile, or with a command argument, overrides the environment.

How do you use variables in makefile?

A variable is a name defined in a makefile to represent a string of text, called the variable's value . These values are substituted by explicit request into targets, dependencies, commands, and other parts of the makefile. (In some other versions of make , variables are called macros .)

What does $() mean in makefile?

The $@ and $< are called automatic variables. The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.


1 Answers

Is this what you want?

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

like image 81
Idelic Avatar answered Sep 17 '22 18:09

Idelic