-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCMakeLists.txt
512 lines (439 loc) · 13.4 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# SPDX-FileCopyrightText: 2020-2024 Jochem Rutgers
#
# SPDX-License-Identifier: MPL-2.0
cmake_minimum_required(VERSION 3.5)
project(libstored)
cmake_policy(VERSION 3.5)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
Debug
CACHE STRING "Build type" FORCE
)
endif()
message(STATUS "CMAKE_BUILD_TYPE is ${CMAKE_BUILD_TYPE}")
if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
message(FATAL_ERROR "CMake generation is not allowed within the source directory.")
endif()
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX
"${CMAKE_CURRENT_BINARY_DIR}/deploy"
CACHE PATH "Override default install path" FORCE
)
endif()
message(STATUS "Install prefix set to ${CMAKE_INSTALL_PREFIX}")
if(APPLE)
list(APPEND CMAKE_INSTALL_RPATH "@executable_path/../lib")
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.8")
list(APPEND CMAKE_BUILD_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
else()
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
endif()
elseif(UNIX)
list(APPEND CMAKE_INSTALL_RPATH "\$ORIGIN/../lib")
endif()
option(LIBSTORED_DEV "Enable by default development related build options" OFF)
if(LIBSTORED_DEV)
set(LIBSTORED_DEV_OPTION ON)
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY TRUE)
else()
set(LIBSTORED_DEV_OPTION OFF)
endif()
option(LIBSTORED_COVERAGE "Enable code coverage instrumentation" OFF)
if(CMAKE_HOST_WIN32)
find_program(PYTHON_EXECUTABLE python)
else()
find_program(PYTHON_EXECUTABLE python3)
find_program(HAVE_VALGRIND valgrind)
endif()
if(NOT PYTHON_EXECUTABLE)
message(FATAL_ERROR "Cannot find python")
endif()
option(LIBSTORED_PYLIBSTORED "Build python libstored package" ON)
if(CMAKE_HOST_WIN32 OR NOT LIBSTORED_PYLIBSTORED)
# The documentation needs wavedrom (via sphinx), but sphinxcontrib-wavedrom needs cairo. In
# turn, that package needs libcairo-2.dll (64-bit, matching python's word size), which
# cannot be installed automatically by bootstrap.cmd. So, by default, leave it out.
option(LIBSTORED_DOCUMENTATION "Create the HTML based API documentation" OFF)
else()
option(LIBSTORED_DOCUMENTATION "Create the HTML based API documentation" ON)
endif()
option(LIBSTORED_VENV "Create a python venv" ON)
if(LIBSTORED_VENV)
message(STATUS "Checking venv...")
if(CMAKE_HOST_UNIX)
if(LIBSTORED_PYLIBSTORED OR LIBSTORED_DOCUMENTATION)
set(venv_check "check")
else()
set(venv_check "check-minimal")
endif()
# Force CC and CXX to default, in case we are cross-compiling and compiling wheels
# for the venv...
execute_process(
COMMAND
${CMAKE_COMMAND} -E env --unset=CC --unset=CXX
${PROJECT_SOURCE_DIR}/dist/common/venv.sh -p "${PYTHON_EXECUTABLE}"
${venv_check}
RESULT_VARIABLE ret
)
list(APPEND CMAKE_PROGRAM_PATH "${PROJECT_SOURCE_DIR}/dist/venv/bin")
set(PYTHON_EXECUTABLE "${PROJECT_SOURCE_DIR}/dist/venv/bin/python3")
elseif(CMAKE_HOST_WIN32)
execute_process(
COMMAND ${CMAKE_COMMAND} -E env --unset=CC --unset=CXX
${PROJECT_SOURCE_DIR}/dist/win32/venv.cmd check RESULT_VARIABLE ret
)
list(APPEND CMAKE_PROGRAM_PATH "${PROJECT_SOURCE_DIR}/dist/venv/Scripts")
set(PYTHON_EXECUTABLE "${PROJECT_SOURCE_DIR}/dist/venv/Scripts/python.exe")
else()
message(FATAL_ERROR "Don't know how to create a venv.")
endif()
if(ret EQUAL "2")
message(FATAL_ERROR "Cannot create venv")
endif()
endif()
message(STATUS "Using python ${PYTHON_EXECUTABLE}")
# Should be defaulted to OFF some day.
option(LIBSTORED_DRAFT_API "Enable draft API" ON)
if(LIBSTORED_DRAFT_API)
message(STATUS "Enable libstored draft API")
endif()
if(MINGW OR MSVC)
set(LIBSTORED_ENABLE_ASAN_DEFAULT OFF)
set(LIBSTORED_ENABLE_LSAN_DEFAULT OFF)
set(LIBSTORED_ENABLE_UBSAN_DEFAULT OFF)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
set(LIBSTORED_ENABLE_LSAN_DEFAULT OFF)
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "9.1.0")
set(LIBSTORED_ENABLE_ASAN_DEFAULT ${LIBSTORED_DEV_OPTION})
set(LIBSTORED_ENABLE_UBSAN_DEFAULT ${LIBSTORED_DEV_OPTION})
else()
set(LIBSTORED_ENABLE_ASAN_DEFAULT OFF)
set(LIBSTORED_ENABLE_UBSAN_DEFAULT OFF)
endif()
else()
set(LIBSTORED_ENABLE_ASAN_DEFAULT ${LIBSTORED_DEV_OPTION})
set(LIBSTORED_ENABLE_LSAN_DEFAULT ${LIBSTORED_DEV_OPTION})
set(LIBSTORED_ENABLE_UBSAN_DEFAULT ${LIBSTORED_DEV_OPTION})
endif()
option(LIBSTORED_ENABLE_ASAN "Build with Address Sanitizer" ${LIBSTORED_ENABLE_ASAN_DEFAULT})
option(LIBSTORED_ENABLE_LSAN "Build with Leak Sanitizer" ${LIBSTORED_ENABLE_LSAN_DEFAULT})
option(LIBSTORED_ENABLE_UBSAN "Build with Undefined Behavior Sanitizer"
${LIBSTORED_ENABLE_UBSAN_DEFAULT}
)
option(LIBSTORED_DISABLE_EXCEPTIONS "Disable exception support" OFF)
option(LIBSTORED_DISABLE_RTTI "Disable run-time type information support" OFF)
option(LIBSTORED_HAVE_HEATSHRINK "Use heatshrink" ON)
if(LIBSTORED_HAVE_HEATSHRINK)
find_package(Heatshrink REQUIRED)
endif()
option(LIBSTORED_HAVE_LIBZMQ "Use libzmq" ON)
if(LIBSTORED_HAVE_LIBZMQ)
find_package(ZeroMQ REQUIRED)
endif()
option(LIBSTORED_HAVE_ZTH "Use Zth" OFF)
if(LIBSTORED_HAVE_ZTH)
find_package(Zth REQUIRED)
endif()
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.10")
cmake_policy(SET CMP0071 NEW)
find_package(
Qt6
COMPONENTS Core
QUIET
)
if(Qt6_FOUND)
set(LIBSTORED_FOUND_QT ON)
else()
# Fallback to Qt5
find_package(
Qt5
COMPONENTS Core
QUIET
)
if(Qt5_FOUND)
set(LIBSTORED_FOUND_QT ON)
endif()
endif()
option(LIBSTORED_HAVE_QT "Use Qt" ${LIBSTORED_FOUND_QT})
if(NOT LIBSTORED_FOUND_QT AND LIBSTORED_HAVE_QT)
message(FATAL_ERROR "Cannot find Qt")
endif()
if(LIBSTORED_HAVE_QT)
if(Qt6_FOUND)
message(STATUS "Found Qt6")
elseif(Qt5_FOUND)
message(STATUS "Found Qt5")
else()
message(WARNING "Found unknown Qt version")
endif()
endif()
endif()
set(LIBSTORED_CLANG_TIDY_DEFAULT OFF)
if(${CMAKE_VERSION} VERSION_GREATER "3.6.0")
find_program(
CLANG_TIDY_EXE
NAMES "clang-tidy"
DOC "Path to clang-tidy executable"
)
if(# We must have clang-tidy.
CLANG_TIDY_EXE
AND # We must compile for C++11 or later...
(NOT CMAKE_CXX_STANDARD OR NOT CMAKE_CXX_STANDARD EQUAL 98)
AND # ...except when running in Windows, which only supports C++14 or later.
(NOT WIN32 OR NOT CMAKE_CXX_STANDARD EQUAL 11)
AND # ...or Zth is used, which is not compatible with the MSVC headers, which are used by
# clang-tidy.
(NOT WIN32 OR NOT LIBSTORED_HAVE_ZTH)
AND # ...and somehow mingw builds don't work properly on newer versions of clang-tidy.
(NOT MINGW)
)
# It seems that if clang is not installed, clang-tidy doesn't work properly.
find_program(
CLANG_EXE
NAMES "clang"
DOC "Path to clang executable"
)
if(CLANG_EXE AND LIBSTORED_DEV)
set(LIBSTORED_CLANG_TIDY_DEFAULT ${LIBSTORED_DEV_OPTION})
endif()
endif()
endif()
option(LIBSTORED_CLANG_TIDY "Run clang-tidy" ${LIBSTORED_CLANG_TIDY_DEFAULT})
set(LIBSTORED_GCC_ANALYZER_DEFAULT OFF)
if(# Need gcc for this...
CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
AND # ...starting from version 10, but let's start from 13, as it gets
# better then, although there is officially no support for C++ yet.
CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "13"
)
set(LIBSTORED_GCC_ANALYZER_DEFAULT ${LIBSTORED_DEV_OPTION})
endif()
option(LIBSTORED_GCC_ANALYZER "Run GCC's analyzer" ${LIBSTORED_GCC_ANALYZER_DEFAULT})
set(LIBSTORED_SOURCE_DIR
${PROJECT_SOURCE_DIR}
CACHE STRING "Path to the libstored sources" FORCE
)
list(APPEND CMAKE_MODULE_PATH ${LIBSTORED_SOURCE_DIR}/cmake)
include(libstored)
add_subdirectory(extern)
if(LIBSTORED_DOCUMENTATION AND LIBSTORED_PYLIBSTORED)
add_subdirectory(sphinx)
endif()
if(DEFINED ENV{XILINX_VIVADO})
find_program(
VIVADO_CMD
NAMES vivado
PATHS "$ENV{XILINX_VIVADO}/bin"
)
endif()
if(NOT VIVADO_CMD)
if(WIN32)
file(GLOB VIVADO_CMDS "C:/Xilinx/Vivado/*/bin/vivado.bat")
else()
file(GLOB VIVADO_CMDS "/opt/Xilinx/Vivado/*/bin/vivado")
endif()
if(VIVADO_CMDS)
list(SORT VIVADO_CMDS)
list(GET VIVADO_CMDS -1 VIVADO_CMD)
endif()
endif()
if(VIVADO_CMD)
message(STATUS "Using vivado ${VIVADO_CMD}")
endif()
if(LIBSTORED_VENV)
set(LIBSTORED_PIP_INSTALL_USER_DEFAULT OFF)
else()
set(LIBSTORED_PIP_INSTALL_USER_DEFAULT ON)
endif()
option(LIBSTORED_PIP_INSTALL_USER "Run pip install with --user flag when installing wheel files"
${LIBSTORED_PIP_INSTALL_USER_DEFAULT}
)
if(LIBSTORED_PYLIBSTORED)
add_subdirectory(python)
endif()
option(LIBSTORED_REGEN_LAUNCH_JSON "Regenerate launch.json" OFF)
if((NOT EXISTS ${LIBSTORED_SOURCE_DIR}/.vscode/launch.json AND NOT MSVC)
OR LIBSTORED_REGEN_LAUNCH_JSON
)
set(LIBSTORED_GEN_LAUNCH_JSON ON)
else()
set(LIBSTORED_GEN_LAUNCH_JSON OFF)
endif()
set_property(
GLOBAL
PROPERTY
LIBSTORED_LAUNCH_JSON
"\
{
\"version\": \"0.2.0\",
\"configurations\": ["
)
function(add_launch_json TARGET)
add_dependencies(examples ${TARGET})
get_property(_launch GLOBAL PROPERTY LIBSTORED_LAUNCH_JSON)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set_property(
GLOBAL
PROPERTY
LIBSTORED_LAUNCH_JSON
"${_launch}
{
\"type\": \"cppdbg\",
\"request\": \"launch\",
\"name\": \"${TARGET}\",
\"preLaunchTask\": \"build-examples\",
\"stopAtEntry\": true,
\"program\": \"$<SHELL_PATH:$<TARGET_FILE:${TARGET}>>\",
\"cwd\": \"$<SHELL_PATH:$<TARGET_FILE_DIR:${TARGET}>>\",
},"
)
else()
set_property(
GLOBAL
PROPERTY
LIBSTORED_LAUNCH_JSON
"${_launch}
{
\"type\": \"f5anything\",
\"request\": \"launch\",
\"name\": \"${TARGET}\",
\"command\": \"$<SHELL_PATH:$<TARGET_FILE:${TARGET}>>\",
},"
)
endif()
endfunction()
option(LIBSTORED_EXAMPLES "Build examples" ON)
add_custom_target(examples)
if(LIBSTORED_EXAMPLES)
add_subdirectory(examples)
endif()
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.11.0")
if(CMAKE_CROSSCOMPILING)
set(LIBSTORED_TESTS_DEFAULT OFF)
else()
set(LIBSTORED_TESTS_DEFAULT ${LIBSTORED_DEV_OPTION})
endif()
option(LIBSTORED_TESTS "Build the tests" ${LIBSTORED_TESTS_DEFAULT})
if(LIBSTORED_TESTS)
enable_testing()
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
set(CTEST_OUTPUT_ON_FAILURE 1)
list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")
endif()
add_subdirectory(tests)
endif()
endif()
find_program(CPPCHECK_CMD NAMES cppcheck)
if(CPPCHECK_CMD)
execute_process(COMMAND ${CPPCHECK_CMD} --version OUTPUT_VARIABLE cppcheck_version)
string(REGEX REPLACE "^Cppcheck " "" cppcheck_version "${cppcheck_version}")
if(NOT "${cppcheck_version}" VERSION_LESS 2.11)
list(APPEND CPPCHECK_ARGS "--check-level=exhaustive")
endif()
if(LIBSTORED_EXAMPLES)
macro(cppcheck_add app)
if(TARGET ${app}-libstored-generate)
set(CPPCHECK_EXAMPLES
${CPPCHECK_EXAMPLES} -I examples/${app}/libstored/include
examples/${app}
)
set(CPPCHECK_DEPENDS ${CPPCHECK_DEPENDS} ${app}-libstored-generate)
endif()
endmacro()
cppcheck_add(1_hello)
cppcheck_add(2_basic)
cppcheck_add(3_scope)
cppcheck_add(4_function)
cppcheck_add(5_debug)
cppcheck_add(6_hooks)
cppcheck_add(7_protocol)
cppcheck_add(8_sync)
cppcheck_add(9_fpga)
cppcheck_add(components)
# cppcheck_add(concurrency) # This one somehow makes cppcheck choke.
cppcheck_add(control)
cppcheck_add(meta)
if(TARGET meta-libstored-generate)
set(CPPCHECK_EXAMPLES ${CPPCHECK_EXAMPLES} -I
${CMAKE_BINARY_DIR}/examples/meta examples/meta
)
set(CPPCHECK_DEPENDS ${CPPCHECK_DEPENDS} meta-generate)
endif()
cppcheck_add(pipes)
cppcheck_add(qt)
cppcheck_add(terminal)
cppcheck_add(zmqserver)
endif()
add_custom_target(
libstored-cppcheck
COMMAND
${CPPCHECK_CMD} --enable=warning,style,information --force --inline-suppr
--quiet --suppressions-list=${CMAKE_CURRENT_SOURCE_DIR}/.cppcheck_suppr
--error-exitcode=1 -j 4 --library=gnu
"--template=[{file}:{line}]: ({severity},{id}) {message}" --std=c++17
--language=c++ -D__cplusplus=201703L -DCPPCHECK -D__GNUC__
# --xml
-I include ${CPPCHECK_EXAMPLES} src
# --check-config
${CPPCHECK_ARGS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Running cppcheck..."
VERBATIM
)
if(CPPCHECK_DEPENDS)
add_dependencies(libstored-cppcheck ${CPPCHECK_DEPENDS})
endif()
option(LIBSTORED_CPPCHECK "Run cppcheck by default" ${LIBSTORED_DEV_OPTION})
if(LIBSTORED_CPPCHECK)
add_custom_target(libstored-cppcheck-all ALL DEPENDS libstored-cppcheck)
endif()
endif()
find_program(FLAWFINDER_EXE NAMES flawfinder)
if(FLAWFINDER_EXE)
add_custom_target(
libstored-flawfinder
DEPENDS all-libstored-generate
COMMAND ${FLAWFINDER_EXE} --error-level=3 examples src include
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Running flawfinder..."
VERBATIM
)
option(LIBSTORED_FLAWFINDER "Run flawfinder by default" ${LIBSTORED_DEV_OPTION})
if(LIBSTORED_FLAWFINDER)
add_custom_target(libstored-flawfinder-all ALL DEPENDS libstored-flawfinder)
endif()
endif()
add_custom_target(
libstored-reuse
COMMAND ${PYTHON_EXECUTABLE} -m reuse lint
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMENT "Checking REUSE compliance"
VERBATIM
)
if(TARGET gui-qrc)
add_dependencies(libstored-reuse gui-qrc)
endif()
if(LIBSTORED_DEV)
add_custom_target(libstored-reuse-all ALL DEPENDS libstored-reuse)
endif()
if(LIBSTORED_DIST_DIR)
add_subdirectory(${LIBSTORED_DIST_DIR})
endif()
if(LIBSTORED_GEN_LAUNCH_JSON)
get_property(_launch GLOBAL PROPERTY LIBSTORED_LAUNCH_JSON)
set(LIBSTORED_LAUNCH_JSON
"${_launch}
]
}
"
)
file(
GENERATE
OUTPUT ${LIBSTORED_SOURCE_DIR}/.vscode/launch.json
CONTENT "${LIBSTORED_LAUNCH_JSON}"
)
set(LIBSTORED_REGEN_LAUNCH_JSON
OFF
CACHE INTERNAL "" FORCE
)
endif()