-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
284 lines (221 loc) · 10.9 KB
/
CMakeLists.txt
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
cmake_minimum_required(VERSION 3.17)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
include(cherry_blazer_util)
project(cherry_blazer LANGUAGES CXX)
# ##################################################################################################
# Build preconditions
if(CMAKE_BUILD_TYPE)
message(
FATAL_ERROR
"Don't set CMAKE_BUILD_TYPE for this project. \
Instead, set more granular options specific to cherry_blazer. To get them:
cmake -S . -B build -LH | grep -B2 CHERRY_BLAZER")
endif()
if(NOT CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
message(FATAL_ERROR "Direct inclusion of cherry_blazer as a subproject is not supported.")
endif()
if(NOT (UNIX AND NOT APPLE))
message(FATAL_ERROR "cherry_blazer only supports Linux.")
endif()
# ##################################################################################################
# Options
option(CHERRY_BLAZER_CI "Set ON if building in CI" OFF)
option(CHERRY_BLAZER_DEBUG "Enable debug symbols" OFF)
add_library(cherry_blazer_flags_debug INTERFACE)
target_compile_options(cherry_blazer_flags_debug INTERFACE -ggdb3 -fno-omit-frame-pointer)
target_compile_definitions(cherry_blazer_flags_debug INTERFACE CHERRY_BLAZER_DEBUG)
option(CHERRY_BLAZER_NDEBUG "Disable asserts" OFF)
add_library(cherry_blazer_flags_ndebug INTERFACE)
target_compile_definitions(cherry_blazer_flags_ndebug INTERFACE NDEBUG)
option(CHERRY_BLAZER_OPTIMIZE "Enable optimizations" OFF)
add_library(cherry_blazer_flags_optimize INTERFACE)
target_compile_options(cherry_blazer_flags_optimize INTERFACE -O2)
option(CHERRY_BLAZER_ASAN "Enable address sanitizer" OFF)
add_library(cherry_blazer_flags_asan INTERFACE)
target_compile_options(cherry_blazer_flags_asan INTERFACE -fsanitize=address
-fno-omit-frame-pointer)
target_link_options(cherry_blazer_flags_asan INTERFACE -fsanitize=address)
option(CHERRY_BLAZER_SPLIT_DWARF "Split DWARF in debug mode" OFF)
add_library(cherry_blazer_flags_split_dwarf INTERFACE)
target_compile_options(cherry_blazer_flags_split_dwarf INTERFACE -gsplit-dwarf)
target_link_options(cherry_blazer_flags_split_dwarf INTERFACE LINKER:--gdb-index)
option(CHERRY_BLAZER_LTO "Enable LTO" OFF)
option(CHERRY_BLAZER_LLD "Use lld" OFF)
set(CHERRY_BLAZER_BOOST_VERSION
"1.77.0"
CACHE STRING "Boost version")
set(CHERRY_BLAZER_USE_CONSTEXPR
OFF
CACHE BOOL "Calculate most things at compile-time.")
# Despite the test __cpp_lib_ranges >= 202106L not working on both gcc 11 and clang 12, ranges work
# on gcc, but not on clang.
set(CHERRY_BLAZER_USE_RANGES
OFF
CACHE BOOL "Use code with new 'ranges' library.")
# ##################################################################################################
# CMAKE_ settings
# Compilation database for code completion.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_MESSAGE_CONTEXT_SHOW ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_CXX_EXTENSIONS OFF)
# ##################################################################################################
# Set of flags built up piecewise to be used to build sources of every target defined by this
# project.
add_library(cherry_blazer_flags INTERFACE)
target_compile_options(
cherry_blazer_flags
INTERFACE
-std=c++20
-Wall
-Wextra
-pedantic-errors # issue errors on standard non-compliance
-Werror=missing-field-initializers # partial ctors is an error
-Wdelete-non-virtual-dtor # https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP52-CPP.+Do+not+delete+a+polymorphic+object+without+a+virtual+destructor
-Wconversion
-Wsign-conversion)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
target_compile_options(
cherry_blazer_flags
INTERFACE
-Wcast-align=strict # https://stackoverflow.com/a/25795795/10986054
# https://trust-in-soft.com/blog/2020/04/06/gcc-always-assumes-aligned-pointer-accesses/
-Wshadow=compatible-local # don't warn about parameters and incompatible local types
)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(
cherry_blazer_flags
INTERFACE -Wcast-align # clang doesn't have =strict
-Wshadow # clang doesn't warn about parameters needlessly
-fconstexpr-steps=4294967295 # uint32_t max, because clang doesn't optimize
# constexpr.
# if this doesn't work (I mean, /when/ this stops working), one can patch clang to
# use uint64.
-fconstexpr-backtrace-limit=0 # to see what code led to "too many steps" above
)
add_compile_options(-stdlib=libc++) # feature parity with compiler
add_link_options(-stdlib=libc++ -lc++abi)
endif()
# Force colors in Ninja: https://github.com/ninja-build/ninja/wiki/FAQ
if("${CMAKE_GENERATOR}" MATCHES "Ninja")
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
target_compile_options(cherry_blazer_flags INTERFACE -fdiagnostics-color=always)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(cherry_blazer_flags INTERFACE -fcolor-diagnostics)
endif()
endif()
if(CHERRY_BLAZER_DEBUG)
if(NOT CHERRY_BLAZER_OPTIMIZE)
target_compile_options(cherry_blazer_flags_debug INTERFACE -Og)
target_link_libraries(cherry_blazer_flags INTERFACE cherry_blazer_flags_debug)
else()
# Optimization flags will clash.
endif()
endif()
if(CHERRY_BLAZER_LLD)
target_link_options(cherry_blazer_flags INTERFACE -fuse-ld=lld)
endif()
if(CHERRY_BLAZER_LTO)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
if(CHERRY_BLAZER_LLD)
message(
FATAL_ERROR
"lld cannot handle GCC LTO. See https://github.com/llvm/llvm-project/issues/41791"
)
endif()
target_compile_options(cherry_blazer_flags INTERFACE -flto=auto -fwhole-program
-fno-fat-lto-objects)
target_link_options(cherry_blazer_flags INTERFACE -flto=auto -fwhole-program
-fno-fat-lto-objects)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(cherry_blazer_flags INTERFACE -flto)
target_link_options(cherry_blazer_flags INTERFACE -flto)
endif()
endif()
if(CHERRY_BLAZER_NDEBUG)
target_link_libraries(cherry_blazer_flags INTERFACE cherry_blazer_flags_ndebug)
endif()
if(CHERRY_BLAZER_OPTIMIZE)
target_link_libraries(cherry_blazer_flags INTERFACE cherry_blazer_flags_optimize)
endif()
if(CHERRY_BLAZER_ASAN)
target_link_libraries(cherry_blazer_flags INTERFACE cherry_blazer_flags_asan)
endif()
if(CHERRY_BLAZER_SPLIT_DWARF)
target_link_libraries(cherry_blazer_flags INTERFACE cherry_blazer_flags_split_dwarf)
endif()
# ##################################################################################################
# googletest
set(cmake_definitions -DCHERRY_BLAZER_SOURCE_DIR='${CMAKE_CURRENT_SOURCE_DIR}')
cherry_blazer_dependency_download(googletest "${cmake_definitions}")
cherry_blazer_dependency_apply_patch(googletest thirdparty-patches/googletest.patch)
option(BUILD_GMOCK OFF)
option(INSTALL_GTEST OFF)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
option(GTEST_USE_LIBCPP ON)
endif()
list(APPEND CMAKE_MESSAGE_CONTEXT "googletest")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/_deps/googletest-src"
"${CMAKE_CURRENT_SOURCE_DIR}/_deps/googletest-build")
list(POP_BACK CMAKE_MESSAGE_CONTEXT)
# ##################################################################################################
# fmt
set(cmake_definitions -DCHERRY_BLAZER_SOURCE_DIR='${CMAKE_CURRENT_SOURCE_DIR}')
cherry_blazer_dependency_download(fmt "${cmake_definitions}")
option(FMT_TEST OFF)
list(APPEND CMAKE_MESSAGE_CONTEXT "fmt")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/_deps/fmt-src"
"${CMAKE_CURRENT_SOURCE_DIR}/_deps/fmt-build")
list(POP_BACK CMAKE_MESSAGE_CONTEXT)
# ##################################################################################################
# boost
set(cmake_definitions -DCHERRY_BLAZER_SOURCE_DIR='${CMAKE_SOURCE_DIR}'
-DCHERRY_BLAZER_BOOST_VERSION='${CHERRY_BLAZER_BOOST_VERSION}')
cherry_blazer_dependency_download(boost "${cmake_definitions}")
cherry_blazer_dependency_apply_patch(boost thirdparty-patches/boost.patch)
cherry_blazer_dependency_configure(
boost "./bootstrap.sh --prefix=../boost-install --with-icu --with-python=python3"
"${CMAKE_SOURCE_DIR}/_deps/boost-src")
include(ProcessorCount)
ProcessorCount(n)
cherry_blazer_dependency_install(
boost "./b2 --build-dir=../boost-build --user-config=boostbuild.sucks install \
variant=release debug-symbols=on threading=multi runtime-link=shared link=shared,static \
--jobs=${n}" "${CMAKE_SOURCE_DIR}/_deps/boost-src")
set(Boost_ROOT "${CMAKE_SOURCE_DIR}/_deps/boost-install")
find_package(Boost ${CHERRY_BLAZER_BOOST_VERSION} CONFIG REQUIRED)
# ~~~
# To list all boost targets:
# find_package(Boost ${CHERRY_BLAZER_BOOST_VERSION} CONFIG REQUIRED ALL)
# message(WARNING "All Boost targets: ${Boost_ALL_TARGETS}")
# ~~~
# ##################################################################################################
# markable (compact_optional)
set(cmake_definitions -DCHERRY_BLAZER_SOURCE_DIR='${CMAKE_CURRENT_SOURCE_DIR}')
cherry_blazer_dependency_download(markable "${cmake_definitions}")
add_library(markable INTERFACE)
target_include_directories(markable SYSTEM
INTERFACE "${CMAKE_SOURCE_DIR}/_deps/markable-src/include")
add_library(ak_toolkit::markable ALIAS markable)
# ##################################################################################################
# doxygen
find_package(Doxygen REQUIRED dot)
# ##################################################################################################
add_subdirectory(src)
if(BUILD_TESTING OR CHERRY_BLAZER_TEST)
enable_testing()
# Set of flags built up piecewise to be used to build tests defined by this project. For tests,
# they stack (through libcherryblazer) on top of cherry_blazer_flags.
add_library(cherry_blazer_test_flags INTERFACE)
# To silence clang-tidy warnings from IDE, #pragma ide is used, which compilers complain about.
target_compile_options(cherry_blazer_test_flags INTERFACE -Wno-unknown-pragmas)
target_compile_definitions(cherry_blazer_test_flags INTERFACE CHERRY_BLAZER_TEST)
add_subdirectory(test)
endif()
# * TODO: specify external dependencies. for example, git 1.6.5+ for ExternalProject (via
# FetchContent)
# * TODO: CI: create configurations for different kinds of builds: w/ or wo/ flags
# * TODO: add check target (will be useful for compile-time tests too)
# * TODO: add cmake presets