Skip to content

Commit e2678bf

Browse files
committed
build: support system installed BlocksRuntime
This uses the standard CMake mechanism for finding the blocks runtime. If one is not found, one will be provided for you.
1 parent 44f67b2 commit e2678bf

File tree

5 files changed

+72
-30
lines changed

5 files changed

+72
-30
lines changed

CMakeLists.txt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ include(DispatchUtilities)
3434

3535
set(SWIFT_LIBDIR "lib" CACHE PATH "Library folder name, defined by swift main buildscript")
3636
set(INSTALL_LIBDIR "${SWIFT_LIBDIR}" CACHE PATH "Path where the libraries should be installed")
37-
set(WITH_BLOCKS_RUNTIME "" CACHE PATH "Path to blocks runtime")
3837

3938
include(DispatchAppleOptions)
4039
include(DispatchSanitization)
@@ -133,14 +132,14 @@ endif()
133132

134133
option(INSTALL_PRIVATE_HEADERS "installs private headers in the same location as the public ones" OFF)
135134

136-
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
137-
CMAKE_SYSTEM_NAME STREQUAL Android OR
138-
CMAKE_SYSTEM_NAME STREQUAL FreeBSD OR
139-
CMAKE_SYSTEM_NAME STREQUAL Windows)
135+
find_package(BlocksRuntime QUIET)
136+
if(NOT BlocksRuntime_FOUND)
137+
set(BlocksRuntime_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/src/BlocksRuntime)
138+
140139
add_library(BlocksRuntime
141140
STATIC
142141
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/data.c
143-
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/runtime.c)
142+
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/runtime.c)
144143
set_target_properties(BlocksRuntime
145144
PROPERTIES
146145
POSITION_INDEPENDENT_CODE TRUE)
@@ -149,8 +148,9 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
149148
PROPERTIES
150149
INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS})
151150
endif()
152-
set(WITH_BLOCKS_RUNTIME "${CMAKE_SOURCE_DIR}/src/BlocksRuntime" CACHE PATH "Path to blocks runtime" FORCE)
153-
151+
152+
add_library(BlocksRuntime::BlocksRuntime ALIAS BlocksRuntime)
153+
154154
install(FILES
155155
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/Block.h
156156
DESTINATION
@@ -161,9 +161,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
161161
DESTINATION
162162
"${INSTALL_BLOCK_HEADERS_DIR}")
163163
endif()
164-
else()
165-
# TODO(compnerd) support system installed BlocksRuntime
166-
# find_package(BlocksRuntime REQUIRED)
167164
endif()
168165

169166
check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)

INSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Typical configuration line for FreeBSD 8.x and 9.x to build libdispatch with
130130
clang and blocks support:
131131
132132
```
133-
cmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DWITH_BLOCKS_RUNTIME=/usr/local/lib <path-to-source>
133+
cmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DBlocksRuntime_INCLUDE_DIR=/usr/local/include -DBlocksRuntime_LIBRARIES=/usr/local/lib/libBlocksRuntime.so <path-to-source>
134134
ninja
135135
ninja test
136136
```

cmake/modules/FindBlocksRuntime.cmake

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
Blocks.h
21+
HINTS
22+
${CMAKE_INSTALL_FULL_INCLUDEDIR})
23+
find_library(BlocksRuntime_LIBRARIES
24+
NAMES
25+
BlocksRuntime libBlocksRuntime
26+
HINTS
27+
${CMAKE_INSTALL_FULL_LIBDIR})
28+
29+
include(FindPackageHandleStandardArgs)
30+
find_package_handle_standard_args(BlocksRuntime
31+
REQUIRED_VARS
32+
BlocksRuntime_LIBRARIES
33+
BlocksRuntime_INCLUDE_DIR)
34+
35+
mark_as_advanced(BlocksRuntime_LIBRARIES BlocksRuntime_INCLUDE_DIR)
36+
endif()
37+
38+
if(BlocksRuntime_FOUND)
39+
if(NOT TARGET BlocksRuntime::BlocksRuntime)
40+
add_library(BlocksRuntime::BlocksRuntime UNKNOWN IMPORTED)
41+
set_target_properties(BlocksRuntime::BlocksRuntime
42+
PROPERTIES
43+
IMPORTED_LOCATION
44+
${BlocksRuntime_LIBRARIES}
45+
INTERFACE_INCLUDE_DIRECTORIES
46+
${BlocksRuntime_INCLUDE_DIR})
47+
endif()
48+
endif()

src/CMakeLists.txt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,9 @@ target_include_directories(dispatch
134134
${CMAKE_CURRENT_SOURCE_DIR}
135135
${CMAKE_CURRENT_BINARY_DIR}
136136
${CMAKE_SOURCE_DIR}/private)
137-
if(WITH_BLOCKS_RUNTIME)
138-
target_include_directories(dispatch
139-
SYSTEM BEFORE PRIVATE
140-
"${WITH_BLOCKS_RUNTIME}")
141-
endif()
137+
target_include_directories(dispatch
138+
SYSTEM BEFORE PRIVATE
139+
"${BlocksRuntime_INCLUDE_DIR}")
142140
if(WIN32)
143141
target_compile_definitions(dispatch
144142
PRIVATE
@@ -190,10 +188,10 @@ endif()
190188
if(BSD_OVERLAY_FOUND)
191189
target_link_libraries(dispatch PRIVATE ${BSD_OVERLAY_LDFLAGS})
192190
endif()
193-
target_link_libraries(dispatch PRIVATE Threads::Threads)
194-
if(WITH_BLOCKS_RUNTIME)
195-
target_link_libraries(dispatch PRIVATE BlocksRuntime)
196-
endif()
191+
target_link_libraries(dispatch
192+
PRIVATE
193+
Threads::Threads
194+
BlocksRuntime::BlocksRuntime)
197195
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
198196
target_link_libraries(dispatch
199197
PRIVATE

tests/CMakeLists.txt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,9 @@ function(add_unit_test name)
7272
target_compile_options(${name} PRIVATE -DLENIENT_DEADLINES=1)
7373
target_link_libraries(${name} PRIVATE swiftCore swiftSwiftOnoneSupport)
7474
endif()
75-
if(WITH_BLOCKS_RUNTIME)
76-
target_include_directories(${name}
77-
SYSTEM BEFORE PRIVATE
78-
"${WITH_BLOCKS_RUNTIME}")
79-
endif()
75+
target_include_directories(${name}
76+
SYSTEM BEFORE PRIVATE
77+
"${BlocksRuntime_INCLUDE_DIR}")
8078
if(BSD_OVERLAY_FOUND AND NOT AUT_NO_BSD_OVERLAY)
8179
target_compile_options(${name}
8280
PRIVATE
@@ -86,10 +84,11 @@ function(add_unit_test name)
8684
# TODO(compnerd) make this portable
8785
target_compile_options(${name} PRIVATE -Wall -Wno-deprecated-declarations)
8886
dispatch_set_linker(${name})
89-
target_link_libraries(${name} PRIVATE dispatch Threads::Threads)
90-
if(WITH_BLOCKS_RUNTIME)
91-
target_link_libraries(${name} PRIVATE BlocksRuntime)
92-
endif()
87+
target_link_libraries(${name}
88+
PRIVATE
89+
dispatch
90+
Threads::Threads
91+
BlocksRuntime::BlocksRuntime)
9392
if(BSD_OVERLAY_FOUND AND NOT AUT_NO_BSD_OVERLAY)
9493
target_link_libraries(${name}
9594
PRIVATE

0 commit comments

Comments
 (0)