Skip to content

Commit 6b5befc

Browse files
[cmake] add FindSwiftCore module for supplemental libraries
Addresses rdar://150396923
1 parent c8f1de2 commit 6b5befc

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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``
19+
Set the path to the Swift SDK Root.
20+
This only affects MacOS 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_LIBRARIES`` OR ``SwiftCore_IMPLIB``
36+
the libraries to be linked
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/arm64e-apple-macos.swiftinterface"
60+
"Swift.swiftmodule/x86_64-apple-macos.swiftinterface"
61+
HINTS
62+
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
63+
find_library(SwiftCore_IMPLIB
64+
NAMES "libswiftCore.tbd"
65+
HINTS
66+
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
67+
add_library(SwiftCore SHARED IMPORTED GLOBAL)
68+
set_target_properties(SwiftCore PROPERTIES
69+
IMPORTED_IMPLIB "${SwiftCore_IMPLIB}")
70+
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
71+
SwiftCore_IMPLIB SwiftCore_INCLUDE_DIR)
72+
elseif(LINUX)
73+
if(SwiftCore_STATIC)
74+
set(SwiftCore_INCLUDE_DIR "${Swift_SDKROOT}/usr/lib/swift_static/linux-static/Swift.swiftmodule" CACHE STRING "")
75+
find_library(SwiftCore_LIBRARY
76+
NAMES "libswiftCore.a"
77+
HINTS "${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
78+
add_library(SwiftCore STATIC IMPORTED GLOBAL)
79+
else()
80+
set(SwiftCore_INCLUDE_DIR "${Swift_SDKROOT}/usr/lib/swift/linux/Swift.swiftmodule" CACHE STRING "")
81+
find_library(SwiftCore_LIBRARY
82+
NAMES "libswiftCore.so"
83+
HINTS "${Swift_SDKROOT}/usr/lib/swift/linux")
84+
add_library(SwiftCore SHARED IMPORTED GLOBAL)
85+
endif()
86+
set_target_properties(SwiftCore PROPERTIES
87+
IMPORTED_LOCATION "${SwiftCore_LIBRARY}"
88+
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
89+
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
90+
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
91+
elseif(WIN32)
92+
set(SwiftCore_INCLUDE_DIR "${Swift_SDKROOT}/usr/lib/swift_static/linux-static/Swift.swiftmodule" CACHE STRING "")
93+
find_library(SwiftCore_LIBRARY
94+
NAMES "libswiftCore.lib"
95+
HINTS
96+
"${Swift_SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
97+
"${Swift_SDKROOT}/usr/lib/swift"
98+
"$ENV{SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
99+
"$ENV{SDKROOT}/usr/lib/swift")
100+
101+
add_library(SwiftCore SHARED IMPORTED GLOBAL)
102+
set_target_properties(SwiftCore PROPERTIES
103+
IMPORTED_LOCATION "${SwiftCore_LIBRARY}"
104+
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
105+
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
106+
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
107+
else()
108+
message(FATAL_ERROR "FindSwiftCore.cmake module search not implemented for targeted platform\n"
109+
" Build Core for your platform and set `SwiftCore_DIR` to"
110+
" the directory containing SwiftCoreConfig.cmake\n")
111+
endif()

0 commit comments

Comments
 (0)