1
1
cmake_minimum_required (VERSION 3.14 )
2
2
3
+ # Set modern CMake policies for better compatibility
4
+ if (POLICY CMP0077 )
5
+ cmake_policy (SET CMP0077 NEW ) # option() honors normal variables
6
+ endif ()
7
+ if (POLICY CMP0079 )
8
+ cmake_policy (SET CMP0079 NEW ) # target_link_libraries() allows use with targets in other directories
9
+ endif ()
10
+ if (POLICY CMP0091 )
11
+ cmake_policy (SET CMP0091 NEW ) # MSVC runtime library flags are selected by an abstraction
12
+ endif ()
13
+
3
14
project (
4
15
fork_union
5
16
VERSION 2.2.0
@@ -18,11 +29,78 @@ target_include_directories(
18
29
fork_union INTERFACE $< BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include> $< INSTALL_INTERFACE:include>
19
30
)
20
31
21
- # Strict compilation flags
32
+ # Set C++17 requirement and features properly for library consumers
33
+ target_compile_features (fork_union INTERFACE cxx_std_17 )
34
+ set_target_properties (
35
+ fork_union
36
+ PROPERTIES CXX_STANDARD 17
37
+ CXX_STANDARD_REQUIRED ON
38
+ CXX_EXTENSIONS OFF
39
+ )
40
+
41
+ # Strictest possible compilation flags with fatal warnings
22
42
target_compile_options (
23
- fork_union INTERFACE
24
- $< $< CXX_COMPILER_ID:GNU,Clang> :-Wall -Wextra -Wpedantic -Wconversion -Wcast-qual -Wcast-align -Wunused -Wno-unused-parameter -Wno-unknown-pragmas -Wno-sign-conversion -Wno-unused-function>
25
- $< $< CXX_COMPILER_ID:MSVC> :/W4 /permissive->
43
+ fork_union
44
+ INTERFACE # GCC/Clang: Maximum warnings + treat warnings as errors + security hardening
45
+ $< $< CXX_COMPILER_ID:GNU,Clang> :-Wall
46
+ -Wextra
47
+ -Wpedantic
48
+ -Werror
49
+ -Wconversion
50
+ -Wcast-qual
51
+ -Wcast-align
52
+ -Wunused
53
+ -Wno-unused-parameter
54
+ -Wno-unknown-pragmas
55
+ -Wno-sign-conversion
56
+ -Wno-unused-function
57
+ -Wshadow
58
+ -Wnon-virtual-dtor
59
+ -Wold-style-cast
60
+ -Woverloaded-virtual
61
+ -Wsign-promo
62
+ -Wduplicated-cond
63
+ -Wduplicated-branches
64
+ -Wlogical-op
65
+ -Wno-null-dereference
66
+ -Wuseless-cast
67
+ -Wdouble-promotion>
68
+ $< $< AND:$< CXX_COMPILER_ID:GNU,Clang> ,$< CONFIG:Release,RelWithDebInfo> > :-Wno-unused-variable>
69
+ # Additional GCC-specific warnings
70
+ $< $< CXX_COMPILER_ID:GNU> :-Wmisleading-indentation
71
+ -Wduplicated-cond
72
+ -Wduplicated-branches
73
+ -Wlogical-op
74
+ -Wno-null-dereference
75
+ -Wuseless-cast>
76
+ # Additional Clang-specific warnings
77
+ $< $< CXX_COMPILER_ID:Clang> :-Wmost
78
+ -Wthread-safety
79
+ -Wthread-safety-negative>
80
+ # MSVC: Maximum warnings + treat warnings as errors
81
+ $< $< CXX_COMPILER_ID:MSVC> :/W4
82
+ /WX
83
+ /permissive-
84
+ /w14242
85
+ /w14254
86
+ /w14263
87
+ /w14265
88
+ /w14287
89
+ /we4289
90
+ /w14296
91
+ /w14311
92
+ /w14545
93
+ /w14546
94
+ /w14547
95
+ /w14549
96
+ /w14555
97
+ /w14619
98
+ /w14640
99
+ /w14826
100
+ /w14905
101
+ /w14906
102
+ /w14928>
103
+ $< $< AND:$< CXX_COMPILER_ID:MSVC> ,$< CONFIG:Release,RelWithDebInfo> > :/wd4101>
26
104
)
27
105
28
106
# Pre-compiled libraries built from `c/lib.cpp`
@@ -41,6 +119,47 @@ set_target_properties(
41
119
target_link_libraries (fork_union_dynamic PUBLIC fork_union )
42
120
target_link_libraries (fork_union_static PUBLIC fork_union )
43
121
122
+ # Security hardening flags for compiled libraries
123
+ target_compile_options (
124
+ fork_union_dynamic
125
+ PRIVATE # Stack protection and buffer overflow detection
126
+ $< $< CXX_COMPILER_ID:GNU,Clang> :-fstack-protector-strong
127
+ -D_FORTIFY_SOURCE=2>
128
+ # Control flow integrity and stack clash protection
129
+ $< $< CXX_COMPILER_ID:GNU> :-fcf-protection=full
130
+ -fstack-clash-protection>
131
+ $< $< CXX_COMPILER_ID:Clang> :-fcf-protection=full>
132
+ # MSVC security features
133
+ $< $< CXX_COMPILER_ID:MSVC> :/GS
134
+ /guard:cf>
135
+ )
136
+ target_compile_options (
137
+ fork_union_static
138
+ PRIVATE $< $< CXX_COMPILER_ID:GNU,Clang> :-fstack-protector-strong
139
+ -D_FORTIFY_SOURCE=2>
140
+ $< $< CXX_COMPILER_ID:GNU> :-fcf-protection=full
141
+ -fstack-clash-protection>
142
+ $< $< CXX_COMPILER_ID:Clang> :-fcf-protection=full>
143
+ $< $< CXX_COMPILER_ID:MSVC> :/GS
144
+ /guard:cf>
145
+ )
146
+
147
+ # Hardened linking flags
148
+ target_link_options (
149
+ fork_union_dynamic
150
+ PRIVATE
151
+ # Enable RELRO, stack canaries, and NX bit
152
+ $< $< PLATFORM_ID:Linux> :-Wl,-z,relro,-z,now,-z,noexecstack>
153
+ # MSVC hardened linking
154
+ $< $< CXX_COMPILER_ID:MSVC> :/DYNAMICBASE
155
+ /NXCOMPAT
156
+ /guard:cf>
157
+ )
158
+ target_link_options (
159
+ fork_union_static PRIVATE $< $< PLATFORM_ID:Linux> :-Wl,-z,relro,-z,now,-z,noexecstack>
160
+ $< $< CXX_COMPILER_ID:MSVC> :/guard:cf>
161
+ )
162
+
44
163
# Set the output directory for all executables - on Windows requires more boilerplate:
45
164
# https://stackoverflow.com/a/25328001
46
165
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} )
@@ -51,47 +170,31 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR})
51
170
52
171
# Static analysis tools
53
172
find_program (CPPCHECK_EXECUTABLE cppcheck )
54
- if (CPPCHECK_EXECUTABLE )
173
+ if (CPPCHECK_EXECUTABLE )
55
174
add_custom_target (
56
175
cppcheck
57
- COMMAND ${CPPCHECK_EXECUTABLE}
58
- --enable=all
59
- --std=c++17
60
- --verbose
61
- --quiet
62
- --error-exitcode=1
63
- --suppress=missingIncludeSystem
64
- --suppress=unusedFunction
65
- --suppress=unmatchedSuppression
66
- --suppress=ConfigurationNotChecked
67
- --suppress=knownConditionTrueFalse
68
- --suppress=shadowFunction
69
- --suppress=shadowVariable
70
- --suppress=useStlAlgorithm
71
- --suppress=noExplicitConstructor
72
- -I${CMAKE_CURRENT_SOURCE_DIR}/include
73
- -DFU_ENABLE_NUMA=1
74
- ${CMAKE_CURRENT_SOURCE_DIR} /include
176
+ COMMAND
177
+ ${CPPCHECK_EXECUTABLE} --enable=all --std=c++17 --verbose --quiet --error-exitcode=1
178
+ --suppress=missingIncludeSystem --suppress=unusedFunction --suppress=unmatchedSuppression
179
+ --suppress=ConfigurationNotChecked --suppress=knownConditionTrueFalse --suppress=shadowFunction
180
+ --suppress=shadowVariable --suppress=useStlAlgorithm --suppress=noExplicitConstructor
181
+ -I${CMAKE_CURRENT_SOURCE_DIR}/include -DFU_ENABLE_NUMA=1 ${CMAKE_CURRENT_SOURCE_DIR} /include
75
182
${CMAKE_CURRENT_SOURCE_DIR} /c
76
183
COMMENT "Running cppcheck static analysis"
77
184
)
78
- endif ()
185
+ endif ()
79
186
80
187
find_program (CLANG_TIDY_EXECUTABLE clang-tidy )
81
- if (CLANG_TIDY_EXECUTABLE )
188
+ if (CLANG_TIDY_EXECUTABLE )
82
189
add_custom_target (
83
190
clang-tidy
84
- COMMAND ${CLANG_TIDY_EXECUTABLE}
85
- --config-file=${CMAKE_CURRENT_SOURCE_DIR}/.clang-tidy
86
- --quiet
87
- ${CMAKE_CURRENT_SOURCE_DIR} /include/*.hpp
88
- ${CMAKE_CURRENT_SOURCE_DIR} /c/*.cpp
89
- --
90
- -I${CMAKE_CURRENT_SOURCE_DIR}/include
91
- -std=c++17
191
+ COMMAND
192
+ ${CLANG_TIDY_EXECUTABLE} --config-file=${CMAKE_CURRENT_SOURCE_DIR}/.clang-tidy --quiet
193
+ ${CMAKE_CURRENT_SOURCE_DIR} /include/*.hpp ${CMAKE_CURRENT_SOURCE_DIR} /c/*.cpp --
194
+ -I${CMAKE_CURRENT_SOURCE_DIR}/include -std=c++17
92
195
COMMENT "Running clang-tidy static analysis"
93
196
)
94
- endif ()
197
+ endif ()
95
198
96
199
# Tests & benchmarking scripts
97
200
include (CTest )
0 commit comments