Skip to content

Commit dda024d

Browse files
committed
Make: Reduce CMake nesting
1 parent 5070321 commit dda024d

File tree

1 file changed

+178
-152
lines changed

1 file changed

+178
-152
lines changed

CMakeLists.txt

Lines changed: 178 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -487,106 +487,117 @@ if (STRINGZILLA_BUILD_TEST)
487487
endif ()
488488
endif ()
489489

490+
# Helper function to set architecture-specific SIMD compile definitions for dynamic dispatch. This is shared between
491+
# stringzilla_shared (serial) and stringzillas_shared (parallel) libraries. This function ONLY sets the SIMD-related
492+
# preprocessor definitions, not compiler flags.
493+
function (set_architecture_simd_definitions target)
494+
if (SZ_IS_64BIT_X86_)
495+
target_compile_definitions(
496+
${target}
497+
PRIVATE "SZ_IS_64BIT_X86_=1"
498+
"SZ_IS_64BIT_ARM_=0"
499+
"SZ_USE_WESTMERE=1"
500+
"SZ_USE_GOLDMONT=1"
501+
"SZ_USE_HASWELL=1"
502+
"SZ_USE_SKYLAKE=1"
503+
"SZ_USE_ICE=1"
504+
"SZ_USE_NEON=0"
505+
"SZ_USE_NEON_AES=0"
506+
"SZ_USE_NEON_SHA=0"
507+
"SZ_USE_SVE=0"
508+
"SZ_USE_SVE2=0"
509+
"SZ_USE_SVE2_AES=0"
510+
)
511+
elseif (SZ_IS_64BIT_ARM_)
512+
target_compile_definitions(
513+
${target}
514+
PRIVATE "SZ_IS_64BIT_ARM_=1"
515+
"SZ_IS_64BIT_X86_=0"
516+
"SZ_USE_WESTMERE=0"
517+
"SZ_USE_GOLDMONT=0"
518+
"SZ_USE_HASWELL=0"
519+
"SZ_USE_SKYLAKE=0"
520+
"SZ_USE_ICE=0"
521+
"SZ_USE_NEON=1"
522+
"SZ_USE_NEON_AES=1"
523+
"SZ_USE_NEON_SHA=1"
524+
"SZ_USE_SVE=1"
525+
"SZ_USE_SVE2=1"
526+
"SZ_USE_SVE2_AES=1"
527+
)
528+
else ()
529+
# Fallback: disable all SIMD and use serial execution only
530+
message(
531+
WARNING
532+
"Neither SZ_IS_64BIT_X86_ nor SZ_IS_64BIT_ARM_ is set for ${target}, falling back to serial execution"
533+
)
534+
target_compile_definitions(
535+
${target}
536+
PRIVATE "SZ_IS_64BIT_X86_=0"
537+
"SZ_IS_64BIT_ARM_=0"
538+
"SZ_USE_WESTMERE=0"
539+
"SZ_USE_GOLDMONT=0"
540+
"SZ_USE_HASWELL=0"
541+
"SZ_USE_SKYLAKE=0"
542+
"SZ_USE_ICE=0"
543+
"SZ_USE_NEON=0"
544+
"SZ_USE_NEON_AES=0"
545+
"SZ_USE_NEON_SHA=0"
546+
"SZ_USE_SVE=0"
547+
"SZ_USE_SVE2=0"
548+
"SZ_USE_SVE2_AES=0"
549+
)
550+
endif ()
551+
endfunction ()
552+
490553
# Define our libraries, first the header-only version
491554
add_library(stringzilla_header INTERFACE)
492555
add_library(${PROJECT_NAME}::stringzilla_header ALIAS stringzilla_header)
493556
target_include_directories(
494557
stringzilla_header INTERFACE $<BUILD_INTERFACE:${STRINGZILLA_INCLUDE_BUILD_DIR}> $<INSTALL_INTERFACE:include>
495558
)
496559

