diff --git a/CMakeLists.txt b/CMakeLists.txt index 73ab35836..3b31e7f00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,10 +11,7 @@ cmake_minimum_required(VERSION 3.16) project(SwiftCollections LANGUAGES C Swift) -list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) - -set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift) -set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY MultiThreadedDLL) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules) if(NOT SWIFT_SYSTEM_NAME) if(CMAKE_SYSTEM_NAME STREQUAL Darwin) @@ -41,6 +38,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 7c4fe2a69..b574561cc 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,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/${swift_os} - LIBRARY DESTINATION lib/swift_static/${swift_os} + 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}/${swift_os}/${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}/${swift_os}/${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 3bc873b40..000000000 --- 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 8920cfa0e..59b29cfbb 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 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) list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET}) endif() - execute_process(COMMAND ${module_triple_command} - OUTPUT_VARIABLE target_info_json) + execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json) +endif() + +if(NOT COLLECTIONS_MODULE_TRIPLE) string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") + 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 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 `COLLECTIONS_MODULE_TRIPLE`. + string(REGEX MATCH "^[^-]+" module_arch "${COLLECTIONS_MODULE_TRIPLE}") + 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}") + 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 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") + else() + # For older compilers, compile the value from `CMAKE_SYSTEM_NAME`. + if(APPLE) + set(swift_platform macosx) + else() + set(swift_platform "$") + endif() 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(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) - 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}/${COLLECTIONS_PLATFORM}$<$:/${COLLECTIONS_ARCH}> + LIBRARY DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}$<$:/${COLLECTIONS_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 - 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}/${swift_os}/${module_name}.swiftmodule - RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftmodule) + DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}/${module_name}.swiftmodule + RENAME ${COLLECTIONS_MODULE_TRIPLE}.swiftmodule) endfunction()