1
- cmake_minimum_required (VERSION 3.9 )
1
+ cmake_minimum_required (VERSION 3.9 FATAL_ERROR )
2
+
3
+ # respect C_EXTENSIONS OFF without explicitly setting C_STANDARD
4
+ if (POLICY CMP0128 )
5
+ cmake_policy (SET CMP0128 NEW )
6
+ endif ()
7
+ # mark_as_advanced does not implicitly create UNINITIALIZED cache entries
8
+ if (POLICY CMP0102 )
9
+ cmake_policy (SET CMP0102 NEW )
10
+ endif ()
2
11
3
12
project (libblake3
4
- VERSION 1.4.0
13
+ VERSION 1.5.2
5
14
DESCRIPTION "BLAKE3 C implementation"
6
15
LANGUAGES C ASM
7
16
)
8
17
9
18
include (FeatureSummary )
10
19
include (GNUInstallDirs )
11
20
21
+ # architecture lists for which to enable assembly / SIMD sources
22
+ set (BLAKE3_AMD64_NAMES amd64 AMD64 x86_64 )
23
+ set (BLAKE3_X86_NAMES i686 x86 X86 )
24
+ set (BLAKE3_ARMv8_NAMES aarch64 AArch64 arm64 ARM64 armv8 armv8a )
12
25
# default SIMD compiler flag configuration (can be overriden by toolchains or CLI)
13
- if (CMAKE_C_COMPILER_ID STREQUAL " MSVC" )
26
+ if (MSVC )
14
27
set (BLAKE3_CFLAGS_SSE2 "/arch:SSE2" CACHE STRING "the compiler flags to enable SSE2" )
15
28
# MSVC has no dedicated sse4.1 flag (see https://learn.microsoft.com/en-us/cpp/build/reference/arch-x86?view=msvc-170)
16
29
set (BLAKE3_CFLAGS_SSE4.1 "/arch:AVX" CACHE STRING "the compiler flags to enable SSE4.1" )
17
30
set (BLAKE3_CFLAGS_AVX2 "/arch:AVX2" CACHE STRING "the compiler flags to enable AVX2" )
18
31
set (BLAKE3_CFLAGS_AVX512 "/arch:AVX512" CACHE STRING "the compiler flags to enable AVX512" )
19
32
33
+ set (BLAKE3_AMD64_ASM_SOURCES
34
+ blake3_avx2_x86-64_windows_msvc.asm
35
+ blake3_avx512_x86-64_windows_msvc.asm
36
+ blake3_sse2_x86-64_windows_msvc.asm
37
+ blake3_sse41_x86-64_windows_msvc.asm
38
+ )
39
+
20
40
elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU"
21
41
OR CMAKE_C_COMPILER_ID STREQUAL "Clang"
22
42
OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang" )
23
43
set (BLAKE3_CFLAGS_SSE2 "-msse2" CACHE STRING "the compiler flags to enable SSE2" )
24
44
set (BLAKE3_CFLAGS_SSE4.1 "-msse4.1" CACHE STRING "the compiler flags to enable SSE4.1" )
25
45
set (BLAKE3_CFLAGS_AVX2 "-mavx2" CACHE STRING "the compiler flags to enable AVX2" )
26
46
set (BLAKE3_CFLAGS_AVX512 "-mavx512f -mavx512vl" CACHE STRING "the compiler flags to enable AVX512" )
47
+
48
+ if (WIN32 )
49
+ set (BLAKE3_AMD64_ASM_SOURCES
50
+ blake3_avx2_x86-64_windows_gnu.S
51
+ blake3_avx512_x86-64_windows_gnu.S
52
+ blake3_sse2_x86-64_windows_gnu.S
53
+ blake3_sse41_x86-64_windows_gnu.S
54
+ )
55
+
56
+ elseif (UNIX )
57
+ set (BLAKE3_AMD64_ASM_SOURCES
58
+ blake3_avx2_x86-64_unix.S
59
+ blake3_avx512_x86-64_unix.S
60
+ blake3_sse2_x86-64_unix.S
61
+ blake3_sse41_x86-64_unix.S
62
+ )
63
+ endif ()
64
+
65
+ if (CMAKE_SYSTEM_PROCESSOR IN_LIST BLAKE3_ARMv8_NAMES
66
+ AND NOT CMAKE_SIZEOF_VOID_P EQUAL 8 )
67
+ # 32-bit ARMv8 needs NEON to be enabled explicitly
68
+ set (BLAKE3_CFLAGS_NEON "-mfpu=neon" CACHE STRING "the compiler flags to enable NEON" )
69
+ endif ()
27
70
endif ()
28
- # architecture lists for which to enable assembly / SIMD sources
29
- set (BLAKE3_AMD64_NAMES amd64 AMD64 x86_64 )
30
- set (BLAKE3_X86_NAMES i686 x86 X86 )
31
- set (BLAKE3_ARMv8_NAMES aarch64 AArch64 arm64 ARM64 armv8 armv8a )
71
+
72
+ mark_as_advanced (BLAKE3_CFLAGS_SSE2 BLAKE3_CFLAGS_SSE4.1 BLAKE3_CFLAGS_AVX2 BLAKE3_CFLAGS_AVX512 BLAKE3_CFLAGS_NEON )
73
+ mark_as_advanced (BLAKE3_AMD64_ASM_SOURCES )
74
+
75
+ message (STATUS "BLAKE3 SIMD configuration: ${CMAKE_C_COMPILER_ARCHITECTURE_ID} " )
76
+ if (MSVC AND DEFINED CMAKE_C_COMPILER_ARCHITECTURE_ID )
77
+ if (CMAKE_C_COMPILER_ARCHITECTURE_ID MATCHES "[Xx]86" )
78
+ set (BLAKE3_SIMD_TYPE "x86-intrinsics" CACHE STRING "the SIMD acceleration type to use" )
79
+
80
+ elseif (CMAKE_C_COMPILER_ARCHITECTURE_ID MATCHES "[Xx]64" )
81
+ set (BLAKE3_SIMD_TYPE "amd64-asm" CACHE STRING "the SIMD acceleration type to use" )
82
+
83
+ elseif (CMAKE_C_COMPILER_ARCHITECTURE_ID MATCHES "[Aa][Rr][Mm]64" )
84
+ set (BLAKE3_SIMD_TYPE "neon-intrinsics" CACHE STRING "the SIMD acceleration type to use" )
85
+
86
+ else ()
87
+ set (BLAKE3_SIMD_TYPE "none" CACHE STRING "the SIMD acceleration type to use" )
88
+ endif ()
89
+
90
+ elseif (CMAKE_SYSTEM_PROCESSOR IN_LIST BLAKE3_AMD64_NAMES )
91
+ set (BLAKE3_SIMD_TYPE "amd64-asm" CACHE STRING "the SIMD acceleration type to use" )
92
+
93
+ elseif (CMAKE_SYSTEM_PROCESSOR IN_LIST BLAKE3_X86_NAMES
94
+ AND DEFINED BLAKE3_CFLAGS_SSE2
95
+ AND DEFINED BLAKE3_CFLAGS_SSE4.1
96
+ AND DEFINED BLAKE3_CFLAGS_AVX2
97
+ AND DEFINED BLAKE3_CFLAGS_AVX512 )
98
+ set (BLAKE3_SIMD_TYPE "x86-intrinsics" CACHE STRING "the SIMD acceleration type to use" )
99
+
100
+ elseif ((CMAKE_SYSTEM_PROCESSOR IN_LIST BLAKE3_ARMv8_NAMES
101
+ OR ANDROID_ABI STREQUAL "armeabi-v7a"
102
+ OR BLAKE3_USE_NEON_INTRINSICS )
103
+ AND (DEFINED BLAKE3_CFLAGS_NEON
104
+ OR CMAKE_SIZEOF_VOID_P EQUAL 8 ))
105
+ set (BLAKE3_SIMD_TYPE "neon-intrinsics" CACHE STRING "the SIMD acceleration type to use" )
106
+
107
+ else ()
108
+ set (BLAKE3_SIMD_TYPE "none" CACHE STRING "the SIMD acceleration type to use" )
109
+ endif ()
110
+
111
+ mark_as_advanced (BLAKE3_SIMD_TYPE )
32
112
33
113
# library target
34
114
add_library (blake3
@@ -41,73 +121,49 @@ add_library(BLAKE3::blake3 ALIAS blake3)
41
121
# library configuration
42
122
set (BLAKE3_PKGCONFIG_CFLAGS )
43
123
if (BUILD_SHARED_LIBS )
44
- target_compile_definitions (blake3
124
+ target_compile_definitions (blake3
45
125
PUBLIC BLAKE3_DLL
46
126
PRIVATE BLAKE3_DLL_EXPORTS
47
127
)
48
128
list (APPEND BLAKE3_PKGCONFIG_CFLAGS -DBLAKE3_DLL )
49
129
endif ()
50
- target_include_directories (blake3 PUBLIC $< INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} > )
130
+ target_include_directories (blake3 PUBLIC
131
+ $< BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >
132
+ $< INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >
133
+ )
51
134
set_target_properties (blake3 PROPERTIES
52
135
VERSION ${PROJECT_VERSION}
53
136
SOVERSION 0
54
137
C_VISIBILITY_PRESET hidden
138
+ C_EXTENSIONS OFF
55
139
)
140
+ target_compile_features (blake3 PUBLIC c_std_99 )
141
+ # ensure C_EXTENSIONS OFF is respected without overriding CMAKE_C_STANDARD
142
+ # which may be set by the user or toolchain file
143
+ if (NOT POLICY CMP0128 AND NOT DEFINED CMAKE_C_STANDARD )
144
+ set_target_properties (blake3 PROPERTIES C_STANDARD 99 )
145
+ endif ()
56
146
57
147
# optional SIMD sources
58
- macro (BLAKE3_DISABLE_SIMD )
59
- set (BLAKE3_SIMD_AMD64_ASM OFF )
60
- set (BLAKE3_SIMD_X86_INTRINSICS OFF )
61
- set (BLAKE3_SIMD_NEON_INTRINSICS OFF )
62
- set_source_files_properties (blake3_dispatch.c PROPERTIES
63
- COMPILE_DEFINITIONS BLAKE3_USE_NEON=0;BLAKE3_NO_SSE2;BLAKE3_NO_SSE41;BLAKE3_NO_AVX2;BLAKE3_NO_AVX512
64
- )
65
- endmacro ()
66
-
67
- if (CMAKE_SYSTEM_PROCESSOR IN_LIST BLAKE3_AMD64_NAMES OR BLAKE3_USE_AMD64_ASM )
148
+ if (BLAKE3_SIMD_TYPE STREQUAL "amd64-asm" )
149
+ if (NOT DEFINED BLAKE3_AMD64_ASM_SOURCES )
150
+ message (FATAL_ERROR "BLAKE3_SIMD_TYPE is set to 'amd64-asm' but no assembly sources are available for the target architecture." )
151
+ endif ()
68
152
set (BLAKE3_SIMD_AMD64_ASM ON )
69
153
70
- if (CMAKE_C_COMPILER_ID STREQUAL " MSVC" )
154
+ if (MSVC )
71
155
enable_language (ASM_MASM )
72
- target_sources (blake3 PRIVATE
73
- blake3_avx2_x86-64_windows_msvc.asm
74
- blake3_avx512_x86-64_windows_msvc.asm
75
- blake3_sse2_x86-64_windows_msvc.asm
76
- blake3_sse41_x86-64_windows_msvc.asm
77
- )
78
-
79
- elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU"
80
- OR CMAKE_C_COMPILER_ID STREQUAL "Clang"
81
- OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang" )
82
- if (WIN32 )
83
- target_sources (blake3 PRIVATE
84
- blake3_avx2_x86-64_windows_gnu.S
85
- blake3_avx512_x86-64_windows_gnu.S
86
- blake3_sse2_x86-64_windows_gnu.S
87
- blake3_sse41_x86-64_windows_gnu.S
88
- )
89
-
90
- elseif (UNIX )
91
- target_sources (blake3 PRIVATE
92
- blake3_avx2_x86-64_unix.S
93
- blake3_avx512_x86-64_unix.S
94
- blake3_sse2_x86-64_unix.S
95
- blake3_sse41_x86-64_unix.S
96
- )
97
-
98
- else ()
99
- BLAKE3_DISABLE_SIMD ()
100
- endif ()
101
-
102
- else ()
103
- BLAKE3_DISABLE_SIMD ()
104
156
endif ()
105
157
106
- elseif ((CMAKE_SYSTEM_PROCESSOR IN_LIST BLAKE3_X86_NAMES OR BLAKE3_USE_X86_INTRINSICS )
107
- AND DEFINED BLAKE3_CFLAGS_SSE2
108
- AND DEFINED BLAKE3_CFLAGS_SSE4.1
109
- AND DEFINED BLAKE3_CFLAGS_AVX2
110
- AND DEFINED BLAKE3_CFLAGS_AVX512 )
158
+ target_sources (blake3 PRIVATE ${BLAKE3_AMD64_ASM_SOURCES} )
159
+
160
+ elseif (BLAKE3_SIMD_TYPE STREQUAL "x86-intrinsics" )
161
+ if (NOT DEFINED BLAKE3_CFLAGS_SSE2
162
+ OR NOT DEFINED BLAKE3_CFLAGS_SSE4.1
163
+ OR NOT DEFINED BLAKE3_CFLAGS_AVX2
164
+ OR NOT DEFINED BLAKE3_CFLAGS_AVX512 )
165
+ message (FATAL_ERROR "BLAKE3_SIMD_TYPE is set to 'x86-intrinsics' but no compiler flags are available for the target architecture." )
166
+ endif ()
111
167
set (BLAKE3_SIMD_X86_INTRINSICS ON )
112
168
113
169
target_sources (blake3 PRIVATE
@@ -121,24 +177,31 @@ elseif((CMAKE_SYSTEM_PROCESSOR IN_LIST BLAKE3_X86_NAMES OR BLAKE3_USE_X86_INTRIN
121
177
set_source_files_properties (blake3_sse2.c PROPERTIES COMPILE_FLAGS "${BLAKE3_CFLAGS_SSE2} " )
122
178
set_source_files_properties (blake3_sse41.c PROPERTIES COMPILE_FLAGS "${BLAKE3_CFLAGS_SSE4.1}" )
123
179
124
- elseif (CMAKE_SYSTEM_PROCESSOR IN_LIST BLAKE3_ARMv8_NAMES
125
- OR ((ANDROID_ABI STREQUAL "armeabi-v7a"
126
- OR BLAKE3_USE_NEON_INTRINSICS )
127
- AND (DEFINED BLAKE3_CFLAGS_NEON
128
- OR CMAKE_SIZEOF_VOID_P EQUAL 8 )))
180
+ elseif (BLAKE3_SIMD_TYPE STREQUAL "neon-intrinsics" )
129
181
set (BLAKE3_SIMD_NEON_INTRINSICS ON )
130
182
131
183
target_sources (blake3 PRIVATE
132
184
blake3_neon.c
133
185
)
134
- set_source_files_properties (blake3_dispatch.c PROPERTIES COMPILE_DEFINITIONS BLAKE3_USE_NEON=1 )
186
+ target_compile_definitions (blake3 PRIVATE
187
+ BLAKE3_USE_NEON=1
188
+ )
135
189
136
190
if (DEFINED BLAKE3_CFLAGS_NEON )
137
191
set_source_files_properties (blake3_neon.c PROPERTIES COMPILE_FLAGS "${BLAKE3_CFLAGS_NEON} " )
138
192
endif ()
139
193
194
+ elseif (BLAKE3_SIMD_TYPE STREQUAL "none" )
195
+ target_compile_definitions (blake3 PRIVATE
196
+ BLAKE3_USE_NEON=0
197
+ BLAKE3_NO_SSE2
198
+ BLAKE3_NO_SSE41
199
+ BLAKE3_NO_AVX2
200
+ BLAKE3_NO_AVX512
201
+ )
202
+
140
203
else ()
141
- BLAKE3_DISABLE_SIMD ( )
204
+ message ( FATAL_ERROR "BLAKE3_SIMD_TYPE is set to an unknown value: ' ${BLAKE3_SIMD_TYPE} '" )
142
205
endif ()
143
206
144
207
# cmake install support
@@ -171,6 +234,7 @@ install(FILES "${CMAKE_BINARY_DIR}/libblake3.pc"
171
234
DESTINATION "${CMAKE_INSTALL_LIBDIR} /pkgconfig" )
172
235
173
236
# print feature summary
237
+ # add_feature_info cannot directly use the BLAKE3_SIMD_TYPE :(
174
238
add_feature_info ("AMD64 assembly" BLAKE3_SIMD_AMD64_ASM "The library uses hand written amd64 SIMD assembly." )
175
239
add_feature_info ("x86 SIMD intrinsics" BLAKE3_SIMD_X86_INTRINSICS "The library uses x86 SIMD intrinsics." )
176
240
add_feature_info ("NEON SIMD intrinsics" BLAKE3_SIMD_NEON_INTRINSICS "The library uses NEON SIMD intrinsics." )
0 commit comments