From a2a50927e90570103ec17ad201348182007f8c7e Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 1 Aug 2016 05:33:09 +0000 Subject: [PATCH] CMake: make code coverage work with Clang. Clang uses a slightly different .gcno/.gcda format than GCC, and it is necessary to point lcov to `llvm-cov gcov` instead of `gcov`. --- CMakeLists.txt | 22 +++++++++++++++++++--- test/CMakeLists.txt | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index baa3c168..b83974e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,10 @@ if(NOT WIN32 AND NOT APPLE) set(GUI gtk2 CACHE STRING "GUI toolkit to use (one of: gtk2 gtk3)") endif() +if(NOT CMAKE_C_COMPILER_ID STREQUAL CMAKE_CXX_COMPILER_ID) + message(FATAL_ERROR "C and C++ compilers should be supplied by the same vendor") +endif() + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) # GCC 4.8/4.9 ship with broken but present . meh. @@ -61,7 +65,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") endif() if(ENABLE_SANITIZE) - if(NOT (CMAKE_C_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")) + if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") message(FATAL_ERROR "Sanitizers are only available when using Clang/Clang++") endif() set(SANITIZE_FLAGS "-O1 -fno-omit-frame-pointer -fno-optimize-sibling-calls") @@ -172,10 +176,22 @@ else() # Linux and compatible systems endif() if(ENABLE_COVERAGE) + if(CMAKE_CXX_COMPILER_ID STREQUAL GNU) + find_program(GCOV gcov) + elseif(CMAKE_CXX_COMPILER_ID MATCHES Clang) + find_program(LLVM_COV llvm-cov) + + if(LLVM_COV) + set(GCOV ${CMAKE_CURRENT_BINARY_DIR}/llvm-gcov.sh) + file(WRITE ${GCOV} "#!/bin/sh -e\n${LLVM_COV} gcov $*") + execute_process(COMMAND chmod +x ${GCOV}) + endif() + endif() + find_program(LCOV lcov) find_program(GENHTML genhtml) - if(NOT LCOV OR NOT GENHTML) - message(FATAL_ERROR "lcov is required for producing coverage reports") + if(NOT GCOV OR NOT LCOV OR NOT GENHTML) + message(FATAL_ERROR "gcov/llvm-cov and lcov are required for producing coverage reports") endif() endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ce94b1d2..096f1192 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,6 +24,7 @@ add_custom_target(solvespace-test ALL if(ENABLE_COVERAGE) set(LCOV_FLAGS -q --rc lcov_branch_coverage=1 --rc lcov_excl_line=ssassert) + set(LCOV_FLAGS ${LCOV_FLAGS} --gcov-tool ${GCOV}) set(LCOV_COLLECT -c -b ${CMAKE_SOURCE_DIR}/src -d ${CMAKE_BINARY_DIR}/src --no-external) add_custom_command(