bilke / cmake-modules

Additional CMake functionality. Most of the modules are from Ryan Pavlik (https://github.com/rpavlik/cmake-modules)
Boost Software License 1.0
548 stars 213 forks source link

Issue with append_coverage_compiler_flags_to_target (but with solution) #70

Closed MuellerSeb closed 1 year ago

MuellerSeb commented 2 years ago

Hi there and thanks for this amazing repo!

I was trying to cleanup my coverage workflow and tried to replace append_coverage_compiler_flags with append_coverage_compiler_flags_to_target.

Two problems came across:

  1. I needed to replace (CodeCoverage.cmake L717):
    target_compile_options(${name} PRIVATE ${COVERAGE_COMPILER_FLAGS})

    with:

    separate_arguments(_flag_list NATIVE_COMMAND "${COVERAGE_COMPILER_FLAGS}")
    target_compile_options(${name} PRIVATE ${_flag_list})

    Since the white spaces were not recognized to split the flags everything was append to -g which resulted in:

    error: unrecognized debug output level ' -fprofile-arcs -ftest-coverage'
  2. Doing the above, I got another error, because the lib was not linked against gcov. So I could solve this by replacing:
    append_coverage_compiler_flags_to_target(${LIB_NAME})

    with:

    target_compile_options(${LIB_NAME} PRIVATE -g --coverage)
    target_link_libraries(${LIB_NAME} PRIVATE gcov)

    Note, that --coverage is equivalent to -fprofile-arcs -ftest-coverage (when compiling) and -lgcov (when linking). link

Maybe it could be a good idea to replace:

function(append_coverage_compiler_flags_to_target name)
    target_compile_options(${name}
        PRIVATE ${COVERAGE_COMPILER_FLAGS})
endfunction()

with

function(append_coverage_compiler_flags_to_target name)
    separate_arguments(_flag_list NATIVE_COMMAND "${COVERAGE_COMPILER_FLAGS}")
    target_compile_options(${name} PRIVATE ${_flag_list})
    target_link_libraries(${name} PRIVATE gcov)
endfunction()

And maybe we could also replace -fprofile-arcs -ftest-coverage with --coverage.

Cheers, Sebastian