497-
if (STRINGZILLA_BUILD_SHARED)
498-
499-
function (define_stringzilla_shared target)
500-
add_library(${target} SHARED c/stringzilla.c)
501-
add_library(${PROJECT_NAME}::${target} ALIAS ${target})
560+
# Helper function used for `stringzilla_shared` and `stringzilla_bare` targets
561+
function (define_stringzilla_shared target)
562+
add_library(${target} SHARED c/stringzilla.c)
563+
add_library(${PROJECT_NAME}::${target} ALIAS ${target})
502564

503-
set_target_properties(
504-
${target}
505-
PROPERTIES VERSION ${PROJECT_VERSION}
506-
SOVERSION 1
507-
POSITION_INDEPENDENT_CODE ON
508-
)
509-
510-
if (SZ_IS_64BIT_X86_)
511-
if (MSVC)
512-
set_compiler_flags(${target} "" "SSE2" "${CMAKE_CXX_COMPILER_ID}")
513-
else ()
514-
set_compiler_flags(${target} "" "ivybridge" "${CMAKE_CXX_COMPILER_ID}")
515-
endif ()
516-
517-
target_compile_definitions(
518-
${target}
519-
PRIVATE "SZ_IS_64BIT_X86_=1"
520-
"SZ_IS_64BIT_ARM_=0"
521-
"SZ_USE_WESTMERE=1"
522-
"SZ_USE_GOLDMONT=1"
523-
"SZ_USE_HASWELL=1"
524-
"SZ_USE_SKYLAKE=1"
525-
"SZ_USE_ICE=1"
526-
"SZ_USE_NEON=0"
527-
"SZ_USE_NEON_AES=0"
528-
"SZ_USE_NEON_SHA=0"
529-
"SZ_USE_SVE=0"
530-
"SZ_USE_SVE2=0"
531-
"SZ_USE_SVE2_AES=0"
532-
)
533-
elseif (SZ_IS_64BIT_ARM_)
534-
set_compiler_flags(${target} "" "armv8-a" "${CMAKE_CXX_COMPILER_ID}")
565+
set_target_properties(
566+
${target}
567+
PROPERTIES VERSION ${PROJECT_VERSION}
568+
SOVERSION 1
569+
POSITION_INDEPENDENT_CODE ON
570+
)
535571

536-
target_compile_definitions(
537-
${target}
538-
PRIVATE "SZ_IS_64BIT_ARM_=1"
539-
"SZ_IS_64BIT_X86_=0"
540-
"SZ_USE_WESTMERE=0"
541-
"SZ_USE_GOLDMONT=0"
542-
"SZ_USE_HASWELL=0"
543-
"SZ_USE_SKYLAKE=0"
544-
"SZ_USE_ICE=0"
545-
"SZ_USE_NEON=1"
546-
"SZ_USE_NEON_AES=1"
547-
"SZ_USE_NEON_SHA=1"
548-
"SZ_USE_SVE=1"
549-
"SZ_USE_SVE2=1"
550-
"SZ_USE_SVE2_AES=1"
551-
)
572+
# Set compiler flags for appropriate architecture
573+
if (SZ_IS_64BIT_X86_)
574+
if (MSVC)
575+
set_compiler_flags(${target} "" "SSE2" "${CMAKE_CXX_COMPILER_ID}")
552576
else ()
553-
# Fallback: disable all SIMD and use serial execution only This happens when SZ_IS_64BIT_X86_ and
554-
# SZ_IS_64BIT_ARM_ are both FALSE
555-
message(
556-
WARNING
557-
"Neither SZ_IS_64BIT_X86_ nor SZ_IS_64BIT_ARM_ is set for ${target}, falling back to serial execution"
558-
)
559-
set_compiler_flags(${target} "" "" "${CMAKE_CXX_COMPILER_ID}")
560-
561-
target_compile_definitions(
562-
${target}
563-
PRIVATE "SZ_IS_64BIT_X86_=0"
564-
"SZ_IS_64BIT_ARM_=0"
565-
"SZ_USE_WESTMERE=0"
566-
"SZ_USE_GOLDMONT=0"
567-
"SZ_USE_HASWELL=0"
568-
"SZ_USE_SKYLAKE=0"
569-
"SZ_USE_ICE=0"
570-
"SZ_USE_NEON=0"
571-
"SZ_USE_NEON_AES=0"
572-
"SZ_USE_NEON_SHA=0"
573-
"SZ_USE_SVE=0"
574-
"SZ_USE_SVE2=0"
575-
"SZ_USE_SVE2_AES=0"
576-
)
577+
set_compiler_flags(${target} "" "ivybridge" "${CMAKE_CXX_COMPILER_ID}")
577578
endif ()
579+
elseif (SZ_IS_64BIT_ARM_)
580+
set_compiler_flags(${target} "" "armv8-a" "${CMAKE_CXX_COMPILER_ID}")
581+
else ()
582+
set_compiler_flags(${target} "" "" "${CMAKE_CXX_COMPILER_ID}")
583+
endif ()
578584

