Skip to content

Commit ea40b13

Browse files
committed
build: Install libraries in an arch sub-folder
This is the proper installation scheme for Swift libraries and prevents having to manually copy them in `build.ps1`.
1 parent c2023c7 commit ea40b13

File tree

6 files changed

+61
-32
lines changed

6 files changed

+61
-32
lines changed

CMakeLists.txt

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ if(FOUNDATION_SWIFTPM_DEPS)
4747
return()
4848
endif()
4949

50-
if(NOT SWIFT_SYSTEM_NAME)
51-
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
52-
set(SWIFT_SYSTEM_NAME macosx)
53-
else()
54-
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
55-
endif()
56-
endif()
57-
5850
# Don't enable WMO on Windows due to linker failures
5951
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
6052
# Enable whole module optimization for release builds & incremental for debug builds
@@ -138,18 +130,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux)
138130
"$<$<COMPILE_LANGUAGE:C,CXX>:-fno-omit-frame-pointer>")
139131
endif()
140132

141-
# Precompute module triple for installation
142-
if(NOT SwiftFoundation_MODULE_TRIPLE)
143-
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
144-
if(CMAKE_Swift_COMPILER_TARGET)
145-
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
146-
endif()
147-
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
148-
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
149-
set(SwiftFoundation_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files")
150-
mark_as_advanced(SwiftFoundation_MODULE_TRIPLE)
151-
endif()
152-
153133
# System dependencies
154134

155135
# We know libdispatch is always unavailable on WASI
@@ -256,6 +236,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
256236
"-D_GNU_SOURCE")
257237
endif()
258238

239+
option(SwiftFoundation_INSTALL_ARCH_SUBDIR "Install libraries under an architecture subdirectory" NO)
240+
259241
include(GNUInstallDirs)
260242
include(FoundationSwiftSupport)
261243

Sources/CoreFoundation/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ install(DIRECTORY
148148

149149
if(NOT BUILD_SHARED_LIBS)
150150
install(TARGETS CoreFoundation
151-
ARCHIVE DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME}
152-
LIBRARY DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME}
151+
ARCHIVE DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM}
152+
LIBRARY DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM}
153153
RUNTIME DESTINATION bin)
154154
endif()

Sources/_CFURLSessionInterface/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ target_link_libraries(_CFURLSessionInterface PRIVATE
3737
if(NOT BUILD_SHARED_LIBS)
3838
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS _CFURLSessionInterface)
3939
install(TARGETS _CFURLSessionInterface
40-
ARCHIVE DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME}
41-
LIBRARY DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME}
40+
ARCHIVE DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM}
41+
LIBRARY DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM}
4242
RUNTIME DESTINATION bin)
4343
endif()

Sources/_CFXMLInterface/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ endif()
3939
if(NOT BUILD_SHARED_LIBS)
4040
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS _CFXMLInterface)
4141
install(TARGETS _CFXMLInterface
42-
ARCHIVE DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME}
43-
LIBRARY DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME}
42+
ARCHIVE DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM}
43+
LIBRARY DESTINATION lib/swift_static/${SwiftFoundation_PLATFORM}
4444
RUNTIME DESTINATION bin)
4545
endif()

Sources/plutil/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ target_link_libraries(plutil PRIVATE
2323
Foundation)
2424

2525
set_target_properties(plutil PROPERTIES
26-
INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SYSTEM_NAME}")
26+
INSTALL_RPATH "$ORIGIN/../lib/swift/${SwiftFoundation_PLATFORM}")
2727

