Skip to content

dispatch: remove DispatchStubs on non-ObjC targets #864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ project(dispatch
VERSION 1.3
LANGUAGES C CXX)

set(CMAKE_POSITION_INDEPENDENT_CODE YES)

if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
include(ClangClCompileRules)
endif()
Expand Down
4 changes: 1 addition & 3 deletions dispatch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(DISPATCH_MODULE_MAP ${PROJECT_SOURCE_DIR}/dispatch/darwin/module.modulemap)
elseif(BUILD_SHARED_LIBS)
set(DISPATCH_MODULE_MAP ${PROJECT_SOURCE_DIR}/dispatch/generic/module.modulemap)
else()
set(DISPATCH_MODULE_MAP ${PROJECT_SOURCE_DIR}/dispatch/generic_static/module.modulemap)
set(DISPATCH_MODULE_MAP ${PROJECT_SOURCE_DIR}/dispatch/generic/module.modulemap)
endif()
configure_file(dispatch-vfs.yaml.in
${CMAKE_BINARY_DIR}/dispatch-vfs-overlay.yaml
Expand Down
19 changes: 0 additions & 19 deletions dispatch/generic_static/module.modulemap

This file was deleted.

3 changes: 0 additions & 3 deletions src/BlocksRuntime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ if(HAVE_OBJC AND CMAKE_DL_LIBS)
${CMAKE_DL_LIBS})
endif()

set_target_properties(BlocksRuntime PROPERTIES
POSITION_INDEPENDENT_CODE TRUE)

if(LINKER_SUPPORTS_BUILD_ID)
target_link_options(BlocksRuntime PRIVATE "LINKER:--build-id=sha1")
endif()
Expand Down
9 changes: 1 addition & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,12 @@ if(ENABLE_DTRACE)
endif()

if(HAVE_OBJC)
# TODO(compnerd) split DispatchStubs.cc into a separate component for the ObjC
# registration and a separate component for the swift compiler's emission of a
# call to the ObjC autorelease elision entry point.
target_sources(dispatch PRIVATE
data.m
object.m
swift/DispatchStubs.cc)
swift/DispatchStubs.m)
endif()


set_target_properties(dispatch PROPERTIES
POSITION_INDEPENDENT_CODE YES)

target_include_directories(dispatch PUBLIC
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}
Expand Down
23 changes: 12 additions & 11 deletions src/swift/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
add_library(DispatchStubs STATIC
DispatchStubs.cc)
target_include_directories(DispatchStubs PRIVATE
${PROJECT_SOURCE_DIR})
set_target_properties(DispatchStubs PROPERTIES
POSITION_INDEPENDENT_CODE YES)
if(HAVE_OBJC)
add_library(DispatchStubs STATIC
DispatchStubs.m)
target_include_directories(DispatchStubs PRIVATE
${PROJECT_SOURCE_DIR})
endif()

add_library(swiftDispatch
Block.swift
Expand All @@ -27,10 +27,14 @@ set_target_properties(swiftDispatch PROPERTIES
Swift_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/swift
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_BINARY_DIR}/swift)
target_link_libraries(swiftDispatch PRIVATE
DispatchStubs
$<$<BOOL:${HAVE_OBJC}>:DispatchStubs>
BlocksRuntime::BlocksRuntime)
target_link_libraries(swiftDispatch PUBLIC
dispatch)
if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Windows")
target_link_options(swiftDispatch PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
set_target_properties(swiftDispatch PROPERTIES INSTALL_RPATH "$ORIGIN")
endif()

get_swift_host_arch(swift_arch)
install(FILES
Expand All @@ -43,12 +47,9 @@ install(TARGETS swiftDispatch
ARCHIVE DESTINATION ${INSTALL_TARGET_DIR}
LIBRARY DESTINATION ${INSTALL_TARGET_DIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
if(NOT BUILD_SHARED_LIBS)
if(HAVE_OBJC AND NOT BUILD_SHARED_LIBS)
set_property(GLOBAL APPEND PROPERTY DISPATCH_EXPORTS DispatchStubs)
install(TARGETS DispatchStubs
EXPORT dispatchExports
DESTINATION ${INSTALL_TARGET_DIR})
elseif(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Windows")
target_link_options(swiftDispatch PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
set_target_properties(swiftDispatch PROPERTIES INSTALL_RPATH "$ORIGIN")
endif()
14 changes: 2 additions & 12 deletions src/swift/DispatchStubs.cc → src/swift/DispatchStubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
//===----------------------------------------------------------------------===//

#include <dispatch/dispatch.h>
#include <stdio.h>

#if defined(__ELF__) || defined(__MACH__) || defined(__WASM__)
#define DISPATCH_RUNTIME_STDLIB_INTERFACE __attribute__((__visibility__("default")))
#else
#define DISPATCH_RUNTIME_STDLIB_INTERFACE __declspec(dllexport)
#endif

#if USE_OBJC
@protocol OS_dispatch_source;
Expand All @@ -34,11 +27,9 @@
@protocol OS_dispatch_source_vnode;
@protocol OS_dispatch_source_write;

// #include <dispatch/private.h>
__attribute__((constructor))
__attribute__((__constructor__))
static void _dispatch_overlay_constructor() {
Class source = objc_lookUpClass("OS_dispatch_source");
if (source) {
if (Class source = objc_lookUpClass("OS_dispatch_source")) {
class_addProtocol(source, @protocol(OS_dispatch_source));
class_addProtocol(source, @protocol(OS_dispatch_source_mach_send));
class_addProtocol(source, @protocol(OS_dispatch_source_mach_recv));
Expand All @@ -54,5 +45,4 @@ static void _dispatch_overlay_constructor() {
class_addProtocol(source, @protocol(OS_dispatch_source_write));
}
}

#endif /* USE_OBJC */