579-
if (MSVC)
580-
# Add dependencies for necessary runtime libraries in case of static linking. This ensures that basic
581-
# runtime functions are available:
582-
#
583-
# * msvcrt.lib: Microsoft Visual C Runtime, required for basic C runtime functions on Windows.
584-
# * vcruntime.lib: Microsoft Visual C++ Runtime library for basic runtime functions.
585-
# * ucrt.lib: Universal C Runtime, necessary for linking basic C functions like I/O.
586-
target_link_libraries(${target} PRIVATE msvcrt.lib vcruntime.lib ucrt.lib)
587-
endif ()
585+
# Set architecture-specific SIMD flags using shared helper function
586+
set_architecture_simd_definitions(${target})
587+
588+
if (MSVC)
589+
# Add dependencies for necessary runtime libraries in case of static linking. This ensures that basic runtime
590+
# functions are available:
591+
#
592+
# * msvcrt.lib: Microsoft Visual C Runtime, required for basic C runtime functions on Windows.
593+
# * vcruntime.lib: Microsoft Visual C++ Runtime library for basic runtime functions.
594+
# * ucrt.lib: Universal C Runtime, necessary for linking basic C functions like I/O.
595+
target_link_libraries(${target} PRIVATE msvcrt.lib vcruntime.lib ucrt.lib)
596+
endif ()
588597

589-
endfunction ()
598+
endfunction ()
599+
600+
if (STRINGZILLA_BUILD_SHARED)
590601

591602
define_stringzilla_shared(stringzilla_shared)
592603
target_compile_definitions(stringzilla_shared PRIVATE "SZ_AVOID_LIBC=0")
@@ -619,93 +630,108 @@ if (STRINGZILLA_BUILD_SHARED)
619630
endif ()
620631
endif ()
621632

622-
if (STRINGZILLAS_BUILD_SHARED)
623-
# StringZillas shared library targets for parallel string operations
624-
function (define_stringzillas_shared target source_file backend_flags)
625-
add_library(${target} SHARED ${source_file})
626-
add_library(${PROJECT_NAME}::${target} ALIAS ${target})
633+
# Helper function used for `stringzillas_cpus_shared`, `stringzillas_cuda_shared`, and `stringzillas_rocm_shared`
634+
# targets
635+
function (define_stringzillas_shared target source_file backend_flags)
636+
add_library(${target} SHARED ${source_file})
637+
add_library(${PROJECT_NAME}::${target} ALIAS ${target})
638+
639+
set_target_properties(
640+
${target}
641+
PROPERTIES VERSION ${PROJECT_VERSION}
642+
SOVERSION 1
643+
POSITION_INDEPENDENT_CODE ON
644+
)
627645

628-
set_target_properties(
629-
${target}
630-
PROPERTIES VERSION ${PROJECT_VERSION}
631-
SOVERSION 1
632-
POSITION_INDEPENDENT_CODE ON
633-
)
646+
target_include_directories(${target} PUBLIC include)
647+
target_include_directories(${target} PRIVATE fork_union/include)
648+
target_compile_definitions(${target} PRIVATE "SZ_DYNAMIC_DISPATCH=1")
649+
target_compile_definitions(${target} PRIVATE "SZ_AVOID_LIBC=0")
650+
target_compile_definitions(${target} PRIVATE "SZ_DEBUG=0")
634651

