Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake Environmental variables

While using CMake and reading tons of other CMakeLists.txt files I noticed that set(ENV{variable_name} value) is used quite often. However, e.g. here https://cliutils.gitlab.io/modern-cmake/chapters/basics/variables.html the author mentions that these variables should be avoided without any further explanation. CMake documentation stands:

This command affects only the current CMake process, not the process from which CMake was called, nor the system environment at large, nor the environment of subsequent build or test processes.

My question is, is it really that bad practice to use environmental variables in CMakeLists.txt? What pitfalls must be expected with such usage?

like image 449
user1415536 Avatar asked Jul 06 '26 07:07

user1415536


1 Answers

Nothing bad in using environment variables in CMakeLists.txt.

For example, many FindXXX.cmake scripts actually use environment variables.

What pitfalls must be expected with such usage?

  1. After reading a path from the environment variable, transform it into cmake-style path (e.g. with file(TO_CMAKE_PATH)) before access a file at this path in CMake commands. Otherwise you could get a problem on Windows, which path separator ("\") differs from the CMake one ("/").

  2. Because setting environment variable affects only on configuration stage, this is rarely needed: better to use CMake variables instead.

    The only important exception: a toolchain file could be executed multiple times during the configuration. And setting an environment variable is the only way to store a value, calculated on the first toolchain invocation, for being usable in futher invocations. (Even CACHE variables don't work in that case).

like image 130
Tsyvarev Avatar answered Jul 07 '26 22:07

Tsyvarev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!