Skip to content

Commit 085630a

Browse files
[cmake] add FindSwiftCore module for supplemental libraries
1 parent c8f1de2 commit 085630a

File tree

1 file changed

+107
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)