From 974fd2ac462406a3937c01660e1dc4713a2ad1a9 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 13 Mar 2025 17:46:34 -0700 Subject: [PATCH 1/2] build: adopt current best practices for installation Add the `*_INSTALL_NESTED_SUBDIR` option and use `-print-target-info` to compute the install locations. This reduces the number of times we need to invoke the compiler and compute these values. --- CMakeLists.txt | 14 +++-- Sources/_FoundationCShims/CMakeLists.txt | 13 ++--- cmake/modules/PlatformInfo.cmake | 54 +++++++++++++++++++ .../modules/SwiftFoundationSwiftSupport.cmake | 47 +++------------- 4 files changed, 70 insertions(+), 58 deletions(-) create mode 100644 cmake/modules/PlatformInfo.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ed9f1e84..db96c4bf1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 "$") - 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) @@ -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$<$>:_static>/${SwiftFoundation_PLATFORM_SUBDIR}$<$:/${SwiftFoundation_ARCH_SUBDIR}>") +set(SwiftFoundation_INSTALL_SWIFTMODULEDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$>:_static>/${SwiftFoundation_PLATFORM_SUBDIR}") + include(SwiftFoundationSwiftSupport) add_subdirectory(Sources) diff --git a/Sources/_FoundationCShims/CMakeLists.txt b/Sources/_FoundationCShims/CMakeLists.txt index 1798a5bb0..378a57e73 100644 --- a/Sources/_FoundationCShims/CMakeLists.txt +++ b/Sources/_FoundationCShims/CMakeLists.txt @@ -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/ @@ -40,13 +34,12 @@ file(COPY install(DIRECTORY include/ DESTINATION - lib/${install_directory}/_FoundationCShims) + lib/swift$<$>:_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$<$>:_static>/${Foundation_PLATFORM_SUBDIR} + LIBRARY DESTINATION lib/swift$<$>:_static>/${Foundation_PLATFORM_SUBDIR} RUNTIME DESTINATION bin) endif() diff --git a/cmake/modules/PlatformInfo.cmake b/cmake/modules/PlatformInfo.cmake new file mode 100644 index 000000000..509e74f58 --- /dev/null +++ b/cmake/modules/PlatformInfo.cmake @@ -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 $) + 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() diff --git a/cmake/modules/SwiftFoundationSwiftSupport.cmake b/cmake/modules/SwiftFoundationSwiftSupport.cmake index cbdfc2aef..022f06e61 100644 --- a/cmake/modules/SwiftFoundationSwiftSupport.cmake +++ b/cmake/modules/SwiftFoundationSwiftSupport.cmake @@ -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 $/${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 $/${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 $/${module_name}.swiftsourceinfo + DESTINATION ${SwiftFoundation_INSTALL_SWIFTMODULEDIR}/${module_name}.swiftmodule + RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftsourceinfo) endfunction() From 438bb9ac66d622f8f562bfd9c1aecef525c939b7 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 17 Mar 2025 17:42:12 -0700 Subject: [PATCH 2/2] Update Sources/_FoundationCShims/CMakeLists.txt Co-authored-by: Jeremy Schonfeld --- Sources/_FoundationCShims/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/_FoundationCShims/CMakeLists.txt b/Sources/_FoundationCShims/CMakeLists.txt index 378a57e73..5a0cc7295 100644 --- a/Sources/_FoundationCShims/CMakeLists.txt +++ b/Sources/_FoundationCShims/CMakeLists.txt @@ -38,8 +38,8 @@ install(DIRECTORY if(NOT BUILD_SHARED_LIBS) install(TARGETS _FoundationCShims - ARCHIVE DESTINATION lib/swift$<$>:_static>/${Foundation_PLATFORM_SUBDIR} - LIBRARY DESTINATION lib/swift$<$>:_static>/${Foundation_PLATFORM_SUBDIR} + ARCHIVE DESTINATION lib/swift$<$>:_static>/${SwiftFoundation_PLATFORM_SUBDIR} + LIBRARY DESTINATION lib/swift$<$>:_static>/${SwiftFoundation_PLATFORM_SUBDIR} RUNTIME DESTINATION bin) endif()