Skip to content

Commit 9c83f07

Browse files
committed
Merge #6: build: Add CMake-based build system (2 of N)
df89945 cmake: Build `leveldb` static library (Hennadii Stepanov) 2e2ce85 cmake: Build `crc32c` static library (Hennadii Stepanov) 4cd5efd cmake: Check compiler features (Hennadii Stepanov) 89be42f cmake: Check system symbols (Hennadii Stepanov) 64b1fd4 cmake: Check system headers (Hennadii Stepanov) 7b87a48 cmake: Add `cmake/introspection.cmake` file (Hennadii Stepanov) Pull request description: The parent PR: bitcoin#25797. The previous PR in the staging branch: #5. This PR details: 1. Added project-wide system introspection. | Autoconf macro | Symbols in `bitcoin-config.h` | CMake implementation | |---|---|---| | [`AC_INIT`](https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/autoconf.html#Initializing-configure) | `PACKAGE_NAME` `PACKAGE_TARNAME` `PACKAGE_VERSION` `PACKAGE_STRING` `PACKAGE_BUGREPORT` `PACKAGE_URL` | Done in #5, except for `PACKAGE_TARNAME` and `PACKAGE_STRING`, which are not currently used | | [`AX_CXX_COMPILE_STDCXX`](https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html) | `HAVE_CXX17` `HAVE_CXX20` | CMake uses its own [implementation](https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html) | | [`AC_C_BIGENDIAN`](https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/autoconf.html#index-AC_005fC_005fBIGENDIAN-892) | `AC_APPLE_UNIVERSAL_BUILD` `WORDS_BIGENDIAN` and C headers | CMake uses its own [implementation](https://cmake.org/cmake/help/latest/module/TestBigEndian.html) | | [`AX_PTHREAD`](https://www.gnu.org/software/autoconf-archive/ax_pthread.html) | `HAVE_PTHREAD` `HAVE_PTHREAD_PRIO_INHERIT` `PTHREAD_CREATE_JOINABLE` | CMake uses its own [implementation](https://cmake.org/cmake/help/latest/module/FindThreads.html) | | [`AC_SYS_LARGEFILE`](https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/autoconf.html#index-AC_005fSYS_005fLARGEFILE-1103) | `_FILE_OFFSET_BITS` `_LARGE_FILES` | _Not implemented yet_ | | [`AC_FUNC_STRERROR_R`](https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/autoconf.html#index-AC_005fFUNC_005fSTRERROR_005fR-517) | `HAVE_DECL_STRERROR_R` `HAVE_STRERROR_R` `STRERROR_R_CHAR_P` and C headers | Implemented `STRERROR_R_CHAR_P`, others are not used | C headers | `HAVE_INTTYPES_H` `HAVE_STDINT_H` `HAVE_STDIO_H` `HAVE_STDLIB_H` `HAVE_STRINGS_H` `HAVE_STRING_H` `HAVE_SYS_STAT_H` `HAVE_SYS_TYPES_H` `HAVE_UNISTD_H` `STDC_HEADERS` | Only `HAVE_STRING_H`, `HAVE_SYS_TYPES_H` and `HAVE_UNISTD_H` are evaluated for internal needs --- 2. Added the `leveldb` build target (including its `crc32c` dependency): - on Linux / *BSD / macOS: ```sh cmake -S . -B build cmake --build build --target leveldb ``` - on Windows: ```cmd cmake -G "Visual Studio 17 2022" -A x64 -S . -B build cmake --build build --config Debug --target leveldb # or --config Release ``` **NOTE**. On Windows (MSVC), the `leveldb` library now uses `crc32c` one. That is not the case when using the current build system from the `build_msvc` directory: https://github.com/hebasto/bitcoin/blob/fe1b3256888bd0e70d0c9655f565e139ec87b606/build_msvc/libleveldb/libleveldb.vcxproj#L53 ACKs for top commit: theuni: > ACK [df89945](df89945) TheCharlatan: ACK df89945 Tree-SHA512: 9a7a4e2d22d5e18af20fec937f64ce43b5c296576abde61db3e4420edadb87d2e830d331fabbf4e8c17e557bf9bd534f491b227d2554839651967f20707bba7b
2 parents 3bc91d2 + df89945 commit 9c83f07

File tree

6 files changed

+676
-0
lines changed

6 files changed

