@@ -2,36 +2,51 @@ cmake_minimum_required(VERSION 3.0)
2
2
3
3
project (error )
4
4
5
- if (MSVC )
6
- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /permissive- /W4 /w14640 /EHsc" )
7
- else ()
8
- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wnon-virtual-dtor -Wpedantic" )
9
- endif ()
10
-
11
- set (CMAKE_CXX_STANDARD 11 )
12
-
5
+ # Import dependencies
13
6
include (cmake/CPM.cmake )
14
7
cpmaddpackage ("gh:fmtlib/fmt#10.0.0" )
15
8
9
+ # Build the main library
16
10
add_library (error src/error.cpp )
17
11
target_include_directories (error PUBLIC include )
18
12
target_link_libraries (error PUBLIC fmt )
19
13
14
+ # Check if this project is the main project
20
15
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR )
16
+ # Import Format.cmake to format source code
21
17
cpmaddpackage ("gh:TheLartians/Format.cmake@1.7.3" )
22
18
23
19
if (BUILD_TESTING )
24
20
enable_testing ()
25
21
22
+ # Import Catch2 as the main testing framework
26
23
cpmaddpackage ("gh:catchorg/Catch2@3.3.2" )
27
24
include ("${Catch2_SOURCE_DIR} /extras/Catch.cmake" )
28
25
29
- if (NOT MSVC )
30
- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -fPIC -O0" )
31
- endif ()
32
-
26
+ # Build tests for the main library
33
27
add_executable (error_test test /error_test.cpp )
34
28
target_link_libraries (error_test PRIVATE error Catch2::Catch2WithMain )
35
29
catch_discover_tests (error_test )
36
30
endif ()
31
+
32
+ # Get all targets in this directory
33
+ get_property (
34
+ TARGETS
35
+ DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
36
+ PROPERTY BUILDSYSTEM_TARGETS )
37
+
38
+ foreach (TARGET IN LISTS TARGETS )
39
+ # Statically analyze code by checking for warnings
40
+ if (MSVC )
41
+ target_compile_options (${TARGET} PRIVATE /WX /permissive- /W4 /w14640 /EHsc )
42
+ else ()
43
+ target_compile_options (${TARGET} PRIVATE -Werror -Wall -Wextra -Wnon-virtual-dtor -Wpedantic )
44
+ endif ()
45
+
46
+ # Enable support to check for test coverage
47
+ if (BUILD_TESTING AND NOT MSVC )
48
+ target_compile_options (${TARGET} PRIVATE --coverage -O0 )
49
+ target_link_options (${TARGET} PRIVATE --coverage )
50
+ endif ()
51
+ endforeach ()
37
52
endif ()
0 commit comments