Skip to content

Commit 4088796

Browse files
committed
build: split out the SDK overlay
This splits out the SDK overlay component and libdispatch runtime itself. Doing so enables the re-use of libdispatch with and without swift and makes the behaviour similar across Darwin and other platforms.
1 parent f8dcdc4 commit 4088796

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

src/CMakeLists.txt

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,50 @@ target_sources(dispatch
7272
PRIVATE
7373
block.cpp)
7474
if(HAVE_OBJC)
75+
# TODO(compnerd) split DispatchStubs.cc into a separate component for the ObjC
76+
# registration and a separate component for the swift compiler's emission of a
77+
# call to the ObjC autorelease elision entry point.
7578
target_sources(dispatch
7679
PRIVATE
7780
data.m
78-
object.m)
81+
object.m
82+
swift/DispatchStubs.cc)
7983
endif()
8084
if(ENABLE_SWIFT)
8185
set(swift_optimization_flags)
8286
if(NOT CMAKE_BUILD_TYPE MATCHES Debug)
8387
set(swift_optimization_flags -O)
8488
endif()
89+
90+
# NOTE(compnerd) Today regardless of whether or not ObjC interop is enabled,
91+
# swift will use an autoreleased return value convention for certain CF
92+
# functions (including some that are used/related to dispatch). This means
93+
# that the swift compiler in callers to such functions will call the function,
94+
# and then pass the result of the function to
95+
# objc_retainAutoreleasedReturnValue. In a context where we have ObjC interop
96+
# disabled, we do not have access to the objc runtime so an implementation of
97+
# objc_retainAutoreleasedReturnValue is not available. To work around this, we
98+
# provide a shim for objc_retainAutoreleasedReturnValue in DispatchStubs.cc
99+
# that just calls retain on the object. Once we fix the swift compiler to
100+
# switch to a different model for handling these arguments with objc-interop
101+
# disabled these shims can be eliminated.
102+
add_library(DispatchStubs
103+
OBJECT
104+
swift/DispatchStubs.cc)
105+
target_include_directories(DispatchStubs
106+
PRIVATE
107+
${PROJECT_SOURCE_DIR})
108+
set_target_properties(DispatchStubs
109+
PROPERTIES
110+
POSITION_INDEPENDENT_CODE YES)
111+
85112
add_swift_library(swiftDispatch
86113
MODULE_NAME
87114
Dispatch
88115
MODULE_LINK_NAME
89116
dispatch
90117
MODULE_PATH
91118
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
92-
OUTPUT
93-
${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o
94119
SOURCES
95120
swift/Block.swift
96121
swift/Data.swift
@@ -110,23 +135,12 @@ if(ENABLE_SWIFT)
110135
-I ${PROJECT_SOURCE_DIR}
111136
-I/usr/include
112137
${swift_optimization_flags}
138+
LINK_FLAGS
139+
$<TARGET_OBJECTS:DispatchStubs>
113140
DEPENDS
114-
${PROJECT_SOURCE_DIR}/dispatch/module.modulemap)
115-
116-
get_filename_component(swift_toolchain ${CMAKE_SWIFT_COMPILER} DIRECTORY)
117-
get_filename_component(swift_toolchain ${swift_toolchain} DIRECTORY)
118-
set(swift_runtime_libdir ${swift_toolchain}/lib/${swift_dir}/${swift_os}/${swift_arch})
119-
120-
target_sources(dispatch
121-
PRIVATE
122-
swift/DispatchStubs.cc
123-
${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o
124-
${swift_runtime_libdir}/swiftrt.o)
125-
if(CMAKE_BUILD_TYPE MATCHES Debug)
126-
target_link_libraries(dispatch
127-
PRIVATE
128-
swiftSwiftOnoneSupport)
129-
endif()
141+
${PROJECT_SOURCE_DIR}/dispatch/module.modulemap
142+
module-map-symlinks
143+
DispatchStubs)
130144
endif()
131145
if(ENABLE_DTRACE)
132146
dtrace_usdt_probe(${CMAKE_CURRENT_SOURCE_DIR}/provider.d
@@ -231,8 +245,6 @@ add_custom_command(TARGET dispatch POST_BUILD
231245
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:dispatch> .libs
232246
COMMENT "Copying libdispatch to .libs")
233247

234-
get_swift_host_arch(SWIFT_HOST_ARCH)
235-
236248
install(TARGETS
237249
dispatch
238250
DESTINATION
@@ -242,6 +254,18 @@ if(ENABLE_SWIFT)
242254
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
243255
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftdoc
244256
DESTINATION
245-
"${INSTALL_TARGET_DIR}/${SWIFT_HOST_ARCH}")
257+
${INSTALL_TARGET_DIR}/${swift_arch})
258+
259+
if(BUILD_SHARED_LIBS)
260+
set(library_kind SHARED)
261+
else()
262+
set(library_kind STATIC)
263+
endif()
264+
set(swiftDispatch_OUTPUT_FILE
265+
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_${library_kind}_LIBRARY_PREFIX}swiftDispatch${CMAKE_${library_kind}_LIBRARY_SUFFIX})
266+
install(FILES
267+
${swiftDispatch_OUTPUT_FILE}
268+
DESTINATION
269+
${INSTALL_TARGET_DIR})
246270
endif()
247271

tests/CMakeLists.txt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ if(BSD_OVERLAY_FOUND)
5656
PRIVATE
5757
${BSD_OVERLAY_LDFLAGS})
5858
endif()
59-
if(ENABLE_SWIFT)
60-
target_link_libraries(bsdtestharness
61-
PRIVATE
62-
swiftCore-${swift_os}-${swift_arch}
63-
swiftSwiftOnoneSupport-${swift_os}-${swift_arch})
64-
endif()
6559

6660
function(add_unit_test name)
6761
set(options DISABLED_TEST;NO_BSD_OVERLAY)
@@ -83,10 +77,6 @@ function(add_unit_test name)
8377
# For testing in swift.org CI system; make deadlines lenient by default
8478
# to reduce probability of test failures due to machine load.
8579
target_compile_options(${name} PRIVATE -DLENIENT_DEADLINES=1)
86-
target_link_libraries(${name}
87-
PRIVATE
88-
swiftCore-${swift_os}-${swift_arch}
89-
swiftSwiftOnoneSupport-${swift_os}-${swift_arch})
9080
endif()
9181
target_include_directories(${name}
9282
SYSTEM BEFORE PRIVATE
@@ -114,12 +104,6 @@ function(add_unit_test name)
114104
PRIVATE
115105
${BSD_OVERLAY_LDFLAGS})
116106
endif()
117-
if(ENABLE_SWIFT)
118-
target_link_libraries(${name}
119-
PRIVATE
120-
swiftCore-${swift_os}-${swift_arch}
121-
swiftSwiftOnoneSupport-${swift_os}-${swift_arch})
122-
endif()
123107
target_link_libraries(${name} PRIVATE bsdtests)
124108
add_test(NAME ${name}
125109
COMMAND bsdtestharness $<TARGET_FILE:${name}>)

0 commit comments

Comments
 (0)