635-
target_include_directories(${target} PUBLIC include)
636-
target_include_directories(${target} PRIVATE fork_union/include)
637-
target_compile_definitions(${target} PRIVATE "SZ_DYNAMIC_DISPATCH=1")
638-
target_compile_definitions(${target} PRIVATE "SZ_AVOID_LIBC=0")
639-
target_compile_definitions(${target} PRIVATE "SZ_DEBUG=0")
652+
# Set backend-specific compilation flags
653+
foreach (flag ${backend_flags})
654+
target_compile_definitions(${target} PRIVATE ${flag})
655+
endforeach ()
640656

641-
# Set backend-specific compilation flags
642-
foreach (flag ${backend_flags})
643-
target_compile_definitions(${target} PRIVATE ${flag})
644-
endforeach ()
657+
# Use C++20 for StringZillas
658+
set_target_properties(${target} PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON)
645659

646-
# Use C++20 for StringZillas
647-
set_target_properties(${target} PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON)
660+
# Architecture-specific optimizations
661+
target_compile_options(
662+
${target} PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang>:-O3;-fPIC>" "$<$<CXX_COMPILER_ID:MSVC>:/O2>"
663+
)
648664

649-
# Architecture-specific optimizations
650-
target_compile_options(
651-
${target} PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang>:-O3;-fPIC>" "$<$<CXX_COMPILER_ID:MSVC>:/O2>"
652-
)
665+
# Set architecture-specific SIMD flags using shared helper function
666+
set_architecture_simd_definitions(${target})
653667

654-
if (SZ_IS_64BIT_X86_)
655-
target_compile_definitions(${target} PRIVATE "SZ_IS_64BIT_X86_=1" "SZ_IS_64BIT_ARM_=0")
656-
elseif (SZ_IS_64BIT_ARM_)
657-
target_compile_definitions(${target} PRIVATE "SZ_IS_64BIT_X86_=0" "SZ_IS_64BIT_ARM_=1")
658-
else ()
659-
# Fallback: disable architecture-specific optimizations
660-
message(
661-
WARNING
662-
"Unknown architecture for ${target}: CMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}, disabling SIMD"
663-
)
664-
target_compile_definitions(${target} PRIVATE "SZ_IS_64BIT_X86_=0" "SZ_IS_64BIT_ARM_=0")
665-
endif ()
668+
# Link threading libraries for CPU backend
669+
find_package(Threads REQUIRED)
670+
target_link_libraries(${target} PRIVATE Threads::Threads)
666671

