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,所以我没有新的env命令可用在3.2。提前感谢!
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 ...
)被命名为 Bracket Argument ,并允许任何字符,而不需要添加反斜杠:
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:何时引用变量?
- 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屋!