Skip to content

Commit 9bc407f

Browse files
compnerdrokhinip
authored andcommitted
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. Signed-off-by: Kim Topley <[email protected]>
1 parent 9012286 commit 9bc407f

File tree

1 file changed

+54
-29
lines changed

1 file changed

+54
-29
lines changed

src/CMakeLists.txt

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,60 @@ 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+
STATIC
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
113+
CFLAGS
114+
-fblocks
115+
-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/module.modulemap
116+
DEPENDS
117+
${PROJECT_SOURCE_DIR}/dispatch/module.modulemap
118+
DispatchStubs
119+
LINK_FLAGS
120+
-lDispatchStubs
121+
-L $<TARGET_LINKER_FILE_DIR:dispatch>
122+
-ldispatch
86123
MODULE_NAME
87124
Dispatch
88125
MODULE_LINK_NAME
89-
dispatch
126+
swiftDispatch
90127
MODULE_PATH
91128
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
92-
OUTPUT
93-
${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o
94129
SOURCES
95130
swift/Block.swift
96131
swift/Data.swift
@@ -101,32 +136,12 @@ if(ENABLE_SWIFT)
101136
swift/Source.swift
102137
swift/Time.swift
103138
swift/Wrapper.swift
104-
TARGET
105-
${CMAKE_C_COMPILER_TARGET}
106-
CFLAGS
107-
-fblocks
108-
-fmodule-map-file=${PROJECT_SOURCE_DIR}/dispatch/module.modulemap
109139
SWIFT_FLAGS
110140
-I ${PROJECT_SOURCE_DIR}
111141
-I/usr/include
112142
${swift_optimization_flags}
113-
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()
143+
TARGET
144+
${CMAKE_C_COMPILER_TARGET})
130145
endif()
131146
if(ENABLE_DTRACE)
132147
dtrace_usdt_probe(${CMAKE_CURRENT_SOURCE_DIR}/provider.d
@@ -231,8 +246,6 @@ add_custom_command(TARGET dispatch POST_BUILD
231246
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:dispatch> .libs
232247
COMMENT "Copying libdispatch to .libs")
233248

234-
get_swift_host_arch(SWIFT_HOST_ARCH)
235-
236249
install(TARGETS
237250
dispatch
238251
DESTINATION
@@ -242,6 +255,18 @@ if(ENABLE_SWIFT)
242255
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
243256
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftdoc
244257
DESTINATION
245-
"${INSTALL_TARGET_DIR}/${SWIFT_HOST_ARCH}")
258+
${INSTALL_TARGET_DIR}/${swift_arch})
259+
260+
if(BUILD_SHARED_LIBS)
261+
set(library_kind SHARED)
262+
else()
263+
set(library_kind STATIC)
264+
endif()
265+
set(swiftDispatch_OUTPUT_FILE
266+
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_${library_kind}_LIBRARY_PREFIX}swiftDispatch${CMAKE_${library_kind}_LIBRARY_SUFFIX})
267+
install(FILES
268+
${swiftDispatch_OUTPUT_FILE}
269+
DESTINATION
270+
${INSTALL_TARGET_DIR})
246271
endif()
247272

0 commit comments

Comments
 (0)