Suppose one wants to use the environment variable MY_VAR
in CMake. This may be accomplished simply by using set(myVar $ENV{MY_VAR})
.
But what if MY_VAR
contains escape characters, say MY_VAR="/path/with/escaped\ chars"
? CMake treats \
followed by a white-space as two individual characters, instead of a single character (white-space). In other words,
# CMakeLists.txt
set(myVar $ENV{MY_VAR})
message(${myVar})
prints /path/with/escaped\ chars
, not /path/with/escaped chars
. How does one get CMake to recognize escape characters in environment variables? Are there any best practices regarding this problem? I'm running CMake on macOS, but hope that there is a platform independent solution...
Context: Cmake is used to configure a C++
framework before installation. The MY_VAR
contains a path provided by the user that is used (a) in a Makefile to set the CMAKE_PREFIX_PATH
and (b) by Cmake to configure_file
a configuration file for a python script that is required by the framework.