CMake set environment variable

Viewed 28954

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
2 Answers
Related