-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
261 lines (241 loc) · 11.7 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
cmake_minimum_required(VERSION 3.5)
foreach(policy IN ITEMS CMP0056 CMP0060 CMP0065 CMP0066 CMP0074 CMP0102)
if(POLICY ${policy})
cmake_policy(SET ${policy} NEW)
endif()
endforeach()
project (Blister C CXX)
include(FindThreads)
include(GNUInstallDirs)
find_library(SYSTEM_RT_LIB NAMES rt)
find_package(Threads QUIET)
find_program(PROGRAM_CCACHE NAMES ccache PATHS ENV PATH)
find_program(PROGRAM_CLANG_TIDY NAMES clang-tidy PATHS ENV PATH)
find_program(PROGRAM_CPPCHECK NAMES cppcheck PATHS ENV PATH)
find_program(PROGRAM_CPPLINT NAMES cpplint cpplint.py PATHS ENV PATH)
find_program(PROGRAM_IWYU NAMES include-what-you-use PATHS ENV PATH)
find_program(PROGRAM_MOLD NAMES mold PATHS ENV PATH)
if(CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE Debug)
endif()
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS TRUE)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_MACOSX_RPATH TRUE)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/build")
set(CMAKE_OPTIMIZE_DEPENDENCIES TRUE)
set(CMAKE_PCH_WARN_INVALID FALSE)
set(CMAKE_PCH_INSTANTIATE_TEMPLATES TRUE)
set(CMAKE_SKIP_RULE_DEPENDENCY TRUE)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
set(CMAKE_COMPILER_IS_CLANG TRUE CACHE BOOL "Clang compiler")
endif()
if(POLICY CMP0063)
cmake_policy(SET CMP0063 NEW)
set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
endif()
option(CHECK_ALL "All code analysis")
option(CHECK_CLANG_TIDY "Clang-tidy static analysis")
option(CHECK_CPPLINT "Cpplint style analysis")
option(CHECK_CPPCHECK "Cppcheck static analysis")
option(CHECK_IWYU "Include-what-you-use header analysis")
if(CHECK_ALL)
set(CHECK_CLANG_TIDY TRUE)
set(CHECK_CPPCHECK TRUE)
set(CHECK_CPPLINT TRUE)
endif()
if(NOT CMAKE_VERSION VERSION_LESS 3.9 AND NOT CMAKE_BUILD_TYPE STREQUAL Debug)
cmake_policy(SET CMP0069 NEW)
include(CheckIPOSupported)
check_ipo_supported(RESULT ipo)
if(ipo)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
if(CMAKE_COMPILER_IS_GNUCXX)
list(APPEND CMAKE_C_COMPILE_OPTIONS_IPO "-flto=4")
list(APPEND CMAKE_CXX_COMPILE_OPTIONS_IPO "-flto=4")
elseif(CMAKE_COMPILER_IS_CLANG)
set(IPO_LDFLAGS "-Wl,-cache_path_lto,${PROJECT_BINARY_DIR}/.lto")
endif()
endif()
endif()
if(NOT DEFINED IPO_LDFLAGS)
set(IPO_LDFLAGS "")
endif()
# CXX CMAKE_BUILD_TYPE flags are not always set
if(NOT CMAKE_CXX_FLAGS_DEBUG AND NOT CMAKE_CXX_FLAGS_RELEASE)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)
if(NOT SANITIZE_FLAGS)
set(SANITIZE_FLAGS "-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3")
endif()
set(COMMON_C_FLAGS "${SANITIZE_FLAGS} -ffast-math -march=native -pipe -Wall -Wconversion -Werror -Wextra -Wno-attributes -Wformat=2 -Wno-format-nonliteral -Wformat-security -Wno-format-y2k -Wmultichar -Wno-pragmas -Wredundant-decls -Wsign-conversion -Wstrict-aliasing -Wstrict-overflow=2 -Wno-unknown-pragmas -Wwrite-strings")
set(COMMON_CXX_FLAGS "-fno-exceptions -fno-rtti -Wno-deprecated -Wno-deprecated-declarations -Wnon-virtual-dtor -Woverloaded-virtual -Wsign-promo")
if(CMAKE_COMPILER_IS_GNUCXX)
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.6)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -D__USE_XOPEN2K8")
set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -std=c++0x")
endif()
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -fmax-errors=8 -fstack-protector-strong -Wdouble-promotion -Wformat-overflow=2 -Wformat-signedness -Wformat-truncation=2 -Wlogical-op -Wodr -Wshadow -Wstack-usage=10000 -Wtrampolines")
set(LDFLAGS_C_STATIC "-static-libgcc")
set(LDFLAGS_CXX_STATIC "${LDFLAGS_C_STATIC} -static-libstdc++")
endif()
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-plt")
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -Wduplicated-branches -Wduplicated-cond -Wnull-dereference -Wstringop-overflow=3")
endif()
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -fcf-protection=full -fstack-clash-protection")
endif()
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)
set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -Wextra-semi")
endif()
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 13.0)
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -fstrict-flex-arrays=2")
endif()
set(COMMON_C_FLAGS_RELWITHDEBINFO "-femit-struct-debug-reduced -ggdb1 -gz")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${IPO_LDFLAGS} ${SANITIZE_FLAGS} -Wl,--as-needed,-gc-sections,-z,relro")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS},--no-undefined")
else()
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -fno-standalone-debug -fvisibility=hidden -Wdouble-promotion -Wnull-dereference -Wno-null-pointer-subtraction -Wodr -Wshadow -Wno-unknown-warning-option")
set(COMMON_C_FLAGS_RELWITHDEBINFO "-gline-tables-only")
set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -Wno-c++11-extensions -Wextra-semi")
set(CMAKE_EXE_LINKER_FLAGS "${IPO_LDFLAGS} ${SANITIZE_FLAGS} -fno-plt")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
endif()
set(COMMON_C_FLAGS_DEBUG "-O1 -fno-omit-frame-pointer -fno-optimize-sibling-calls -ggdb -gz -D_DEBUG")
set(COMMON_C_FLAGS_MINSIZEREL "-Os")
set(COMMON_C_FLAGS_RELEASE "-O3 -fdata-sections -ffunction-sections")
elseif(MSVC)
string(REPLACE "/W3" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
string(REPLACE "/W3" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(COMMON_C_FLAGS "/arch:AVX2 /fp:fast /FC /Gd /GF /Gm- /GS /Gy /MP /Qpar /W4 /WX /Zc:inline /Zc:wchar_t")
set(COMMON_C_FLAGS_DEBUG "/JMC /MDd /Od /Oy- /RTC1 /sdl")
set(COMMON_C_FLAGS_MINSIZEREL "/GL /MD /Os /Oi")
set(COMMON_C_FLAGS_RELEASE "/GL /Gw /MD /O2")
string(REGEX REPLACE "/EHsc|/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS} /EHsc- /GR-")
endif()
set(COMMON_C_FLAGS_MINSIZEREL "${COMMON_C_FLAGS_RELEASE} ${COMMON_C_FLAGS_MINSIZEREL}")
set(COMMON_C_FLAGS_RELWITHDEBINFO "${COMMON_C_FLAGS_RELEASE} ${COMMON_C_FLAGS_RELWITHDEBINFO}")
set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS} ${COMMON_CXX_FLAGS}")
set(COMMON_CXX_FLAGS_DEBUG "${COMMON_C_FLAGS_DEBUG}")
set(COMMON_CXX_FLAGS_MINSIZEREL "${COMMON_C_FLAGS_MINSIZEREL}")
set(COMMON_CXX_FLAGS_RELEASE "${COMMON_C_FLAGS_RELEASE}")
set(COMMON_CXX_FLAGS_RELWITHDEBINFO "${COMMON_C_FLAGS_RELWITHDEBINFO}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_C_FLAGS}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${COMMON_C_FLAGS_DEBUG}")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} ${COMMON_C_FLAGS_MINSIZEREL}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${COMMON_C_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} ${COMMON_C_FLAGS_RELWITHDEBINFO}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${COMMON_CXX_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${COMMON_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${COMMON_CXX_FLAGS_RELWITHDEBINFO}")
set(PROFILE_FLAGS "-pg -O0 -U_FORTIFY_SOURCE")
set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_DEBUG} ${PROFILE_FLAGS}")
set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS_DEBUG} ${PROFILE_FLAGS}")
if(PROGRAM_MOLD AND CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_COMPILER_IS_CLANG)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=mold")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=mold")
endif()
set(CMAKE_EXE_LINKER_FLAGS_PROFILE "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PROFILE_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_PROFILE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${PROFILE_FLAGS}")
mark_as_advanced(CMAKE_C_FLAGS_PROFILE CMAKE_CXX_FLAGS_PROFILE CMAKE_EXE_LINKER_FLAGS_PROFILE CMAKE_SHARED_LINKER_FLAGS_PROFILE)
if(CHECK_CLANG_TIDY)
if(PROGRAM_CLANG_TIDY)
execute_process(COMMAND bash -c "dirname $(dirname $(dirname $(clang -print-resource-dir)))" OUTPUT_VARIABLE CLANG_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
if(APPLE)
execute_process(COMMAND bash -c "dirname ${CLANG_DIR}" OUTPUT_VARIABLE osxdir OUTPUT_STRIP_TRAILING_WHITESPACE)
set(osxdir "${osxdir}/SDKs/MacOSX.sdk")
file(GLOB CLANG_C_INC ${osxdir}/usr/include)
file(GLOB CLANG_CXX_INC ${osxdir}/usr/include/c++/*)
file(GLOB CLANG_SYS_INC ${osxdir}/System/Library/Frameworks/Kernel.framework/Versions/Current/Headers)
else()
file(GLOB CLANG_C_INC ${CLANG_DIR}/lib*/clang/*/include)
file(GLOB CLANG_CXX_INC ${CLANG_DIR}/include/c++/*)
file(GLOB CLANG_SYS_INC ${CLANG_DIR}/include/x86_64-*/c++/*)
endif()
set(CMAKE_C_CLANG_TIDY ${PROGRAM_CLANG_TIDY} --quiet
--extra-arg=-isystem --extra-arg=${CLANG_C_INC}
--extra-arg=-isystem --extra-arg=${CLANG_SYS_INC})
set(CMAKE_CXX_CLANG_TIDY ${PROGRAM_CLANG_TIDY} --quiet
--extra-arg=-isystem --extra-arg=${CLANG_CXX_INC}
--extra-arg=-isystem --extra-arg=${CLANG_C_INC}
--extra-arg=-isystem --extra-arg=${CLANG_SYS_INC}
--extra-arg=-std=c++${CMAKE_CXX_STANDARD})
set(CMAKE_DISABLE_PRECOMPILE_HEADERS TRUE)
set(CMAKE_C_FLAGS "-Wno-pragmas")
set(CMAKE_C_COMPILE_OPTIONS_IPO)
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated")
set(CMAKE_CXX_COMPILE_OPTIONS_IPO)
else()
message(AUTHOR_WARNING "clang-tidy not available")
endif()
endif()
if(CHECK_CPPCHECK)
if(PROGRAM_CPPCHECK)
set(CMAKE_C_CPPCHECK ${PROGRAM_CPPCHECK}
--enable=warning,style,performance,portability,missingInclude
--force --inline-suppr -j 4 -q
--suppressions-list=${CMAKE_SOURCE_DIR}/.cppcheck-suppressions
--template="{file}:{line} {severity}={id} {message}")
set(CMAKE_CXX_CPPCHECK ${CMAKE_C_CPPCHECK})
else()
message(AUTHOR_WARNING "cppcheck not available")
endif()
endif()
if(CHECK_CPPLINT)
if(PROGRAM_CPPLINT)
set(CMAKE_C_CPPLINT ${PROGRAM_CPPLINT} --quiet)
set(CMAKE_CXX_CPPLINT ${CMAKE_C_CPPLINT})
else()
message(AUTHOR_WARNING "cpplint not available")
endif()
endif()
if(CHECK_IWYU)
if(PROGRAM_IWYU)
set(CMAKE_C_INCLUDE_WHAT_YOU_USE ${PROGRAM_IWYU} -Xiwyu --pch_in_code)
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${CMAKE_C_INCLUDE_WHAT_YOU_USE})
else()
message(WARNING "include-what-you-use not available")
endif()
endif()
if(PROGRAM_CCACHE AND NOT CMAKE_CXX_CLANG_TIDY AND NOT CMAKE_CXX_CPPCHECK AND NOT CMAKE_CXX_CPPLINT AND NOT CMAKE_CXX_INCLUDE_WHAT_YOU_USE)
set(COMPILE_PCH TRUE)
if(NOT CMAKE_VERSION VERSION_LESS 3.5)
set(CMAKE_C_COMPILER_LAUNCHER CCACHE_DEPEND=1 CCACHE_HARDLINK=1 CCACHE_SLOPPINESS=file_stat_matches,include_file_ctime,include_file_mtime,locale,pch_defines,system_headers,time_macros ${PROGRAM_CCACHE})
set(CMAKE_CXX_COMPILER_LAUNCHER ${CMAKE_C_COMPILER_LAUNCHER})
else()
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${PROGRAM_CCACHE}")
endif()
endif()
if(WIN32)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
# set only one of BLISTER_DLL or BLISTER_LIB
set(BLISTER_DLL TRUE)
if(BLISTER_DLL)
add_definitions("-DBLISTER_DLL")
endif()
else()
set(BLISTER_DLL TRUE)
set(BLISTER_LIB TRUE)
endif()
# distclean equivalent
add_custom_target(distclean)
add_custom_command(
COMMENT "Cleaning distribution"
COMMAND $(MAKE) ${CLEAN_COTIRE} clean
COMMAND find . -name .lto -o -name CMakeFiles -o -name CMakeCache.txt -o -name cmake_install.cmake -o -name \\*.ninja -o -name Makefile | xargs rm -rf
TARGET distclean)
add_subdirectory(lib)
add_subdirectory(test)