Skip to content

Commit 93bf3f8

Browse files
committed
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.
1 parent a95c2c8 commit 93bf3f8

File tree

4 files changed

+70
-58
lines changed

4 files changed

+70
-58
lines changed

CMakeLists.txt

+6-8
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ endif()
2727
project(SwiftFoundation
2828
LANGUAGES C Swift)
2929

30-
if(NOT SWIFT_SYSTEM_NAME)
31-
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
32-
set(SWIFT_SYSTEM_NAME macosx)
33-
else()
34-
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
35-
endif()
36-
endif()
37-
3830
# Don't enable WMO on Windows hosts due to linker failures, and the use of swift's
3931
# old driver from build.ps1 when building for windows/android on a windows host.
4032
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
@@ -145,6 +137,12 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
145137
endif()
146138

147139
include(GNUInstallDirs)
140+
include(PlatformInfo)
141+
142+
option(SwiftFoundation_INSTALL_NESTED_SUBDIR "Install libraries under a platform and architecture subdirectory" NO)
143+
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}>")
144+
set(SwiftFoundation_INSTALL_SWIFTMODULEDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SwiftFoundation_PLATFORM_SUBDIR}")
145+
148146
include(SwiftFoundationSwiftSupport)
149147

150148
add_subdirectory(Sources)

Sources/_FoundationCShims/CMakeLists.txt

+3-10
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ target_compile_options(_FoundationCShims INTERFACE
2424

2525
set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS _FoundationCShims)
2626

27-
if(BUILD_SHARED_LIBS)
28-
set(install_directory swift)
29-
else()
30-
set(install_directory swift_static)
31-
endif()
32-
3327
# Copy Headers to known directory for direct client (XCTest) test builds
3428
file(COPY
3529
include/
@@ -40,13 +34,12 @@ file(COPY
4034
install(DIRECTORY
4135
include/
4236
DESTINATION
43-
lib/${install_directory}/_FoundationCShims)
37+
lib/swift$<$<BOOL:${BUILD_SHARED_LIBS}>:_static>/_FoundationCShims)
4438

4539
if(NOT BUILD_SHARED_LIBS)
46-
get_swift_host_os(swift_os)
4740
install(TARGETS _FoundationCShims
48-
ARCHIVE DESTINATION lib/${install_directory}/${swift_os}
49-
LIBRARY DESTINATION lib/${install_directory}/${swift_os}
41+
ARCHIVE DESTINATION lib/swift$<$<BOOL:${BUILD_SHARED_LIBS}>:_static>/${Foundation_PLATFORM_SUBDIR}
42+
LIBRARY DESTINATION lib/swift$<$<BOOL:${BUILD_SHARED_LIBS}>:_static>/${Foundation_PLATFORM_SUBDIR}
5043
RUNTIME DESTINATION bin)
5144
endif()
5245

cmake/modules/PlatformInfo.cmake

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the Swift open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
## Licensed under Apache License v2.0
7+
##
8+
## See LICENSE.txt for license information
9+
## See CONTRIBUTORS.md for the list of Swift project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
set(print_target_info_invocation "${CMAKE_Swift_COMPILER}" -print-target-info)
16+
if(CMAKE_Swift_COMPILER_TARGET)
17+
list(APPEND print_target_info_invocation -target ${CMAKE_Swift_COMPILER_TARGET})
18+
endif()
19+
execute_process(COMMAND ${print_target_info_invocation} OUTPUT_VARIABLE target_info_json)
20+
message(CONFIGURE_LOG "Swift Target Info: ${print_target_info_invocation}\n"
21+
"${target_info_json}")
22+
23+
if(NOT SwiftFoundation_MODULE_TRIPLE)
24+
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
25+
set(SwiftFoundation_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used for installed swift{doc,module,interface} files")
26+
mark_as_advanced(SwiftFoundation_MODULE_TRIPLE)
27+
28+
message(CONFIGURE_LOG "Swift Module Triple: ${module_triple}")
29+
endif()
30+
31+
if(NOT SwiftFoundation_PLATFORM_SUBDIR)
32+
string(JSON platform GET "${target_info_json}" "target" "platform")
33+
if(NOT platform)
34+
if(NOT SWIFT_SYSTEM_NAME)
35+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
36+
set(platform macosx)
37+
else()
38+
set(platform $<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
39+
endif()
40+
endif()
41+
endif()
42+
set(SwiftFoundation_PLATFORM_SUBDIR "${platform}" CACHE STRING "Platform name used for installed swift{doc,module,interface} files")
43+
mark_as_advanced(SwiftFoundation_PLATFORM_SUBDIR)
44+
45+
message(CONFIGURE_LOG "Swift Platform: ${platform}")
46+
endif()
47+
48+
if(NOT SwiftFoundation_ARCH_SUBDIR)
49+
string(JSON arch GET "${target_info_json}" "target" "arch")
50+
set(SwiftFoundation_ARCH_SUBDIR "${arch}" CACHE STRING "Architecture used for setting the architecture subdirectory")
51+
mark_as_advanced(SwiftFoundation_ARCH_SUBDIR)
52+
53+
message(CONFIGURE_LOG "Swift Architecture: ${arch}")
54+
endif()
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,21 @@
1-
# Returns the os name in a variable
2-
#
3-
# Usage:
4-
# get_swift_host_os(result_var_name)
5-
#
6-
#
7-
# Sets ${result_var_name} with the converted OS name derived from
8-
# CMAKE_SYSTEM_NAME.
9-
function(get_swift_host_os result_var_name)
10-
set(${result_var_name} ${SWIFT_SYSTEM_NAME} PARENT_SCOPE)
11-
endfunction()
12-
131
function(_swift_foundation_install_target module)
14-
get_swift_host_os(swift_os)
15-
get_target_property(type ${module} TYPE)
16-
17-
if(type STREQUAL STATIC_LIBRARY)
18-
set(swift swift_static)
19-
else()
20-
set(swift swift)
21-
endif()
22-
232
install(TARGETS ${module}
24-
ARCHIVE DESTINATION lib/${swift}/${swift_os}
25-
LIBRARY DESTINATION lib/${swift}/${swift_os}
3+
ARCHIVE DESTINATION ${SwiftFoundation_INSTALL_LIBDIR}
4+
LIBRARY DESTINATION ${SwiftFoundation_INSTALL_LIBDIR}
265
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
27-
if(type STREQUAL EXECUTABLE)
28-
return()
29-
endif()
306

317
get_target_property(module_name ${module} Swift_MODULE_NAME)
328
if(NOT module_name)
339
set(module_name ${module})
3410
endif()
3511

36-
if(NOT SwiftFoundation_MODULE_TRIPLE)
37-
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
38-
if(CMAKE_Swift_COMPILER_TARGET)
39-
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
40-
endif()
41-
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
42-
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
43-
set(SwiftFoundation_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files")
44-
mark_as_advanced(SwiftFoundation_MODULE_TRIPLE)
45-
endif()
46-
4712
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
48-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
13+
DESTINATION ${SwiftFoundation_INSTALL_SWIFTMODULEDIR}/${module_name}.swiftmodule
4914
RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftdoc)
5015
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
51-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
16+
DESTINATION ${SwiftFoundation_INSTALL_SWIFTMODULEDIR}/${module_name}.swiftmodule
5217
RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftmodule)
53-
18+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftsourceinfo
19+
DESTINATION ${SwiftFoundation_INSTALL_SWIFTMODULEDIR}/${module_name}.swiftmodule
20+
RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftsourceinfo)
5421
endfunction()

0 commit comments

Comments
 (0)