Perhaps I am missing something obvious, but I can't seem to figure out how to explicitly set environment variables that can be seen by processes launched through add_custom_target()
.
I tried the following:
set(ENV{PATH} "C:/Some/Path;$ENV{PATH}")
add_custom_target(newtarget somecommand)
Unfortunately, the %PATH%
environment variable appears unchanged to somecommand
. (I have set up a Gist that reproduces the problem here.)
What am I doing wrong?
A portable way of setting environment variables for a custom target is to use CMake's command-line tool mode command
env
:E.g.:
Also see Command Line Tool Mode.
You set environment variable at configuration step, but command specified for
add_custom_target
is executed at build step. See also CMake FAQ: How can I get or set environment variables?Currently
add_custom_target
(and others commands, which define actions for build step, e.g.add_custom_command
) doesn't support simple setting environment variables. As adviced in this bugreport, for set variable's value without spaces on Linux you may prepend command with "VAR=VAL" clauses. For general cases you may prepare wrapper script, which setups environment and run actual command:On Windows:
wrapper.bat:
CMakeLists.txt:
On Linux:
wrapper.sh:
CMakeLists.txt:
If value of variable depends on configuration, you may configure wrapper script with
configure_file
.UPDATE:
As noted by @sakra,
env
tool mode ofcmake
executable can be used as a wrapper script:This way is available since CMake 3.2.