-
Notifications
You must be signed in to change notification settings - Fork 2
/
Options.cmake
198 lines (184 loc) · 6.36 KB
/
Options.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# if build type is not set yet
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message("No build type is configured! Default to Debug")
set(CMAKE_BUILD_TYPE
"Debug"
CACHE STRING
"Choose a build type (Debug, Release, MinSizeRel, RelWithDebInfo)"
FORCE)
set_property(CACHE CMAKE_BUILD_TYPE
PROPERTY STRINGS "Debug;Release;MinSizeRel;RelWithDebInfo")
endif()
option(btree_USE_CCACHE "Use ccache" OFF)
option(btree_USE_LLD "Use lld instead of ld for linking" OFF)
option(btree_USE_LIBCXX "Use libcxx instead of stdlibcxx" OFF)
option(btree_ENABLE_OPTIMIZATION
"Add some optimization flags. Maybe useful only when build type is Debug"
ON)
option(btree_ENABLE_PCH "Enable precompiled header" ON)
option(btree_ENABLE_TESTING "Enable Google test" ON)
option(btree_ENABLE_WARNING "Enable compiler warnings" ON)
option(btree_WARNING_AS_ERROR "Change compiler warnings to errors" ON)
option(btree_ENABLE_ASAN "Compile with AddressSanitizer" OFF)
option(btree_ENABLE_UBSAN "Compile with UndefinedBehaviorSanitizer" OFF)
option(btree_ENABLE_MSAN "Compile with MemorySanitizer" OFF)
option(btree_ENABLE_COVERAGE "Compile with coverage flag" OFF)
option(btree_ENABLE_FUZZ
"Enable fuzz testing. Currently only working with clang" OFF)
option(
btree_CLANGD_COMPAT
"Enable supposedly unnecessary compile flags for the b-tree target, mainly so that clangd doesn't throw a bunch of false positives"
OFF)
if(NOT PROJECT_IS_TOP_LEVEL)
mark_as_advanced(btree_USE_CCACHE
btree_USE_LLD
btree_USE_LIBCXX
btree_ENABLE_OPTIMIZATION
btree_ENABLE_PCH
btree_ENABLE_TESTING
btree_ENABLE_ASAN
btree_ENABLE_UBSAN
btree_ENABLE_MSAN
btree_ENABLE_COVERAGE
btree_ENABLE_FUZZ)
endif()
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
mark_as_advanced(FORCE btree_ENABLE_OPTIMIZATION)
else()
mark_as_advanced(CLEAR btree_ENABLE_OPTIMIZATION)
endif()
# configure accordingly to options
if(btree_ENABLE_CCACHE)
find_program(CCACHE ccache)
if(NOT CCACHE)
message("Cannot find ccache")
else()
message("Found ccache and is using it")
set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE})
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE})
endif()
endif()
if(btree_ENABLE_OPTIMIZATION)
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
if(MSVC)
target_compile_options(btree_compile_opts INTERFACE "/Zo")
else(MSVC)
target_compile_options(btree_compile_opts INTERFACE "-Og")
endif()
endif()
else()
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
message(
WARNING
"Optimization is on for all types but Debug, for obvious reasons."
)
message(
WARNING "Turning off optimization is only allowed in debug build.")
else()
if(MSVC)
target_compile_options(btree_compile_opts INTERFACE "/Od")
else(MSVC)
target_compile_options(btree_compile_opts INTERFACE "-O0")
endif()
endif()
endif()
if(btree_USE_LLD)
find_program(LLD lld REQUIRED)
if(NOT LLD)
message(
FATAL_ERROR
"Error, LLD not found, please either turn off option USE_LLD or add directory of LLD to PATH"
)
endif()
set(CMAKE_LINKER lld)
endif()
if(btree_USE_LIBCXX)
if(NOT MSVC)
target_compile_options(btree_compile_opts INTERFACE "-stdlib=libc++")
target_link_options(btree_compile_opts INTERFACE
"-stdlib=libc++;-lc++abi")
endif()
endif()
if(btree_ENABLE_TESTING)
add_subdirectory(unittest)
endif()
if(btree_ENABLE_WARNING)
if(MSVC)
target_compile_options(btree_compile_opts INTERFACE "/W4")
else(MSVC)
target_compile_options(
btree_compile_opts
INTERFACE
"-Wall;-Wextra;-Wformat=2;-fdiagnostics-color=always;-Wshadow;-Wconversion"
)
endif()
endif()
if(btree_WARNING_AS_ERROR)
if(MSVC)
target_compile_options(btree_compile_opts INTERFACE "/WX")
else(MSVC)
target_compile_options(btree_compile_opts INTERFACE "-Werror")
endif()
endif()
if(btree_ENABLE_ASAN)
if(MSVC)
target_compile_options(
btree_compile_opts
INTERFACE
"/fsanitize=address;/D_DISABLE_VECTOR_ANNOTATION;/D_DISABLE_STRING_ANNOTATION"
)
else(MSVC)
target_compile_options(
btree_compile_opts
INTERFACE "-fsanitize=address;-fno-omit-frame-pointer")
target_link_options(btree_compile_opts INTERFACE "-fsanitize=address")
endif()
endif()
if(btree_ENABLE_UBSAN)
if(MSVC)
message(
"We don't know if there's UBSan support on MSVC :(. Currently disabling it."
)
else(MSVC)
target_compile_options(btree_compile_opts
INTERFACE "-fsanitize=undefined")
target_link_options(btree_compile_opts INTERFACE "-fsanitize=undefined")
endif()
endif()
if(btree_ENABLE_MSAN)
if("${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC")
message(
"We don't know if there's MSan support on MSVC :(. Currently disabling it."
)
else()
target_compile_options(
btree_compile_opts
INTERFACE
"-fsanitize=memory;-fno-omit-frame-pointer;-fno-optimize-sibling-calls"
)
target_link_options(btree_compile_opts INTERFACE "-fsanitize=memory")
endif()
endif()
if(btree_ENABLE_COVERAGE)
if(MSVC)
target_compile_options("/fsanitize-coverage=edge")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(btree_compile_opts INTERFACE "--coverage")
target_link_options(btree_compile_opts INTERFACE "--coverage")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL ".*Clang")
target_compile_options(
btree_compile_opts
INTERFACE "-fprofile-instr-generate;-fcoverage-mapping")
target_link_options(btree_compile_opts INTERFACE
"-fprofile-instr-generate;-fcoverage-mapping")
endif()
endif()
if(btree_ENABLE_FUZZ)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
message(
"Note that gcc/g++ won't work with libFuzzer. This is a LLVM-only tool."
)
else()
add_subdirectory(fuzztest)
endif()
endif()