@@ -6,6 +6,8 @@ project(omath VERSION ${OMATH_VERSION} LANGUAGES CXX)
66include (CMakePackageConfigHelpers)
77include (CheckCXXCompilerFlag)
88
9+ include (cmake/Coverage.cmake)
10+
911if (MSVC )
1012 check_cxx_compiler_flag("/arch:AVX2" COMPILER_SUPPORTS_AVX2)
1113else ()
@@ -23,7 +25,7 @@ option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OF
2325option (OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON )
2426option (OMATH_USE_UNITY_BUILD "Will enable unity build to speed up compilation" OFF )
2527option (OMATH_ENABLE_LEGACY "Will enable legacy classes that MUST be used ONLY for backward compatibility" ON )
26-
28+ option (OMATH_ENABLE_COVERAGE "Enable compiling tests with coverage. (Linux only)" ON )
2729
2830if (VCPKG_MANIFEST_FEATURES)
2931 foreach (omath_feature IN LISTS VCPKG_MANIFEST_FEATURES)
@@ -60,6 +62,7 @@ if (${PROJECT_IS_TOP_LEVEL})
6062 message (STATUS "[${PROJECT_NAME} ]: ImGUI integration feature status ${OMATH_IMGUI_INTEGRATION} " )
6163 message (STATUS "[${PROJECT_NAME} ]: Legacy features support ${OMATH_ENABLE_LEGACY} " )
6264 message (STATUS "[${PROJECT_NAME} ]: Building using vcpkg ${OMATH_BUILD_VIA_VCPKG} " )
65+ message (STATUS "[${PROJECT_NAME} ]: Coverage is ${OMATH_ENABLE_COVERAGE} " )
6366endif ()
6467
6568file (GLOB_RECURSE OMATH_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR} /source/*.cpp" )
@@ -76,6 +79,13 @@ add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
7679
7780target_compile_definitions (${PROJECT_NAME} PUBLIC OMATH_VERSION="${PROJECT_VERSION} " )
7881
82+ # If forward_helpers.cpp is present, compile it with inlining disabled so that
83+ # emitted symbols/debug-info are produced in that TU (helpful for coverage attribution).
84+ # Delegate coverage-related configuration to cmake/Coverage.cmake
85+ if (OMATH_ENABLE_COVERAGE AND CMAKE_HOST_LINUX AND OMATH_BUILD_TESTS)
86+ omath_setup_coverage_for_root(${PROJECT_NAME} )
87+ endif ()
88+
7989if (OMATH_IMGUI_INTEGRATION)
8090 target_compile_definitions (${PROJECT_NAME} PUBLIC OMATH_IMGUI_INTEGRATION)
8191
@@ -135,6 +145,11 @@ if (OMATH_USE_AVX2)
135145 endif ()
136146endif ()
137147
148+ if (EMSCRIPTEN)
149+ target_compile_options (${PROJECT_NAME} PRIVATE -fexceptions)
150+ target_link_options (${PROJECT_NAME} PRIVATE -fexceptions)
151+ endif ()
152+
138153target_compile_features (${PROJECT_NAME} PUBLIC cxx_std_23)
139154
140155if (OMATH_BUILD_TESTS)
@@ -150,6 +165,56 @@ if (OMATH_BUILD_EXAMPLES)
150165 add_subdirectory (examples)
151166endif ()
152167
168+ if (OMATH_ENABLE_COVERAGE AND CMAKE_HOST_LINUX AND OMATH_BUILD_TESTS)
169+ # Configure coverage flags per-compiler:
170+ # - For Clang/AppleClang use LLVM's instrumentation flags so the build
171+ # produces .profraw files usable by llvm-profdata/llvm-cov.
172+ # - For GCC use the traditional gcov flags (-fprofile-arcs -ftest-coverage).
173+ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|AppleClang" )
174+ target_compile_options (${PROJECT_NAME} PRIVATE
175+ $<$<CONFIG:Debug>:-g>
176+ $<$<CONFIG:Debug>:-fprofile-instr-generate>
177+ $<$<CONFIG:Debug>:-fcoverage-mapping>
178+ )
179+ # No special link flags needed for Clang llvm profile instrumentation.
180+ else ()
181+ # Default to GCC-style gcov instrumentation for other compilers.
182+ target_compile_options (${PROJECT_NAME} PRIVATE
183+ $<$<CONFIG:Debug>:-g>
184+ $<$<CONFIG:Debug>:-fprofile-arcs>
185+ $<$<CONFIG:Debug>:-ftest-coverage>
186+ )
187+ # Link-time flags to ensure coverage support for gcov
188+ target_link_libraries (${PROJECT_NAME} PRIVATE
189+ $<$<CONFIG:Debug>:-fprofile-arcs>
190+ $<$<CONFIG:Debug>:-ftest-coverage>
191+ )
192+ endif ()
193+
194+ # Normalize recorded source file paths in debug info and coverage by
195+ # rewriting build/source prefixes to a stable value. This helps tools
196+ # like geninfo/gcov map execution addresses to canonical header paths.
197+ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang" )
198+ # Map the absolute source and binary paths to a short placeholder
199+ file (TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR} " OMATH_SRC_PATH)
200+ file (TO_CMAKE_PATH "${CMAKE_CURRENT_BINARY_DIR} " OMATH_BUILD_PATH)
201+ string (REPLACE "/" "\\ /" OMATH_SRC_PATH_ESCAPED "${OMATH_SRC_PATH} " )
202+ string (REPLACE "/" "\\ /" OMATH_BUILD_PATH_ESCAPED "${OMATH_BUILD_PATH} " )
203+
204+ # Add compiler flags that rewrite recorded paths in debug info.
205+ # Map them to '.' so geninfo can find sources relative to the
206+ # build working directory used when collecting coverage.
207+ target_compile_options (${PROJECT_NAME} PRIVATE
208+ # Map source tree to one level up so geninfo running from the
209+ # binary directory can find ../include/... and ../source/...
210+ $<$<CONFIG:Debug>:-ffile-prefix -map=${OMATH_SRC_PATH} =..>
211+ $<$<CONFIG:Debug>:-ffile-prefix -map=${OMATH_BUILD_PATH} =.>
212+ $<$<CONFIG:Debug>:-fdebug-prefix -map=${OMATH_SRC_PATH} =..>
213+ $<$<CONFIG:Debug>:-fdebug-prefix -map=${OMATH_BUILD_PATH} =.>
214+ )
215+ endif ()
216+ endif ()
217+
153218if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND OMATH_THREAT_WARNING_AS_ERROR)
154219 target_compile_options (${PROJECT_NAME} PRIVATE /W4 /WX)
155220elseif (OMATH_THREAT_WARNING_AS_ERROR)
@@ -167,6 +232,8 @@ target_include_directories(${PROJECT_NAME}
167232 $<INSTALL_INTERFACE:include > # Use this path when the project is installed
168233)
169234
235+ # Coverage targets are configured by cmake/Coverage.cmake via
236+ # omath_setup_coverage_for_root().
170237
171238# Installation rules
172239
@@ -188,7 +255,6 @@ install(EXPORT ${PROJECT_NAME}Targets
188255 DESTINATION lib/cmake/${PROJECT_NAME} COMPONENT ${PROJECT_NAME}
189256)
190257
191-
192258# Generate the omathConfigVersion.cmake file
193259write_basic_package_version_file(
194260 "${CMAKE_CURRENT_BINARY_DIR} /omathConfigVersion.cmake"
0 commit comments