@@ -72,25 +72,50 @@ target_sources(dispatch
72
72
PRIVATE
73
73
block .cpp )
74
74
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.
75
78
target_sources (dispatch
76
79
PRIVATE
77
80
data.m
78
- object.m )
81
+ object.m
82
+ swift/DispatchStubs.cc )
79
83
endif ()
80
84
if (ENABLE_SWIFT )
81
85
set (swift_optimization_flags )
82
86
if (NOT CMAKE_BUILD_TYPE MATCHES Debug )
83
87
set (swift_optimization_flags -O )
84
88
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
+
85
112
add_swift_library (swiftDispatch
86
113
MODULE_NAME
87
114
Dispatch
88
115
MODULE_LINK_NAME
89
- dispatch
116
+ swiftDispatch
90
117
MODULE_PATH
91
118
${CMAKE_CURRENT_BINARY_DIR} /swift/Dispatch.swiftmodule
92
- OUTPUT
93
- ${CMAKE_CURRENT_BINARY_DIR} /swiftDispatch.o
94
119
SOURCES
95
120
swift/Block.swift
96
121
swift/Data.swift
@@ -110,23 +135,14 @@ if(ENABLE_SWIFT)
110
135
-I ${PROJECT_SOURCE_DIR}
111
136
-I/usr/include
112
137
${swift_optimization_flags}
138
+ LINK_FLAGS
139
+ -lDispatchStubs
140
+ -L $< TARGET_LINKER_FILE_DIR:dispatch>
141
+ -ldispatch
113
142
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 )
130
146
endif ()
131
147
if (ENABLE_DTRACE )
132
148
dtrace_usdt_probe (${CMAKE_CURRENT_SOURCE_DIR} /provider.d
@@ -231,8 +247,6 @@ add_custom_command(TARGET dispatch POST_BUILD
231
247
COMMAND ${CMAKE_COMMAND} -E copy $< TARGET_FILE:dispatch> .libs
232
248
COMMENT "Copying libdispatch to .libs" )
233
249
234
- get_swift_host_arch (SWIFT_HOST_ARCH )
235
-
236
250
install (TARGETS
237
251
dispatch
238
252
DESTINATION
@@ -242,6 +256,18 @@ if(ENABLE_SWIFT)
242
256
${CMAKE_CURRENT_BINARY_DIR} /swift/Dispatch.swiftmodule
243
257
${CMAKE_CURRENT_BINARY_DIR} /swift/Dispatch.swiftdoc
244
258
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} )
246
272
endif ()
247
273
0 commit comments