From ea40b130ee7632cfb73570d72d6366ae99a5adb1 Mon Sep 17 00:00:00 2001 From: Fabrice de Gans Date: Tue, 5 Aug 2025 16:53:26 -0700 Subject: [PATCH] build: Install libraries in an `arch` sub-folder This is the proper installation scheme for Swift libraries and prevents having to manually copy them in `build.ps1`. --- CMakeLists.txt | 22 +------ Sources/CoreFoundation/CMakeLists.txt | 4 +- Sources/_CFURLSessionInterface/CMakeLists.txt | 4 +- Sources/_CFXMLInterface/CMakeLists.txt | 4 +- Sources/plutil/CMakeLists.txt | 2 +- cmake/modules/FoundationSwiftSupport.cmake | 57 +++++++++++++++++-- 6 files changed, 61 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f191165743..0a6e09d2c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,14 +47,6 @@ if(FOUNDATION_SWIFTPM_DEPS) return() endif() -if(NOT SWIFT_SYSTEM_NAME) - if(CMAKE_SYSTEM_NAME STREQUAL Darwin) - set(SWIFT_SYSTEM_NAME macosx) - else() - set(SWIFT_SYSTEM_NAME "$") - endif() -endif() - # Don't enable WMO on Windows due to linker failures if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL Windows) # Enable whole module optimization for release builds & incremental for debug builds @@ -138,18 +130,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux) "$<$:-fno-omit-frame-pointer>") endif() -# Precompute module triple for installation -if(NOT SwiftFoundation_MODULE_TRIPLE) - set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info) - if(CMAKE_Swift_COMPILER_TARGET) - list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET}) - endif() - execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json) - string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") - set(SwiftFoundation_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files") - mark_as_advanced(SwiftFoundation_MODULE_TRIPLE) -endif() - # System dependencies # We know libdispatch is always unavailable on WASI @@ -256,6 +236,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") "-D_GNU_SOURCE") endif() +option(SwiftFoundation_INSTALL_ARCH_SUBDIR "Install libraries under an architecture subdirectory" NO) + include(GNUInstallDirs) include(FoundationSwiftSupport) diff --git a/Sources/CoreFoundation/CMakeLists.txt b/Sources/CoreFoundation/CMakeLists.txt index bb01502580..82efb82470 100644 --- a/Sources/CoreFoundation/CMakeLists.txt +++ b/Sources/CoreFoundation/CMakeLists.txt @@ -148,7 +148,7 @@ install(DIRECTORY if(NOT BUILD_SHARED_LIBS) install(TARGETS CoreFoundation - ARCHIVE DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME} - LIBRARY DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME} + ARCHIVE DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM} + LIBRARY DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM} RUNTIME DESTINATION bin) endif() diff --git a/Sources/_CFURLSessionInterface/CMakeLists.txt b/Sources/_CFURLSessionInterface/CMakeLists.txt index 5ae7da27ff..bb95e380f0 100644 --- a/Sources/_CFURLSessionInterface/CMakeLists.txt +++ b/Sources/_CFURLSessionInterface/CMakeLists.txt @@ -37,7 +37,7 @@ target_link_libraries(_CFURLSessionInterface PRIVATE if(NOT BUILD_SHARED_LIBS) set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS _CFURLSessionInterface) install(TARGETS _CFURLSessionInterface - ARCHIVE DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME} - LIBRARY DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME} + ARCHIVE DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM} + LIBRARY DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM} RUNTIME DESTINATION bin) endif() diff --git a/Sources/_CFXMLInterface/CMakeLists.txt b/Sources/_CFXMLInterface/CMakeLists.txt index 80c7520594..86eadb5631 100644 --- a/Sources/_CFXMLInterface/CMakeLists.txt +++ b/Sources/_CFXMLInterface/CMakeLists.txt @@ -39,7 +39,7 @@ endif() if(NOT BUILD_SHARED_LIBS) set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS _CFXMLInterface) install(TARGETS _CFXMLInterface - ARCHIVE DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME} - LIBRARY DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME} + ARCHIVE DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM} + LIBRARY DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM} RUNTIME DESTINATION bin) endif() diff --git a/Sources/plutil/CMakeLists.txt b/Sources/plutil/CMakeLists.txt index bfb6ae4cc0..922c6f34e4 100644 --- a/Sources/plutil/CMakeLists.txt +++ b/Sources/plutil/CMakeLists.txt @@ -23,7 +23,7 @@ target_link_libraries(plutil PRIVATE Foundation) set_target_properties(plutil PROPERTIES - INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SYSTEM_NAME}") + INSTALL_RPATH "$ORIGIN/../lib/swift/${SwiftFoundation_PLATFORM}") set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS plutil) install(TARGETS plutil diff --git a/cmake/modules/FoundationSwiftSupport.cmake b/cmake/modules/FoundationSwiftSupport.cmake index 85868392f0..406ad4158a 100644 --- a/cmake/modules/FoundationSwiftSupport.cmake +++ b/cmake/modules/FoundationSwiftSupport.cmake @@ -12,8 +12,55 @@ ## ##===----------------------------------------------------------------------===## +if(NOT SwiftFoundation_MODULE_TRIPLE OR NOT SwiftFoundation_ARCH OR NOT SwiftFoundation_PLATFORM) + # Get the target information from the Swift compiler. + set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info) + if(CMAKE_Swift_COMPILER_TARGET) + list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET}) + endif() + execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json) +endif() + +if(NOT SwiftFoundation_MODULE_TRIPLE) + string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") + set(SwiftFoundation_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used to install swiftmodule files") + mark_as_advanced(SwiftFoundation_MODULE_TRIPLE) + message(CONFIGURE_LOG "Swift module triple: ${module_triple}") +endif() + +if(NOT SwiftFoundation_ARCH) + if(CMAKE_Swift_COMPILER_VERSION VERSION_EQUAL 0.0.0 OR CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 6.2) + # For newer compilers, we can use the -print-target-info command to get the architecture. + string(JSON module_arch GET "${target_info_json}" "target" "arch") + else() + # For older compilers, extract the value from `SwiftFoundation_MODULE_TRIPLE`. + string(REGEX MATCH "^[^-]+" module_arch "${SwiftFoundation_MODULE_TRIPLE}") + endif() + + set(SwiftFoundation_ARCH "${module_arch}" CACHE STRING "Arch folder name used to install libraries") + mark_as_advanced(SwiftFoundation_ARCH) + message(CONFIGURE_LOG "Swift arch: ${SwiftFoundation_ARCH}") +endif() + +if(NOT SwiftFoundation_PLATFORM) + if(CMAKE_Swift_COMPILER_VERSION VERSION_EQUAL 0.0.0 OR CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 6.2) + # For newer compilers, we can use the -print-target-info command to get the platform. + string(JSON swift_platform GET "${target_info_json}" "target" "platform") + else() + # For older compilers, compile the value from `CMAKE_SYSTEM_NAME`. + if(APPLE) + set(swift_platform macosx) + else() + set(swift_platform "$") + endif() + endif() + + set(SwiftFoundation_PLATFORM "${swift_platform}" CACHE STRING "Platform folder name used to install libraries") + mark_as_advanced(SwiftFoundation_PLATFORM) + message(CONFIGURE_LOG "Swift platform: ${SwiftFoundation_PLATFORM}") +endif() + function(_foundation_install_target module) - set(swift_os ${SWIFT_SYSTEM_NAME}) get_target_property(type ${module} TYPE) if(type STREQUAL STATIC_LIBRARY) @@ -23,8 +70,8 @@ function(_foundation_install_target module) endif() install(TARGETS ${module} - ARCHIVE DESTINATION lib/${swift}/${swift_os} - LIBRARY DESTINATION lib/${swift}/${swift_os} + ARCHIVE DESTINATION lib/${swift}/${SwiftFoundation_PLATFORM}$<$:/${SwiftFoundation_ARCH}> + LIBRARY DESTINATION lib/${swift}/${SwiftFoundation_PLATFORM}$<$:/${SwiftFoundation_ARCH}> RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) if(type STREQUAL EXECUTABLE) return() @@ -36,10 +83,10 @@ function(_foundation_install_target module) endif() install(FILES $/${module_name}.swiftdoc - DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + DESTINATION lib/${swift}/${SwiftFoundation_PLATFORM}/${module_name}.swiftmodule RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftdoc) install(FILES $/${module_name}.swiftmodule - DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + DESTINATION lib/${swift}/${SwiftFoundation_PLATFORM}/${module_name}.swiftmodule RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftmodule) endfunction()