Skip to content

Commit 0cb933a

Browse files
[cmake] add FindSwiftPlatformOverlay module
1 parent 529cc3c commit 0cb933a

File tree

1 file changed

+240
-0
lines changed

1 file changed

+240
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
#[=======================================================================[.rst:
2+
FindSwiftOverlay
3+
------------
4+
5+
Find SwiftOverlay, deferring to the associated <Overlay>Config.cmake when requested.
6+
This is meant to find the platform overlays to be linked by the Supplemental libraries.
7+
8+
Imported Targets
9+
^^^^^^^^^^^^^^^^
10+
11+
The following :prop_tgt:`IMPORTED` TARGETS may be defined:
12+
13+
``SwiftOverlay``
14+
15+
Hint Variables
16+
^^^^^^^^^^^^^^
17+
18+
``SDKROOT`` (environment variable)
19+
Set the path to the Swift SDK Root.
20+
This only affects Windows and Android builds.
21+
22+
``Swift_SDKROOT``
23+
Set the path to the Swift SDK installation.
24+
This affects Linux, Android, and Windows builds.
25+
Apple builds always use the overlay provided by the SDK.
26+
27+
Result Variables
28+
^^^^^^^^^^^^^^^^
29+
30+
The module may set the following variables if `swiftDarwin_DIR` is not set and targeting the swiftDarwin overlay.
31+
32+
``swiftDarwin_DIR_FOUND``
33+
true if the overlay was found
34+
35+
``swiftDarwin_INCLUDE_DIR``
36+
the directory containing the corresponding overlay swift module folder
37+
38+
The module may set the following variables if `swiftGlibc_DIR` is not set and targeting the GlibC overlay.
39+
40+
``swiftGlibc_DIR_FOUND``
41+
true if the overlay was found
42+
43+
``swiftGlibc_INCLUDE_DIR``
44+
the directory containing the corresponding overlay swift module folder
45+
46+
The module may set the following variables if `swiftWinSDK_DIR` is not set and targeting the WinSDK overlay.
47+
48+
``swiftWinSDK_DIR_FOUND``
49+
true if the overlay was found
50+
51+
``swiftWinSDK_INCLUDE_DIR``
52+
the directory containing the corresponding overlay swift module folder
53+
54+
The module may set the following variables if `swiftCRT_DIR` is not set and targeting the CRT overlay.
55+
56+
``swiftCRT_DIR_FOUND``
57+
true if the overlay was found
58+
59+
``swiftCRT_INCLUDE_DIR``
60+
the directory containing the corresponding overlay swift module folder
61+
62+
The module may set the following variables if `swiftAndroid_DIR` is not set and targeting the Android overlay.
63+
64+
``swiftAndroid_DIR_FOUND``
65+
true if the overlay was found
66+
67+
``swiftAndroid_INCLUDE_DIR``
68+
the directory containing the corresponding overlay swift module folder
69+
70+
71+
#]=======================================================================]
72+
73+
include_guard(GLOBAL)
74+
75+
# This was loosely modelled after other find modules
76+
# (namely FindGLEW), where the equivalent parameter
77+
# is not stored in cache (possibly because we want
78+
# the project importing it to be able to
79+
# it "immediately")
80+
if(NOT DEFINED SwiftOverlay_USE_STATIC_LIBS)
81+
set(SwiftOverlay_USE_STATIC_LIBS OFF)
82+
if(NOT BUILD_SHARED_LIBS AND NOT APPLE)
83+
set(SwiftOverlay_USE_STATIC_LIBS ON)
84+
endif()
85+
endif()
86+
87+
if(SwiftOverlay_DIR)
88+
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
89+
list(APPEND args REQUIRED)
90+
endif()
91+
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
92+
list(APPEND args QUIET)
93+
endif()
94+
find_package(SwiftOverlay CONFIG ${args})
95+
return()
96+
endif()
97+
98+
include(FindPackageHandleStandardArgs)
99+
include(PlatformInfo)
100+
101+
if(APPLE)
102+
list(APPEND OVERLAY_TARGET_NAMES "swiftDarwin")
103+
# Look in the SDK
104+
list(APPEND swiftDarwin_INCLUDE_DIR_HINTS
105+
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
106+
list(APPEND swiftDarwin_LIBRARY_HINTS
107+
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
108+
# When building for Apple platforms, swiftDarwin always comes from within the
109+
# SDK as a tbd for a shared library in the shared cache.
110+
list(APPEND swiftDarwin_NAMES libswiftDarwin.tbd)
111+
set(swiftDarwin_MODULE_NAME "Darwin.swiftmodule")
112+
elseif(LINUX)
113+
#ToDo(swiftlang/swift/issues/83014): Handle the static MUSL SDK case
114+
list(APPEND OVERLAY_TARGET_NAMES "swiftGlibc")
115+
116+
# Look in the SDK
117+
if (SwiftOverlay_USE_STATIC_LIBS)
118+
list(APPEND swiftGlibc_INCLUDE_DIR_HINTS
119+
"${Swift_SDKROOT}/usr/lib/swift_static/linux")
120+
list(APPEND swiftGlibc_LIBRARY_HINTS
121+
"${Swift_SDKROOT}/usr/lib/swift_static/linux")
122+
list(APPEND swiftGlibc_NAMES libswiftGlibc.a)
123+
else()
124+
list(APPEND swiftGlibc_INCLUDE_DIR_HINTS
125+
"${Swift_SDKROOT}/usr/lib/swift/linux/")
126+
list(APPEND swiftGlibc_LIBRARY_HINTS
127+
"${Swift_SDKROOT}/usr/lib/swift/linux")
128+
list(APPEND swiftGlibc_NAMES libswiftGlibc.so)
129+
endif()
130+
set(swiftGlibc_MODULE_NAME "Glibc.swiftmodule")
131+
elseif(WIN32)
132+
list(APPEND OVERLAY_TARGET_NAMES "swiftWinSDK")
133+
list(APPEND OVERLAY_TARGET_NAMES "swiftCRT")
134+
135+
list(APPEND swiftWinSDK_INCLUDE_DIR_HINTS
136+
"${Swift_SDKROOT}/usr/lib/swift/windows"
137+
"$ENV{SDKROOT}/usr/lib/swift/windows")
138+
list(APPEND swiftWinSDK_LIBRARY_HINTS
139+
"${Swift_SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}"
140+
"${Swift_SDKROOT}/usr/lib/swift"
141+
"$ENV{SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}"
142+
"$ENV{SDKROOT}/usr/lib/swift")
143+
144+
if(SwiftOverlay_USE_STATIC_LIBS)
145+
list(APPEND swiftCRT_NAMES libswiftWinSDK.lib)
146+
else()
147+
list(APPEND swiftCRT_NAMES swiftWinSDK.lib)
148+
endif()
149+
list(APPEND swiftWinSDK_MODULE_NAME "WinSDK.swiftmodule")
150+
151+
list(APPEND swiftCRT_INCLUDE_DIR_HINTS
152+
"${Swift_SDKROOT}/usr/lib/swift/windows"
153+
"$ENV{SDKROOT}/usr/lib/swift/windows")
154+
list(APPEND swiftCRT_LIBRARY_HINTS
155+
"${Swift_SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}"
156+
"${Swift_SDKROOT}/usr/lib/swift"
157+
"$ENV{SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}"
158+
"$ENV{SDKROOT}/usr/lib/swift")
159+
160+
if(SwiftOverlay_USE_STATIC_LIBS)
161+
list(APPEND swiftCRT_NAMES libswiftCRT.lib)
162+
else()
163+
list(APPEND swiftCRT_NAMES swiftCRT.lib)
164+
endif()
165+
set(swiftCRT_MODULE_NAME "CRT.swiftmodule")
166+
elseif(ANDROID)
167+
list(APPEND OVERLAY_TARGET_NAMES "swiftAndroid")
168+
169+
if (SwiftOverlay_USE_STATIC_LIBS)
170+
list(APPEND swiftAndroid_INCLUDE_DIR_HINTS
171+
"${Swift_SDKROOT}/usr/lib/swift_static/android"
172+
"$ENV{SDKROOT}/usr/lib/swift_static/android")
173+
list(APPEND swiftAndroid_LIBRARY_HINTS
174+
"${Swift_SDKROOT}/usr/lib/swift_static/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
175+
"${Swift_SDKROOT}/usr/lib/swift_static"
176+
"$ENV{SDKROOT}/usr/lib/swift_static/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
177+
"$ENV{SDKROOT}/usr/lib/swift_static")
178+
list(APPEND swiftAndroid_NAMES libswiftAndroid.a)
179+
else()
180+
list(APPEND swiftAndroid_INCLUDE_DIR_HINTS
181+
"${Swift_SDKROOT}/usr/lib/swift/android"
182+
"$ENV{SDKROOT}/usr/lib/swift/android")
183+
list(APPEND swiftAndroid_LIBRARY_HINTS
184+
"${Swift_SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
185+
"${Swift_SDKROOT}/usr/lib/swift"
186+
"$ENV{SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
187+
"$ENV{SDKROOT}/usr/lib/swift")
188+
list(APPEND swiftAndroid_NAMES libswiftAndroid.so)
189+
endif()
190+
set(swiftAndroid_MODULE_NAME "Android.swiftmodule")
191+
else()
192+
message(FATAL_ERROR "FindSwiftOverlay.cmake module search not implemented for targeted platform\n"
193+
" Build the Overlays for your platform and set the appropriate `<OverlayTarget>_DIR` variable to"
194+
" the directory containing <OverlayTarget>Config.cmake\n")
195+
endif()
196+
197+
# Setup SwiftOverlay interface library and link it against the explicit
198+
# overlay targets
199+
add_library(SwiftOverlay INTERFACE)
200+
201+
foreach(OVERLAY_TARGET ${OVERLAY_TARGET_NAMES})
202+
find_path(${OVERLAY_TARGET}_INCLUDE_DIR
203+
${${OVERLAY_TARGET}_MODULE_NAME}
204+
NO_CMAKE_FIND_ROOT_PATH
205+
HINTS
206+
${${OVERLAY_TARGET}_INCLUDE_DIR_HINTS})
207+
find_library(${OVERLAY_TARGET}_LIBRARY
208+
NAMES
209+
${${OVERLAY_TARGET}_NAMES}
210+
NO_CMAKE_FIND_ROOT_PATH
211+
HINTS
212+
${${OVERLAY_TARGET}_LIBRARY_HINTS})
213+
214+
if(SwiftOverlay_USE_STATIC_LIBS)
215+
add_library(${OVERLAY_TARGET} STATIC IMPORTED GLOBAL)
216+
else()
217+
add_library(${OVERLAY_TARGET} SHARED IMPORTED GLOBAL)
218+
endif()
219+
220+
target_include_directories(${OVERLAY_TARGET} INTERFACE
221+
"${${OVERLAY_TARGET}_INCLUDE_DIR}")
222+
223+
if(LINUX OR ANDROID)
224+
set_target_properties(${OVERLAY_TARGET} PROPERTIES
225+
IMPORTED_LOCATION "${${OVERLAY_TARGET}_LIBRARY}")
226+
else()
227+
set_target_properties(${OVERLAY_TARGET} PROPERTIES
228+
IMPORTED_IMPLIB "${${OVERLAY_TARGET}_LIBRARY}")
229+
endif()
230+
231+
target_link_libraries(SwiftOverlay INTERFACE
232+
${OVERLAY_TARGET})
233+
endforeach()
234+
235+
foreach(OVERLAY_TARGET ${OVERLAY_TARGET_NAMES})
236+
list(APPEND vars_to_check "${OVERLAY_TARGET}_LIBRARY" "${OVERLAY_TARGET}_INCLUDE_DIR")
237+
endforeach()
238+
239+
find_package_handle_standard_args(SwiftOverlay DEFAULT_MSG
240+
${vars_to_check})

0 commit comments

Comments
 (0)