diff --git a/Sources/_FoundationCShims/CMakeLists.txt b/Sources/_FoundationCShims/CMakeLists.txt index 1798a5bb0..606b2eed2 100644 --- a/Sources/_FoundationCShims/CMakeLists.txt +++ b/Sources/_FoundationCShims/CMakeLists.txt @@ -43,10 +43,9 @@ install(DIRECTORY lib/${install_directory}/_FoundationCShims) if(NOT BUILD_SHARED_LIBS) - get_swift_host_os(swift_os) install(TARGETS _FoundationCShims - ARCHIVE DESTINATION lib/${install_directory}/${swift_os} - LIBRARY DESTINATION lib/${install_directory}/${swift_os} + ARCHIVE DESTINATION lib/${install_directory}/${SwiftFoundation_PLATFORM}/${SwiftFoundation_ARCH} + LIBRARY DESTINATION lib/${install_directory}/${SwiftFoundation_PLATFORM}/${SwiftFoundation_ARCH} RUNTIME DESTINATION bin) endif() diff --git a/cmake/modules/SwiftFoundationSwiftSupport.cmake b/cmake/modules/SwiftFoundationSwiftSupport.cmake index cbdfc2aef..a422b9e01 100644 --- a/cmake/modules/SwiftFoundationSwiftSupport.cmake +++ b/cmake/modules/SwiftFoundationSwiftSupport.cmake @@ -1,17 +1,66 @@ -# 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() +##===----------------------------------------------------------------------===## +## +## This source file is part of the Swift open source project +## +## Copyright (c) 2025 Apple Inc. and the Swift project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.md for the list of Swift project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +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(_swift_foundation_install_target module) - get_swift_host_os(swift_os) get_target_property(type ${module} TYPE) if(type STREQUAL STATIC_LIBRARY) @@ -21,8 +70,8 @@ function(_swift_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() @@ -33,22 +82,11 @@ function(_swift_foundation_install_target module) set(module_name ${module}) endif() - 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() - 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()