From feaa8ab18f5f6ec702633326036fb96e758b1cea Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Tue, 2 Jun 2026 11:08:56 -0400 Subject: [PATCH 1/2] Use distinct Swift modules for Apple crypto static archive Compile the Apple crypto Swift bindings separately for the shared library and static archive so the static archive gets a distinct Swift module name. This avoids duplicate ObjC runtime class registration when both variants are loaded into one process. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../CMakeLists.txt | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt b/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt index abceed80bb2bf9..2938e7329d31db 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt +++ b/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt @@ -19,7 +19,6 @@ set(NATIVECRYPTO_SOURCES pal_symmetric.c pal_x509.c pal_x509chain.c - pal_swiftbindings.o pal_networkframework.m ) @@ -87,13 +86,35 @@ if (PRERELEASE) set(SWIFT_COMPILER_ERRORS_FLAG "-warnings-as-errors") endif() -add_custom_command( - OUTPUT pal_swiftbindings.o - COMMAND xcrun swiftc -emit-object -static -parse-as-library -enable-library-evolution ${SWIFT_COMPILER_ERRORS_FLAG} -gline-tables-only ${SWIFT_OPTIMIZATION_FLAG} -runtime-compatibility-version none ${SWIFT_SDK_FLAG} -target ${SWIFT_COMPILER_TARGET} ${CMAKE_CURRENT_SOURCE_DIR}/pal_swiftbindings.swift -o pal_swiftbindings.o - MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/pal_swiftbindings.swift - COMMENT "Compiling Swift file pal_swiftbindings.swift" -) -set_source_files_properties(pal_swiftbindings.o PROPERTIES EXTERNAL_OBJECT true GENERATED true) +set(SWIFTBINDINGS_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/pal_swiftbindings.swift) +set(SWIFTBINDINGS_COMPILER_ARGS + -emit-object + -static + -parse-as-library + -enable-library-evolution + ${SWIFT_COMPILER_ERRORS_FLAG} + -gline-tables-only + ${SWIFT_OPTIMIZATION_FLAG} + -runtime-compatibility-version none + ${SWIFT_SDK_FLAG} + -target ${SWIFT_COMPILER_TARGET}) + +function(compile_swiftbindings_object output module_name target_kind) + add_custom_command( + OUTPUT ${output} + COMMAND xcrun swiftc ${SWIFTBINDINGS_COMPILER_ARGS} -module-name ${module_name} ${SWIFTBINDINGS_SOURCE} -o ${output} + MAIN_DEPENDENCY ${SWIFTBINDINGS_SOURCE} + COMMENT "Compiling Swift file pal_swiftbindings.swift for ${target_kind} library" + VERBATIM + ) + set_source_files_properties(${output} PROPERTIES EXTERNAL_OBJECT true GENERATED true) +endfunction() + +# Swift classes are registered with process-global ObjC runtime names. Build the +# shared library and static archive with different module names so both can load +# into the same process without duplicate Swift class registrations. +compile_swiftbindings_object(pal_swiftbindings_shared.o pal_swiftbindings shared) +compile_swiftbindings_object(pal_swiftbindings_static.o pal_swiftbindings_static static) if (CLR_CMAKE_TARGET_MACCATALYST) add_definitions(-DTARGET_MACCATALYST) @@ -111,6 +132,7 @@ if (GEN_SHARED_LIB) add_library(System.Security.Cryptography.Native.Apple SHARED ${NATIVECRYPTO_SOURCES} + pal_swiftbindings_shared.o ${VERSION_FILE_PATH} ) endif() @@ -122,6 +144,7 @@ endif() add_library(System.Security.Cryptography.Native.Apple-Static STATIC ${NATIVECRYPTO_SOURCES} + pal_swiftbindings_static.o ) set_target_properties(System.Security.Cryptography.Native.Apple-Static PROPERTIES OUTPUT_NAME System.Security.Cryptography.Native.Apple CLEAN_DIRECT_OUTPUT 1) From 791d289d19f0fcd00b2c6d5cb8278355cdacca69 Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Tue, 2 Jun 2026 11:27:19 -0400 Subject: [PATCH 2/2] Only build the shared object when needed --- .../System.Security.Cryptography.Native.Apple/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt b/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt index 2938e7329d31db..841e96514194f3 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt +++ b/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt @@ -113,7 +113,6 @@ endfunction() # Swift classes are registered with process-global ObjC runtime names. Build the # shared library and static archive with different module names so both can load # into the same process without duplicate Swift class registrations. -compile_swiftbindings_object(pal_swiftbindings_shared.o pal_swiftbindings shared) compile_swiftbindings_object(pal_swiftbindings_static.o pal_swiftbindings_static static) if (CLR_CMAKE_TARGET_MACCATALYST) @@ -129,6 +128,8 @@ if (CLR_CMAKE_TARGET_TVOS) endif() if (GEN_SHARED_LIB) + compile_swiftbindings_object(pal_swiftbindings_shared.o pal_swiftbindings shared) + add_library(System.Security.Cryptography.Native.Apple SHARED ${NATIVECRYPTO_SOURCES}