Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a makefile update the calling environment?

Is it possible to update the environment from a makefile? I want to be able to create a target to set the client environment variables for them. Something like this:

AXIS2_HOME ?= /usr/local/axis2-1.4.1
JAVA_HOME  ?= /usr/java/latest
CLASSPATH  := foo foo

setenv:
    export AXIS2_HOME
    export JAVA_HOME
    export CLASSPATH

So that the client can simply do:

make setenv all
java MainClass

and have it work without them needing to set the classpath for the java execution themselves.

Or am I looking to do this the wrong way and there is a better way?

like image 440
brofield Avatar asked Mar 24 '09 03:03

brofield


2 Answers

No, you can't update the environment in the calling process this way. In general, a subprocess cannot modify the environment of the parent process. One notable exception is batch files on Windows, when run from a cmd shell. Based on the example you show, I guess you are not running on Windows though.

Usually, what you're trying to accomplish is done with a shell script that sets up the environment and then invokes your intended process. For example, you might write a go.sh script like this:

!#/bin/sh
AXIS2_HOME=/usr/local/axix2-1.4.1
JAVA_HOME=/usr/java/latest
CLASSPATH=foo foo
export AXIS2_HOME
export JAVA_HOME
export CLASSPATH
java MainClass

Make go.sh executable and now you can run your app as ./go.sh. You can make your script more elaborate too, if you like -- for example, you may want to make "MainClass" a parameter to the script rather than hard coding it.

like image 103
Eric Melski Avatar answered Sep 30 '22 01:09

Eric Melski


From your question I am assuming you're using the bash shell.

You can place the variable definitions in a shell script, like so:

AXIS2_HOME=/usr/local/axis2-1.4.1
export AXIS2_HOME
#etc

And then source the script into the current environment, with

source <filename>

or just

. <filename>

That executes the script in the current shell (i.e. no child process), so any environment changes the script makes will persist.

like image 28
Kieron Avatar answered Sep 30 '22 03:09

Kieron