8

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
  • 863
  • 1
  • 9
  • 24
Pedro Vicente
  • 593
  • 2
  • 8
  • 18

2 Answers2

0

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
  • 4,836
  • 1
  • 20
  • 34
  • 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
  • 1
    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
0

Prefix your command with cmake -E env XXX=YYY.

To test, use cmake -E env XXX=YYY cmake -E environment.

This command appears to be available since CMake 3.1. For more info see https://cmake.org/cmake/help/latest/manual/cmake.1.html#run-a-command-line-tool.

Pugsley
  • 867
  • 12
  • 12