forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CMakeLists.txt
664 lines (599 loc) · 29.5 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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# Copyright (c) 2023 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
# IMPORTANT: Changes which affect binary results may not be quietly gated
# by CMake version.
#
# Debian 10 Buster, https://wiki.debian.org/LTS, EOL 2024:
# - CMake 3.13.4, https://packages.debian.org/buster/cmake
#
# Ubuntu 22.04 Jammy, https://wiki.ubuntu.com/Releases, EOL 2032:
# - CMake 3.22.1, https://packages.ubuntu.com/jammy/cmake
#
# Visual Studio 17 2022, https://visualstudio.microsoft.com:
# - CMake 3.24
#
# All policies known to the running version of CMake and introduced
# in the 3.24 version or earlier will be set to use NEW behavior.
# All policies introduced in later versions will be unset.
# See: https://cmake.org/cmake/help/latest/manual/cmake-policies.7.html
cmake_minimum_required(VERSION 3.13...3.24)
project("Bitcoin Core"
VERSION 24.99.0
DESCRIPTION "Bitcoin client software"
HOMEPAGE_URL "https://bitcoincore.org/"
LANGUAGES CXX ASM
)
set(PACKAGE_NAME ${PROJECT_NAME})
set(CLIENT_VERSION_IS_RELEASE "false")
set(COPYRIGHT_YEAR "2023")
set(COPYRIGHT_HOLDERS "The %s developers")
set(COPYRIGHT_HOLDERS_FINAL "The ${PROJECT_NAME} developers")
set(PACKAGE_BUGREPORT "https://github.com/bitcoin/bitcoin/issues")
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/module)
# Configurable options.
# When adding a new option, end the <help_text> with a full stop for consistency.
include(CMakeDependentOption)
option(BUILD_DAEMON "Build bitcoind executable." ON)
option(BUILD_CLI "Build bitcoin-cli executable." ON)
option(BUILD_TX "Build bitcoin-tx executable." ON)
option(BUILD_UTIL "Build bitcoin-util executable." ON)
option(BUILD_UTIL_CHAINSTATE "Build experimental bitcoin-chainstate executable." OFF)
# We do not use CMake's BUILD_SHARED_LIBS option.
option(BUILD_SHARED "Build shared libraries." ON)
option(BUILD_STATIC "Build static libraries." ON)
if(NOT BUILD_SHARED AND NOT BUILD_STATIC)
message(FATAL_ERROR "At least one of BUILD_SHARED and BUILD_STATIC must be enabled.")
endif()
option(BUILD_BITCOINCONSENSUS_LIB "Build bitcoinconsensus library." ON)
option(BUILD_BITCOINKERNEL_LIB "Build experimental bitcoinkernel library." ${BUILD_UTIL_CHAINSTATE})
option(ASM "Use assembly routines." ON)
option(ENABLE_WALLET "Enable wallet." ON)
# TODO: These tri-state options will be removed and most features
# will become opt-in by default before merging into master.
include(TristateOption)
tristate_option(WITH_SQLITE "Enable SQLite wallet support." "if libsqlite3 is found." AUTO)
tristate_option(WITH_BDB "Enable Berkeley DB (BDB) wallet support." "if libdb_cxx is found." AUTO)
option(WARN_INCOMPATIBLE_BDB "Warn when using a Berkeley DB (BDB) version other than 4.8." ON)
cmake_dependent_option(BUILD_WALLET_TOOL "Build bitcoin-wallet tool." ON "ENABLE_WALLET" OFF)
cmake_dependent_option(CXX20 "Enable compilation in C++20 mode." OFF "NOT MSVC" ON)
option(THREADLOCAL "Enable features that depend on the C++ thread_local keyword (currently just thread names in debug logs)." ON)
option(HARDENING "Attempt to harden the resulting executables." ON)
option(REDUCE_EXPORTS "Attempt to reduce exported symbols in the resulting executables." OFF)
option(WERROR "Treat compiler warnings as errors." OFF)
tristate_option(CCACHE "Use ccache for compiling." "if ccache is found." AUTO)
tristate_option(WITH_NATPMP "Enable NAT-PMP." "if libnatpmp is found." AUTO)
tristate_option(WITH_MINIUPNPC "Enable UPnP." "if libminiupnpc is found." AUTO)
tristate_option(WITH_ZMQ "Enable ZMQ notifications." "if libzmq is found." AUTO)
tristate_option(WITH_USDT
"Enable tracepoints for Userspace, Statically Defined Tracing."
"if sys/sdt.h is found."
AUTO
)
tristate_option(WITH_SECCOMP
"Enable experimental syscall sandbox feature (-sandbox)."
"if seccomp-bpf is found under Linux x86_64."
AUTO
)
tristate_option(WITH_QRENCODE "Enable QR code support." "if libqrencode is found." AUTO)
tristate_option(WITH_EXTERNAL_SIGNER "Enable external signer support." "if Boost.Process is found." AUTO)
tristate_option(MULTIPROCESS "Build multiprocess bitcoin-node, bitcoin-wallet, and bitcoin-gui executables in addition to monolithic bitcoind and bitcoin-qt executables. Requires libmultiprocess library. Experimental." "if libmultiprocess is found." OFF)
option(BUILD_TESTS "Build test_bitcoin executable." ON)
option(BUILD_BENCH "Build bench_bitcoin executable." ON)
cmake_dependent_option(BUILD_FUZZ_BINARY "Build fuzz binary." ON "NOT MSVC" OFF)
cmake_dependent_option(FUZZ "Build for fuzzing. Enabling this will disable all other targets and override BUILD_FUZZ_BINARY." OFF "NOT MSVC" OFF)
if(FUZZ)
message(WARNING "FUZZ=ON will disable all other targets and force BUILD_FUZZ_BINARY=ON.")
set(BUILD_DAEMON OFF)
set(BUILD_CLI OFF)
set(BUILD_TX OFF)
set(BUILD_UTIL OFF)
set(BUILD_UTIL_CHAINSTATE OFF)
set(BUILD_SHARED OFF)
set(BUILD_BITCOINKERNEL_LIB OFF)
set(BUILD_WALLET_TOOL OFF)
set(WITH_NATPMP OFF)
set(WITH_MINIUPNPC OFF)
set(WITH_ZMQ OFF)
set(WITH_EXTERNAL_SIGNER OFF)
set(BUILD_TESTS OFF)
set(BUILD_BENCH OFF)
set(WITH_GUI no)
set(BUILD_FUZZ_BINARY ON)
endif()
option(INSTALL_MAN "Install man pages." ON)
if(CXX20)
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(configure_warnings)
include(TryAppendCompilerFlag)
include(TryAppendLinkerFlag)
if(WIN32)
#[=[
This build system supports two ways to build binaries for Windows.
1. Building on Windows using MSVC.
Implementation notes:
- /DWIN32 and /D_WINDOWS definitions are included into the CMAKE_CXX_FLAGS_INIT
and CMAKE_CXX_FLAGS_INIT variables by default.
- A run-time library is selected using the CMAKE_MSVC_RUNTIME_LIBRARY variable.
- MSVC-specific options, for example, /Zc:__cplusplus, are additionally required.
2. Cross-compiling using MinGW.
Implementation notes:
- WIN32 and _WINDOWS definitions must be provided explicitly.
- A run-time library must be specified explicitly using _MT definition.
]=]
add_compile_definitions(_WIN32_WINNT=0x0601 _WIN32_IE=0x0501 WIN32_LEAN_AND_MEAN NOMINMAX)
if(MSVC)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
add_compile_options(/utf-8 /Zc:__cplusplus)
endif()
if(MINGW)
add_compile_definitions(WIN32 _WINDOWS _MT)
set(mingw_linker_flags "")
# We require Windows 7 (NT 6.1) or later.
try_append_linker_flag(mingw_linker_flags "-Wl,--major-subsystem-version,6")
try_append_linker_flag(mingw_linker_flags "-Wl,--minor-subsystem-version,1")
separate_arguments(mingw_linker_flags)
add_link_options(${mingw_linker_flags})
endif()
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
add_compile_definitions(MAC_OSX)
# These flags are specific to ld64, and may cause issues with other linkers.
# For example: GNU ld will interpret -dead_strip as -de and then try and use
# "ad_strip" as the symbol for the entry point.
set(macos_linker_flags "")
try_append_linker_flag(macos_linker_flags "-Wl,-dead_strip")
try_append_linker_flag(macos_linker_flags "-Wl,-dead_strip_dylibs")
try_append_linker_flag(macos_linker_flags "-Wl,-headerpad_max_install_names")
separate_arguments(macos_linker_flags)
add_link_options(${macos_linker_flags})
endif()
if(CMAKE_CROSSCOMPILING AND DEPENDS_ALLOW_HOST_PACKAGES)
list(APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_SYSTEM_PREFIX_PATH}")
endif()
include(AddThreadsIfNeeded)
add_threads_if_needed()
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14)
include(CheckPIESupported)
check_pie_supported(OUTPUT_VARIABLE check_pie_output LANGUAGES CXX)
if(CMAKE_CXX_LINK_PIE_SUPPORTED)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
else()
include(CheckSourceCompilesAndLinks)
check_cxx_source_links_with_flags(-fPIE "int main(){}" COMPILER_SUPPORTS_PIE)
if(COMPILER_SUPPORTS_PIE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
endif()
if(SANITIZERS)
# First check if the compiler accepts flags. If an incompatible pair like
# -fsanitize=address,thread is used here, this check will fail. This will also
# fail if a bad argument is passed, e.g. -fsanitize=undfeined
set(sanitizers_cxx_flags "")
try_append_cxxflag(sanitizers_cxx_flags "-fsanitize=${SANITIZERS}")
if(NOT try_append_cxxflag_result)
message(FATAL_ERROR "Compiler did not accept requested flags.")
endif()
separate_arguments(sanitizers_cxx_flags)
add_compile_options(${sanitizers_cxx_flags})
# Some compilers (e.g. GCC) require additional libraries like libasan,
# libtsan, libubsan, etc. Make sure linking still works with the sanitize
# flag. This is a separate check so we can give a better error message when
# the sanitize flags are supported by the compiler but the actual sanitizer
# libs are missing.
set(sanitizers_linker_flags "")
set(sanitizers_linker_code "
#include <cstdint>
#include <cstddef>
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; }
__attribute__((weak)) // allow for libFuzzer linking
int main() { return 0; }
")
try_append_linker_flag(sanitizers_linker_flags "-fsanitize=${SANITIZERS}"
SOURCE "${sanitizers_linker_code}"
)
if(NOT try_append_linker_flag_result)
message(FATAL_ERROR "Linker did not accept requested flags, you are missing required libraries.")
endif()
separate_arguments(sanitizers_linker_flags)
add_link_options(${sanitizers_linker_flags})
if(WITH_SECCOMP)
message(WARNING "Specifying -DSANITIZERS forces -DWITH_SECCOMP=OFF since the sanitizers introduce use of syscalls not allowed by the bitcoind syscall sandbox (-sandbox=<mode>).")
set(WITH_SECCOMP OFF)
endif()
endif()
include(AddBoostIfNeeded)
add_boost_if_needed()
include(CheckSourceCompilesAndLinks)
include(AddLibeventIfNeeded)
add_libevent_if_needed()
include(cmake/introspection.cmake)
include(cmake/crc32c.cmake)
include(cmake/leveldb.cmake)
include(cmake/minisketch.cmake)
include(cmake/secp256k1.cmake)
set(warn_cxx_flags "")
if(MSVC)
try_append_cxxflag(warn_cxx_flags "/W3")
try_append_cxxflag(warn_cxx_flags "/wd4018")
try_append_cxxflag(warn_cxx_flags "/wd4244")
try_append_cxxflag(warn_cxx_flags "/wd4267")
try_append_cxxflag(warn_cxx_flags "/wd4715")
try_append_cxxflag(warn_cxx_flags "/wd4805")
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
else()
try_append_cxxflag(warn_cxx_flags "-Wall")
try_append_cxxflag(warn_cxx_flags "-Wextra")
try_append_cxxflag(warn_cxx_flags "-Wgnu")
# Some compilers will ignore -Wformat-security without -Wformat, so just combine the two here.
try_append_cxxflag(warn_cxx_flags "-Wformat -Wformat-security")
try_append_cxxflag(warn_cxx_flags "-Wvla")
try_append_cxxflag(warn_cxx_flags "-Wshadow-field")
try_append_cxxflag(warn_cxx_flags "-Wthread-safety")
try_append_cxxflag(warn_cxx_flags "-Wloop-analysis")
try_append_cxxflag(warn_cxx_flags "-Wredundant-decls")
try_append_cxxflag(warn_cxx_flags "-Wunused-member-function")
try_append_cxxflag(warn_cxx_flags "-Wdate-time")
try_append_cxxflag(warn_cxx_flags "-Wconditional-uninitialized")
try_append_cxxflag(warn_cxx_flags "-Wduplicated-branches")
try_append_cxxflag(warn_cxx_flags "-Wduplicated-cond")
try_append_cxxflag(warn_cxx_flags "-Wlogical-op")
try_append_cxxflag(warn_cxx_flags "-Woverloaded-virtual")
try_append_cxxflag(warn_cxx_flags "-Wunreachable-code-loop-increment")
try_append_cxxflag(warn_cxx_flags "-Wimplicit-fallthrough")
try_append_cxxflag(warn_cxx_flags "-Wdocumentation")
# -Wsuggest-override is broken with GCC before 9.2
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78010
try_append_cxxflag(warn_cxx_flags "-Wsuggest-override"
CHECK_PASSED_FLAG "-Wsuggest-override"
CHECK_FAILED_FLAG ""
SOURCE "struct A { virtual void f(); }; struct B : A { void f() final; };"
)
# Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
# unknown options if any other warning is produced. Test the -Wfoo case, and
# set the -Wno-foo case if it works.
try_append_cxxflag(warn_cxx_flags "-Wunused-parameter" CHECK_PASSED_FLAG "-Wno-unused-parameter")
try_append_cxxflag(warn_cxx_flags "-Wself-assign" CHECK_PASSED_FLAG "-Wno-self-assign")
try_append_cxxflag(warn_cxx_flags "-Wgnu-zero-variadic-macro-arguments" CHECK_PASSED_FLAG "-Wno-gnu-zero-variadic-macro-arguments")
if(CMAKE_EXPORT_COMPILE_COMMANDS)
try_append_cxxflag(warn_cxx_flags "-Wgnu-include-next" CHECK_PASSED_FLAG "-Wno-gnu-include-next")
endif()
endif()
separate_arguments(warn_cxx_flags)
add_compile_options(${warn_cxx_flags})
if(NOT MSVC)
# Don't allow extended (non-ASCII) symbols in identifiers. This is easier for code review.
try_append_cxxflag(core_cxx_flags "-fno-extended-identifiers")
add_compile_options(${core_cxx_flags})
endif()
# Redefine configuration flags.
add_compile_definitions($<$<CONFIG:Debug>:DEBUG>)
add_compile_definitions($<$<CONFIG:Debug>:DEBUG_LOCKORDER>)
add_compile_definitions($<$<CONFIG:Debug>:DEBUG_LOCKCONTENTION>)
add_compile_definitions($<$<CONFIG:Debug>:RPC_DOC_CHECK>)
add_compile_definitions($<$<CONFIG:Debug>:ABORT_ON_FAILED_ASSUME>)
if(MSVC)
string(REGEX REPLACE "/DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
string(REGEX REPLACE "/DNDEBUG[ \t\r\n]*" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
string(REGEX REPLACE "/DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
string(REGEX REPLACE "/DNDEBUG[ \t\r\n]*" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REGEX REPLACE "/DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
string(REGEX REPLACE "/DNDEBUG[ \t\r\n]*" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
else()
string(REGEX REPLACE "-DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
string(REGEX REPLACE "-DNDEBUG[ \t\r\n]*" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
string(REGEX REPLACE "-DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
string(REGEX REPLACE "-DNDEBUG[ \t\r\n]*" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REGEX REPLACE "-DNDEBUG[ \t\r\n]*" "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
string(REGEX REPLACE "-DNDEBUG[ \t\r\n]*" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
string(REGEX REPLACE "-O3[ \t\r\n]*" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
string(REGEX REPLACE "-O3[ \t\r\n]*" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(debug_c_flags "")
try_append_cflag(debug_c_flags "-O0")
try_append_cflag(debug_c_flags "-g3")
if(NOT C_SUPPORTS_G3)
try_append_cflag(debug_c_flags "-g")
endif()
set(CMAKE_C_FLAGS_DEBUG "${debug_c_flags}")
set(debug_cxx_flags "")
try_append_cxxflag(debug_cxx_flags "-O0")
try_append_cxxflag(debug_cxx_flags "-g3")
if(NOT CXX_SUPPORTS_G3)
try_append_cxxflag(debug_cxx_flags "-g")
endif()
try_append_cxxflag(debug_cxx_flags "-ftrapv")
if(MINGW)
try_append_cxxflag(debug_cxx_flags "-Wa,-mbig-obj")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "${debug_cxx_flags}")
endif()
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
set(default_build_type "RelWithDebInfo")
if(is_multi_config)
set(CMAKE_CONFIGURATION_TYPES "${default_build_type}" "Release" "Debug" "MinSizeRel" CACHE STRING
"Supported configuration types."
FORCE
)
else()
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
STRINGS "${default_build_type}" "Release" "Debug" "MinSizeRel"
)
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to \"${default_build_type}\" as none was specified")
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING
"Choose the type of build."
FORCE
)
endif()
endif()
include(cmake/optional_qt.cmake)
include(cmake/optional.cmake)
set(hardened_cxx_flags "")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# All versions of gcc that we commonly use for building are subject to bug
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90348. To work around that, set
# -fstack-reuse=none for all gcc builds.
try_append_cxxflag(hardened_cxx_flags "-fstack-reuse=none")
endif()
if(HARDENING)
if(MSVC)
add_link_options(/DYNAMICBASE /HIGHENTROPYVA /NXCOMPAT)
else()
try_append_cxxflag(hardened_cxx_flags "-Wstack-protector")
try_append_cxxflag(hardened_cxx_flags "-fstack-protector-all")
try_append_cxxflag(hardened_cxx_flags "-fcf-protection=full")
if(MINGW)
link_libraries(ssp)
else()
# stack-clash-protection doesn't currently work, and likely should just be skipped for Windows.
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458 for more details.
try_append_cxxflag(hardened_cxx_flags "-fstack-clash-protection")
endif()
set(hardened_linker_flags "")
try_append_linker_flag(hardened_linker_flags "-Wl,--enable-reloc-section")
try_append_linker_flag(hardened_linker_flags "-Wl,--dynamicbase")
try_append_linker_flag(hardened_linker_flags "-Wl,--nxcompat")
try_append_linker_flag(hardened_linker_flags "-Wl,--high-entropy-va")
try_append_linker_flag(hardened_linker_flags "-Wl,-z,relro")
try_append_linker_flag(hardened_linker_flags "-Wl,-z,now")
try_append_linker_flag(hardened_linker_flags "-Wl,-z,separate-code")
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
try_append_linker_flag(hardened_linker_flags "-Wl,-bind_at_load")
endif()
separate_arguments(hardened_linker_flags)
add_link_options(${hardened_linker_flags})
endif()
endif()
separate_arguments(hardened_cxx_flags)
add_compile_options(${hardened_cxx_flags})
if(REDUCE_EXPORTS)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_link_options(-Wl,--exclude-libs,ALL)
endif()
endif()
if(WERROR)
if(MSVC)
add_compile_options(/WX)
else()
set(error_cxx_flags "")
try_append_cxxflag(error_cxx_flags "-Werror")
if(NOT CXX_SUPPORTS_WERROR)
message(FATAL_ERROR "WERROR set but -Werror is not usable.")
endif()
# -Wreturn-type is broken in GCC for MinGW-w64.
# https://sourceforge.net/p/mingw-w64/bugs/306/
try_append_cxxflag(error_cxx_flags "-Werror=return-type"
CHECK_PASSED_FLAG ""
CHECK_FAILED_FLAG "-Wno-error=return-type"
SOURCE "#include <cassert>\nint f(){ assert(false); }"
)
separate_arguments(error_cxx_flags)
add_compile_options(${error_cxx_flags})
endif()
endif()
if(BUILD_BITCOINCONSENSUS_LIB)
set(HAVE_CONSENSUS_LIB ON)
endif()
find_package(Python3 3.8 COMPONENTS Interpreter)
set(PYTHON_COMMAND ${Python3_EXECUTABLE})
add_subdirectory(src)
add_subdirectory(test)
include(cmake/tests.cmake)
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(OBJCOPY ${CMAKE_OBJCOPY})
set(STRIP ${CMAKE_STRIP})
configure_file(contrib/devtools/split-debug.sh.in split-debug.sh @ONLY)
endif()
include(GetAllExecutables)
get_all_executables(executables)
add_custom_target(check-symbols
COMMAND echo "Running symbol and dynamic library checks..."
COMMAND ${PYTHON_COMMAND} ${CMAKE_SOURCE_DIR}/contrib/devtools/symbol-check.py ${executables}
VERBATIM
)
if(HARDENING)
add_custom_target(check-security
COMMAND echo "Checking binary security..."
COMMAND ${PYTHON_COMMAND} ${CMAKE_SOURCE_DIR}/contrib/devtools/security-check.py ${executables}
VERBATIM
)
else()
add_custom_target(check-security)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(exe_format MACHO)
elseif(WIN32)
set(exe_format PE)
elseif(UNIX)
set(exe_format ELF)
endif()
if(CMAKE_CROSSCOMPILING)
string(REPLACE ";" " " c_compiler_command "${DEPENDS_C_COMPILER_WITH_LAUNCHER}")
else()
set(c_compiler_command "${CMAKE_C_COMPILER}")
endif()
add_custom_target(test-security-check
COMMAND env CC=${c_compiler_command} CFLAGS=${CMAKE_C_FLAGS} LDFLAGS=${CMAKE_EXE_LINKER_FLAGS} ${PYTHON_COMMAND} ${CMAKE_SOURCE_DIR}/contrib/devtools/test-security-check.py TestSecurityChecks.test_${exe_format}
COMMAND env CC=${c_compiler_command} CFLAGS=${CMAKE_C_FLAGS} LDFLAGS=${CMAKE_EXE_LINKER_FLAGS} ${PYTHON_COMMAND} ${CMAKE_SOURCE_DIR}/contrib/devtools/test-symbol-check.py TestSymbolChecks.test_${exe_format}
VERBATIM
)
if(MINGW AND TARGET bitcoin-qt AND TARGET bitcoind AND TARGET bitcoin-cli AND TARGET bitcoin-tx AND TARGET bitcoin-wallet AND TARGET bitcoin-util AND TARGET test_bitcoin)
# TODO: After dropping Autotools-based build system, replace
# this code with the CPack NSIS Generator.
# See https://cmake.org/cmake/help/latest/cpack_gen/nsis.html
include(GenerateSetupNsi)
generate_setup_nsi()
add_custom_target(deploy
COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_BINARY_DIR}" --config "$<CONFIG>" --prefix "${CMAKE_BINARY_DIR}/release" --strip
COMMAND "${CMAKE_COMMAND}" -E rename "${CMAKE_BINARY_DIR}/release/bin/$<TARGET_FILE_NAME:bitcoind>" "${CMAKE_BINARY_DIR}/release/$<TARGET_FILE_NAME:bitcoind>"
COMMAND "${CMAKE_COMMAND}" -E rename "${CMAKE_BINARY_DIR}/release/bin/$<TARGET_FILE_NAME:bitcoin-qt>" "${CMAKE_BINARY_DIR}/release/$<TARGET_FILE_NAME:bitcoin-qt>"
COMMAND "${CMAKE_COMMAND}" -E rename "${CMAKE_BINARY_DIR}/release/bin/$<TARGET_FILE_NAME:test_bitcoin>" "${CMAKE_BINARY_DIR}/release/$<TARGET_FILE_NAME:test_bitcoin>"
COMMAND "${CMAKE_COMMAND}" -E rename "${CMAKE_BINARY_DIR}/release/bin/$<TARGET_FILE_NAME:bitcoin-cli>" "${CMAKE_BINARY_DIR}/release/$<TARGET_FILE_NAME:bitcoin-cli>"
COMMAND "${CMAKE_COMMAND}" -E rename "${CMAKE_BINARY_DIR}/release/bin/$<TARGET_FILE_NAME:bitcoin-tx>" "${CMAKE_BINARY_DIR}/release/$<TARGET_FILE_NAME:bitcoin-tx>"
COMMAND "${CMAKE_COMMAND}" -E rename "${CMAKE_BINARY_DIR}/release/bin/$<TARGET_FILE_NAME:bitcoin-wallet>" "${CMAKE_BINARY_DIR}/release/$<TARGET_FILE_NAME:bitcoin-wallet>"
COMMAND "${CMAKE_COMMAND}" -E rename "${CMAKE_BINARY_DIR}/release/bin/$<TARGET_FILE_NAME:bitcoin-util>" "${CMAKE_BINARY_DIR}/release/$<TARGET_FILE_NAME:bitcoin-util>"
COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_BINARY_DIR}/release/bin"
COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_BINARY_DIR}/release/lib"
COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_BINARY_DIR}/release/include"
COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_BINARY_DIR}/release/share"
COMMAND makensis -V2 "${CMAKE_BINARY_DIR}/share/setup.nsi"
VERBATIM
)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_CROSSCOMPILING AND TARGET bitcoin-qt)
set(macos_app "Bitcoin-Qt.app")
string(REPLACE " " "-" OSX_VOLNAME ${PROJECT_NAME})
configure_file(cmake/macdeploy/osx_volname.in osx_volname @ONLY)
configure_file(cmake/macdeploy/InfoPlist.strings.in ${macos_app}/Contents/Resources/Base.lproj/InfoPlist.strings @ONLY)
configure_file(cmake/macdeploy/PkgInfo ${macos_app}/Contents/PkgInfo COPYONLY)
configure_file(contrib/macdeploy/background.tiff contrib/macdeploy/background.tiff COPYONLY)
configure_file(share/qt/Info.plist.in ${macos_app}/Contents/Info.plist @ONLY)
configure_file(src/qt/res/icons/bitcoin.icns ${macos_app}/Contents/Resources/bitcoin.icns COPYONLY)
add_custom_command(
OUTPUT dist/${macos_app}/Contents/MacOS/Bitcoin-Qt
COMMAND "${CMAKE_COMMAND}" -E touch "${CMAKE_BINARY_DIR}/${macos_app}/Contents/Resources/empty.lproj"
COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_BINARY_DIR}" --config "$<CONFIG>" --component GUI --prefix "${CMAKE_BINARY_DIR}/release" --strip
COMMAND "${CMAKE_COMMAND}" -E make_directory "${CMAKE_BINARY_DIR}/${macos_app}/Contents/MacOS"
COMMAND "${CMAKE_COMMAND}" -E rename "${CMAKE_BINARY_DIR}/release/bin/$<TARGET_FILE_NAME:bitcoin-qt>" "${CMAKE_BINARY_DIR}/${macos_app}/Contents/MacOS/Bitcoin-Qt"
COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_BINARY_DIR}/release"
COMMAND PYTHONPATH=${PYTHONPATH} INSTALL_NAME_TOOL=${CMAKE_INSTALL_NAME_TOOL} OTOOL=${OTOOL} STRIP=${CMAKE_STRIP} ${PYTHON_COMMAND} ${CMAKE_SOURCE_DIR}/contrib/macdeploy/macdeployqtplus ${macos_app} ${OSX_VOLNAME} -translations-dir=${QT_TRANSLATIONS_DIR}
VERBATIM
)
add_custom_target(deploydir
DEPENDS dist/${macos_app}/Contents/MacOS/Bitcoin-Qt
)
set(xorrisofs_options)
if(DEFINED ENV{SOURCE_DATE_EPOCH})
set(xorrisofs_options -volume_date all_file_dates =$ENV{SOURCE_DATE_EPOCH})
endif()
add_custom_target(deploy
COMMAND xorrisofs -D -l -V "${OSX_VOLNAME}" -no-pad -r -dir-mode 0755 -o ${OSX_VOLNAME}.dmg ${CMAKE_BINARY_DIR}/dist -- ${xorrisofs_options}
VERBATIM
)
add_dependencies(deploy deploydir)
endif()
get_directory_property(definitions COMPILE_DEFINITIONS)
include(SeparateConfigs)
separate_configs(definitions)
message("\n")
message("Configure summary")
message("=================")
message("Executables:")
message(" bitcoind ............................ ${BUILD_DAEMON}")
message(" bitcoin-cli ......................... ${BUILD_CLI}")
message(" bitcoin-tx .......................... ${BUILD_TX}")
message(" bitcoin-util ........................ ${BUILD_UTIL}")
message(" bitcoin-chainstate (experimental) ... ${BUILD_UTIL_CHAINSTATE}")
message(" bitcoin-wallet ...................... ${BUILD_WALLET_TOOL}")
message("Libraries:")
message(" build shared ........................ ${BUILD_SHARED}")
message(" build static ........................ ${BUILD_STATIC}")
message(" libbitcoinconsensus ................. ${BUILD_BITCOINCONSENSUS_LIB}")
message(" libbitcoinkernel (experimental) ..... ${BUILD_BITCOINKERNEL_LIB}")
message("Wallet support:")
message(" SQLite, descriptor wallets .......... ${WITH_SQLITE}")
message(" Berkeley DB, legacy wallets ......... ${WITH_BDB}")
message("Optional packages:")
message(" GUI ................................. ${WITH_GUI}")
if(WITH_GUI)
message(" QR code (GUI) ....................... ${WITH_QRENCODE}")
endif()
message(" external signer ..................... ${WITH_EXTERNAL_SIGNER}")
message(" multiprocess ........................ ${MULTIPROCESS}")
message(" NAT-PMP ............................. ${WITH_NATPMP}")
message(" UPnP ................................ ${WITH_MINIUPNPC}")
message(" ZeroMQ .............................. ${WITH_ZMQ}")
message(" USDT tracing ........................ ${WITH_USDT}")
message(" experimental syscall sandbox ........ ${WITH_SECCOMP}")
message("Tests:")
message(" test_bitcoin ........................ ${BUILD_TESTS}")
message(" bench_bitcoin ....................... ${BUILD_BENCH}")
message(" fuzz binary ......................... ${BUILD_FUZZ_BINARY}")
message("")
if(CMAKE_CROSSCOMPILING)
if(ANDROID)
set(cross_status "TRUE, for ${CMAKE_SYSTEM_NAME}, ${CMAKE_ANDROID_ARCH_ABI}")
else()
set(cross_status "TRUE, for ${CMAKE_SYSTEM_NAME}, ${CMAKE_SYSTEM_PROCESSOR}")
endif()
else()
set(cross_status "FALSE")
endif()
message("Cross compiling ....................... ${cross_status}")
message("Preprocessor defined macros ........... ${definitions_ALL}")
message("C compiler ............................ ${CMAKE_C_COMPILER}")
message("CFLAGS ................................ ${CMAKE_C_FLAGS}")
message("C++ compiler .......................... ${CMAKE_CXX_COMPILER}")
message("CXXFLAGS .............................. ${CMAKE_CXX_FLAGS}")
get_directory_property(common_compile_options COMPILE_OPTIONS)
string(REPLACE ";" " " common_compile_options "${common_compile_options}")
message("Common compile options ................ ${common_compile_options}")
get_directory_property(common_link_options LINK_OPTIONS)
string(REPLACE ";" " " common_link_options "${common_link_options}")
message("Common link options ................... ${common_link_options}")
message("Linker flags for executables .......... ${CMAKE_EXE_LINKER_FLAGS}")
message("Linker flags for shared libraries ..... ${CMAKE_SHARED_LINKER_FLAGS}")
if(NOT is_multi_config)
message("Build type:")
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
message(" - Preprocessor defined macros ........ ${definitions_${build_type}}")
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${build_type}}")
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_${build_type}}")
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${build_type}}")
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${build_type}}")
else()
message("Supported configurations .............. ${CMAKE_CONFIGURATION_TYPES}")
message("Debug configuration:")
message(" - Preprocessor defined macros ........ ${definitions_DEBUG}")
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_DEBUG}")
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_DEBUG}")
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
message("Release configuration:")
message(" - Preprocessor defined macros ........ ${definitions_RELEASE}")
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_RELEASE}")
message(" - CXXFLAGS ........................... ${CMAKE_CXX_FLAGS_RELEASE}")
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
endif()
message("Use assembly routines ................. ${ASM}")
message("Treat compiler warnings as errors ..... ${WERROR}")
message("Use ccache for compiling .............. ${CCACHE}")
message("\n")
if(configure_warnings)
message(" ******\n")
foreach(warning IN LISTS configure_warnings)
message(WARNING "${warning}")
endforeach()
message(" ******\n")
endif()