@@ -72,25 +72,50 @@ target_sources(dispatch
7272 PRIVATE
7373 block.cpp)
7474if (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)
7983endif ()
8084if (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
86113 MODULE_NAME
87114 Dispatch
88115 MODULE_LINK_NAME
89- dispatch
116+ swiftDispatch
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,14 @@ if(ENABLE_SWIFT)
110135 -I ${PROJECT_SOURCE_DIR}
111136 -I/usr/include
112137 ${swift_optimization_flags}
138+ LINK_FLAGS
139+ -lDispatchStubs
140+ -L $<TARGET_LINKER_FILE_DIR:dispatch>
141+ -ldispatch
113142 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+ ${PROJECT_SOURCE_DIR} /dispatch/module.modulemap
144+ module-map-symlinks
145+ DispatchStubs)
130146endif ()
131147if (ENABLE_DTRACE)
132148 dtrace_usdt_probe(${CMAKE_CURRENT_SOURCE_DIR} /provider.d
@@ -231,8 +247,6 @@ add_custom_command(TARGET dispatch POST_BUILD
231247 COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:dispatch> .libs
232248 COMMENT "Copying libdispatch to .libs" )
233249
234- get_swift_host_arch(SWIFT_HOST_ARCH)
235-
236250install (TARGETS
237251 dispatch
238252 DESTINATION
@@ -242,6 +256,18 @@ if(ENABLE_SWIFT)
242256 ${CMAKE_CURRENT_BINARY_DIR} /swift/Dispatch.swiftmodule
243257 ${CMAKE_CURRENT_BINARY_DIR} /swift/Dispatch.swiftdoc
244258 DESTINATION
245- "${INSTALL_TARGET_DIR} /${SWIFT_HOST_ARCH} " )
259+ ${INSTALL_TARGET_DIR} /${swift_arch} )
260+
261+ if (BUILD_SHARED_LIBS )
262+ set (library_kind SHARED)
263+ else ()
264+ set (library_kind STATIC )
265+ endif ()
266+ set (swiftDispatch_OUTPUT_FILE
267+ ${CMAKE_CURRENT_BINARY_DIR} /${CMAKE_${library_kind} _LIBRARY_PREFIX}swiftDispatch${CMAKE_${library_kind} _LIBRARY_SUFFIX})
268+ install (FILES
269+ ${swiftDispatch_OUTPUT_FILE}
270+ DESTINATION
271+ ${INSTALL_TARGET_DIR} )
246272endif ()
247273
0 commit comments