Skip to content

Commit 77af8de

Browse files
committed
Refactor: add target_link_with_sanitizers() helper
1 parent 6e23b99 commit 77af8de

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ target_link_libraries(${PROJECT_NAME}
3131
PRIVATE arithmetic
3232
)
3333

34-
if(TARGET sanitizers)
35-
target_link_libraries(${PROJECT_NAME} PRIVATE sanitizers)
36-
endif()
34+
target_link_with_sanitizers(${PROJECT_NAME})
3735

3836
# === Unit Testing ===
3937
option(ENABLE_TESTING "Build tests" ON)

cmake/Sanitizers.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ if(CMAKE_BUILD_TYPE STREQUAL "Sanitize")
1111
target_compile_options(sanitizers INTERFACE ${SANITIZER_FLAGS})
1212
target_link_options(sanitizers INTERFACE ${SANITIZER_FLAGS})
1313

14-
message(STATUS "Sanitize build: enabled with flags: ${SANITIZER_FLAGS}")
14+
message(STATUS "Sanitize build: enabled with flags: ${SANITIZER_FLAGS}")
1515
endif()
16+
17+
function(target_link_with_sanitizers target)
18+
if(TARGET sanitizers)
19+
message(STATUS "✅ Enabled sanitizers for target ${target}")
20+
target_link_libraries(${target} PRIVATE sanitizers)
21+
endif()
22+
endfunction()

src/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@ target_include_directories(arithmetic
66
PUBLIC ${PROJECT_SOURCE_DIR}/include
77
)
88

9-
if(TARGET sanitizers)
10-
target_link_libraries(arithmetic PUBLIC sanitizers)
11-
endif()
9+
target_link_with_sanitizers(arithmetic)

tests/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ target_link_libraries(unit_tests
2222
PRIVATE gtest_main arithmetic
2323
)
2424

25-
if(TARGET sanitizers)
26-
message(STATUS "✅ Enabled sanitizers for unit tests")
27-
target_link_libraries(unit_tests PRIVATE sanitizers)
28-
endif()
25+
target_link_with_sanitizers(unit_tests)
2926

3027
# Automatically discover and register all test cases
3128
include(GoogleTest)

0 commit comments

Comments
 (0)