Files
Evan Wilde 2fe5c4a31d Use full module triple for Swift modules
Install swiftmodules using the full module triple. This replaces the
CMake logic that attempts to compute the appropriate architecture using
values passed into CMake with a query to ask the compiler what the
appropriate module triple is for the given input target triple.
This is more robust mechanism and will future-proof this part of the
code against any new platforms and architectures to anything that the
Swift compiler building the project can support.

This is currently needed for the x86_64 FreeBSD bringup effort, where
the `amd64` architecture should be spelled `x86_64` in the swiftmodule
triple for the Swift compiler to find it. A surgical approach adding
another condition for amd64 FreeBSD would have less risk, but would also
require a new tag for every new platform that the `get_swift_host_arch`
didn't compute correctly or hadn't considered. The approach in the
commit will work for all new platforms. Bugs in this approach will come
from issues in the compiler when targeting the new platform and can be
fixed in the compiler without changes to Swift-Collections.
2025-07-01 10:07:59 -07:00

65 lines
2.1 KiB
CMake

#[[
This source file is part of the Swift Collections Open Source Project
Copyright (c) 2021 - 2024 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
#]]
if(NOT SwiftCollections_MODULE_TRIPLE)
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)
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)
endif()
# 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()
function(_install_target module)
get_swift_host_os(swift_os)
get_target_property(type ${module} TYPE)
if(type STREQUAL STATIC_LIBRARY)
set(swift swift_static)
else()
set(swift swift)
endif()
install(TARGETS ${module}
ARCHIVE DESTINATION lib/${swift}/${swift_os}
LIBRARY DESTINATION lib/${swift}/${swift_os}
RUNTIME DESTINATION bin)
if(type STREQUAL EXECUTABLE)
return()
endif()
get_target_property(module_name ${module} Swift_MODULE_NAME)
if(NOT module_name)
set(module_name ${module})
endif()
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftdoc)
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
RENAME ${SwiftCollections_MODULE_TRIPLE}.swiftmodule)
endfunction()