+676
-0
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ set(COPYRIGHT_HOLDERS "The %s developers")
3333
set(COPYRIGHT_HOLDERS_FINAL "The ${PROJECT_NAME} developers")
3434
set(PACKAGE_BUGREPORT "https://github.com/bitcoin/bitcoin/issues")
3535

36+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/module)
37+
3638
# Configurable options.
3739
# When adding a new option, end the <help_text> with a full stop for consistency.
3840
include(CMakeDependentOption)
@@ -58,6 +60,12 @@ else()
5860
endif()
5961
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
6062

63+
include(CheckSourceCompilesAndLinks)
64+
include(cmake/introspection.cmake)
65+
66+
include(cmake/crc32c.cmake)
67+
include(cmake/leveldb.cmake)
68+
6169
add_subdirectory(src)
6270

6371
message("\n")

cmake/bitcoin-config.h.in

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
#define BITCOIN_CONFIG_H
88

9+
/* Define this symbol if type char equals int8_t */
10+
#cmakedefine CHAR_EQUALS_INT8 1
11+
912
/* Version Build */
1013
#define CLIENT_VERSION_BUILD @PROJECT_VERSION_PATCH@
1114

@@ -30,6 +33,156 @@
3033
/* Copyright year */
3134
#define COPYRIGHT_YEAR @COPYRIGHT_YEAR@
3235

