Skip to content

build: adopt current best practices for installation #1212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
14 changes: 6 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ endif()
project(SwiftFoundation
LANGUAGES C Swift)

if(NOT SWIFT_SYSTEM_NAME)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(SWIFT_SYSTEM_NAME macosx)
else()
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
endif()
endif()

# Don't enable WMO on Windows hosts due to linker failures, and the use of swift's
# old driver from build.ps1 when building for windows/android on a windows host.
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
Expand Down Expand Up @@ -145,6 +137,12 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
endif()

include(GNUInstallDirs)
include(PlatformInfo)

option(SwiftFoundation_INSTALL_NESTED_SUBDIR "Install libraries under a platform and architecture subdirectory" NO)
set(SwiftFoundation_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SwiftFoundation_PLATFORM_SUBDIR}$<$<BOOL:${SwiftFoundation_INSTALL_NESTED_SUBDIR}>:/${SwiftFoundation_ARCH_SUBDIR}>")
set(SwiftFoundation_INSTALL_SWIFTMODULEDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SwiftFoundation_PLATFORM_SUBDIR}")

include(SwiftFoundationSwiftSupport)

add_subdirectory(Sources)
Expand Down
13 changes: 3 additions & 10 deletions Sources/_FoundationCShims/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ target_compile_options(_FoundationCShims INTERFACE

set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS _FoundationCShims)

if(BUILD_SHARED_LIBS)
set(install_directory swift)
else()
set(install_directory swift_static)
endif()

# Copy Headers to known directory for direct client (XCTest) test builds
file(COPY
include/
Expand All @@ -40,13 +34,12 @@ file(COPY
install(DIRECTORY
include/
DESTINATION
lib/${install_directory}/_FoundationCShims)
lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/_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/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SwiftFoundation_PLATFORM_SUBDIR}
LIBRARY DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SwiftFoundation_PLATFORM_SUBDIR}
RUNTIME DESTINATION bin)
endif()

54 changes: 54 additions & 0 deletions cmake/modules/PlatformInfo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 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
##
##===----------------------------------------------------------------------===##

set(print_target_info_invocation "${CMAKE_Swift_COMPILER}" -print-target-info)
if(CMAKE_Swift_COMPILER_TARGET)
list(APPEND print_target_info_invocation -target ${CMAKE_Swift_COMPILER_TARGET})
endif()
execute_process(COMMAND ${print_target_info_invocation} OUTPUT_VARIABLE target_info_json)
message(CONFIGURE_LOG "Swift Target Info: ${print_target_info_invocation}\n"
"${target_info_json}")

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 for installed swift{doc,module,interface} files")
mark_as_advanced(SwiftFoundation_MODULE_TRIPLE)

message(CONFIGURE_LOG "Swift Module Triple: ${module_triple}")
endif()

if(NOT SwiftFoundation_PLATFORM_SUBDIR)
string(JSON platform GET "${target_info_json}" "target" "platform")
if(NOT platform)
if(NOT SWIFT_SYSTEM_NAME)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(platform macosx)
else()
set(platform $<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
endif()
endif()
endif()
set(SwiftFoundation_PLATFORM_SUBDIR "${platform}" CACHE STRING "Platform name used for installed swift{doc,module,interface} files")
mark_as_advanced(SwiftFoundation_PLATFORM_SUBDIR)

message(CONFIGURE_LOG "Swift Platform: ${platform}")
endif()

if(NOT SwiftFoundation_ARCH_SUBDIR)
string(JSON arch GET "${target_info_json}" "target" "arch")
set(SwiftFoundation_ARCH_SUBDIR "${arch}" CACHE STRING "Architecture used for setting the architecture subdirectory")
mark_as_advanced(SwiftFoundation_ARCH_SUBDIR)

message(CONFIGURE_LOG "Swift Architecture: ${arch}")
endif()
47 changes: 7 additions & 40 deletions cmake/modules/SwiftFoundationSwiftSupport.cmake
Original file line number Diff line number Diff line change
@@ -1,54 +1,21 @@
# 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(_swift_foundation_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}
ARCHIVE DESTINATION ${SwiftFoundation_INSTALL_LIBDIR}
LIBRARY DESTINATION ${SwiftFoundation_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
if(type STREQUAL EXECUTABLE)
return()
endif()

get_target_property(module_name ${module} Swift_MODULE_NAME)
if(NOT module_name)
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 $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
DESTINATION ${SwiftFoundation_INSTALL_SWIFTMODULEDIR}/${module_name}.swiftmodule
RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftdoc)
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
DESTINATION ${SwiftFoundation_INSTALL_SWIFTMODULEDIR}/${module_name}.swiftmodule
RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftmodule)

install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftsourceinfo
DESTINATION ${SwiftFoundation_INSTALL_SWIFTMODULEDIR}/${module_name}.swiftmodule
RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftsourceinfo)
endfunction()