Skip to content

Commit a6a4f7c

Browse files
authored
Cleanup CMake configuration for external blocks runtime support (#355)
* remove unused CMake directory * Add FindBlocksRuntime.cmake from the libdispatch project This is a slightly modified version of the file found in swift-corelibs-libdispatch. Notabily, the non-existent HINTS are removed. * CMakeLists.txt: Use FindBlocksRuntime Use cmake/FindBlocksRuntime.cmake to locate the external blocks runtime, and do not compile the internal one. * Only call init_trampolines when using the internal blocks rt * Conditionally include BlockImpTest.m * External symbols should be imported from the DLL
1 parent 0e02765 commit a6a4f7c

File tree

7 files changed

+86
-102
lines changed

7 files changed

+86
-102
lines changed

CMake/CMakeLists.txt

Lines changed: 0 additions & 17 deletions
This file was deleted.

CMake/typeinfo_test.cc

Lines changed: 0 additions & 62 deletions
This file was deleted.

CMakeLists.txt

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ if (MINGW)
2626
set(CMAKE_OBJCXX_IMPLICIT_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
2727
endif ()
2828

29+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
30+
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_INSTALL_PREFIX})
31+
2932
INCLUDE (CheckCXXSourceCompiles)
3033
INCLUDE (FetchContent)
3134
INCLUDE (CheckSymbolExists)
@@ -50,7 +53,6 @@ add_compile_definitions(GNUSTEP __OBJC_RUNTIME_INTERNAL__=1 __OBJC_BOOL)
5053
set(CMAKE_CXX_STANDARD 17)
5154

