[CMake] Setting environment variables

Chris Hillery chillery-cmake at lambda.nu
Tue Sep 14 07:14:00 EDT 2010


On Tue, Sep 14, 2010 at 4:03 AM, Gerhard Stengel <gerhards2 at ast.dfs.de>wrote:

> I've experienced the same problem and unfortunately, I think there's no way
> to set them permanently. The reason seems to
> be that cmake invokes subshells for most of the commands, so the change of
> the environment variable only happens in the
> subshell and never reaches the parent.
>

This would be true if he were executing commands to modify the environment.
However, that's not what he said he was doing, at least if I understood him
correctly. Certainly the SET (ENV{FOO} ...) calls in his envsetup.cmake
should work, at least.

When he said

and sometimes a few execute_process calls which store the output into an
> environment variable
>

I was assuming he meant something like

  EXECUTE_PROCESS (COMMAND /bin/mycmd OUTPUT_VARIABLE ENV{FOO})

which, I just discovered, does NOT work even though I figured it would.

Johny, if that's what you were trying, replace the above with

  EXECUTE_PROCESS (COMMAND /bin/mycmd OUTPUT_VARIABLE tempvar)
  SET (ENV{FOO} "${tempvar}")

That does work as it should.

If, as Gerhard suspects, you're doing something like

  EXECUTE_PROCESS (COMMAND /my/custom/envprogram)

and expecting envprogram to be able to modify your environment, however,
that won't work for the reasons Gerhard and I said - the command is
executing in a subshell, and by definition a subshell cannot affect the
environment of the parent. (At least on Unix.)

Ceej
aka Chris Hillery
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20100914/b2c6e553/attachment.htm>


More information about the CMake mailing list