Skip to content

Commit fa2515f

Browse files
committed
Add warnings and strict_warnings interfaces
1 parent e3a6a0d commit fa2515f

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
88
# Export compile_commands.json for clangd and clang-tidy
99
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1010

11-
# Add custom "Sanitize" build type for multi-config generators (e.g., Visual Studio)
11+
# Add custom "Sanitize" build type
1212
if(CMAKE_CONFIGURATION_TYPES)
1313
list(APPEND CMAKE_CONFIGURATION_TYPES Sanitize)
1414
list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
1515
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING "" FORCE)
1616
endif()
1717

18-
# Load sanitizer flags if needed
18+
# Load sanitizers
1919
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
2020
include(Sanitizers)
2121

22+
# Load warnings
23+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
24+
include(Warnings)
25+
2226
# === Libraries ===
2327
add_subdirectory(src)
2428

@@ -31,13 +35,13 @@ target_link_libraries(${PROJECT_NAME}
3135
PRIVATE arithmetic
3236
)
3337

38+
enable_strict_warnings(${PROJECT_NAME})
3439
target_link_with_sanitizers(${PROJECT_NAME})
3540

3641
# === Unit Testing ===
3742
option(ENABLE_TESTING "Build tests" ON)
3843

3944
if(ENABLE_TESTING)
40-
message(STATUS "✅ Enabled unit tests")
4145
enable_testing()
4246
add_subdirectory(tests)
4347
endif()

cmake/Sanitizers.cmake

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ if(CMAKE_BUILD_TYPE STREQUAL "Sanitize")
1010
add_library(sanitizers INTERFACE)
1111
target_compile_options(sanitizers INTERFACE ${SANITIZER_FLAGS})
1212
target_link_options(sanitizers INTERFACE ${SANITIZER_FLAGS})
13-
1413
message(STATUS "✅ Sanitize build: enabled with flags: ${SANITIZER_FLAGS}")
1514
endif()
1615

1716
function(target_link_with_sanitizers target)
1817
if(TARGET sanitizers)
19-
message(STATUS "✅ Enabled sanitizers for target ${target}")
20-
target_link_libraries(${target} PRIVATE sanitizers)
18+
target_link_libraries(${target} PRIVATE sanitizers)
19+
message(STATUS "✅ Enabled sanitizers for target ${target}")
2120
endif()
2221
endfunction()

cmake/Warnings.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Standard warnings
2+
add_library(warnings INTERFACE)
3+
4+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
5+
target_compile_options(warnings INTERFACE
6+
-Wall
7+
-Wextra
8+
-Wpedantic
9+
-Wconversion
10+
-Wsign-conversion
11+
)
12+
endif()
13+
14+
# Warnings as errors
15+
add_library(strict_warnings INTERFACE)
16+
17+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
18+
target_compile_options(strict_warnings INTERFACE -Werror)
19+
target_link_libraries(strict_warnings PRIVATE warnings)
20+
endif()
21+
22+
# Function to apply warnings
23+
function(enable_warnings target)
24+
target_link_libraries(${target} PRIVATE warnings)
25+
message(STATUS "⚠️ Enabled warnings for target ${target}")
26+
endfunction()
27+
28+
# Function to apply warnings as errors
29+
function(enable_strict_warnings target)
30+
target_link_libraries(${target} PRIVATE strict_warnings)
31+
message(STATUS "⚠️ Enabled warnings as errors for target ${target}")
32+
endfunction()

src/CMakeLists.txt

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

9+
enable_strict_warnings(arithmetic)
910
target_link_with_sanitizers(arithmetic)

tests/CMakeLists.txt

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

25+
enable_warnings(unit_tests)
2526
target_link_with_sanitizers(unit_tests)
2627

2728
# Automatically discover and register all test cases

0 commit comments

Comments
 (0)