11

According to the CMake documentation

https://cmake.org/cmake/help/v3.3/command/set.html

One can do

set(ENV{<variable>} <value>)

but this gives the result

set(ENV{FOO} foo)
message("variable is $ENV{FOO}")

at configure time

variable is foo

But at Linux command

echo $FOO

the variable is not set.

EDIT:

Here's a partial solution to the problem, which was to set $PATH, so that a user has CMAKE_INSTALL_PREFIX listed first

set(file_sh ${CMAKE_CURRENT_BINARY_DIR}/path.sh)
set(path "${CMAKE_INSTALL_PREFIX}:$ENV{PATH}")
file(WRITE ${file_sh} "#!/usr/bin/env bash\n")
file(APPEND ${file_sh} "export PATH=\"${path}\"")
execute_process(COMMAND chmod a+x ${file_sh} RESULT_VARIABLE res)

this creates this file

#!/usr/bin/env bash
export PATH="/install/prefix/path:/other/path"

that later can be executed on a bash terminal with

source path.sh
Milan
  • 1,743
  • 2
  • 13
  • 36
Pedro Vicente
  • 681
  • 2
  • 9
  • 21

1 Answers1

-2

The last paragraph of the documentation you cited gives the answer:

Set the current process environment <variable> to the given value.

It influences the current process environment that is created when CMake is started from the shell. It is not the environment of the shell itself.

vre
  • 6,041
  • 1
  • 25
  • 39
  • 3
    ok , but the paragraph title says **Set Environment Variable** The Cmake documentation is not the best I've found – Pedro Vicente Sep 17 '17 at 22:25
  • 3
    I understand your complains about the documentation, but how the environment is provided to the process is operation system specific and similar in Unix/Linux and Windows. Every process (e.g. your bash) that creates a child process (your cmake) provides the newly process a copy of its environment. You cannot change the environment of the parent process later. But you can start a intermediate process that sets the environment variables and then starts cmake. – vre Sep 19 '17 at 17:50