Skip to content

Commit 6afa780

Browse files
committed
Add Finddispatch module
Implementing an initial `Finddispatch.cmake` module to find Dispatch from the system SDK on Apple platforms. Still should implement a basic mechanism for finding it on Windows and Linux. Note that those platforms will likely use the dispatch config mechanism most frequently, but this should still work as a fallback. This mechanism is the only one that works for Apple platforms though as those use libdispatch from the SDK rather than building it with corelibs-libdispatch. When building for Linux and Windows, it is possible to build a custom corelibs-libdispatch runtime with `dispatch_DIR` and avoid this module. This module makes it possible to use a pre-built corelibs-libdispatch when one is available without needing to build it. The module uses the `Swift_SDKROOT` filepath as a hint on where to look for the runtime libraries and the `dispatch_STATIC` boolean to specify whether it should find the dynamic library or the static archive.
1 parent 7484f01 commit 6afa780

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#[=======================================================================[.rst:
2+
Finddispatch
3+
------------
4+
5+
Find libdispatch, deferring to dispatchConfig.cmake when requested.
6+
This is meant to find the C implementation of libdispatch for use as the
7+
executor for Swift concurrency. This library is not intended for use by
8+
third-party program on Linux or Windows.
9+
10+
Imported Targets
11+
^^^^^^^^^^^^^^^^
12+
13+
The following :prop_tgt:`IMPORTED` TARGETS may be defined:
14+
15+
``dispatch``
16+
17+
Hint Variables
18+
^^^^^^^^^^^^^^
19+
20+
``swift_SDKROOT``
21+
Set the path to the Swift SDK installation.
22+
This only affects Linux and Windows builds.
23+
Apple builds always use the library provided by the SDK.
24+
25+
``dispatch_STATIC``
26+
Look for the libdispatch static archive instead of the dynamic library.
27+
This only affects Linux. Apple builds always use the
28+
dynamic library provided by the SDK. Windows does not currently have a static
29+
libdispatch implementation.
30+
31+
Result Variables
32+
^^^^^^^^^^^^^^^^
33+
34+
The module may set the following variables if `dispatch_DIR` is not set.
35+
36+
``dispatch_FOUND``
37+
true if dispatch headers and libraries were found
38+
39+
``dispatch_INCLUDE_DIR``
40+
the directory containing the libdispatch headers
41+
42+
``dispatch_LIBRARIES`` OR ``dispatch_IMPLIB``
43+
the libraries to be linked
44+
45+
#]=======================================================================]
46+
47+
# If the dispatch_DIR is specified, look there instead. The cmake-generated
48+
# config file is more accurate, but requires that the SDK has one available.
49+
if(dispatch_DIR)
50+
if(dispatch_FIND_REQUIRED)
51+
list(APPEND args REQUIRED)
52+
endif()
53+
if(dispatch_FIND_QUIETLY)
54+
list(APPEND args QUIET)
55+
endif()
56+
find_package(dispatch NO_MODULE ${args})
57+
return()
58+
endif()
59+
60+
include(FindPackageHandleStandardArgs)
61+
62+
if(APPLE)
63+
# When building for Apple platforms, libdispatch always comes from within the
64+
# SDK as a tbd for a shared library in the shared cache.
65+
find_path(dispatch_INCLUDE_DIR "dispatch/dispatch.h")
66+
find_library(dispatch_IMPLIB
67+
NAMES "libdispatch.tbd"
68+
PATH "usr/lib"
69+
PATH_SUFFIXES system)
70+
add_library(dispatch SHARED IMPORTED GLOBAL)
71+
set_target_properties(dispatch PROPERTIES
72+
IMPORTED_IMPLIB "${dispatch_IMPLIB}"
73+
INTERFACE_INCLUDE_DIRECTORIES "${dispatch_INCLUDE_DIR}")
74+
find_package_handle_standard_args(dispatch DEFAULT_MSG
75+
dispatch_IMPLIB dispatch_INCLUDE_DIR)
76+
elseif(LINUX)
77+
if(dispatch_STATIC)
78+
find_path(dispatch_INCLUDE_DIR
79+
"dispatch/dispatch.h"
80+
HINTS "${Swift_SDKROOT}/usr/lib/swift_static")
81+
find_library(dispatch_LIBRARY
82+
NAMES "libdispatch.a"
83+
HINTS "${Swift_SDKROOT}/usr/lib/swift_static/linux")
84+
add_library(dispatch STATIC IMPORTED GLOBAL)
85+
else()
86+
find_path(dispatch_INCLUDE_DIR
87+
"dispatch/dispatch.h"
88+
HINTS "${Swift_SDKROOT}/usr/lib/swift")
89+
find_library(dispatch_LIBRARY
90+
NAMES "libdispatch.so"
91+
HINTS "${Swift_SDKROOT}/usr/lib/swift/linux")
92+
add_library(dispatch SHARED IMPORTED GLOBAL)
93+
endif()
94+
set_target_properties(dispatch
95+
IMPORTED_LOCATION "${dispatch_LIBRARY}"
96+
INTERFACE_INCLUDE_DIRECTORIES "${dispatch_INCLUDE_DIR}")
97+
find_package_handle_standard_args(dispatch DEFAULT_MSG
98+
dispatch_LIBRARY dispatch_INCLUDE_DIR)
99+
elseif(WIN32)
100+
find_path(dispatch_INCLUDE_DIR
101+
"dispatch/dispatch.h"
102+
HINTS
103+
"${Swift_SDKROOT}/usr/include"
104+
"$ENV{SDKROOT}/usr/include")
105+
find_library(dispatch_LIBRARY
106+
NAMES "dispatch.lib"
107+
HINTS
108+
"${Swift_SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
109+
"${Swift_SDKROOT}/usr/lib/swift"
110+
"$ENV{SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
111+
"$ENV{SDKROOT}/usr/lib/swift")
112+
113+
add_library(dispatch SHARED IMPORTED GLOBAL)
114+
set_target_properties(dispatch
115+
IMPORTED_IMPLIB "${dispatch_LIBRARY}"
116+
INTERFACE_INCLUDE_DIRECTORIES "${dispatch_INCLUDE_DIR}")
117+
find_package_handle_standard_args(dispatch DEFAULT_MSG
118+
dispatch_IMPLIB dispatch_INCLUDE_DIR)
119+
else()
120+
message(FATAL_ERROR "Finddispatch.cmake module search not implemented for targeted platform\n"
121+
" Build corelibs libdispatch for your platform and set `dispatch_DIR` to"
122+
" the directory containing dispatchConfig.cmake\n")
123+
endif()

0 commit comments

Comments
 (0)