如何修改传递给自定义CMake目标的环境变量? [英] How to modify environment variables passed to custom CMake target?
问题描述
也许我缺少一些明显的东西,但我似乎不明白如何显式设置环境变量,可以通过 add_custom_target()
。
设定(ENV {PATH}C:/ Some / Path; $ ENV {PATH})$ b $不幸的是,%PATH%$ <$ p $%>
$ b pre pre pre $ p code>环境变量显示为 somecommand
。 (我设置了一个Gist,可以重现问题,此处。)
我做错了什么?
解决方案 ,但是在 add_custom_target
中指定的命令在 build 步骤中执行。另请参阅CMake常见问题解答:如何获取或设置环境变量?
[...]
环境变量 SET
对于cmake本身(配置时间),
生效,因此您不能使用此方法设置自定义命令可能需要的环境变量
(构建时间)。
禁止各种CMake命令的环境变量支持
(例如 add_custom_command()
,目前尚不支持),
可接受的解决方法可能是
目前 add_custom_target code>(和其他命令,其定义构建步骤的动作,例如 add_custom_command
)不支持简单设置环境变量。正如此错误报告中的建议,对于设置变量的值不带空格 em>在Linux上,可以在命令前加上VAR = VAL子句。对于一般情况,您可以准备脚本脚本,其中设置环境并运行实际命令:
在Windows上:
wrapper.bat :
@ECHO OFF
PATH =C:\\Some\\Path;%PATH%
%*
CMakeLists.txt :
add_custom_target(...
cmd / c $ {CMAKE_CURRENT_SOURCE_DIR} /wrapper.bat< real_command> args ...
)
在Linux上:
wrapper.sh :
exportPATH = / Some / Path:$ PATH
eval$ *
b $ b
CMakeLists.txt :
add_custom_target(...
COMMAND / bin / sh $ {CMAKE_CURRENT_SOURCE_DIR} /wrapper.sh< real_command> args ...
)
如果变量的值取决于配置,您可以使用 configure_file
配置包装脚本。
strong> UPDATE :
正如@sakra, env
工具模式 cmake
可用作封装脚本:
add_custom_target $ b COMMAND $ {CMAKE_COMMAND} -E envPATH = C:/ Some / Path; $ ENV {PATH}< real_command> args ...
)
这种方式自CMake 3.2起可用。
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?
解决方案 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?
[...]
environment variables SET
in the CMakeLists.txt only
take effect for cmake itself (configure-time),
so you cannot use this method to set an environment variable
that a custom command might need (build-time).
Barring environment variable support by various CMake commands
(e.g. add_custom_command()
, currently not supported yet),
an acceptable workaround may be to invoke shell scripts instead
which wrap the commands to be executed.
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:
@ECHO OFF
set PATH="C:\\Some\\Path;%PATH%"
%*
CMakeLists.txt:
add_custom_target(...
COMMAND cmd /c ${CMAKE_CURRENT_SOURCE_DIR}/wrapper.bat <real_command> args...
)
On Linux:
wrapper.sh:
export "PATH=/Some/Path:$PATH"
eval "$*"
CMakeLists.txt:
add_custom_target(...
COMMAND /bin/sh ${CMAKE_CURRENT_SOURCE_DIR}/wrapper.sh <real_command> args...
)
If value of variable depends on configuration, you may configure wrapper script with configure_file
.
UPDATE:
As noted by @sakra, env
tool mode of cmake
executable can be used as a wrapper script:
add_custom_target(...
COMMAND ${CMAKE_COMMAND} -E env "PATH=C:/Some/Path;$ENV{PATH}" <real_command> args...
)
This way is available since CMake 3.2.
这篇关于如何修改传递给自定义CMake目标的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!