5255
set(libobjc_ASM_SRCS
53-
block_trampolines.S
5456
objc_msgSend.S)
5557
set(libobjc_OBJCXX_SRCS
5658
arc.mm
@@ -62,7 +64,6 @@ set(libobjc_OBJC_SRCS
6264
properties.m)
6365
set(libobjc_C_SRCS
6466
alias_table.c
65-
block_to_imp.c
6667
builtin_classes.c
6768
caps.c
6869
category_loader.c
@@ -105,6 +106,7 @@ set(libobjc_HDRS
105106
set(libobjc_CXX_SRCS
106107
selector_table.cc
107108
)
109+
108110
# Windows does not use DWARF EH, except when using the GNU ABI (MinGW)
109111
if (WIN32 AND NOT MINGW)
110112
list(APPEND libobjc_CXX_SRCS eh_win32_msvc.cc)
@@ -154,6 +156,11 @@ add_compile_definitions($<$<BOOL:${STRICT_APPLE_COMPATIBILITY}>:STRICT_APPLE_COM
154156
configure_file(objc/objc-config.h.in objc/objc-config.h @ONLY)
155157
include_directories("${PROJECT_BINARY_DIR}/objc/")
156158

159+
if (EMBEDDED_BLOCKS_RUNTIME)
160+
list(APPEND libobjc_ASM_SRCS block_trampolines.S)
161+
list(APPEND libobjc_C_SRCS block_to_imp.c)
162+
endif()
163+
157164
if (OLDABI_COMPAT)
158165
list(APPEND libobjc_C_SRCS legacy.c abi_version.c statics_loader.c)
159166
add_definitions(-DOLDABI_COMPAT=1)
@@ -242,18 +249,9 @@ if (EMBEDDED_BLOCKS_RUNTIME)
242249
list(APPEND libobjc_HDRS objc/blocks_runtime.h)
243250
add_definitions(-DEMBEDDED_BLOCKS_RUNTIME)
244251
else ()
245-
find_library(BLOCKS_RUNTIME_LIBRARY BlocksRuntime)
246-
if (BLOCKS_RUNTIME_LIBRARY)
247-
set(CMAKE_REQUIRED_LIBRARIES ${BLOCKS_RUNTIME_LIBRARY})
248-
check_symbol_exists(_Block_use_RR2 "Block_private.h" HAVE_BLOCK_USE_RR2)
249-
if (HAVE_BLOCK_USE_RR2)
250-
add_definitions(-DHAVE_BLOCK_USE_RR2)
251-
else ()
252-
message(FATAL_ERROR "_Block_use_RR2() could not be found in libBlocksRuntime. Did you install libdispatch with INSTALL_PRIVATE_HEADERS=ON? Alternatively, enable EMBEDDED_BLOCKS_RUNTIME to use the built-in blocks runtime.")
253-
endif ()
254-
unset(CMAKE_REQUIRED_LIBRARIES)
255-
else ()
256-
message(FATAL_ERROR "libBlocksRuntime not found. Enable EMBEDDED_BLOCKS_RUNTIME to use the built-in blocks runtime.")
252+
find_package(BlocksRuntime)
253+
if (NOT BlocksRuntime_FOUND)
254+
message(FATAL_ERROR "An external blocks runtime is required when not using the embedded one.")
257255
endif ()
258256
endif ()
259257

@@ -270,6 +268,22 @@ if (WIN32 AND NOT MINGW)
270268
target_link_libraries(objc PUBLIC ntdll.dll)
271269
endif()
272270

271+
if (NOT EMBEDDED_BLOCKS_RUNTIME)
272+
# Check if the blocks runtime exports _Block_use_RR2
273+
set(CMAKE_REQUIRED_LIBRARIES ${BlocksRuntime_LIBRARIES})
274+
set(CMAKE_REQUIRED_INCLUDES ${BlocksRuntime_INCLUDE_DIR})
275+
check_symbol_exists(_Block_use_RR2 "Block_private.h" HAVE_BLOCK_USE_RR2)
276+
unset(CMAKE_REQUIRED_LIBRARIES)
277+
unset(CMAKE_REQUIRED_INCLUDES)
278+
279+
if (NOT HAVE_BLOCK_USE_RR2)
280+
message(FATAL_ERROR "The external blocks runtime is not supported. It does not export _Block_use_RR2().")
281+
endif()
282+
283+
target_compile_definitions(objc PRIVATE HAVE_BLOCK_USE_RR2)
284+
target_link_libraries(objc PUBLIC BlocksRuntime::BlocksRuntime)
285+
endif()
286+
273287
target_link_libraries(objc PRIVATE tsl::robin_map)
274288

275289
set_target_properties(objc PROPERTIES
@@ -295,10 +309,6 @@ if (M_LIBRARY)
295309
target_link_libraries(objc PUBLIC ${M_LIBRARY})
296310
endif ()
297311

298-
if (BLOCKS_RUNTIME_LIBRARY)
299-
target_link_libraries(objc PUBLIC ${BLOCKS_RUNTIME_LIBRARY})
300-
endif ()
301-
302312
# Make weak symbols work on OS X
303313
if (APPLE)
304314
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS

NSBlocks.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
#include <assert.h>
88

99
#ifdef EMBEDDED_BLOCKS_RUNTIME
10-
#define BLOCK_STORAGE OBJC_PUBLIC
10+
# define BLOCK_STORAGE OBJC_PUBLIC
1111
#else
12-
#define BLOCK_STORAGE extern
13-
#endif
12+
# if defined(_WIN32)
13+
# define BLOCK_STORAGE __attribute__((dllimport))
14+
# else
15+
# define BLOCK_STORAGE extern
16+
# endif // defined(_WIN32)
17+
#endif // EMBEDDED_BLOCKS_RUNTIME
1418

1519
BLOCK_STORAGE struct objc_class _NSConcreteGlobalBlock;
1620
BLOCK_STORAGE struct objc_class _NSConcreteStackBlock;
@@ -76,4 +80,4 @@ PRIVATE void init_early_blocks(void)
7680
_NSConcreteMallocBlock.info = objc_class_flag_is_block;
7781
_NSConcreteAutoBlock.info = objc_class_flag_is_block;
7882
_NSConcreteFinalizingBlock.info = objc_class_flag_is_block;
79-
}
83+
}

Test/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ set(TESTS
1616
AllocatePair.m
1717
AssociatedObject.m
1818
AssociatedObject2.m
19-
BlockImpTest.m
2019
BlockTest_arc.m
2120
ConstantString.m
2221
Category.m
@@ -54,6 +53,10 @@ set(TESTS
5453
UnexpectedException.m
5554
)
5655

56+
if (EMBEDDED_BLOCKS_RUNTIME)
57+
list(APPEND TESTS BlockImpTest.m)
58+
endif()
59+
5760
set(ENABLE_ALL_OBJC_ARC_TESTS On)
5861

5962
if (WIN32)

cmake/FindBlocksRuntime.cmake

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#.rst:
2+
# FindBlocksRuntime
3+
# -----------------
4+
#
5+
# Find libBlocksRuntime library and headers.
6+
#
7+
# The module defines the following variables:
8+
#
9+
# ##
10+
#
11+
# BlocksRuntime_FOUND - true if libBlocksRuntime was found
12+
# BlocksRuntime_INCLUDE_DIR - include search path
13+
# BlocksRuntime_LIBRARIES - libraries to link
14+
15+
if(BlocksRuntime_INCLUDE_DIR AND BlocksRuntime_LIBRARIES)
16+
set(BlocksRuntime_FOUND TRUE)
17+
else()
18+
find_path(BlocksRuntime_INCLUDE_DIR
19+
NAMES
20+
Block.h Block_private.h)
21+
find_library(BlocksRuntime_LIBRARIES
22+
NAMES
23+
BlocksRuntime libBlocksRuntime)
24+
25+
include(FindPackageHandleStandardArgs)
26+
find_package_handle_standard_args(BlocksRuntime
27+
REQUIRED_VARS
28+
BlocksRuntime_LIBRARIES
29+
BlocksRuntime_INCLUDE_DIR)
30+
31+
mark_as_advanced(BlocksRuntime_LIBRARIES BlocksRuntime_INCLUDE_DIR)
32+
endif()
33+
34+
if(BlocksRuntime_FOUND)
35+
if(NOT TARGET BlocksRuntime::BlocksRuntime)
36+
add_library(BlocksRuntime::BlocksRuntime UNKNOWN IMPORTED)
37+
set_target_properties(BlocksRuntime::BlocksRuntime
38+
PROPERTIES
39+
IMPORTED_LOCATION
40+
${BlocksRuntime_LIBRARIES}
41+
INTERFACE_INCLUDE_DIRECTORIES
42+
${BlocksRuntime_INCLUDE_DIR})
43+
endif()
44+
endif()

loader.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ static void init_runtime(void)
5959
init_alias_table();
6060
init_early_blocks();
6161
init_arc();
62+
#if defined(EMBEDDED_BLOCKS_RUNTIME)
6263
init_trampolines();
64+
#endif
6365
init_builtin_classes();
6466
first_run = NO;
6567
if (getenv("LIBOBJC_MEMORY_PROFILE"))

0 commit comments

Comments
 (0)