Skip to content

Commit 2e2ce85

Browse files
committed
cmake: Build crc32c static library
1 parent 4cd5efd commit 2e2ce85

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
6363
include(CheckSourceCompilesAndLinks)
6464
include(cmake/introspection.cmake)
6565

66+
include(cmake/crc32c.cmake)
67+
6668
add_subdirectory(src)
6769

6870
message("\n")

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)