如何在自定义CMake命令中包含文字双引号? [英] How do I include a literal double quote in a custom CMake command?
问题描述
我正在尝试创建一个自定义命令,该命令与某些环境变量(例如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)
产量"LDFLAGS=\"-Ldir -Ldir2\"" echo blah
COMMAND LDFLAGS=\"-Ldir -Ldir2\" echo blah VERBATIM)
产量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
变体,这些变体可以工作并且不需要外壳,但是-我不得不承认-仍然部分依赖于平台.
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
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:
替代(对于CMake> = 3.0)
Alternative (for CMake >= 3.0)
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:何时引用变量?
- 0005145: Set environment variables for ADD_CUSTOM_COMMAND/ADD_CUSTOM_TARGET
- How to modify environment variables passed to custom CMake target?
- [CMake] How to set environment variable for custom command
- cmake: when to quote variables?
这篇关于如何在自定义CMake命令中包含文字双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!