@@ -38,6 +38,14 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
38
38
"MinSizeRel" "RelWithDebInfo" )
39
39
endif ()
40
40
41
+ if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|amd64" )
42
+ SET (SZ_PLATFORM_X86 TRUE )
43
+ message (STATUS "Platform: x86" )
44
+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|AARCH64|arm64|ARM64" )
45
+ SET (SZ_PLATFORM_ARM TRUE )
46
+ message (STATUS "Platform: ARM" )
47
+ endif ()
48
+
41
49
# Determine if StringZilla is built as a subproject (using `add_subdirectory`)
42
50
# or if it is the main project
43
51
set (STRINGZILLA_IS_MAIN_PROJECT OFF )
@@ -99,8 +107,17 @@ if(${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13)
99
107
enable_testing ()
100
108
endif ()
101
109
110
+ if (MSVC )
111
+ # Remove /RTC* from MSVC debug flags by default (it will be added back in the set_compiler_flags function)
112
+ # Beacuse /RTC* cannot be used without the crt so it needs to be disabled for that specifc target
113
+ string (REGEX REPLACE "/RTC[^ ]*" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} " )
114
+ string (REGEX REPLACE "/RTC[^ ]*" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} " )
115
+ endif ()
116
+
102
117
# Function to set compiler-specific flags
103
118
function (set_compiler_flags target cpp_standard target_arch)
119
+ get_target_property (target_type ${target} TYPE )
120
+
104
121
target_include_directories (${target} PRIVATE scripts)
105
122
target_link_libraries (${target} PRIVATE ${STRINGZILLA_TARGET_NAME} )
106
123
@@ -152,17 +169,26 @@ function(set_compiler_flags target cpp_standard target_arch)
152
169
"$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>>:/Zi>"
153
170
)
154
171
172
+ if (NOT target_type STREQUAL "SHARED_LIBRARY" )
173
+ if (MSVC )
174
+ target_compile_options (${target} PRIVATE "$<$<CONFIG:Debug>:/RTC1>" )
175
+ endif ()
176
+ endif ()
177
+
155
178
# If available, enable Position Independent Code
156
- if (CMAKE_POSITION_INDEPENDENT_CODE )
179
+ get_target_property (target_pic ${target} POSITION_INDEPENDENT_CODE )
180
+ if (target_pic)
157
181
target_compile_options (${target} PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fPIC>" )
158
182
target_link_options (${target} PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fPIC>" )
183
+ target_compile_definitions (${target} PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:SZ_PIC>" )
159
184
endif ()
160
185
161
186
# Avoid builtin functions where we know what we are doing.
162
187
target_compile_options (${target} PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fno-builtin-memcmp>" )
163
188
target_compile_options (${target} PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fno-builtin-memchr>" )
164
189
target_compile_options (${target} PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fno-builtin-memcpy>" )
165
190
target_compile_options (${target} PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-fno-builtin-memset>" )
191
+ target_compile_options (${target} PRIVATE "$<$<CXX_COMPILER_ID:MSVC>:/Oi->" )
166
192
167
193
# Check for ${target_arch} and set it or use the current system if not defined
168
194
if ("${target_arch} " STREQUAL "" )
@@ -202,17 +228,19 @@ function(set_compiler_flags target cpp_standard target_arch)
202
228
203
229
# Sanitizer options for Debug mode
204
230
if (CMAKE_BUILD_TYPE STREQUAL "Debug" )
205
- target_compile_options (
206
- ${target}
207
- PRIVATE
208
- "$<$<CXX_COMPILER_ID:GNU,Clang>:-fsanitize=address;-fsanitize=leak>"
209
- "$<$<CXX_COMPILER_ID:MSVC>:/fsanitize=address>" )
210
-
211
- target_link_options (
212
- ${target}
213
- PRIVATE
214
- "$<$<CXX_COMPILER_ID:GNU,Clang>:-fsanitize=address;-fsanitize=leak>"
215
- "$<$<CXX_COMPILER_ID:MSVC>:/fsanitize=address>" )
231
+ if (NOT target_type STREQUAL "SHARED_LIBRARY" )
232
+ target_compile_options (
233
+ ${target}
234
+ PRIVATE
235
+ "$<$<CXX_COMPILER_ID:GNU,Clang>:-fsanitize=address;-fsanitize=leak>"
236
+ "$<$<CXX_COMPILER_ID:MSVC>:/fsanitize=address>" )
237
+
238
+ target_link_options (
239
+ ${target}
240
+ PRIVATE
241
+ "$<$<CXX_COMPILER_ID:GNU,Clang>:-fsanitize=address;-fsanitize=leak>"
242
+ "$<$<CXX_COMPILER_ID:MSVC>:/fsanitize=address>" )
243
+ endif ()
216
244
217
245
# Define SZ_DEBUG macro based on build configuration
218
246
target_compile_definitions (
@@ -248,7 +276,7 @@ if(${STRINGZILLA_BUILD_TEST})
248
276
249
277
# Check system architecture to avoid complex cross-compilation workflows, but
250
278
# compile multiple backends: disabling all SIMD, enabling only AVX2, only AVX-512, only Arm Neon.
251
- if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|amd64" )
279
+ if (SZ_PLATFORM_X86 )
252
280
# x86 specific backends
253
281
if (MSVC )
254
282
define_launcher(stringzilla_test_cpp20_x86_serial scripts/test .cpp 20 "AVX" )
@@ -259,37 +287,60 @@ if(${STRINGZILLA_BUILD_TEST})
259
287
define_launcher(stringzilla_test_cpp20_x86_avx2 scripts/test .cpp 20 "haswell" )
260
288
define_launcher(stringzilla_test_cpp20_x86_avx512 scripts/test .cpp 20 "sapphirerapids" )
261
289
endif ()
262
- elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|AARCH64|arm64|ARM64" )
290
+ elseif (SZ_PLATFORM_ARM )
263
291
# ARM specific backends
264
292
define_launcher(stringzilla_test_cpp20_arm_serial scripts/test .cpp 20 "armv8-a" )
265
293
define_launcher(stringzilla_test_cpp20_arm_neon scripts/test .cpp 20 "armv8-a+simd" )
266
294
endif ()
267
295
endif ()
268
296
269
297
if (${STRINGZILLA_BUILD_SHARED} )
270
- add_library (stringzilla_shared SHARED c/lib.c)
271
- set_compiler_flags(stringzilla_shared "" "${STRINGZILLA_TARGET_ARCH} " )
298
+
299
+ function (define_shared target )
300
+ add_library (${target} SHARED c/lib.c)
301
+
302
+ set_target_properties (${target} PROPERTIES
303
+ VERSION ${PROJECT_VERSION}
304
+ SOVERSION 1
305
+ POSITION_INDEPENDENT_CODE ON
306
+ PUBLIC_HEADER include /stringzilla/stringzilla.h)
307
+
308
+ if (SZ_PLATFORM_X86)
309
+ if (MSVC )
310
+ set_compiler_flags(${target} "" "SSE2" )
311
+ else ()
312
+ set_compiler_flags(${target} "" "ivybridge" )
313
+ endif ()
314
+
315
+ target_compile_definitions (${target} PRIVATE
316
+ "SZ_USE_X86_AVX512=1"
317
+ "SZ_USE_X86_AVX2=1"
318
+ "SZ_USE_ARM_NEON=0"
319
+ "SZ_USE_ARM_SVE=0" )
320
+ elseif (SZ_PLATFORM_ARM)
321
+ set_compiler_flags(${target} "" "armv8-a" )
322
+
323
+ target_compile_definitions (${target} PRIVATE
324
+ "SZ_USE_X86_AVX512=0"
325
+ "SZ_USE_X86_AVX2=0"
326
+ "SZ_USE_ARM_NEON=1"
327
+ "SZ_USE_ARM_SVE=1" )
328
+ endif ()
329
+ endfunction ()
330
+
331
+ define_shared(stringzilla_shared)
272
332
target_compile_definitions (stringzilla_shared PRIVATE "SZ_AVOID_LIBC=0" )
273
333
target_compile_definitions (stringzilla_shared PRIVATE "SZ_OVERRIDE_LIBC=1" )
274
- set_target_properties (stringzilla_shared PROPERTIES
275
- VERSION ${PROJECT_VERSION}
276
- SOVERSION 1
277
- POSITION_INDEPENDENT_CODE ON
278
- PUBLIC_HEADER include /stringzilla/stringzilla.h)
279
-
334
+
280
335
# Try compiling a version without linking the LibC
281
- add_library (stringzillite SHARED c/lib.c)
282
- set_compiler_flags(stringzillite "" "${STRINGZILLA_TARGET_ARCH} " )
336
+ define_shared(stringzillite)
283
337
target_compile_definitions (stringzillite PRIVATE "SZ_AVOID_LIBC=1" )
284
338
target_compile_definitions (stringzillite PRIVATE "SZ_OVERRIDE_LIBC=1" )
285
- set_target_properties (stringzillite PROPERTIES
286
- VERSION ${PROJECT_VERSION}
287
- SOVERSION 1
288
- POSITION_INDEPENDENT_CODE ON
289
- PUBLIC_HEADER include /stringzilla/stringzilla.h)
290
339
291
340
# Avoid built-ins on MSVC and other compilers, as that will cause compileration errors
292
341
target_compile_options (stringzillite PRIVATE
293
342
"$<$<CXX_COMPILER_ID:GNU,Clang>:-fno-builtin;-nostdlib>"
294
- "$<$<CXX_COMPILER_ID:MSVC>:/Oi->" )
295
- endif ()
343
+ "$<$<CXX_COMPILER_ID:MSVC>:/Oi-;/GS->" )
344
+ target_link_options (stringzillite PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang>:-nostdlib>" )
345
+ target_link_options (stringzillite PRIVATE "$<$<CXX_COMPILER_ID:MSVC>:/NODEFAULTLIB>" )
346
+ endif ()
0 commit comments