cmake:如何在自定义命令中包含文字双引号? [英] cmake: How to include literal double-quote in custom 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
yields "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命令。感谢提前!
Would appreciate either a way to include the literal double-quote or as an alternative a better way to set environement 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. Thanks in advance!
请注意,这不是 cmake:何时引用变量?,因为这些引用方法都不适用于这种特殊情况。
Note that this is not a duplicate of cmake: 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
-
另一个例子将会在一个函数中隐藏一些复杂性,如果你想的话将其添加到所有自定义命令调用中 - 使用全局/目录
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_TAR设置环境变量GET
- 如何修改传递给自定义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屋!