667-
# Link threading libraries for CPU backend
668-
find_package(Threads REQUIRED)
669-
target_link_libraries(${target} PRIVATE Threads::Threads)
672+
# Platform-specific runtime libraries (similar to define_stringzilla_shared)
673+
if (WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
674+
target_link_libraries(${target} PRIVATE msvcrt.lib vcruntime.lib ucrt.lib)
675+
endif ()
670676

671-
# Platform-specific runtime libraries (similar to define_stringzilla_shared)
672-
if (WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
673-
target_link_libraries(${target} PRIVATE msvcrt.lib vcruntime.lib ucrt.lib)
674-
endif ()
677+
endfunction ()
675678

676-
endfunction ()
679+
if (STRINGZILLAS_BUILD_SHARED)
677680

678681
# Define StringZillas CPU shared library
679682
define_stringzillas_shared(stringzillas_cpus_shared c/stringzillas.cpp "SZ_USE_CUDA=0;SZ_USE_ROCM=0")
680683

681-
# Define StringZillas CUDA shared library (only if CUDA is available)
682-
if (STRINGZILLA_BUILD_CUDA)
683-
define_stringzillas_shared(stringzillas_cuda_shared c/stringzillas.cu "SZ_USE_CUDA=1;SZ_USE_ROCM=0")
684+
# Set compiler flags for C++ target
685+
if (SZ_IS_64BIT_X86_)
686+
if (MSVC)
687+
set_compiler_flags(stringzillas_cpus_shared "" "SSE2" "${CMAKE_CXX_COMPILER_ID}")
688+
else ()
689+
set_compiler_flags(stringzillas_cpus_shared "" "ivybridge" "${CMAKE_CXX_COMPILER_ID}")
690+
endif ()
691+
elseif (SZ_IS_64BIT_ARM_)
692+
set_compiler_flags(stringzillas_cpus_shared "" "armv8-a" "${CMAKE_CXX_COMPILER_ID}")
693+
else ()
694+
set_compiler_flags(stringzillas_cpus_shared "" "" "${CMAKE_CXX_COMPILER_ID}")
695+
endif ()
696+
endif ()
684697

685-
# Link CUDA libraries
686-
target_link_libraries(stringzillas_cuda_shared PRIVATE CUDA::cudart)
698+
# Define StringZillas CUDA shared library (only if CUDA is available)
699+
if (STRINGZILLA_BUILD_SHARED AND STRINGZILLA_BUILD_CUDA)
700+
define_stringzillas_shared(stringzillas_cuda_shared c/stringzillas.cu "SZ_USE_CUDA=1;SZ_USE_ROCM=0")
687701

688-
# Set CUDA-specific properties
689-
set_target_properties(stringzillas_cuda_shared PROPERTIES CUDA_STANDARD 20 CUDA_STANDARD_REQUIRED ON)
690-
set_target_properties(stringzillas_cuda_shared PROPERTIES CUDA_ARCHITECTURES "90a") # We dispatch manually
702+
# Link CUDA libraries
703+
target_link_libraries(stringzillas_cuda_shared PRIVATE CUDA::cudart)
691704

692-
# Enable CUDA separable compilation for device code
693-
set_target_properties(stringzillas_cuda_shared PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
705+
# Set CUDA-specific properties
706+
set_target_properties(stringzillas_cuda_shared PROPERTIES CUDA_STANDARD 20 CUDA_STANDARD_REQUIRED ON)
707+
set_target_properties(stringzillas_cuda_shared PROPERTIES CUDA_ARCHITECTURES "90a") # We dispatch manually
694708

695-
# Add CUDA-specific compiler flags
696-
target_compile_options(stringzillas_cuda_shared PRIVATE "--expt-relaxed-constexpr")
709+
# Enable CUDA separable compilation for device code
710+
set_target_properties(stringzillas_cuda_shared PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
697711

698-
# Set the source file as CUDA
699-
set_source_files_properties(
700-
c/stringzillas.cu TARGET_DIRECTORY stringzillas_cuda_shared PROPERTIES LANGUAGE CUDA
701-
)
702-
endif ()
712+
# Add CUDA-specific compiler flags
713+
target_compile_options(stringzillas_cuda_shared PRIVATE "--expt-relaxed-constexpr")
703714

704-
# TODO: Define StringZillas ROCm shared library when ROCm support is added if (ENABLE_ROCM)
705-
# define_stringzillas_shared(stringzillas_rocm_shared "SZ_USE_CUDA=0;SZ_USE_ROCM=1") endif ()
715+
# Set the source file as CUDA
716+
set_source_files_properties(c/stringzillas.cu TARGET_DIRECTORY stringzillas_cuda_shared PROPERTIES LANGUAGE CUDA)
706717

718+
# Set compiler flags for CUDA target (must be done AFTER file language is set to CUDA)
719+
if (SZ_IS_64BIT_X86_)
720+
if (MSVC)
721+
set_compiler_flags(stringzillas_cuda_shared "" "SSE2" "${CMAKE_CUDA_COMPILER_ID}")
722+
else ()
723+
set_compiler_flags(stringzillas_cuda_shared "" "ivybridge" "${CMAKE_CUDA_COMPILER_ID}")
724+
endif ()
725+
elseif (SZ_IS_64BIT_ARM_)
726+
set_compiler_flags(stringzillas_cuda_shared "" "armv8-a" "${CMAKE_CUDA_COMPILER_ID}")
727+
else ()
728+
set_compiler_flags(stringzillas_cuda_shared "" "" "${CMAKE_CUDA_COMPILER_ID}")
729+
endif ()
707730
endif ()
708731

732+
# TODO: Define StringZillas ROCm shared library when ROCm support is added if (ENABLE_ROCM)
733+
# define_stringzillas_shared(stringzillas_rocm_shared "SZ_USE_CUDA=0;SZ_USE_ROCM=1") endif ()
734+
709735
if (STRINGZILLA_INSTALL)
710736
if (TARGET stringzilla_header)
711737
install(

0 commit comments

Comments
 (0)