From 2b8499a518805b1591dfeee78f10edb2d8ebf70b Mon Sep 17 00:00:00 2001 From: Fabrice de Gans Date: Tue, 5 Aug 2025 16:50:26 -0700 Subject: [PATCH 1/2] 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. --- Sources/CMakeLists.txt | 9 ++-- cmake/modules/SwiftSupport.cmake | 73 ++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt index 7c4fe2a6..f551b636 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -28,7 +28,6 @@ if(COLLECTIONS_SINGLE_MODULE) INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) if(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE) - get_swift_host_os(swift_os) if(BUILD_SHARED_LIBS) # Install only the swift module and not the library set(swift swift) @@ -36,15 +35,15 @@ if(COLLECTIONS_SINGLE_MODULE) # Install both the swift module and the binary set(swift swift_static) install(TARGETS ${COLLECTIONS_MODULE_NAME} - ARCHIVE DESTINATION lib/swift_static/${swift_os} - LIBRARY DESTINATION lib/swift_static/${swift_os} + ARCHIVE DESTINATION lib/swift_static/${SwiftCollections_PLATFORM}/${SwiftCollections_ARCH} + LIBRARY DESTINATION lib/swift_static/${SwiftCollections_PLATFORM}/${SwiftCollections_ARCH} RUNTIME DESTINATION bin) endif() install(FILES $/${COLLECTIONS_MODULE_NAME}.swiftdoc - DESTINATION lib/${swift}/${swift_os}/${COLLECTIONS_MODULE_NAME}.swiftmodule + DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${COLLECTIONS_MODULE_NAME}.swiftmodule RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftdoc) install(FILES $/${COLLECTIONS_MODULE_NAME}.swiftmodule - DESTINATION lib/${swift}/${swift_os}/${COLLECTIONS_MODULE_NAME}.swiftmodule + DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${COLLECTIONS_MODULE_NAME}.swiftmodule RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftmodule) else() _install_target(${COLLECTIONS_MODULE_NAME}) diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake index 8920cfa0..63699689 100644 --- a/cmake/modules/SwiftSupport.cmake +++ b/cmake/modules/SwiftSupport.cmake @@ -7,42 +7,56 @@ Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information #]] -# Returns the os name in a variable -# -# Usage: -# get_swift_host_os(result_var_name) -# -# -# Sets ${result_var_name} with the converted OS name derived from -# CMAKE_SYSTEM_NAME. -function(get_swift_host_os result_var_name) - set(${result_var_name} ${SWIFT_SYSTEM_NAME} PARENT_SCOPE) -endfunction() -if(NOT Swift_MODULE_TRIPLE) - # Attempt to get the module triple from the Swift compiler. +if(NOT SwiftCollections_MODULE_TRIPLE OR NOT SwiftCollections_ARCH OR NOT SwiftCollections_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) - string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") + execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json) +endif() - # Exit now if we failed to infer the triple. - if(NOT module_triple) - message(FATAL_ERROR - "Failed to get module triple from Swift compiler. " - "Compiler output: ${target_info_json}") +if(NOT SwiftCollections_MODULE_TRIPLE) + string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") + set(SwiftCollections_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used to install swiftmodule files") + mark_as_advanced(SwiftCollections_MODULE_TRIPLE) + message(CONFIGURE_LOG "Swift module triple: ${module_triple}") +endif() + +if(NOT SwiftCollections_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 `SwiftCollections_MODULE_TRIPLE`. + string(REGEX MATCH "^[^-]+" module_arch "${SwiftCollections_MODULE_TRIPLE}") endif() - # Cache the module triple for future use. - set(Swift_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files") - mark_as_advanced(Swift_MODULE_TRIPLE) + set(SwiftCollections_ARCH "${module_arch}" CACHE STRING "Arch folder name used to install libraries") + mark_as_advanced(SwiftCollections_ARCH) + message(CONFIGURE_LOG "Swift arch: ${SwiftCollections_ARCH}") +endif() + +if(NOT SwiftCollections_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(SwiftCollections_PLATFORM "${swift_platform}" CACHE STRING "Platform folder name used to install libraries") + mark_as_advanced(SwiftCollections_PLATFORM) + message(CONFIGURE_LOG "Swift platform: ${SwiftCollections_PLATFORM}") endif() function(_install_target module) - get_swift_host_os(swift_os) get_target_property(type ${module} TYPE) if(type STREQUAL STATIC_LIBRARY) @@ -51,7 +65,10 @@ function(_install_target module) set(swift swift) endif() - install(TARGETS ${module}) + install(TARGETS ${module} + ARCHIVE DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${SwiftCollections_ARCH} + LIBRARY DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${SwiftCollections_ARCH} + RUNTIME DESTINATION bin) if(type STREQUAL EXECUTABLE) return() endif() @@ -62,10 +79,10 @@ function(_install_target module) endif() install(FILES $/${module_name}.swiftdoc - DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${module_name}.swiftmodule RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftdoc) install(FILES $/${module_name}.swiftmodule - DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${module_name}.swiftmodule RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftmodule) endfunction() From 16f8d5b6bf91315e87e3922d2acae52d38c6bfe1 Mon Sep 17 00:00:00 2001 From: Fabrice de Gans Date: Fri, 29 Aug 2025 15:10:34 -0700 Subject: [PATCH 2/2] Make the arch subdir optional * Add an option to use an arch sub-directory for installing libraries. * Rename variables to be consistent with other CMake variables in the project. * Remove PlatformInfo as it is no longer needed. --- CMakeLists.txt | 3 +-- Sources/CMakeLists.txt | 12 +++++----- cmake/modules/PlatformInfo.cmake | 24 ------------------- cmake/modules/SwiftSupport.cmake | 40 ++++++++++++++++---------------- 4 files changed, 27 insertions(+), 52 deletions(-) delete mode 100644 cmake/modules/PlatformInfo.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c1e6ab4..f590da38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,8 +13,6 @@ project(SwiftCollections list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules) -include(PlatformInfo) - if(NOT SWIFT_SYSTEM_NAME) if(CMAKE_SYSTEM_NAME STREQUAL Darwin) set(SWIFT_SYSTEM_NAME macosx) @@ -38,6 +36,7 @@ endif() option(COLLECTIONS_SINGLE_MODULE "Build as a single module" NO) option(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE "Build a module for Foundation in the toolchain" NO) +option(COLLECTIONS_INSTALL_ARCH_SUBDIR "Install libraries under an architecture subdirectory" NO) if(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE) set(COLLECTIONS_SINGLE_MODULE YES) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt index f551b636..b574561c 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -35,16 +35,16 @@ if(COLLECTIONS_SINGLE_MODULE) # Install both the swift module and the binary set(swift swift_static) install(TARGETS ${COLLECTIONS_MODULE_NAME} - ARCHIVE DESTINATION lib/swift_static/${SwiftCollections_PLATFORM}/${SwiftCollections_ARCH} - LIBRARY DESTINATION lib/swift_static/${SwiftCollections_PLATFORM}/${SwiftCollections_ARCH} + ARCHIVE DESTINATION lib/swift_static/${COLLECTIONS_PLATFORM}$<$:/${COLLECTIONS_ARCH}> + LIBRARY DESTINATION lib/swift_static/${COLLECTIONS_PLATFORM}$<$:/${COLLECTIONS_ARCH}> RUNTIME DESTINATION bin) endif() install(FILES $/${COLLECTIONS_MODULE_NAME}.swiftdoc - DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${COLLECTIONS_MODULE_NAME}.swiftmodule - RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftdoc) + DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}/${COLLECTIONS_MODULE_NAME}.swiftmodule + RENAME ${COLLECTIONS_MODULE_TRIPLE}.swiftdoc) install(FILES $/${COLLECTIONS_MODULE_NAME}.swiftmodule - DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${COLLECTIONS_MODULE_NAME}.swiftmodule - RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftmodule) + DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}/${COLLECTIONS_MODULE_NAME}.swiftmodule + RENAME ${COLLECTIONS_MODULE_TRIPLE}.swiftmodule) else() _install_target(${COLLECTIONS_MODULE_NAME}) endif() diff --git a/cmake/modules/PlatformInfo.cmake b/cmake/modules/PlatformInfo.cmake deleted file mode 100644 index 3bc873b4..00000000 --- a/cmake/modules/PlatformInfo.cmake +++ /dev/null @@ -1,24 +0,0 @@ -#[[ -This source file is part of the Swift Collections Open Source Project - -Copyright (c) 2025 Apple Inc. and the Swift project authors -Licensed under Apache License v2.0 with Runtime Library Exception - -See https://swift.org/LICENSE.txt for license information -#]] - -set(target_info_cmd "${CMAKE_Swift_COMPILER}" -print-target-info) -if(CMAKE_Swift_COMPILER_TARGET) - list(APPEND target_info_cmd -target ${CMAKE_Swift_COMPILER_TARGET}) -endif() -execute_process(COMMAND ${target_info_cmd} OUTPUT_VARIABLE target_info_json) -message(CONFIGURE_LOG "Swift target info: ${target_info_cmd}\n" -"${target_info_json}") - -if(NOT SwiftCollections_MODULE_TRIPLE) - string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") - set(SwiftCollections_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used for installed swift{doc,module, interface} files") - mark_as_advanced(SwiftCollections_MODULE_TRIPLE) - - message(CONFIGURE_LOG "Swift module triple: ${module_triple}") -endif() diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake index 63699689..59b29cfb 100644 --- a/cmake/modules/SwiftSupport.cmake +++ b/cmake/modules/SwiftSupport.cmake @@ -8,7 +8,7 @@ See https://swift.org/LICENSE.txt for license information #]] -if(NOT SwiftCollections_MODULE_TRIPLE OR NOT SwiftCollections_ARCH OR NOT SwiftCollections_PLATFORM) +if(NOT COLLECTIONS_MODULE_TRIPLE OR NOT COLLECTIONS_ARCH OR NOT COLLECTIONS_PLATFORM) # Get the target information from the Swift compiler. set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info) if(CMAKE_Swift_COMPILER_TARGET) @@ -17,28 +17,28 @@ if(NOT SwiftCollections_MODULE_TRIPLE OR NOT SwiftCollections_ARCH OR NOT SwiftC execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json) endif() -if(NOT SwiftCollections_MODULE_TRIPLE) +if(NOT COLLECTIONS_MODULE_TRIPLE) string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") - set(SwiftCollections_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used to install swiftmodule files") - mark_as_advanced(SwiftCollections_MODULE_TRIPLE) + set(COLLECTIONS_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used to install swiftmodule files") + mark_as_advanced(COLLECTIONS_MODULE_TRIPLE) message(CONFIGURE_LOG "Swift module triple: ${module_triple}") endif() -if(NOT SwiftCollections_ARCH) +if(NOT COLLECTIONS_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 `SwiftCollections_MODULE_TRIPLE`. - string(REGEX MATCH "^[^-]+" module_arch "${SwiftCollections_MODULE_TRIPLE}") + # For older compilers, extract the value from `COLLECTIONS_MODULE_TRIPLE`. + string(REGEX MATCH "^[^-]+" module_arch "${COLLECTIONS_MODULE_TRIPLE}") endif() - set(SwiftCollections_ARCH "${module_arch}" CACHE STRING "Arch folder name used to install libraries") - mark_as_advanced(SwiftCollections_ARCH) - message(CONFIGURE_LOG "Swift arch: ${SwiftCollections_ARCH}") + set(COLLECTIONS_ARCH "${module_arch}" CACHE STRING "Arch folder name used to install libraries") + mark_as_advanced(COLLECTIONS_ARCH) + message(CONFIGURE_LOG "Swift arch: ${COLLECTIONS_ARCH}") endif() -if(NOT SwiftCollections_PLATFORM) +if(NOT COLLECTIONS_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") @@ -51,9 +51,9 @@ if(NOT SwiftCollections_PLATFORM) endif() endif() - set(SwiftCollections_PLATFORM "${swift_platform}" CACHE STRING "Platform folder name used to install libraries") - mark_as_advanced(SwiftCollections_PLATFORM) - message(CONFIGURE_LOG "Swift platform: ${SwiftCollections_PLATFORM}") + set(COLLECTIONS_PLATFORM "${swift_platform}" CACHE STRING "Platform folder name used to install libraries") + mark_as_advanced(COLLECTIONS_PLATFORM) + message(CONFIGURE_LOG "Swift platform: ${COLLECTIONS_PLATFORM}") endif() function(_install_target module) @@ -66,8 +66,8 @@ function(_install_target module) endif() install(TARGETS ${module} - ARCHIVE DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${SwiftCollections_ARCH} - LIBRARY DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${SwiftCollections_ARCH} + ARCHIVE DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}$<$:/${COLLECTIONS_ARCH}> + LIBRARY DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}$<$:/${COLLECTIONS_ARCH}> RUNTIME DESTINATION bin) if(type STREQUAL EXECUTABLE) return() @@ -79,10 +79,10 @@ function(_install_target module) endif() install(FILES $/${module_name}.swiftdoc - DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${module_name}.swiftmodule - RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftdoc) + DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}/${module_name}.swiftmodule + RENAME ${COLLECTIONS_MODULE_TRIPLE}.swiftdoc) install(FILES $/${module_name}.swiftmodule - DESTINATION lib/${swift}/${SwiftCollections_PLATFORM}/${module_name}.swiftmodule - RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftmodule) + DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}/${module_name}.swiftmodule + RENAME ${COLLECTIONS_MODULE_TRIPLE}.swiftmodule) endfunction()