Skip to content

Commit 3c05130

Browse files
committed
cmake: Build crc32c static library
1 parent a3397a7 commit 3c05130

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
6060

6161
include(cmake/introspection.cmake)
6262

63+
include(cmake/crc32c.cmake)
64+
6365
add_subdirectory(src)
6466

6567
message("\n")

cmake/crc32c.cmake

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
set(CMAKE_REQUIRED_FLAGS ${SSE42_CXXFLAGS})
46+
check_cxx_source_compiles("
47+
#include <cstdint>
48+
#if defined(_MSC_VER)
49+
#include <intrin.h>
50+
#elif defined(__GNUC__) && defined(__SSE4_2__)
51+
#include <nmmintrin.h>
52+
#endif
53+
54+
int main() {
55+
uint64_t l = 0;
56+
l = _mm_crc32_u8(l, 0);
57+
l = _mm_crc32_u32(l, 0);
58+
l = _mm_crc32_u64(l, 0);
59+
return l;
60+
}
61+
" HAVE_SSE42
62+
)
63+
64+
# Check for ARMv8 w/ CRC and CRYPTO extensions support in the compiler.
65+
set(ARM_CRC_CXXFLAGS -march=armv8-a+crc)
66+
set(CMAKE_REQUIRED_FLAGS ${ARM_CRC_CXXFLAGS})
67+
check_cxx_source_compiles("
68+
#include <arm_acle.h>
69+
#include <arm_neon.h>
70+
71+
int main() {
72+
#ifdef __aarch64__
73+
__crc32cb(0, 0); __crc32ch(0, 0); __crc32cw(0, 0); __crc32cd(0, 0);
74+
vmull_p64(0, 0);
75+
#else
76+
#error crc32c library does not support hardware acceleration on 32-bit ARM
77+
#endif
78+
return 0;
79+
}
80+
" HAVE_ARM64_CRC32C
81+
)
82+
83+
set(CMAKE_REQUIRED_FLAGS)
84+
85+
add_library(crc32c STATIC EXCLUDE_FROM_ALL
86+
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c.cc
87+
${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_portable.cc
88+
)
89+
90+
target_compile_definitions(crc32c
91+
PRIVATE
92+
HAVE_BUILTIN_PREFETCH=$<BOOL:${HAVE_BUILTIN_PREFETCH}>
93+
HAVE_MM_PREFETCH=$<BOOL:${HAVE_MM_PREFETCH}>
94+
HAVE_STRONG_GETAUXVAL=$<BOOL:${HAVE_STRONG_GETAUXVAL}>
95+
HAVE_SSE42=$<BOOL:${HAVE_SSE42}>
96+
HAVE_ARM64_CRC32C=$<BOOL:${HAVE_ARM64_CRC32C}>
97+
BYTE_ORDER_BIG_ENDIAN=${WORDS_BIGENDIAN}
98+
)
99+
100+
target_include_directories(crc32c
101+
PUBLIC
102+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/crc32c/include>
103+
)
104+
105+
if(HAVE_SSE42)
106+
target_sources(crc32c PRIVATE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc)
107+
set_property(SOURCE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_sse42.cc
108+
APPEND PROPERTY COMPILE_OPTIONS ${SSE42_CXXFLAGS}
109+
)
110+
endif()
111+
112+
if(HAVE_ARM64_CRC32C)
113+
target_sources(crc32c PRIVATE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc)
114+
set_property(SOURCE ${PROJECT_SOURCE_DIR}/src/crc32c/src/crc32c_arm64.cc
115+
APPEND PROPERTY COMPILE_OPTIONS ${ARM_CRC_CXXFLAGS}
116+
)
117+
endif()

0 commit comments

Comments
 (0)