36+
/* Define this symbol if you have __builtin_clzl */
37+
#cmakedefine HAVE_BUILTIN_CLZL 1
38+
39+
/* Define this symbol if you have __builtin_clzll */
40+
#cmakedefine HAVE_BUILTIN_CLZLL 1
41+
42+
/* Define to 1 if you have the <byteswap.h> header file. */
43+
#cmakedefine HAVE_BYTESWAP_H 1
44+
45+
/* Define to 1 if you have the declaration of `be16toh', and to 0 if you
46+
don't. */
47+
#cmakedefine01 HAVE_DECL_BE16TOH
48+
49+
/* Define to 1 if you have the declaration of `be32toh', and to 0 if you
50+
don't. */
51+
#cmakedefine01 HAVE_DECL_BE32TOH
52+
53+
/* Define to 1 if you have the declaration of `be64toh', and to 0 if you
54+
don't. */
55+
#cmakedefine01 HAVE_DECL_BE64TOH
56+
57+
/* Define to 1 if you have the declaration of `bswap_16', and to 0 if you
58+
don't. */
59+
#cmakedefine01 HAVE_DECL_BSWAP_16
60+
61+
/* Define to 1 if you have the declaration of `bswap_32', and to 0 if you
62+
don't. */
63+
#cmakedefine01 HAVE_DECL_BSWAP_32
64+
65+
/* Define to 1 if you have the declaration of `bswap_64', and to 0 if you
66+
don't. */
67+
#cmakedefine01 HAVE_DECL_BSWAP_64
68+
69+
/* Define to 1 if you have the declaration of `fork', and to 0 if you don't.
70+
*/
71+
#cmakedefine01 HAVE_DECL_FORK
72+
73+
/* Define to 1 if you have the declaration of `freeifaddrs', and to 0 if you
74+
don't. */
75+
#cmakedefine01 HAVE_DECL_FREEIFADDRS
76+
77+
/* Define to 1 if you have the declaration of `getifaddrs', and to 0 if you
78+
don't. */
79+
#cmakedefine01 HAVE_DECL_GETIFADDRS
80+
81+
/* Define to 1 if you have the declaration of `htobe16', and to 0 if you
82+
don't. */
83+
#cmakedefine01 HAVE_DECL_HTOBE16
84+
85+
/* Define to 1 if you have the declaration of `htobe32', and to 0 if you
86+
don't. */
87+
#cmakedefine01 HAVE_DECL_HTOBE32
88+
89+
/* Define to 1 if you have the declaration of `htobe64', and to 0 if you
90+
don't. */
91+
#cmakedefine01 HAVE_DECL_HTOBE64
92+
93+
/* Define to 1 if you have the declaration of `htole16', and to 0 if you
94+
don't. */
95+
#cmakedefine01 HAVE_DECL_HTOLE16
96+
97+
/* Define to 1 if you have the declaration of `htole32', and to 0 if you
98+
don't. */
99+
#cmakedefine01 HAVE_DECL_HTOLE32
100+
101+
/* Define to 1 if you have the declaration of `htole64', and to 0 if you
102+
don't. */
103+
#cmakedefine01 HAVE_DECL_HTOLE64
104+
105+
/* Define to 1 if you have the declaration of `le16toh', and to 0 if you
106+
don't. */
107+
#cmakedefine01 HAVE_DECL_LE16TOH
108+
109+
/* Define to 1 if you have the declaration of `le32toh', and to 0 if you
110+
don't. */
111+
#cmakedefine01 HAVE_DECL_LE32TOH
112+
113+
/* Define to 1 if you have the declaration of `le64toh', and to 0 if you
114+
don't. */
115+
#cmakedefine01 HAVE_DECL_LE64TOH
116+
117+
/* Define to 1 if you have the declaration of `pipe2', and to 0 if you don't.
118+
*/
119+
#cmakedefine01 HAVE_DECL_PIPE2
120+
121+
/* Define to 1 if you have the declaration of `setsid', and to 0 if you don't.
122+
*/
123+
#cmakedefine01 HAVE_DECL_SETSID
124+
125+
/* Define if the visibility attribute is supported. */
126+
#cmakedefine HAVE_DEFAULT_VISIBILITY_ATTRIBUTE 1
127+
128+
/* Define if the dllexport attribute is supported. */
129+
#cmakedefine HAVE_DLLEXPORT_ATTRIBUTE 1
130+
131+
/* Define to 1 if you have the <endian.h> header file. */
132+
#cmakedefine HAVE_ENDIAN_H 1
133+
134+
/* Define to 1 if fdatasync is available. */
135+
#cmakedefine HAVE_FDATASYNC 1
136+
137+
/* Define this symbol if the BSD getentropy system call is available with
138+
sys/random.h */
139+
#cmakedefine HAVE_GETENTROPY_RAND 1
140+
141+
/* Define this symbol if gmtime_r is available */
142+
#cmakedefine HAVE_GMTIME_R 1
143+
144+
/* Define this symbol if you have malloc_info */
145+
#cmakedefine HAVE_MALLOC_INFO 1
146+
147+
/* Define this symbol if you have mallopt with M_ARENA_MAX */
148+
#cmakedefine HAVE_MALLOPT_ARENA_MAX 1
149+
150+
/* Define to 1 if O_CLOEXEC flag is available. */
151+
#cmakedefine01 HAVE_O_CLOEXEC
152+
153+
/* Define this symbol if you have posix_fallocate */
154+
#cmakedefine HAVE_POSIX_FALLOCATE 1
155+
156+
/* Define this symbol to build code that uses getauxval) */
157+
#cmakedefine HAVE_STRONG_GETAUXVAL 1
158+
159+
/* Define this symbol if the BSD sysctl() is available */
160+
#cmakedefine HAVE_SYSCTL 1
161+
162+
/* Define this symbol if the BSD sysctl(KERN_ARND) is available */
163+
#cmakedefine HAVE_SYSCTL_ARND 1
164+
165+
/* Define to 1 if std::system or ::wsystem is available. */
166+
#cmakedefine HAVE_SYSTEM 1
167+
168+
/* Define to 1 if you have the <sys/endian.h> header file. */
169+
#cmakedefine HAVE_SYS_ENDIAN_H 1
170+
171+
/* Define this symbol if the Linux getrandom system call is available */
172+
#cmakedefine HAVE_SYS_GETRANDOM 1
173+
174+
/* Define to 1 if you have the <sys/prctl.h> header file. */
175+
#cmakedefine HAVE_SYS_PRCTL_H 1
176+
177+
/* Define to 1 if you have the <sys/resources.h> header file. */
178+
#cmakedefine HAVE_SYS_RESOURCES_H 1
179+
180+
/* Define to 1 if you have the <sys/vmmeter.h> header file. */
181+
#cmakedefine HAVE_SYS_VMMETER_H 1
182+
183+
/* Define to 1 if you have the <vm/vm_param.h> header file. */
184+
#cmakedefine HAVE_VM_VM_PARAM_H 1
185+
33186
/* Define to the address where bug reports for this package should be sent. */
34187
#define PACKAGE_BUGREPORT "@PACKAGE_BUGREPORT@"
35188

@@ -42,4 +195,11 @@
42195
/* Define to the version of this package. */
43196
#define PACKAGE_VERSION "@PROJECT_VERSION@"
44197

198+
/* Define to 1 if strerror_r returns char *. */
199+
#cmakedefine STRERROR_R_CHAR_P 1
200+
201+
/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
202+
significant byte first (like Motorola and SPARC, unlike Intel). */
203+
#cmakedefine WORDS_BIGENDIAN 1
204+
45205
#endif //BITCOIN_CONFIG_H

