Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,23 @@ 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)
else()
# 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}$<$<BOOL:${COLLECTIONS_INSTALL_ARCH_SUBDIR}>:/${COLLECTIONS_ARCH}>
LIBRARY DESTINATION lib/swift_static/${COLLECTIONS_PLATFORM}$<$<BOOL:${COLLECTIONS_INSTALL_ARCH_SUBDIR}>:/${COLLECTIONS_ARCH}>
RUNTIME DESTINATION bin)
endif()
install(FILES $<TARGET_PROPERTY:${COLLECTIONS_MODULE_NAME},Swift_MODULE_DIRECTORY>/${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 $<TARGET_PROPERTY:${COLLECTIONS_MODULE_NAME},Swift_MODULE_DIRECTORY>/${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()
Expand Down
24 changes: 0 additions & 24 deletions cmake/modules/PlatformInfo.cmake

This file was deleted.

75 changes: 46 additions & 29 deletions cmake/modules/SwiftSupport.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
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)
Expand All @@ -51,7 +65,10 @@ function(_install_target module)
set(swift swift)
endif()

install(TARGETS ${module})
install(TARGETS ${module}
ARCHIVE DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}$<$<BOOL:${COLLECTIONS_INSTALL_ARCH_SUBDIR}>:/${COLLECTIONS_ARCH}>
LIBRARY DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}$<$<BOOL:${COLLECTIONS_INSTALL_ARCH_SUBDIR}>:/${COLLECTIONS_ARCH}>
RUNTIME DESTINATION bin)
if(type STREQUAL EXECUTABLE)
return()
endif()
Expand All @@ -62,10 +79,10 @@ function(_install_target 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)
DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}/${module_name}.swiftmodule
RENAME ${COLLECTIONS_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)
DESTINATION lib/${swift}/${COLLECTIONS_PLATFORM}/${module_name}.swiftmodule
RENAME ${COLLECTIONS_MODULE_TRIPLE}.swiftmodule)
endfunction()