-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCMakeLists.txt
315 lines (254 loc) · 9.34 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
cmake_minimum_required(VERSION 3.12)
project(dispar)
set(CMAKE_COLOR_MAKEFILE ON)
set(CMAKE_VERBOSE_MAKEFILE OFF)
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
set(
CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
${CMAKE_CURRENT_SOURCE_DIR}/cmake
${CMAKE_CURRENT_SOURCE_DIR}/misc # To find "MacOSXBundleInfo.plist.in"
)
include(policies NO_POLICY_SCOPE)
option(BUILD_TESTS "Build test suite" OFF)
option(USE_CCACHE "Use ccache (if found) to speed up compile and link" OFF)
option(CODE_COVERAGE "Instrument for code coverage (clang only and builds tests!)" OFF)
option(ADDRESS_SANITIZER "Use memory error detector (clang only and implies debug mode!)" OFF)
option(STATIC_ANALYZER "Do static analysis (clang only and implies debug mode!)" OFF)
option(EVERY_WARNING "Show every(!) warning (clang only!)" OFF)
option(TIME_TRACE "Trace compilation time into .json files (clang only!)" OFF)
option(TIME_REPORT "Trace compilation time report (clang and gcc only!)" OFF)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
# Output compile_commands.json (Ninja generator only!)
set(CMAKE_EXPORT_COMPILE_COMMANDS YES)
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}")
include(misc)
include(platform)
include(target)
include(ProcessorCount)
ProcessorCount(CPU_COUNT)
if (${CPU_COUNT} EQUAL 0)
message("Could not determine processor count! Defaulting to 1.")
set(CPU_COUNT 1)
endif()
if (CODE_COVERAGE)
if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
message(FATAL_ERROR "Clang is required to do code coverage!")
endif()
if (NOT BUILD_TESTS)
message("Enabling building of tests since they are required for code coverage!")
set(BUILD_TESTS ON)
endif()
if (NOT LLVM_PROFDATA)
set(LLVM_PROFDATA llvm-profdata)
endif()
if (NOT LLVM_COV)
set(LLVM_COV llvm-cov)
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
endif()
if (ADDRESS_SANITIZER)
if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
message(FATAL_ERROR "Clang is required to use address sanitizer to detect memory errors!")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls")
set(CMAKE_BUILD_TYPE Debug)
endif()
if (STATIC_ANALYZER)
if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
message(FATAL_ERROR "Clang is required to do static analysis!")
endif()
if (NOT SCAN_BUILD)
set(SCAN_BUILD scan-build)
endif()
set(CMAKE_BUILD_TYPE Debug)
endif()
if (EVERY_WARNING)
if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
message(FATAL_ERROR "Clang is required to use -Weverything!")
endif()
# But ignore some warnings.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weverything -Wno-c++98-compat -Wno-padded -Wno-shadow-field-in-constructor -Wno-c++98-compat-pedantic -Wno-sign-conversion -Wno-old-style-cast -Wno-switch-enum -Wno-float-conversion -Wno-documentation-unknown-command -Wno-global-constructors -Wno-exit-time-destructors -Wno-return-std-move-in-c++11 -Wno-missing-prototypes -Wno-date-time")
endif()
if (TIME_TRACE)
if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
message(FATAL_ERROR "Clang is required to use -ftime-trace!")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftime-trace")
endif()
if (TIME_REPORT)
if (NOT ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU"))
message(FATAL_ERROR "Clang or GCC is required to use -ftime-report!")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftime-report")
endif()
# Detect major and minor program version from "Version.h".
file(
STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h" _versions
REGEX "_VERSION ="
)
list(GET _versions 0 _version)
string(REGEX MATCH "[0-9]+" num ${_version})
set(MAJOR_VERSION ${num})
list(GET _versions 1 _version)
string(REGEX MATCH "[0-9]+" num ${_version})
set(MINOR_VERSION ${num})
# Names
set(libName core)
set(binName dispar)
if (BUILD_TESTS)
include(CTest)
enable_testing()
add_subdirectory(tests)
endif()
# Add a target to generate API documentation with Doxygen.
# find_package(Doxygen)
# if (DOXYGEN_FOUND)
# configure_file(
# ${PROJECT_SOURCE_DIR}/doc/doxyfile.in
# ${CMAKE_CURRENT_BINARY_DIR}/doxyfile @ONLY
# )
# add_custom_target(
# doc ${DOXYGEN_EXECUTABLE}
# ${CMAKE_CURRENT_BINARY_DIR}/doxyfile
# WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
# COMMENT "Generating API documentation with Doxygen"
# VERBATIM
# )
# endif()
add_subdirectory(lib)
add_subdirectory(src)
if (APPLE)
set(INSTALL_DIR "dispar.app")
else()
set(INSTALL_DIR "dispar")
endif()
set(DIST_ZIP "dispar-${MAJOR_VERSION}.${MINOR_VERSION}-${CMAKE_HOST_SYSTEM_NAME}.zip")
string(TOLOWER ${DIST_ZIP} DIST_ZIP)
add_custom_target(
dist
${CMAKE_COMMAND} -E tar c ${DIST_ZIP} --format=zip -- ${INSTALL_DIR}
USES_TERMINAL
DEPENDS install
COMMENT "Generating distribution: ${DIST_ZIP}"
)
# Requires llvm/clang v4+!
# Setup: CC=clang-10 CXX=clang++ cmake -G Ninja -DCODE_COVERAGE=ON ../../
# Setup: CC=clang-mp-5.0 CXX=clang++-mp-5.0 cmake -G Ninja -DCODE_COVERAGE=ON -DLLVM_PROFDATA=llvm-profdata-mp-5.0 -DLLVM_COV=llvm-cov-mp-5.0 ../../
if (CODE_COVERAGE)
add_custom_target(
codecov
sh ${CMAKE_SOURCE_DIR}/scripts/code_coverage.sh "${CMAKE_CURRENT_BINARY_DIR}" "${LLVM_PROFDATA}" "${LLVM_COV}"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
USES_TERMINAL
COMMENT "Generating test code coverage report"
DEPENDS tests
)
endif()
# Setup: scan-build cmake -G Ninja -DSTATIC_ANALYZER=ON ../../
# Setup: scan-build-mp-5.0 cmake -DSTATIC_ANALYZER=ON -DSCAN_BUILD=scan-build-mp-5.0 ../../
if (STATIC_ANALYZER)
add_custom_target(
sa
sh ${CMAKE_SOURCE_DIR}/scripts/static_analysis.sh ${SCAN_BUILD} ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${Qt5Core_DIR}/../../ ${binName}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
USES_TERMINAL
COMMENT "Doing static analysis"
)
endif()
# cppcheck can be cloned and compiled: git clone [email protected]:danmar/cppcheck.git
find_program(found_cppcheck cppcheck)
if (found_cppcheck)
add_custom_target(
cppcheck
cppcheck -j ${CPU_COUNT} --enable=all --std=c++17 --suppress=*:${CMAKE_SOURCE_DIR}/lib/* --suppress=*:${CMAKE_SOURCE_DIR}/tests/gtest/* --project=${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json --xml 2> cppcheck.xml && python ${CMAKE_SOURCE_DIR}/scripts/cppcheck-htmlreport.py --file=cppcheck.xml --report-dir=cppcheck
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
USES_TERMINAL
COMMENT "Running cppcheck"
)
endif()
find_program(found_clang_tidy clang-tidy)
if (found_clang_tidy)
set(clang_tidy_cmd_pre find src \\\( -iname '*.cc' -or -iname '*.h' \\\) | xargs -I{} clang-tidy)
set(clang_tidy_cmd_post {})
add_custom_target(
clang-tidy
${clang_tidy_cmd_pre} ${clang_tidy_cmd_post}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Checking with clang-tidy"
USES_TERMINAL
)
add_custom_target(
clang-tidy-fix
${clang_tidy_cmd_pre} --fix --fix-errors ${clang_tidy_cmd_post}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Fixing with clang-tidy"
USES_TERMINAL
)
endif()
message(STATUS "====================================================================")
message(STATUS "Program version: ${MAJOR_VERSION}.${MINOR_VERSION}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
if (APPLE)
message(STATUS "Mac SDK: ${DEV_SDK}")
endif()
message(STATUS "Detected processor count: ${CPU_COUNT}")
message(STATUS "Building test suite: ${BUILD_TESTS}")
if (USE_CCACHE)
message(STATUS "CCache: ${CCACHE} (using to speed up compile + link)")
else()
message(STATUS "CCache: OFF")
endif()
if (CODE_COVERAGE)
message(STATUS "Code coverage: ${CODE_COVERAGE} (target: codecov)")
else()
message(STATUS "Code coverage: ${CODE_COVERAGE}")
endif()
message(STATUS "Address sanitizer: ${ADDRESS_SANITIZER}")
if (STATIC_ANALYZER)
message(STATUS "Static analyzer: ${STATIC_ANALYZER} (target: sa)")
else()
message(STATUS "Static analyzer: ${STATIC_ANALYZER}")
endif()
if (EVERY_WARNING)
message(STATUS "Every warning: ${EVERY_WARNING} (-Weverything)")
else()
message(STATUS "Every warning: OFF")
endif()
if (TIME_TRACE)
message(STATUS "Compilation time trace: ${TIME_TRACE} (-ftime-trace)")
else()
message(STATUS "Compilation time trace: OFF")
endif()
if (TIME_REPORT)
message(STATUS "Compilation time report: ${TIME_REPORT} (-ftime-report)")
else()
message(STATUS "Compilation time report: OFF")
endif()
if (found_cppcheck)
message(STATUS "cppcheck target: enabled")
else()
message(STATUS "cppcheck target: disabled (cppcheck not found)")
endif()
if (found_clang_tidy)
message(STATUS "clang-tidy targets: clang-tidy, clang-tidy-fix")
else()
message(STATUS "clang-tidy targets: disabled (clang-tidy not found)")
endif()
message(STATUS "Found Qt: ${FOUND_QT} (min. v. ${QT_MINIMUM_VERSION})")
if (FOUND_QT)
message(STATUS " Qt5Core_DIR ${Qt5Core_DIR}")
message(STATUS " Qt5Gui_DIR ${Qt5Gui_DIR}")
message(STATUS " Qt5Widgets_DIR ${Qt5Widgets_DIR}")
if (APPLE)
message(STATUS " Qt5PrintSupport_DIR ${Qt5PrintSupport_DIR}")
endif()
if (APPLE OR LINUX)
message(STATUS " Qt5DBus_DIR ${Qt5DBus_DIR}")
endif()
endif()
message(STATUS "====================================================================")