11cmake_minimum_required (VERSION 3.12)
22project (GMGPolar VERSION 2.0.0 LANGUAGES CXX)
33
4+ # Options should be defined before they're used
45option (GMGPOLAR_BUILD_TESTS "Build GMGPolar unit tests." ON )
56option (GMGPOLAR_USE_LIKWID "Use LIKWID to measure code (regions)." OFF )
67option (GMGPOLAR_USE_MUMPS "Use MUMPS to solve linear systems." OFF )
8+ option (GMGPOLAR_ENABLE_COVERAGE "Enable code coverage reporting (requires GCC/Clang)" OFF )
79
810set (CMAKE_CXX_STANDARD 20)
911set (CMAKE_CXX_STANDARD_REQUIRED True )
1012
1113set (CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR} /cmake" ${CMAKE_MODULE_PATH} )
1214
15+ # Set build type - must come before compiler flags
1316if (NOT CMAKE_BUILD_TYPE )
1417 set (CMAKE_BUILD_TYPE Release)
1518endif ()
1619
20+ # Set compiler flags
1721set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -pedantic -Wno-unused -Wno-psabi" )
1822set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -mtune=generic -Wno-psabi" )
1923
20- # code coverage analysis
21- # Note: this only works under linux and with make
22- # Ninja creates different directory names which do not work together with this scrupt
23- # as STREQUAL is case-sensitive https://github.com/TriBITSPub/TriBITS/issues/131, also allow DEBUG as accepted input
24- option (GMGPOLAR_TEST_COVERAGE "Enable GCov coverage analysis (adds a 'coverage' target)" OFF )
25-
26- if (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
27- if (GMGPOLAR_TEST_COVERAGE)
28- message (STATUS "Coverage enabled" )
29- include (CodeCoverage)
30- append_coverage_compiler_flags()
31- setup_target_for_coverage_lcov(
32- NAME coverage
33- EXECUTABLE tests/gmgpolar_tests
34- LCOV_ARGS --ignore -errors gcov,mismatch
35- EXCLUDE "${CMAKE_SOURCE_DIR} /tests*" "${CMAKE_SOURCE_DIR} /src/InputFunctions*" "${CMAKE_BINARY_DIR} /*" "/usr*"
36- )
24+ # Set coverage compiler flags - must come before any targets are defined
25+ if (GMGPOLAR_ENABLE_COVERAGE)
26+ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" )
27+ message (STATUS "Enabling code coverage flags" )
28+ # Use generator expressions to apply only to specific configurations
29+ add_compile_options ($<$<CONFIG:Debug>:--coverage>)
30+ add_link_options ($<$<CONFIG:Debug>:--coverage>)
31+ # Force Debug build when coverage is enabled
32+ if (CMAKE_BUILD_TYPE STREQUAL "Release" )
33+ message (STATUS "Forcing Debug build for coverage" )
34+ set (CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE)
35+ endif ()
36+ else ()
37+ message (WARNING "Code coverage requires GCC or Clang. Current compiler: ${CMAKE_CXX_COMPILER_ID} " )
3738 endif ()
3839endif ()
3940
@@ -54,4 +55,40 @@ if(GMGPOLAR_BUILD_TESTS)
5455 enable_testing ()
5556 add_subdirectory (third-party)
5657 add_subdirectory (tests)
58+
59+ # Add coverage target - moved after test configuration
60+ if (GMGPOLAR_ENABLE_COVERAGE)
61+ find_program (LCOV_PATH lcov)
62+ find_program (GENHTML_PATH genhtml)
63+
64+ if (LCOV_PATH AND GENHTML_PATH)
65+ add_custom_target (coverage
66+ # Reset counters
67+ COMMAND ${LCOV_PATH} --directory ${CMAKE_BINARY_DIR} --zerocounters
68+ # Run tests
69+ COMMAND ctest --test -dir ${CMAKE_BINARY_DIR} || true
70+ # Capture coverage data
71+ COMMAND ${LCOV_PATH} --directory ${CMAKE_BINARY_DIR} --capture
72+ --output -file ${CMAKE_BINARY_DIR} /coverage.info
73+ --ignore -errors mismatch,unused
74+ --rc geninfo_unexecuted_blocks=1
75+ # Filter out system and unwanted directories
76+ COMMAND ${LCOV_PATH} --remove ${CMAKE_BINARY_DIR} /coverage.info
77+ '/usr/*'
78+ '*/tests/*'
79+ '*/third-party/*'
80+ '*/_deps/googletest-src/*'
81+ '*/include /InputFunctions/*'
82+ '*/src/InputFunctions/*'
83+ --output -file ${CMAKE_BINARY_DIR} /coverage-filtered.info
84+ --ignore -errors unused
85+ # Generate HTML report
86+ COMMAND ${GENHTML_PATH} ${CMAKE_BINARY_DIR} /coverage-filtered.info
87+ --output -directory ${CMAKE_BINARY_DIR} /coverage-report
88+ --ignore -errors source
89+ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
90+ COMMENT "Generating code coverage report"
91+ )
92+ endif ()
93+ endif ()
5794endif ()
0 commit comments