问题描述
我正在尝试创建一个使用一些环境变量(例如 LDFLAGS)运行的自定义命令,如果它包含空格,则需要引用其值:
I'm trying to create a custom command that runs with some environment variables, such as LDFLAGS, whose value needs to be quoted if it contains spaces:
LDFLAGS="-Lmydir -Lmyotherdir"
由于 CMake 的转义规则,我找不到在 CMake 自定义命令中包含此参数的方法.这是我迄今为止尝试过的:
I cannot find a way to include this argument in a CMake custom command, due to CMake's escaping rules. Here's what I've tried so far:
COMMAND LDFLAGS="-Ldir -Ldir2" echo blah VERBATIM)
yields "LDFLAGS="-Ldir -Ldir2"" echo blah
COMMAND LDFLAGS="-Ldir -Ldir2" echo blah VERBATIM)
yields LDFLAGS="-Ldir -Ldir2" echo blah
似乎我要么引用了整个字符串,要么在用作命令的一部分时转义的引号无法解析.
It seems I either get the whole string quoted, or the escaped quotes don't resolve when used as part of the command.
我希望包含文字双引号的方法或作为替代方法为命令设置环境变量的更好方法.请注意,我仍在使用 CMake 2.8,所以我在 3.2 中没有新的env"命令.
I would appreciate either a way to include the literal double-quote or as an alternative a better way to set environment variables for a command. Please note that I'm still on CMake 2.8, so I don't have the new "env" command available in 3.2.
请注意,这不是何时引用变量? 因为这些引用方法都不适用于这种特殊情况.
Note that this is not a duplicate of When to quote variables? as none of those quoting methods work for this particular case.
推荐答案
显而易见的选择 - 通常建议在遇到 COMMAND
的边界时,尤其是在旧版本的 CMake 中 - 是使用外部脚本.
The obvious choice - often recommended when hitting the boundaries of COMMAND
especially with older versions of CMake - is to use an external script.
我只是想添加一些简单的 COMMAND
变体,这些变体只是可以工作并且不需要 shell,但是 - 我必须承认 - 仍然部分依赖于平台.
I just wanted to add some simple COMMAND
only variations that do work and won't need a shell, but are - I have to admit - still partly platform dependent.
一个例子是只将引用的部分放入变量中:
One example would be to put only the quoted part into a variable:
set(vars_as_string "-Ldir -Ldir2")
add_custom_target(
QuotedEnvVar
COMMAND env LD_FLAGS=${vars_as_string} | grep LD_FLAGS
)
实际上确实转义了空格而不是引号.
Which actually does escape the space and not the quotes.
另一个例子是添加转义引号作为启动器"规则:
Another example would be to add it with escaped quotes as a "launcher" rule:
add_custom_target(
LauncherEnvVar
COMMAND env | grep LD_FLAGS
)
set_target_properties(
LauncherEnvVar
PROPERTIES RULE_LAUNCH_CUSTOM "env LD_FLAGS="-Ldir -Ldir2""
)
编辑:添加了多个引用参数的示例,无需转义引号
Edit: Added examples for multiple quoted arguments without the need of escaping quotes
另一个例子是在一个函数中隐藏一些复杂性",如果你想把它添加到你所有的自定义命令调用中 - 使用 global/directory
RULE_LAUNCH_CUSTOM
属性:
function(set_env)
get_property(_env GLOBAL PROPERTY RULE_LAUNCH_CUSTOM)
if (NOT _env)
set_property(GLOBAL PROPERTY RULE_LAUNCH_CUSTOM "env")
endif()
foreach(_arg IN LISTS ARGN)
set_property(GLOBAL APPEND_STRING PROPERTY RULE_LAUNCH_CUSTOM " ${_arg}")
endforeach()
endfunction(set_env)
set_env(LDFLAGS="-Ldir1 -Ldir2" CFLAGS="-Idira -Idirb")
add_custom_target(
MultipleEnvVar
COMMAND env | grep -E 'LDFLAGS|CFLAGS'
)
替代方案(适用于 CMake >= 3.0)
Alternative (for CMake >= 3.0)
我认为我们在这里真正寻找的东西(除了
cmake -E env ...
)被命名为 括号参数 并且允许任何字符而无需添加反斜杠:
I think what we actually are looking for here (besides the
cmake -E env ...
) is named Bracket Argument and does allow any character without the need of adding backslashes:
set_property(
GLOBAL PROPERTY
RULE_LAUNCH_CUSTOM [=[env LDFLAGS="-Ldir1 -Ldir2" CFLAGS="-Idira -Idirb"]=]
)
add_custom_target(
MultipleEnvVarNew
COMMAND env | grep -E 'LDFLAGS|CFLAGS'
)
参考资料
- 0005145:为 ADD_CUSTOM_COMMAND/ADD_CUSTOM_TARGET 设置环境变量
- 如何修改传递给自定义 CMake 的环境变量目标?
- [CMake] 如何为自定义命令设置环境变量
- cmake:何时引用变量?
这篇关于如何在自定义 CMake 命令中包含文字双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!