1

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."
)
Barth
  • 15,135
  • 20
  • 70
  • 105
  • You have to escape the char with a backslash like in `\'` – usr1234567 May 11 '16 at 08:02
  • Have you tried to escape the quotes with `\ `? Would it be an option to don't quote the paths and just do `cmake -DCODE_COVERAGE_EXCLUSION_LIST="tests/*;/usr/*"`? – Florian May 11 '16 at 08:05
  • @usr1234567 @Florian I have tried to escape with backslash but it says `Invalid escape sequence \'`. – Barth May 11 '16 at 08:37
  • @Florian I need the single quotes because the string is then used in a custom command. – Barth May 11 '16 at 08:37
  • 2
    @Barth Ok, could you please add the `add_custom_command()` code? Because the semicolon separated CMake list `"tests/*;/usr/*"` should normally be expanded to parameters again (with spaces as delimiter). And if you need the parameters quoted you could try adding the `VERBATIM` option. – Florian May 11 '16 at 08:45
  • It's not clear to me why exactly you need single quotes. What you need is a separator, and a list separator for cmake is the semicolon `;`. Please show how you use the input variable in your code. – Antonio May 11 '16 at 11:24

1 Answers1

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

Florian
  • 39,996
  • 9
  • 133
  • 149