cmake/crc32c.cmake

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright (c) 2023 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
# This file is part of the transition from Autotools to CMake. Once CMake
6+
# support has been merged we should switch to using the upstream CMake
7+
# buildsystem.
8+
9+
include(CheckCXXSourceCompiles)
10+
11+
# Check for __builtin_prefetch support in the compiler.
12+
check_cxx_source_compiles("
13+
int main() {
14+
char data = 0;
15+
const char* address = &data;
16+
__builtin_prefetch(address, 0, 0);
17+
return 0;
18+
}
19+
" HAVE_BUILTIN_PREFETCH
20+
)
21+
22+
# Check for _mm_prefetch support in the compiler.
23+
check_cxx_source_compiles("
24+
#if defined(_MSC_VER)
25+
#include <intrin.h>
26+
#else
27+
#include <xmmintrin.h>
28+
#endif
29+
30+
int main() {
31+
char data = 0;
32+
const char* address = &data;
33+
_mm_prefetch(address, _MM_HINT_NTA);
34+
return 0;
35+
}
36+
" HAVE_MM_PREFETCH
37+
)
38+
39+
# Check for SSE4.2 support in the compiler.
40+
if(MSVC)
41+
set(SSE42_CXXFLAGS /arch:AVX)
42+
else()
43+
set(SSE42_CXXFLAGS -msse4.2)
44+
endif()
45+
check_cxx_source_compiles_with_flags("${SSE42_CXXFLAGS}" "
46+
#include <cstdint>
47+
#if defined(_MSC_VER)
48+
#include <intrin.h>
49+
#elif defined(__GNUC__) && defined(__SSE4_2__)
50+
#include <nmmintrin.h>
51+
#endif
52+
53+
int main() {
54+
uint64_t l = 0;
55+
l = _mm_crc32_u8(l, 0);
56+
l = _mm_crc32_u32(l, 0);
57+
l = _mm_crc32_u64(l, 0);
58+
return l;
59+
}
60+
" HAVE_SSE42
61+
)
62+
63+
# Check for ARMv8 w/ CRC and CRYPTO extensions support in the compiler.
64+
set(ARM_CRC_CXXFLAGS -march=armv8-a+crc)
65+
check_cxx_source_compiles_with_flags("${ARM_CRC_CXXFLAGS}" "
66+
#include <arm_acle.h>
67+
#include <arm_neon.h>
68+
69+
int main() {
70+
#ifdef __aarch64__
71+
__crc32cb(0, 0); __crc32ch(0, 0); __crc32cw(0, 0); __crc32cd(0, 0);
72+
vmull_p64(0, 0);
73+
#else
74+
#error crc32c library does not support hardware acceleration on 32-bit ARM
75+
#endif
76+
return 0;
77+
}
78+
" HAVE_ARM64_CRC32C
79+
)
80+
81+
add_library(crc32c STATIC EXCLUDE_FROM_ALL
82+
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c.cc
83+
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_portable.cc
84+
)
85+
86+
target_compile_definitions(crc32c
87+
PRIVATE
88+
HAVE_BUILTIN_PREFETCH=$<BOOL:${HAVE_BUILTIN_PREFETCH}>
89+
HAVE_MM_PREFETCH=$<BOOL:${HAVE_MM_PREFETCH}>
90+
HAVE_STRONG_GETAUXVAL=$<BOOL:${HAVE_STRONG_GETAUXVAL}>
91+
HAVE_SSE42=$<BOOL:${HAVE_SSE42}>
92+
HAVE_ARM64_CRC32C=$<BOOL:${HAVE_ARM64_CRC32C}>
93+
BYTE_ORDER_BIG_ENDIAN=${WORDS_BIGENDIAN}
94+
)
95+
96+
target_include_directories(crc32c
97+
PUBLIC
98+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/crc32c/include>
99+
)
100+
101+
if(HAVE_SSE42)
102+
target_sources(crc32c PRIVATE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc)
103+
set_property(SOURCE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc
104+
APPEND PROPERTY COMPILE_OPTIONS ${SSE42_CXXFLAGS}
105+
)
106+
endif()
107+
108+
if(HAVE_ARM64_CRC32C)
109+
target_sources(crc32c PRIVATE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc)
110+
set_property(SOURCE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc
111+
APPEND PROPERTY COMPILE_OPTIONS ${ARM_CRC_CXXFLAGS}
112+
)
113+
endif()

0 commit comments

Comments
 (0)