问题描述
我有一个变量
SET(CODE_COVERAGE_EXCLUSION_LIST
""
CACHE STRING "List of resources to exclude from code coverage analysis")
它必须包含一个表达式列表,例如:'tests / *''/ usr / *'
It must contain a list of expressions such as : 'tests/*' '/usr/*'
尝试将默认值设置为上述表达式时,单个引号已删除。
When trying to set the default value to the above expressions, the single quotes are removed.
如何保存它们?
此外,当我尝试通过这样的排除列表时
Moreover, when I try to pass the exclusion list like this
cmake -DCODE_COVERAGE_EXCLUSION_LIST="'tests/*' '/usr/*'" ..
初始和最终单引号均丢失。
The initial and final single quotes are lost. How to preserve them as well ?
最后,在使用cmake-gui时,同样的问题同样适用。
Finally, the same question applies when using cmake-gui.
编辑:我试图使用反斜杠来避免引号:
EDIT : I tried to use backslash to escape the quotes :
SET(CODE_COVERAGE_EXCLUSION_LIST
" \'tests/*\' \'/usr/*\'"
CACHE STRING "List of resources to exclude from code coverage analysis : ")
它给了我以下错误:
Syntax error in cmake code at
xxx.cmake:106
when parsing string
\'tests/*\' \'/usr/*\'
Invalid escape sequence \'
EDIT2:add_custom_target的代码(而不是add_custom_command,我不好)
EDIT2 : code of the add_custom_target (and not add_custom_command, my bad)
ADD_CUSTOM_TARGET(${_targetname}
# Cleanup lcov
${LCOV_PATH} --directory . --zerocounters
# Run tests
COMMAND ${_testrunner} ${ARGV3}
# Capturing lcov counters and generating report
COMMAND ${LCOV_PATH} --directory . --capture --output-file ${_outputname}.info
COMMAND ${LCOV_PATH} --remove ${_outputname}.info 'tests/*' '/usr/*' ${CODE_COVERAGE_EXCLUSION_LIST} --output-file ${_outputname}.info.cleaned
COMMAND ${GENHTML_PATH} -o ${_outputname} ${_outputname}.info.cleaned
COMMAND ${CMAKE_COMMAND} -E remove ${_outputname}.info ${_outputname}.info.cleaned
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
)
推荐答案
我的评论
首先-考虑为什么您还需要这些引号-我可以重现您的问题并找到几种可能的解决方案:
First - taking the question why you need those quotes aside - I could reproduce your problem and found several possible solutions:
-
在缓存变量的开头和结尾添加空格
Adding spaces at the begin and end of your cached variable
cmake -DCODE_COVERAGE_EXCLUSION_LIST=" 'tests/*' '/usr/*' " ..
使用转义双引号代替单引号
Using "escaped" double quotes instead of single quotes
cmake -DCODE_COVERAGE_EXCLUSION_LIST:STRING="\"tests/*\" \"/usr/\"" ..
通过设置策略开关随CMake 3.1一起推出:
Using your set(... CACHE ...)
example by setting policy CMP0053 switch introduced with CMake 3.1:
cmake_policy(SET CMP0053 NEW)
set(CODE_COVERAGE_EXCLUSION_LIST
"\'tests/*\' \'/usr/*\'"
CACHE STRING "List of resources to exclude from code coverage analysis : ")
但是在代码中进行设置时,我也可以
But when setting this in the code I could also just do
set(CODE_COVERAGE_EXCLUSION_LIST
"'tests/*' '/usr/*'"
CACHE STRING "List of resources to exclude from code coverage analysis : ")
从命令行调用报价问题似乎只是一个问题
The quoting issue seems only to be a problem when called from command line
然后-如果我确实认为您可能不需要引号-您可以将路径作为列表传递:
Then - if I do assume you may not need the quotes - you could pass the paths as a list:
-
在
COMMAND
cmake -DCODE_COVERAGE_EXCLUSION_LIST:STRING="tests/*;/usr/*" ..
with
add_custom_target(
...
COMMAND ${LCOV_PATH} --remove ${_outputname}.info ${CODE_COVERAGE_EXCLUSION_LIST} --output-file ${_outputname}.info.cleaned
)
会给出类似的内容
.../lcov --remove output.info tests/* /usr/* --output-file output.info.cleaned
我也尝试添加 VERBATIM
选项,因为对于构建工具,将正确转义命令的所有参数,以便调用的命令不变地接收每个参数。
I also tried to add the VERBATIM
option, because "all arguments to the commands will be escaped properly for the build tool so that the invoked command receives each argument unchanged". But in this case, it didn't change anything.
参考
add_custom_target()
- CMake Language: Escape Sequences
- 0015200: Odd quoting issue when mixing single, double and escaped quotes to COMMAND
这篇关于如何在CMake缓存变量中保留单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!