'CMake set environment variable

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


Solution 1:[1]

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.

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vre
Solution 2 Pugsley