From 7b87a4834a5ae858237eaa87af8f370b2e31f87a Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 16 Feb 2023 11:12:22 +0000 Subject: [PATCH 1/6] cmake: Add `cmake/introspection.cmake` file --- CMakeLists.txt | 2 ++ cmake/bitcoin-config.h.in | 4 ++++ cmake/introspection.cmake | 7 +++++++ 3 files changed, 13 insertions(+) create mode 100644 cmake/introspection.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bc8e3b9894af..295e1bb2f6f3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,8 @@ else() endif() set(CMAKE_POSITION_INDEPENDENT_CODE ON) +include(cmake/introspection.cmake) + add_subdirectory(src) message("\n") diff --git a/cmake/bitcoin-config.h.in b/cmake/bitcoin-config.h.in index 3195c6a526c3d..1f0ff4d17a582 100644 --- a/cmake/bitcoin-config.h.in +++ b/cmake/bitcoin-config.h.in @@ -42,4 +42,8 @@ /* Define to the version of this package. */ #define PACKAGE_VERSION "@PROJECT_VERSION@" +/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most + significant byte first (like Motorola and SPARC, unlike Intel). */ +#cmakedefine WORDS_BIGENDIAN 1 + #endif //BITCOIN_CONFIG_H diff --git a/cmake/introspection.cmake b/cmake/introspection.cmake new file mode 100644 index 0000000000000..edb09d22c1fed --- /dev/null +++ b/cmake/introspection.cmake @@ -0,0 +1,7 @@ +# 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. + +include(TestBigEndian) + +test_big_endian(WORDS_BIGENDIAN) From 64b1fd4cf6bbb14bc131e999f0f11c0aaf23925e Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Tue, 21 Feb 2023 20:30:08 +0000 Subject: [PATCH 2/6] cmake: Check system headers --- cmake/bitcoin-config.h.in | 21 +++++++++++++++++++++ cmake/introspection.cmake | 10 ++++++++++ 2 files changed, 31 insertions(+) diff --git a/cmake/bitcoin-config.h.in b/cmake/bitcoin-config.h.in index 1f0ff4d17a582..4bcf82df9899d 100644 --- a/cmake/bitcoin-config.h.in +++ b/cmake/bitcoin-config.h.in @@ -30,6 +30,27 @@ /* Copyright year */ #define COPYRIGHT_YEAR @COPYRIGHT_YEAR@ +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_BYTESWAP_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_ENDIAN_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_ENDIAN_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_PRCTL_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_RESOURCES_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_VMMETER_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_VM_VM_PARAM_H 1 + /* Define to the address where bug reports for this package should be sent. */ #define PACKAGE_BUGREPORT "@PACKAGE_BUGREPORT@" diff --git a/cmake/introspection.cmake b/cmake/introspection.cmake index edb09d22c1fed..5c399d1cf1872 100644 --- a/cmake/introspection.cmake +++ b/cmake/introspection.cmake @@ -2,6 +2,16 @@ # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. +include(CheckIncludeFileCXX) include(TestBigEndian) test_big_endian(WORDS_BIGENDIAN) + +# The following HAVE_{HEADER}_H variables go to the bitcoin-config.h header. +check_include_file_cxx(byteswap.h HAVE_BYTESWAP_H) +check_include_file_cxx(endian.h HAVE_ENDIAN_H) +check_include_file_cxx(sys/endian.h HAVE_SYS_ENDIAN_H) +check_include_file_cxx(sys/prctl.h HAVE_SYS_PRCTL_H) +check_include_file_cxx(sys/resources.h HAVE_SYS_RESOURCES_H) +check_include_file_cxx(sys/vmmeter.h HAVE_SYS_VMMETER_H) +check_include_file_cxx(vm/vm_param.h HAVE_VM_VM_PARAM_H) From 89be42f0d7f7f4cfc48809b445442b48f25a0712 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:53:26 +0000 Subject: [PATCH 3/6] cmake: Check system symbols Co-authored-by: Cory Fields Co-authored-by: Vasil Dimov --- CMakeLists.txt | 3 + cmake/bitcoin-config.h.in | 126 ++++++++++ cmake/introspection.cmake | 225 ++++++++++++++++++ .../module/CheckSourceCompilesAndLinks.cmake | 36 +++ 4 files changed, 390 insertions(+) create mode 100644 cmake/module/CheckSourceCompilesAndLinks.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 295e1bb2f6f3a..605b2675f1b64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,8 @@ 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 with a full stop for consistency. include(CMakeDependentOption) @@ -58,6 +60,7 @@ else() endif() set(CMAKE_POSITION_INDEPENDENT_CODE ON) +include(CheckSourceCompilesAndLinks) include(cmake/introspection.cmake) add_subdirectory(src) diff --git a/cmake/bitcoin-config.h.in b/cmake/bitcoin-config.h.in index 4bcf82df9899d..759bcb8d1cb28 100644 --- a/cmake/bitcoin-config.h.in +++ b/cmake/bitcoin-config.h.in @@ -30,15 +30,138 @@ /* Copyright year */ #define COPYRIGHT_YEAR @COPYRIGHT_YEAR@ +/* Define this symbol if you have __builtin_clzl */ +#cmakedefine HAVE_BUILTIN_CLZL 1 + +/* Define this symbol if you have __builtin_clzll */ +#cmakedefine HAVE_BUILTIN_CLZLL 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_BYTESWAP_H 1 +/* Define to 1 if you have the declaration of `be16toh', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_BE16TOH + +/* Define to 1 if you have the declaration of `be32toh', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_BE32TOH + +/* Define to 1 if you have the declaration of `be64toh', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_BE64TOH + +/* Define to 1 if you have the declaration of `bswap_16', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_BSWAP_16 + +/* Define to 1 if you have the declaration of `bswap_32', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_BSWAP_32 + +/* Define to 1 if you have the declaration of `bswap_64', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_BSWAP_64 + +/* Define to 1 if you have the declaration of `fork', and to 0 if you don't. + */ +#cmakedefine01 HAVE_DECL_FORK + +/* Define to 1 if you have the declaration of `freeifaddrs', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_FREEIFADDRS + +/* Define to 1 if you have the declaration of `getifaddrs', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_GETIFADDRS + +/* Define to 1 if you have the declaration of `htobe16', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_HTOBE16 + +/* Define to 1 if you have the declaration of `htobe32', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_HTOBE32 + +/* Define to 1 if you have the declaration of `htobe64', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_HTOBE64 + +/* Define to 1 if you have the declaration of `htole16', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_HTOLE16 + +/* Define to 1 if you have the declaration of `htole32', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_HTOLE32 + +/* Define to 1 if you have the declaration of `htole64', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_HTOLE64 + +/* Define to 1 if you have the declaration of `le16toh', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_LE16TOH + +/* Define to 1 if you have the declaration of `le32toh', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_LE32TOH + +/* Define to 1 if you have the declaration of `le64toh', and to 0 if you + don't. */ +#cmakedefine01 HAVE_DECL_LE64TOH + +/* Define to 1 if you have the declaration of `pipe2', and to 0 if you don't. + */ +#cmakedefine01 HAVE_DECL_PIPE2 + +/* Define to 1 if you have the declaration of `setsid', and to 0 if you don't. + */ +#cmakedefine01 HAVE_DECL_SETSID + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_ENDIAN_H 1 +/* Define to 1 if fdatasync is available. */ +#cmakedefine HAVE_FDATASYNC 1 + +/* Define this symbol if the BSD getentropy system call is available with + sys/random.h */ +#cmakedefine HAVE_GETENTROPY_RAND 1 + +/* Define this symbol if gmtime_r is available */ +#cmakedefine HAVE_GMTIME_R 1 + +/* Define this symbol if you have malloc_info */ +#cmakedefine HAVE_MALLOC_INFO 1 + +/* Define this symbol if you have mallopt with M_ARENA_MAX */ +#cmakedefine HAVE_MALLOPT_ARENA_MAX 1 + +/* Define to 1 if O_CLOEXEC flag is available. */ +#cmakedefine01 HAVE_O_CLOEXEC + +/* Define this symbol if you have posix_fallocate */ +#cmakedefine HAVE_POSIX_FALLOCATE 1 + +/* Define this symbol to build code that uses getauxval) */ +#cmakedefine HAVE_STRONG_GETAUXVAL 1 + +/* Define this symbol if the BSD sysctl() is available */ +#cmakedefine HAVE_SYSCTL 1 + +/* Define this symbol if the BSD sysctl(KERN_ARND) is available */ +#cmakedefine HAVE_SYSCTL_ARND 1 + +/* Define to 1 if std::system or ::wsystem is available. */ +#cmakedefine HAVE_SYSTEM 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SYS_ENDIAN_H 1 +/* Define this symbol if the Linux getrandom system call is available */ +#cmakedefine HAVE_SYS_GETRANDOM 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SYS_PRCTL_H 1 @@ -63,6 +186,9 @@ /* Define to the version of this package. */ #define PACKAGE_VERSION "@PROJECT_VERSION@" +/* Define to 1 if strerror_r returns char *. */ +#cmakedefine STRERROR_R_CHAR_P 1 + /* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most significant byte first (like Motorola and SPARC, unlike Intel). */ #cmakedefine WORDS_BIGENDIAN 1 diff --git a/cmake/introspection.cmake b/cmake/introspection.cmake index 5c399d1cf1872..624f70ad10cb9 100644 --- a/cmake/introspection.cmake +++ b/cmake/introspection.cmake @@ -2,6 +2,8 @@ # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. +include(CheckCXXSourceCompiles) +include(CheckCXXSymbolExists) include(CheckIncludeFileCXX) include(TestBigEndian) @@ -15,3 +17,226 @@ check_include_file_cxx(sys/prctl.h HAVE_SYS_PRCTL_H) check_include_file_cxx(sys/resources.h HAVE_SYS_RESOURCES_H) check_include_file_cxx(sys/vmmeter.h HAVE_SYS_VMMETER_H) check_include_file_cxx(vm/vm_param.h HAVE_VM_VM_PARAM_H) + +check_cxx_source_compiles(" + int main() + { + (void) __builtin_clzl(0); + } + " HAVE_BUILTIN_CLZL +) + +check_cxx_source_compiles(" + int main() + { + (void) __builtin_clzll(0); + } + " HAVE_BUILTIN_CLZLL +) + +check_cxx_symbol_exists(O_CLOEXEC "fcntl.h" HAVE_O_CLOEXEC) + +if(HAVE_BYTESWAP_H) + check_cxx_symbol_exists(bswap_16 "byteswap.h" HAVE_DECL_BSWAP_16) + check_cxx_symbol_exists(bswap_32 "byteswap.h" HAVE_DECL_BSWAP_32) + check_cxx_symbol_exists(bswap_64 "byteswap.h" HAVE_DECL_BSWAP_64) +endif() + +if(HAVE_ENDIAN_H OR HAVE_SYS_ENDIAN_H) + if(HAVE_ENDIAN_H) + set(ENDIAN_HEADER "endian.h") + else() + set(ENDIAN_HEADER "sys/endian.h") + endif() + check_cxx_symbol_exists(be16toh ${ENDIAN_HEADER} HAVE_DECL_BE16TOH) + check_cxx_symbol_exists(be32toh ${ENDIAN_HEADER} HAVE_DECL_BE32TOH) + check_cxx_symbol_exists(be64toh ${ENDIAN_HEADER} HAVE_DECL_BE64TOH) + check_cxx_symbol_exists(htobe16 ${ENDIAN_HEADER} HAVE_DECL_HTOBE16) + check_cxx_symbol_exists(htobe32 ${ENDIAN_HEADER} HAVE_DECL_HTOBE32) + check_cxx_symbol_exists(htobe64 ${ENDIAN_HEADER} HAVE_DECL_HTOBE64) + check_cxx_symbol_exists(htole16 ${ENDIAN_HEADER} HAVE_DECL_HTOLE16) + check_cxx_symbol_exists(htole32 ${ENDIAN_HEADER} HAVE_DECL_HTOLE32) + check_cxx_symbol_exists(htole64 ${ENDIAN_HEADER} HAVE_DECL_HTOLE64) + check_cxx_symbol_exists(le16toh ${ENDIAN_HEADER} HAVE_DECL_LE16TOH) + check_cxx_symbol_exists(le32toh ${ENDIAN_HEADER} HAVE_DECL_LE32TOH) + check_cxx_symbol_exists(le64toh ${ENDIAN_HEADER} HAVE_DECL_LE64TOH) +endif() + +check_include_file_cxx(unistd.h HAVE_UNISTD_H) +if(HAVE_UNISTD_H) + check_cxx_symbol_exists(fdatasync "unistd.h" HAVE_FDATASYNC) + check_cxx_symbol_exists(fork "unistd.h" HAVE_DECL_FORK) + check_cxx_symbol_exists(pipe2 "unistd.h" HAVE_DECL_PIPE2) + check_cxx_symbol_exists(setsid "unistd.h" HAVE_DECL_SETSID) +endif() + +check_include_file_cxx(sys/types.h HAVE_SYS_TYPES_H) +check_include_file_cxx(ifaddrs.h HAVE_IFADDRS_H) +if(HAVE_SYS_TYPES_H AND HAVE_IFADDRS_H) + check_cxx_symbol_exists(freeifaddrs "sys/types.h;ifaddrs.h" HAVE_DECL_FREEIFADDRS) + check_cxx_symbol_exists(getifaddrs "sys/types.h;ifaddrs.h" HAVE_DECL_GETIFADDRS) + # Illumos/SmartOS requires linking with -lsocket if + # using getifaddrs & freeifaddrs. + # See: https://github.com/bitcoin/bitcoin/pull/21486 + if(HAVE_DECL_GETIFADDRS AND HAVE_DECL_FREEIFADDRS) + set(check_socket_source " + #include + #include + + int main() { + struct ifaddrs* ifaddr; + getifaddrs(&ifaddr); + freeifaddrs(ifaddr); + } + ") + check_cxx_source_links("${check_socket_source}" IFADDR_LINKS_WITHOUT_LIBSOCKET) + if(NOT IFADDR_LINKS_WITHOUT_LIBSOCKET) + check_cxx_source_links_with_libs(socket "${check_socket_source}" IFADDR_NEEDS_LINK_TO_LIBSOCKET) + if(IFADDR_NEEDS_LINK_TO_LIBSOCKET) + link_libraries(socket) + else() + message(FATAL_ERROR "Cannot figure out how to use getifaddrs/freeifaddrs.") + endif() + endif() + endif() +endif() + +# Check for gmtime_r(), fallback to gmtime_s() if that is unavailable. +# Fail if neither are available. +check_cxx_source_compiles(" + #include + + int main() + { + gmtime_r((const time_t*)nullptr, (struct tm*)nullptr); + } + " HAVE_GMTIME_R +) +if(NOT HAVE_GMTIME_R) + check_cxx_source_compiles(" + #include + + int main() + { + gmtime_s((struct tm*)nullptr, (const time_t*)nullptr); + } + " HAVE_GMTIME_S + ) + if(NOT HAVE_GMTIME_S) + message(FATAL_ERROR "Both gmtime_r and gmtime_s are unavailable.") + endif() +endif() + +check_cxx_symbol_exists(std::system "cstdlib" HAVE_STD_SYSTEM) +check_cxx_symbol_exists(::_wsystem "stdlib.h" HAVE__WSYSTEM) +if(HAVE_STD_SYSTEM OR HAVE__WSYSTEM) + set(HAVE_SYSTEM 1) +endif() + +check_include_file_cxx(string.h HAVE_STRING_H) +if(HAVE_STRING_H) + check_cxx_source_compiles(" + #include + + int main() + { + char buf[100]; + char* p{strerror_r(0, buf, sizeof buf)}; + (void)p; + } + " STRERROR_R_CHAR_P + ) +endif() + +# Check for malloc_info (for memory statistics information in getmemoryinfo). +check_cxx_symbol_exists(malloc_info "malloc.h" HAVE_MALLOC_INFO) + +# Check for mallopt(M_ARENA_MAX) (to set glibc arenas). +check_cxx_source_compiles(" + #include + + int main() + { + mallopt(M_ARENA_MAX, 1); + } + " HAVE_MALLOPT_ARENA_MAX +) + +# Check for posix_fallocate(). +check_cxx_source_compiles(" + // same as in src/util/system.cpp + #ifdef __linux__ + #ifdef _POSIX_C_SOURCE + #undef _POSIX_C_SOURCE + #endif + #define _POSIX_C_SOURCE 200112L + #endif // __linux__ + #include + + int main() + { + return posix_fallocate(0, 0, 0); + } + " HAVE_POSIX_FALLOCATE +) + +# Check for strong getauxval() support in the system headers. +check_cxx_source_compiles(" + #include + + int main() + { + getauxval(AT_HWCAP); + } + " HAVE_STRONG_GETAUXVAL +) + +# Check for different ways of gathering OS randomness: +# - Linux getrandom() +check_cxx_source_compiles(" + #include + #include + #include + + int main() + { + syscall(SYS_getrandom, nullptr, 32, 0); + } + " HAVE_SYS_GETRANDOM +) + +# - BSD getentropy() +check_cxx_symbol_exists(getentropy "unistd.h;sys/random.h" HAVE_GETENTROPY_RAND) + +# - BSD sysctl() +check_cxx_source_compiles(" + #include + #include + + #ifdef __linux__ + #error Don't use sysctl on Linux, it's deprecated even when it works + #endif + + int main() + { + sysctl(nullptr, 2, nullptr, nullptr, nullptr, 0); + } + " HAVE_SYSCTL +) + +# - BSD sysctl(KERN_ARND) +check_cxx_source_compiles(" + #include + #include + + #ifdef __linux__ + #error Don't use sysctl on Linux, it's deprecated even when it works + #endif + + int main() + { + static int name[2] = {CTL_KERN, KERN_ARND}; + sysctl(name, 2, nullptr, nullptr, nullptr, 0); + } + " HAVE_SYSCTL_ARND +) diff --git a/cmake/module/CheckSourceCompilesAndLinks.cmake b/cmake/module/CheckSourceCompilesAndLinks.cmake new file mode 100644 index 0000000000000..2e5ab54e67e70 --- /dev/null +++ b/cmake/module/CheckSourceCompilesAndLinks.cmake @@ -0,0 +1,36 @@ +# 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. + +include(CheckCXXSourceCompiles) +include(CMakePushCheckState) + +# This avoids running the linker. +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + +macro(check_cxx_source_links source) + set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE) + check_cxx_source_compiles("${source}" ${ARGN}) + set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +endmacro() + +macro(check_cxx_source_compiles_with_flags flags source) + cmake_push_check_state(RESET) + string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${flags}") + check_cxx_source_compiles("${source}" ${ARGN}) + cmake_pop_check_state() +endmacro() + +macro(check_cxx_source_links_with_flags flags source) + cmake_push_check_state(RESET) + string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${flags}") + check_cxx_source_links("${source}" ${ARGN}) + cmake_pop_check_state() +endmacro() + +macro(check_cxx_source_links_with_libs libs source) + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_LIBRARIES "${libs}") + check_cxx_source_links("${source}" ${ARGN}) + cmake_pop_check_state() +endmacro() From 4cd5efd37913fc2386a031f63e65876ab0b298e5 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:53:47 +0000 Subject: [PATCH 4/6] cmake: Check compiler features --- cmake/bitcoin-config.h.in | 9 +++++++++ cmake/introspection.cmake | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cmake/bitcoin-config.h.in b/cmake/bitcoin-config.h.in index 759bcb8d1cb28..7a3cf24f440a5 100644 --- a/cmake/bitcoin-config.h.in +++ b/cmake/bitcoin-config.h.in @@ -6,6 +6,9 @@ #define BITCOIN_CONFIG_H +/* Define this symbol if type char equals int8_t */ +#cmakedefine CHAR_EQUALS_INT8 1 + /* Version Build */ #define CLIENT_VERSION_BUILD @PROJECT_VERSION_PATCH@ @@ -119,6 +122,12 @@ */ #cmakedefine01 HAVE_DECL_SETSID +/* Define if the visibility attribute is supported. */ +#cmakedefine HAVE_DEFAULT_VISIBILITY_ATTRIBUTE 1 + +/* Define if the dllexport attribute is supported. */ +#cmakedefine HAVE_DLLEXPORT_ATTRIBUTE 1 + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_ENDIAN_H 1 diff --git a/cmake/introspection.cmake b/cmake/introspection.cmake index 624f70ad10cb9..3e637bc4d04e8 100644 --- a/cmake/introspection.cmake +++ b/cmake/introspection.cmake @@ -240,3 +240,26 @@ check_cxx_source_compiles(" } " HAVE_SYSCTL_ARND ) + +check_cxx_source_compiles(" + #include + #include + + int main() + { + static_assert(std::is_same::value); + } + " CHAR_EQUALS_INT8 +) + +check_cxx_source_compiles(" + int foo(void) __attribute__((visibility(\"default\"))); + int main(){} + " HAVE_DEFAULT_VISIBILITY_ATTRIBUTE +) + +check_cxx_source_compiles(" + __declspec(dllexport) int foo(void); + int main(){} + " HAVE_DLLEXPORT_ATTRIBUTE +) From 2e2ce85705f486eee184a3ece636cf317b3e3058 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:53:57 +0000 Subject: [PATCH 5/6] cmake: Build `crc32c` static library --- CMakeLists.txt | 2 + cmake/crc32c.cmake | 113 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 cmake/crc32c.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 605b2675f1b64..4feb82ff4bb3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,8 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) include(CheckSourceCompilesAndLinks) include(cmake/introspection.cmake) +include(cmake/crc32c.cmake) + add_subdirectory(src) message("\n") diff --git a/cmake/crc32c.cmake b/cmake/crc32c.cmake new file mode 100644 index 0000000000000..fa3e629dc1d0b --- /dev/null +++ b/cmake/crc32c.cmake @@ -0,0 +1,113 @@ +# 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. + +# This file is part of the transition from Autotools to CMake. Once CMake +# support has been merged we should switch to using the upstream CMake +# buildsystem. + +include(CheckCXXSourceCompiles) + +# Check for __builtin_prefetch support in the compiler. +check_cxx_source_compiles(" + int main() { + char data = 0; + const char* address = &data; + __builtin_prefetch(address, 0, 0); + return 0; + } + " HAVE_BUILTIN_PREFETCH +) + +# Check for _mm_prefetch support in the compiler. +check_cxx_source_compiles(" + #if defined(_MSC_VER) + #include + #else + #include + #endif + + int main() { + char data = 0; + const char* address = &data; + _mm_prefetch(address, _MM_HINT_NTA); + return 0; + } + " HAVE_MM_PREFETCH +) + +# Check for SSE4.2 support in the compiler. +if(MSVC) + set(SSE42_CXXFLAGS /arch:AVX) +else() + set(SSE42_CXXFLAGS -msse4.2) +endif() +check_cxx_source_compiles_with_flags("${SSE42_CXXFLAGS}" " + #include + #if defined(_MSC_VER) + #include + #elif defined(__GNUC__) && defined(__SSE4_2__) + #include + #endif + + int main() { + uint64_t l = 0; + l = _mm_crc32_u8(l, 0); + l = _mm_crc32_u32(l, 0); + l = _mm_crc32_u64(l, 0); + return l; + } + " HAVE_SSE42 +) + +# Check for ARMv8 w/ CRC and CRYPTO extensions support in the compiler. +set(ARM_CRC_CXXFLAGS -march=armv8-a+crc) +check_cxx_source_compiles_with_flags("${ARM_CRC_CXXFLAGS}" " + #include + #include + + int main() { + #ifdef __aarch64__ + __crc32cb(0, 0); __crc32ch(0, 0); __crc32cw(0, 0); __crc32cd(0, 0); + vmull_p64(0, 0); + #else + #error crc32c library does not support hardware acceleration on 32-bit ARM + #endif + return 0; + } + " HAVE_ARM64_CRC32C +) + +add_library(crc32c STATIC EXCLUDE_FROM_ALL + ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c.cc + ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_portable.cc +) + +target_compile_definitions(crc32c + PRIVATE + HAVE_BUILTIN_PREFETCH=$ + HAVE_MM_PREFETCH=$ + HAVE_STRONG_GETAUXVAL=$ + HAVE_SSE42=$ + HAVE_ARM64_CRC32C=$ + BYTE_ORDER_BIG_ENDIAN=${WORDS_BIGENDIAN} +) + +target_include_directories(crc32c + PUBLIC + $ +) + +if(HAVE_SSE42) + target_sources(crc32c PRIVATE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc) + set_property(SOURCE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc + APPEND PROPERTY COMPILE_OPTIONS ${SSE42_CXXFLAGS} + ) +endif() + +if(HAVE_ARM64_CRC32C) + target_sources(crc32c PRIVATE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc) + set_property(SOURCE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc + APPEND PROPERTY COMPILE_OPTIONS ${ARM_CRC_CXXFLAGS} + ) +endif() From df899456fbca934a3030572bb890aa00b3bcfe4c Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:54:05 +0000 Subject: [PATCH 6/6] cmake: Build `leveldb` static library Co-authored-by: Cory Fields --- CMakeLists.txt | 1 + cmake/leveldb.cmake | 94 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 cmake/leveldb.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 4feb82ff4bb3f..2360366bd61cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ include(CheckSourceCompilesAndLinks) include(cmake/introspection.cmake) include(cmake/crc32c.cmake) +include(cmake/leveldb.cmake) add_subdirectory(src) diff --git a/cmake/leveldb.cmake b/cmake/leveldb.cmake new file mode 100644 index 0000000000000..e137ef461db02 --- /dev/null +++ b/cmake/leveldb.cmake @@ -0,0 +1,94 @@ +# 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. + +# This file is part of the transition from Autotools to CMake. Once CMake +# support has been merged we should switch to using the upstream CMake +# buildsystem. + +include(CheckCXXSymbolExists) +check_cxx_symbol_exists(F_FULLFSYNC "fcntl.h" HAVE_FULLFSYNC) + +add_library(leveldb STATIC EXCLUDE_FROM_ALL + ${PROJECT_SOURCE_DIR}/src/leveldb/db/builder.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/c.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/db_impl.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/db_iter.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/dbformat.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/dumpfile.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/filename.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/log_reader.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/log_writer.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/memtable.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/repair.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/table_cache.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/version_edit.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/version_set.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/db/write_batch.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/table/block.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/table/block_builder.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/table/filter_block.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/table/format.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/table/iterator.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/table/merger.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/table/table.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/table/table_builder.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/table/two_level_iterator.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/arena.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/bloom.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/cache.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/coding.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/comparator.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/crc32c.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/env.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/filter_policy.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/hash.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/histogram.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/logging.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/options.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/util/status.cc + ${PROJECT_SOURCE_DIR}/src/leveldb/helpers/memenv/memenv.cc +) +if(WIN32) + target_sources(leveldb PRIVATE ${PROJECT_SOURCE_DIR}/src/leveldb/util/env_windows.cc) + set_property(SOURCE ${PROJECT_SOURCE_DIR}/src/leveldb/util/env_windows.cc + APPEND PROPERTY COMPILE_OPTIONS $<$,$>:/wd4722> + ) +else() + target_sources(leveldb PRIVATE ${PROJECT_SOURCE_DIR}/src/leveldb/util/env_posix.cc) +endif() + +target_compile_definitions(leveldb + PRIVATE + HAVE_SNAPPY=0 + HAVE_CRC32C=1 + HAVE_FDATASYNC=$ + HAVE_FULLFSYNC=$ + HAVE_O_CLOEXEC=$ + FALLTHROUGH_INTENDED=[[fallthrough]] + LEVELDB_IS_BIG_ENDIAN=${WORDS_BIGENDIAN} +) + +if(WIN32) + target_compile_definitions(leveldb + PRIVATE + LEVELDB_PLATFORM_WINDOWS + _UNICODE + UNICODE + __USE_MINGW_ANSI_STDIO=1 + ) +else() + target_compile_definitions(leveldb PRIVATE LEVELDB_PLATFORM_POSIX) +endif() + +target_include_directories(leveldb + PRIVATE + $ + PUBLIC + $ +) + +#TODO: figure out how to filter out: +# -Wconditional-uninitialized -Werror=conditional-uninitialized -Wsuggest-override -Werror=suggest-override + +target_link_libraries(leveldb PRIVATE crc32c)