Skip to content

Commit 9a57aa0

Browse files
authored
build: limit coverage flags to specific target (#71)
* build: only set coverage flags to `errors_test` * build: append a library source files instead of linking the library * build: append compile features of `errors` to `errors_test`
1 parent d7b8dcd commit 9a57aa0

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

CMakeLists.txt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,24 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
3030
cpmaddpackage("gh:catchorg/[email protected]")
3131
include("${Catch2_SOURCE_DIR}/extras/Catch.cmake")
3232

33+
# Append the main library properties instead of linking the library.
34+
get_target_property(errors_SOURCES errors SOURCES)
35+
get_target_property(errors_INCLUDES errors INCLUDE_DIRECTORIES)
36+
get_target_property(errors_LIBRARIES errors LINK_LIBRARIES)
37+
get_target_property(errors_FEATURES errors COMPILE_FEATURES)
38+
3339
# Build tests for the main library
34-
add_executable(errors_test test/error_test.cpp)
35-
target_link_libraries(errors_test PRIVATE errors Catch2::Catch2WithMain)
40+
add_executable(errors_test test/error_test.cpp ${errors_SOURCES})
41+
target_include_directories(errors_test PRIVATE ${errors_INCLUDES})
42+
target_link_libraries(errors_test PRIVATE Catch2::Catch2WithMain ${errors_LIBRARIES})
43+
target_compile_features(errors_test PRIVATE ${errors_FEATURES})
44+
45+
# Enable support to check for test coverage
46+
if(NOT MSVC)
47+
target_compile_options(errors_test PRIVATE --coverage -O0)
48+
target_link_options(errors_test PRIVATE --coverage)
49+
endif()
50+
3651
catch_discover_tests(errors_test)
3752
endif()
3853

@@ -43,12 +58,6 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
4358
foreach(TARGET IN LISTS TARGETS)
4459
# Statically analyze code by checking for warnings
4560
target_check_warning(${TARGET})
46-
47-
# Enable support to check for test coverage
48-
if(BUILD_TESTING AND NOT MSVC)
49-
target_compile_options(${TARGET} PRIVATE --coverage -O0)
50-
target_link_options(${TARGET} PRIVATE --coverage)
51-
endif()
5261
endforeach()
5362

5463
# Build XML documentation

0 commit comments

Comments
 (0)