Skip to content

Commit 78285e0

Browse files
committed
build: adopt current best practices for installing modules
This introduces the new option to control installation to a nested subdirectory. It also converts the current installation into a thick module format.
1 parent 09c2ade commit 78285e0

File tree

6 files changed

+82
-69
lines changed

6 files changed

+82
-69
lines changed

CMakeLists.txt

+8-11
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ include(DispatchAppleOptions)
118118
include(DispatchSanitization)
119119
include(DispatchCompilerWarnings)
120120
include(DTrace)
121-
include(SwiftSupport)
122121

123122
# NOTE(abdulras) this is the CMake supported way to control whether we generate
124123
# shared or static libraries. This impacts the behaviour of `add_library` in
@@ -157,6 +156,14 @@ option(INSTALL_PRIVATE_HEADERS "installs private headers in the same location as
157156
option(ENABLE_SWIFT "enable libdispatch swift overlay" OFF)
158157
if(ENABLE_SWIFT)
159158
enable_language(Swift)
159+
160+
include(PlatformInfo)
161+
162+
option(Dispatch_INSTALL_NESTED_SUBDIR "Install libraries under a platform and architecture subdirectory" NO)
163+
set(Dispatch_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${Dispatch_PLATFORM_SUBDIR}$<$<BOOL:${Dispatch_INSTALL_NESTED_SUBDIR}>:/${Dispatch_ARCH_SUBDIR}>")
164+
set(Dispatch_INSTALL_SWIFTMODULEDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${Dispatch_PLATFORM_SUBDIR}$<$<BOOL:${Dispatch_INSTALL_NESTED_SUBDIR}>:/${Dispatch_ARCH_SUBDIR}>")
165+
else()
166+
set(Dispatch_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
160167
endif()
161168

162169
option(ENABLE_THREAD_LOCAL_STORAGE "enable usage of thread local storage via _Thread_local" ON)
@@ -307,20 +314,10 @@ add_compile_definitions($<$<COMPILE_LANGUAGE:C,CXX>:HAVE_CONFIG_H>)
307314

308315

309316
if(ENABLE_SWIFT)
310-
if(NOT SWIFT_SYSTEM_NAME)
311-
if(APPLE)
312-
set(SWIFT_SYSTEM_NAME macosx)
313-
else()
314-
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
315-
endif()
316-
endif()
317-
318-
set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/${SWIFT_SYSTEM_NAME}" CACHE PATH "Path where the libraries will be installed")
319317
set(INSTALL_DISPATCH_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
320318
set(INSTALL_BLOCK_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
321319
set(INSTALL_OS_HEADERS_DIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/os" CACHE PATH "Path where the os/ headers will be installed")
322320
else()
323-
set(INSTALL_TARGET_DIR "${CMAKE_INSTALL_LIBDIR}" CACHE PATH "Path where the libraries will be installed")
324321
set(INSTALL_DISPATCH_HEADERS_DIR "include/dispatch" CACHE PATH "Path where the headers will be installed")
325322
set(INSTALL_BLOCK_HEADERS_DIR "include" CACHE PATH "Path where the headers will be installed for the blocks runtime")
326323
set(INSTALL_OS_HEADERS_DIR "include/os" CACHE PATH "Path where the headers will be installed")

cmake/modules/PlatformInfo.cmake

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2025 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
set(print_target_info_invocation "${CMAKE_Swift_COMPILER}" -print-target-info)
10+
if(CMAKE_Swift_COMPILER_TARGET)
11+
list(APPEND print_target_info_invocation -target ${CMAKE_Swift_COMPILER_TARGET})
12+
endif()
13+
execute_process(COMMAND ${print_target_info_invocation} OUTPUT_VARIABLE target_info_json)
14+
message(CONFIGURE_LOG "Swift Target Info: ${print_target_info_invocation}\n"
15+
"${target_info_json}")
16+
17+
if(NOT Dispatch_MODULE_TRIPLE)
18+
string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
19+
set(Dispatch_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used for installed swift{doc,module,interface} files")
20+
mark_as_advanced(Dispatch_MODULE_TRIPLE)
21+
22+
message(CONFIGURE_LOG "Swift Module Triple: ${module_triple}")
23+
endif()
24+
25+
if(NOT Dispatch_PLATFORM_SUBDIR)
26+
string(JSON platform GET "${target_info_json}" "target" "platform")
27+
if(NOT platform)
28+
if(NOT SWIFT_SYSTEM_NAME)
29+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
30+
set(platform macosx)
31+
else()
32+
set(platform $<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
33+
endif()
34+
endif()
35+
endif()
36+
set(Dispatch_PLATFORM_SUBDIR "${platform}" CACHE STRING "Platform name used for installed swift{doc,module,interface} files")
37+
mark_as_advanced(Dispatch_PLATFORM_SUBDIR)
38+
39+
message(CONFIGURE_LOG "Swift Platform: ${platform}")
40+
endif()
41+
42+
if(NOT Dispatch_ARCH_SUBDIR)
43+
string(JSON arch GET "${target_info_json}" "target" "arch")
44+
set(Dispatch_ARCH_SUBDIR "${arch}" CACHE STRING "Architecture used for setting the architecture subdirectory")
45+
mark_as_advanced(Dispatch_ARCH_SUBDIR)
46+
47+
message(CONFIGURE_LOG "Swift Architecture: ${arch}")
48+
endif()

cmake/modules/SwiftSupport.cmake

-41
This file was deleted.

src/BlocksRuntime/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ endif()
3737
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS BlocksRuntime)
3838
install(TARGETS BlocksRuntime
3939
EXPORT dispatchExports
40-
ARCHIVE DESTINATION ${INSTALL_TARGET_DIR}
41-
LIBRARY DESTINATION ${INSTALL_TARGET_DIR}
40+
ARCHIVE DESTINATION ${Dispatch_INSTALL_LIBDIR}
41+
LIBRARY DESTINATION ${Dispatch_INSTALL_LIBDIR}
4242
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

src/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,6 @@ endif()
189189
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS dispatch)
190190
install(TARGETS dispatch
191191
EXPORT dispatchExports
192-
ARCHIVE DESTINATION ${INSTALL_TARGET_DIR}
193-
LIBRARY DESTINATION ${INSTALL_TARGET_DIR}
192+
ARCHIVE DESTINATION ${Dispatch_INSTALL_LIBDIR}
193+
LIBRARY DESTINATION ${Dispatch_INSTALL_LIBDIR}
194194
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

src/swift/CMakeLists.txt

+22-13
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,33 @@ target_link_libraries(swiftDispatch PRIVATE
3131
BlocksRuntime::BlocksRuntime)
3232
target_link_libraries(swiftDispatch PUBLIC
3333
dispatch)
34+
if(NOT DARWIN AND NOT WIN32)
35+
target_link_options(swiftDispatch PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
36+
set_target_properties(swiftDispatch PROPERTIES INSTALL_RPATH "$ORIGIN")
37+
endif()
3438

35-
get_swift_host_arch(swift_arch)
36-
install(FILES
37-
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
38-
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftdoc
39-
DESTINATION ${INSTALL_TARGET_DIR}/${swift_arch})
40-
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS swiftDispatch)
4139
install(TARGETS swiftDispatch
4240
EXPORT dispatchExports
43-
ARCHIVE DESTINATION ${INSTALL_TARGET_DIR}
44-
LIBRARY DESTINATION ${INSTALL_TARGET_DIR}
45-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
41+
ARCHIVE DESTINATION "${Dispatch_INSTALL_LIBDIR}"
42+
LIBRARY DESTINATION "${Dispatch_INSTALL_LIBDIR}"
43+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
44+
INSTALL(FILES $<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_DIRECTORY>/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftdoc
45+
DESTINATION ${Dispatch_INSTALL_SWIFTMODULEDIR}/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
46+
RENAME ${Dispatch_MODULE_TRIPLE}.swiftdoc)
47+
# INSTALL(FILES $<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_DIRECTORY>/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftinterface
48+
# DESTINATION ${Dispatch_INSTALL_SWIFTMODULEDIR}/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
49+
# RENAME ${Dispach_MODULE_TRIPLE}.swiftinterface)
50+
INSTALL(FILES $<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_DIRECTORY>/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
51+
DESTINATION ${Dispatch_INSTALL_SWIFTMODULEDIR}/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
52+
RENAME ${Dispatch_MODULE_TRIPLE}.swiftmodule)
53+
INSTALL(FILES $<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_DIRECTORY>/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftsourceinfo
54+
DESTINATION ${Dispatch_INSTALL_SWIFTMODULEDIR}/$<TARGET_PROPERTY:swiftDispatch,Swift_MODULE_NAME>.swiftmodule
55+
RENAME ${Dispatch_MODULE_TRIPLE}.swiftsourceinfo)
4656
if(NOT BUILD_SHARED_LIBS)
4757
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS DispatchStubs)
4858
install(TARGETS DispatchStubs
4959
EXPORT dispatchExports
50-
DESTINATION ${INSTALL_TARGET_DIR})
51-
elseif(NOT DARWIN AND NOT WIN32)
52-
target_link_options(swiftDispatch PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
53-
set_target_properties(swiftDispatch PROPERTIES INSTALL_RPATH "$ORIGIN")
60+
DESTINATION ${Dispatch_INSTALL_LIBDIR})
5461
endif()
62+
63+
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS swiftDispatch)

0 commit comments

Comments
 (0)