How to preserve single quotes in a CMake cached variable?

给你一囗甜甜゛ 提交于 2020-01-06 04:21:16

问题


I have a variable

SET(CODE_COVERAGE_EXCLUSION_LIST
""
CACHE STRING "List of resources to exclude from code coverage analysis")

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.

How to preserve them ?

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 ?

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 : ")

It gave me the following error :

 Syntax error in cmake code at
    xxx.cmake:106
  when parsing string
     \'tests/*\' \'/usr/*\'
  Invalid escape sequence \'

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."
)

回答1:


Turning my comments into an answer

First - taking the question why you need those quotes aside - I could reproduce your problem and found several possible solutions:

  1. Adding spaces at the begin and end of your cached variable

    cmake -DCODE_COVERAGE_EXCLUSION_LIST=" 'tests/*' '/usr/*' " ..
    
  2. Using "escaped" double quotes instead of single quotes

    cmake -DCODE_COVERAGE_EXCLUSION_LIST:STRING="\"tests/*\" \"/usr/\"" ..
    
  3. 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 : ")
    
  4. 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:

  1. A semicolon separated CMake list is expanded to parameters again (with spaces as delimiter) when used in a 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
    )
    

    would give something like

    .../lcov --remove output.info tests/* /usr/* --output-file output.info.cleaned
    
  2. 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.

References

  • add_custom_target()
  • CMake Language: Escape Sequences
  • 0015200: Odd quoting issue when mixing single, double and escaped quotes to COMMAND


来源:https://stackoverflow.com/questions/37156362/how-to-preserve-single-quotes-in-a-cmake-cached-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!