Discussion:
how to set an environment variable from within a makefile
Mark Galeck (CW)
2012-03-18 03:34:23 UTC
Permalink
Hello,

The export directive can be used to communicate variables to sub-makes.

Is there a way to communicate a variable up-stream, or between makefiles in the same depth of recursion. I thought I would try to setup an environment variable from within a makefile,
either using
FOOBAR=value
in a recipe,
or $(shell FOOBAR=value) , outside one.

But this does not seem to work - it only works in the context of that same makefile, but not outside of it,

How can I do this through environment variables?

(I can probably use writing to a file to store that information, but I would rather use env variables).


I somehow suspect this is a stupid question. Oh well, let Paul & others laugh at my naivety.

Mark
Mark Galeck (CW)
2012-03-18 03:38:08 UTC
Permalink
OK, doing it within a recipe is not going to work, except perhaps with .ONESHELL (which I can't use, I can't use 3.82). Please don't laugh.

Why not outside?
Post by Mark Galeck (CW)
Is there a way to communicate a variable up-stream, or between makefiles in the same depth of recursion. I thought I would try to setup an environment variable from within a makefile,
either using
FOOBAR=value
in a recipe,
Philip Guenther
2012-03-18 04:04:35 UTC
Permalink
Post by Mark Galeck (CW)
The export directive can be used to communicate variables to sub-makes.
Is there a way to communicate a variable up-stream, or between makefiles in the same depth of recursion.
I thought I would try to setup an environment variable from within a makefile, either using
               FOOBAR=value
in a recipe,
or $(shell FOOBAR=value) , outside one.
But this does not seem to work - it only works in the context of that same makefile, but not outside of it,
How can I do this through environment variables?
In UNIX, when a process creates a child process, the child process
gets its own environment, separate from that of the process that
created it. There's nothing the child can do to affect the
environment of its parent.

Programs that have needed to do this are generally written to output
to stdout the variable assignments that are desired and then document
that their caller, the program that invokes them, should capture that
output and evaluate it. For example, the 'tset' program is generally
invoked from shell startup scripts with something like this:
eval `tset -sQ $TERM`

it writes to its stdout something like:
TERM=screen;
TERMCAP='screen:am:bs:<560 more bytes of termcap info>';
export TERM TERMCAP

The eval `...` form then makes the shell capture that and parse it so
that the shell sets the variables in its own environment.
Post by Mark Galeck (CW)
(I can probably use writing to a file to store that information, but I would rather use env variables).
Write it to a file and then include that in your other Makefiles.


Philip Guenther
Michael Ludwig
2012-03-18 08:50:55 UTC
Permalink
Post by Philip Guenther
In UNIX, when a process creates a child process, the child process
gets its own environment, separate from that of the process that
created it. There's nothing the child can do to affect the
environment of its parent.
Programs that have needed to do this are generally written to output
to stdout the variable assignments that are desired and then document
that their caller, the program that invokes them, should capture that
output and evaluate it. For example, the 'tset' program […]
Another example is ssh-agent :

$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-jimlbwZEu764/agent.764; export SSH_AUTH_SOCK;
SSH_AGENT_PID=5064; export SSH_AGENT_PID;
echo Agent pid 5064;

Michael

Continue reading on narkive:
Search results for 'how to set an environment variable from within a makefile' (Questions and Answers)
4
replies
"installing wireless on linux"?
started 2006-08-07 20:33:47 UTC
computer networking
Loading...