2828
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS plutil)
2929
install(TARGETS plutil

cmake/modules/FoundationSwiftSupport.cmake

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,55 @@
1212
##
1313
##===----------------------------------------------------------------------===##
1414

15+
if(NOT SwiftFoundation_MODULE_TRIPLE OR NOT SwiftFoundation_ARCH OR NOT SwiftFoundation_PLATFORM)
16+
# Get the target information from the Swift compiler.
17+
set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
18+
if(CMAKE_Swift_COMPILER_TARGET)
19+
list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
20+
endif()
21+
execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
22+
endif()
23+
24+
if(NOT SwiftFoundation_MODULE_TRIPLE)
25+
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
26+
set(SwiftFoundation_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used to install swiftmodule files")
27+
mark_as_advanced(SwiftFoundation_MODULE_TRIPLE)
28+
message(CONFIGURE_LOG "Swift module triple: ${module_triple}")
29+
endif()
30+
31+
if(NOT SwiftFoundation_ARCH)
32+
if(CMAKE_Swift_COMPILER_VERSION VERSION_EQUAL 0.0.0 OR CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 6.2)
33+
# For newer compilers, we can use the -print-target-info command to get the architecture.
34+
string(JSON module_arch GET "${target_info_json}" "target" "arch")
35+
else()
36+
# For older compilers, extract the value from `SwiftFoundation_MODULE_TRIPLE`.
37+
string(REGEX MATCH "^[^-]+" module_arch "${SwiftFoundation_MODULE_TRIPLE}")
38+
endif()
39+
40+
set(SwiftFoundation_ARCH "${module_arch}" CACHE STRING "Arch folder name used to install libraries")
41+
mark_as_advanced(SwiftFoundation_ARCH)
42+
message(CONFIGURE_LOG "Swift arch: ${SwiftFoundation_ARCH}")
43+
endif()
44+
45+
if(NOT SwiftFoundation_PLATFORM)
46+
if(CMAKE_Swift_COMPILER_VERSION VERSION_EQUAL 0.0.0 OR CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 6.2)
47+
# For newer compilers, we can use the -print-target-info command to get the platform.
48+
string(JSON swift_platform GET "${target_info_json}" "target" "platform")
49+
else()
50+
# For older compilers, compile the value from `CMAKE_SYSTEM_NAME`.
51+
if(APPLE)
52+
set(swift_platform macosx)
53+
else()
54+
set(swift_platform "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
55+
endif()
56+
endif()
57+
58+
set(SwiftFoundation_PLATFORM "${swift_platform}" CACHE STRING "Platform folder name used to install libraries")
59+
mark_as_advanced(SwiftFoundation_PLATFORM)
60+
message(CONFIGURE_LOG "Swift platform: ${SwiftFoundation_PLATFORM}")
61+
endif()
62+
1563
function(_foundation_install_target module)
16-
set(swift_os ${SWIFT_SYSTEM_NAME})
1764
get_target_property(type ${module} TYPE)
1865

1966
if(type STREQUAL STATIC_LIBRARY)
@@ -23,8 +70,8 @@ function(_foundation_install_target module)
2370
endif()
2471

2572
install(TARGETS ${module}
26-
ARCHIVE DESTINATION lib/${swift}/${swift_os}
27-
LIBRARY DESTINATION lib/${swift}/${swift_os}
73+
ARCHIVE DESTINATION lib/${swift}/${SwiftFoundation_PLATFORM}$<$<BOOL:${SwiftFoundation_INSTALL_ARCH_SUBDIR}>:/${SwiftFoundation_ARCH}>
74+
LIBRARY DESTINATION lib/${swift}/${SwiftFoundation_PLATFORM}$<$<BOOL:${SwiftFoundation_INSTALL_ARCH_SUBDIR}>:/${SwiftFoundation_ARCH}>
2875
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2976
if(type STREQUAL EXECUTABLE)
3077
return()
@@ -36,10 +83,10 @@ function(_foundation_install_target module)
3683
endif()
3784

3885
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
39-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
86+
DESTINATION lib/${swift}/${SwiftFoundation_PLATFORM}/${module_name}.swiftmodule
4087
RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftdoc)
4188
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
42-
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
89+
DESTINATION lib/${swift}/${SwiftFoundation_PLATFORM}/${module_name}.swiftmodule
4390
RENAME ${SwiftFoundation_MODULE_TRIPLE}.swiftmodule)
4491

4592
endfunction()

0 commit comments

Comments
 (0)