Skip to content

Commit

Permalink
cmake: improved coverage compiler flags handling
Browse files Browse the repository at this point in the history
  • Loading branch information
program-- authored and PhilMiller committed Dec 27, 2023
1 parent a91aa8e commit 153c533
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 49 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ endif()
if(NGEN_WITH_COVERAGE)
include(CodeCoverage)
append_coverage_compiler_flags()
link_libraries(--coverage)
endif()

# -----------------------------------------------------------------------------
Expand Down
72 changes: 24 additions & 48 deletions cmake/CodeCoverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
# - Add support for llvm-cov with gcov emulation
# - Modify gcovr functions to allow generating coverage even if tests fail
# - Modify coverage compiler flags from -O0 to -Og
# - Modify append compiler flags function to use CMake functions rather than
# modifying variables.
# - Include -fdiagnostics-absolute-paths/-fprofile-abs-path in coverage flags
# depending on compiler.
# - Removed language-specific compiler flags.
#
# USAGE:
#
Expand Down Expand Up @@ -179,48 +184,6 @@ foreach(LANG ${LANGUAGES})
endif()
endforeach()

set(COVERAGE_COMPILER_FLAGS "--coverage -Og"
CACHE INTERNAL "")
if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-fprofile-abs-path HAVE_cxx_fprofile_abs_path)
if(HAVE_cxx_fprofile_abs_path)
set(COVERAGE_CXX_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-abs-path")
endif()
include(CheckCCompilerFlag)
check_c_compiler_flag(-fprofile-abs-path HAVE_c_fprofile_abs_path)
if(HAVE_c_fprofile_abs_path)
set(COVERAGE_C_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-abs-path")
endif()
endif()

set(CMAKE_Fortran_FLAGS_COVERAGE
${COVERAGE_COMPILER_FLAGS}
CACHE STRING "Flags used by the Fortran compiler during coverage builds."
FORCE )
set(CMAKE_CXX_FLAGS_COVERAGE
${COVERAGE_COMPILER_FLAGS}
CACHE STRING "Flags used by the C++ compiler during coverage builds."
FORCE )
set(CMAKE_C_FLAGS_COVERAGE
${COVERAGE_COMPILER_FLAGS}
CACHE STRING "Flags used by the C compiler during coverage builds."
FORCE )
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used for linking binaries during coverage builds."
FORCE )
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
FORCE )
mark_as_advanced(
CMAKE_Fortran_FLAGS_COVERAGE
CMAKE_CXX_FLAGS_COVERAGE
CMAKE_C_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )

get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR GENERATOR_IS_MULTI_CONFIG))
message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
Expand Down Expand Up @@ -440,18 +403,31 @@ function(setup_target_for_coverage_gcovr_html)

endfunction() # setup_target_for_coverage_gcovr_html

# =============================================================================

# _fabs_path_arg will be a flag for the compiler that signals
# absolute paths should be used in .gcno files. This reduces the
# chance of issues, since relative paths are used by default.
if(${CMAKE_CXX_COMPILER_ID} MATCHES "LLVM|Clang")
set(_fabs_path_arg "-fdiagnostics-absolute-paths")
else()
set(_fabs_path_arg "-fprofile-abs-path")
endif()

set(COVERAGE_COMPILER_FLAGS
"--coverage -Og ${_fabs_path_arg}"
CACHE INTERNAL "")

function(append_coverage_compiler_flags)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
separate_arguments(_flag_list NATIVE_COMMAND "${COVERAGE_COMPILER_FLAGS}")
add_compile_options(${_flag_list})
link_libraries(--coverage)
message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
endfunction() # append_coverage_compiler_flags

# Setup coverage for specific library
function(append_coverage_compiler_flags_to_target name)
separate_arguments(_flag_list NATIVE_COMMAND "${COVERAGE_COMPILER_FLAGS}")
target_compile_options(${name} PRIVATE ${_flag_list})
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
target_link_libraries(${name} PRIVATE gcov)
endif()
target_link_libraries(${name} PRIVATE "--coverage")
endfunction()

0 comments on commit 153c533

Please sign in to comment.