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

CodeCoverage: clang requires llvm-cov #40

Open CJxD opened 4 years ago

CJxD commented 4 years ago

When using gcov with a project built using Clang, gcov is unable to parse the .gcno files, giving the error: version '402*', prefer 'A92*'.

This seems to be because llvm-cov is required. I've hacked around this myself by doing the following before including CodeCoverage.cmake:

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
    find_program(LLVMCOV_PATH llvm-cov)
    if(LLVMCOV_PATH)
        set(GCOV_PATH "${CMAKE_CURRENT_SOURCE_DIR}/llvm-cov.sh")
    endif()
endif()
include(CodeCoverage.cmake)

where llvm-cov.sh is the following (marked with executable permissions):

#!/bin/bash
exec llvm-cov gcov "$@"

Could this be added in with a somewhat less hacky workaround?

gollth commented 2 years ago

When you want to use the gcovr_* targets, another way could be to use the --gcov-executable option of gcovr:

include(CodeCoverage)

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  set(GCOVR_ADDITIONAL_ARGS --gcov-executable "llvm-cov gcov")
endif()

append_coverage_compiler_flags()

setup_target_for_coverage_gcovr_html(
  NAME       coverage-html
  EXECUTABLE ctest
)

Looking at its help it seems you could also set the GCOV environment variable instead:

$ gcovr --help
GCOV Options:
  The 'gcov' tool turns raw coverage files (.gcda and .gcno) into .gcov
  files that are then processed by gcovr. The gcno files are generated by
  the compiler. The gcda files are generated when the instrumented program
  is executed.

  --gcov-executable GCOV_CMD
                        Use a particular gcov executable. Must match the
                        compiler you are using, e.g. 'llvm-cov gcov' for
                        Clang. Can include additional arguments. Defaults to
                        the GCOV environment variable, or 'gcov': 'gcov'.