Skip to content

Commit f639c4b

Browse files
justice-adams-appleCatfish-Man
authored andcommitted
[cmake] add FindSwiftCore module for supplemental libraries (#81215)
Add FindSwiftCore to Supplemental cmake modules so we can link against libswiftCore in a given end users SDK if `SwiftCore_DIR ` is not provided
1 parent c3c03bb commit f639c4b

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#[=======================================================================[.rst:
2+
FindSwiftCore
3+
------------
4+
5+
Find SwiftCore, deferring to SwiftCoreConfig.cmake when requested.
6+
This is meant to find the core library 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+
``SwiftCore``
14+
15+
Hint Variables
16+
^^^^^^^^^^^^^^
17+
18+
``SDKROOT`` (environment variable)
19+
Set the path to the Swift SDK Root.
20+
This only affects Windows builds.
21+
22+
``Swift_SDKROOT``
23+
Set the path to the Swift SDK installation.
24+
This only affects Linux and Windows builds.
25+
Apple builds always use the library provided by the SDK.
26+
27+
Result Variables
28+
^^^^^^^^^^^^^^^^
29+
30+
The module may set the following variables if `SwiftCore_DIR` is not set.
31+
32+
``SwiftCore_FOUND``
33+
true if core was found
34+
35+
``SwiftCore_INCLUDE_DIR``
36+
the directory containing the Swift.swiftmodule folder
37+
38+
#]=======================================================================]
39+
40+
# If the SwiftCore_DIR_FLAG is specified, look there instead. The cmake-generated
41+
# config file is more accurate, but requires that the SDK has one available.
42+
if(SwiftCore_DIR)
43+
if(SwiftCore_FIND_REQUIRED)
44+
list(APPEND args REQUIRED)
45+
endif()
46+
if(SwiftCore_FIND_QUIETLY)
47+
list(APPEND args QUIET)
48+
endif()
49+
find_package(SwiftCore NO_MODULE ${args})
50+
return()
51+
endif()
52+
53+
include(FindPackageHandleStandardArgs)
54+
55+
if(APPLE)
56+
# When building for Apple platforms, SwiftCore always comes from within the
57+
# SDK as a tbd for a shared library in the shared cache.
58+
find_path(SwiftCore_INCLUDE_DIR
59+
"Swift.swiftmodule"
60+
HINTS
61+
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
62+
find_library(SwiftCore_IMPLIB
63+
NAMES "libswiftCore.tbd"
64+
HINTS
65+
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
66+
add_library(SwiftCore SHARED IMPORTED GLOBAL)
67+
set_target_properties(SwiftCore PROPERTIES
68+
IMPORTED_IMPLIB "${SwiftCore_IMPLIB}"
69+
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
70+
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
71+
SwiftCore_IMPLIB SwiftCore_INCLUDE_DIR)
72+
elseif(LINUX)
73+
if (NOT BUILD_SHARED_LIBS)
74+
find_path(SwiftCore_INCLUDE_DIR
75+
"Swift.swiftmodule"
76+
HINTS
77+
"${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
78+
find_library(SwiftCore_LIBRARY
79+
NAMES "libswiftCore.a"
80+
HINTS "${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
81+
add_library(SwiftCore STATIC IMPORTED GLOBAL)
82+
else()
83+
find_path(SwiftCore_INCLUDE_DIR
84+
"Swift.swiftmodule"
85+
HINTS
86+
"${Swift_SDKROOT}/usr/lib/swift/linux")
87+
find_library(SwiftCore_LIBRARY
88+
NAMES "libswiftCore.so"
89+
HINTS "${Swift_SDKROOT}/usr/lib/swift/linux")
90+
add_library(SwiftCore SHARED IMPORTED GLOBAL)
91+
endif()
92+
set_target_properties(SwiftCore PROPERTIES
93+
IMPORTED_LOCATION "${SwiftCore_LIBRARY}"
94+
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
95+
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
96+
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
97+
elseif(WIN32)
98+
find_path(SwiftCore_INCLUDE_DIR
99+
"Swift.swiftmodule"
100+
HINTS
101+
"${Swift_SDKROOT}/usr/lib/swift/windows"
102+
"$ENV{SDKROOT}/usr/lib/swift/windows")
103+
find_library(SwiftCore_LIBRARY
104+
NAMES "libswiftCore.lib"
105+
HINTS
106+
"${Swift_SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
107+
"${Swift_SDKROOT}/usr/lib/swift"
108+
"$ENV{SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
109+
"$ENV{SDKROOT}/usr/lib/swift")
110+
111+
add_library(SwiftCore SHARED IMPORTED GLOBAL)
112+
set_target_properties(SwiftCore PROPERTIES
113+
IMPORTED_IMPLIB "${SwiftCore_LIBRARY}"
114+
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
115+
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
116+
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
117+
else()
118+
message(FATAL_ERROR "FindSwiftCore.cmake module search not implemented for targeted platform\n"
119+
" Build Core for your platform and set `SwiftCore_DIR` to"
120+
" the directory containing SwiftCoreConfig.cmake\n")
121+
endif()

0 commit comments

Comments
 (0)