1 //===-- XcodeSDK.cpp ------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/XcodeSDK.h"
10 #include "lldb/Utility/FileSpec.h"
12 #include "lldb/lldb-types.h"
14 #include "llvm/TargetParser/Triple.h"
19 using namespace lldb_private
;
21 static llvm::StringRef
GetName(XcodeSDK::Type type
) {
23 case XcodeSDK::MacOSX
:
25 case XcodeSDK::iPhoneSimulator
:
26 return "iPhoneSimulator";
27 case XcodeSDK::iPhoneOS
:
29 case XcodeSDK::AppleTVSimulator
:
30 return "AppleTVSimulator";
31 case XcodeSDK::AppleTVOS
:
33 case XcodeSDK::WatchSimulator
:
34 return "WatchSimulator";
35 case XcodeSDK::watchOS
:
37 case XcodeSDK::bridgeOS
:
41 case XcodeSDK::unknown
:
44 llvm_unreachable("Unhandled sdk type!");
47 XcodeSDK::XcodeSDK(XcodeSDK::Info info
) : m_name(GetName(info
.type
).str()) {
48 if (!m_name
.empty()) {
49 if (!info
.version
.empty())
50 m_name
+= info
.version
.getAsString();
52 m_name
+= ".Internal";
57 XcodeSDK
&XcodeSDK::operator=(const XcodeSDK
&other
) = default;
59 bool XcodeSDK::operator==(const XcodeSDK
&other
) const {
60 return m_name
== other
.m_name
;
63 static XcodeSDK::Type
ParseSDKName(llvm::StringRef
&name
) {
64 if (name
.consume_front("MacOSX"))
65 return XcodeSDK::MacOSX
;
66 if (name
.consume_front("iPhoneSimulator"))
67 return XcodeSDK::iPhoneSimulator
;
68 if (name
.consume_front("iPhoneOS"))
69 return XcodeSDK::iPhoneOS
;
70 if (name
.consume_front("AppleTVSimulator"))
71 return XcodeSDK::AppleTVSimulator
;
72 if (name
.consume_front("AppleTVOS"))
73 return XcodeSDK::AppleTVOS
;
74 if (name
.consume_front("WatchSimulator"))
75 return XcodeSDK::WatchSimulator
;
76 if (name
.consume_front("WatchOS"))
77 return XcodeSDK::watchOS
;
78 if (name
.consume_front("bridgeOS"))
79 return XcodeSDK::bridgeOS
;
80 if (name
.consume_front("Linux"))
81 return XcodeSDK::Linux
;
82 static_assert(XcodeSDK::Linux
== XcodeSDK::numSDKTypes
- 1,
83 "New SDK type was added, update this list!");
84 return XcodeSDK::unknown
;
87 static llvm::VersionTuple
ParseSDKVersion(llvm::StringRef
&name
) {
89 while (i
< name
.size() && name
[i
] >= '0' && name
[i
] <= '9')
91 if (i
== name
.size() || name
[i
++] != '.')
93 while (i
< name
.size() && name
[i
] >= '0' && name
[i
] <= '9')
95 if (i
== name
.size() || name
[i
++] != '.')
98 llvm::VersionTuple version
;
99 version
.tryParse(name
.slice(0, i
- 1));
100 name
= name
.drop_front(i
);
104 static bool ParseAppleInternalSDK(llvm::StringRef
&name
) {
105 return name
.consume_front("Internal.") || name
.consume_front(".Internal.");
108 XcodeSDK::Info
XcodeSDK::Parse() const {
110 llvm::StringRef
input(m_name
);
111 info
.type
= ParseSDKName(input
);
112 info
.version
= ParseSDKVersion(input
);
113 info
.internal
= ParseAppleInternalSDK(input
);
117 bool XcodeSDK::IsAppleInternalSDK() const {
118 llvm::StringRef
input(m_name
);
120 ParseSDKVersion(input
);
121 return ParseAppleInternalSDK(input
);
124 llvm::VersionTuple
XcodeSDK::GetVersion() const {
125 llvm::StringRef
input(m_name
);
127 return ParseSDKVersion(input
);
130 XcodeSDK::Type
XcodeSDK::GetType() const {
131 llvm::StringRef
input(m_name
);
132 return ParseSDKName(input
);
135 llvm::StringRef
XcodeSDK::GetString() const { return m_name
; }
137 bool XcodeSDK::Info::operator<(const Info
&other
) const {
138 return std::tie(type
, version
, internal
) <
139 std::tie(other
.type
, other
.version
, other
.internal
);
142 bool XcodeSDK::Info::operator==(const Info
&other
) const {
143 return std::tie(type
, version
, internal
) ==
144 std::tie(other
.type
, other
.version
, other
.internal
);
147 void XcodeSDK::Merge(const XcodeSDK
&other
) {
148 // The "bigger" SDK always wins.
150 auto r
= other
.Parse();
154 // The Internal flag always wins.
155 if (llvm::StringRef(m_name
).endswith(".sdk"))
156 if (!l
.internal
&& r
.internal
)
158 m_name
.substr(0, m_name
.size() - 3) + std::string("Internal.sdk");
162 std::string
XcodeSDK::GetCanonicalName(XcodeSDK::Info info
) {
168 case iPhoneSimulator
:
169 name
= "iphonesimulator";
174 case AppleTVSimulator
:
175 name
= "appletvsimulator";
181 name
= "watchsimulator";
195 if (!info
.version
.empty())
196 name
+= info
.version
.getAsString();
202 bool XcodeSDK::SDKSupportsModules(XcodeSDK::Type sdk_type
,
203 llvm::VersionTuple version
) {
206 return version
>= llvm::VersionTuple(10, 10);
208 case Type::iPhoneSimulator
:
209 case Type::AppleTVOS
:
210 case Type::AppleTVSimulator
:
211 return version
>= llvm::VersionTuple(8);
213 case Type::WatchSimulator
:
214 return version
>= llvm::VersionTuple(6);
222 bool XcodeSDK::SupportsSwift() const {
223 XcodeSDK::Info info
= Parse();
226 return info
.version
.empty() || info
.version
>= llvm::VersionTuple(10, 10);
228 case Type::iPhoneSimulator
:
229 return info
.version
.empty() || info
.version
>= llvm::VersionTuple(8);
230 case Type::AppleTVSimulator
:
231 case Type::AppleTVOS
:
232 return info
.version
.empty() || info
.version
>= llvm::VersionTuple(9);
233 case Type::WatchSimulator
:
235 return info
.version
.empty() || info
.version
>= llvm::VersionTuple(2);
243 bool XcodeSDK::SDKSupportsModules(XcodeSDK::Type desired_type
,
244 const FileSpec
&sdk_path
) {
245 ConstString last_path_component
= sdk_path
.GetFilename();
247 if (!last_path_component
)
250 XcodeSDK
sdk(last_path_component
.GetStringRef().str());
251 if (sdk
.GetType() != desired_type
)
253 return SDKSupportsModules(sdk
.GetType(), sdk
.GetVersion());
256 XcodeSDK::Type
XcodeSDK::GetSDKTypeForTriple(const llvm::Triple
&triple
) {
257 using namespace llvm
;
258 switch (triple
.getOS()) {
261 return XcodeSDK::MacOSX
;
263 switch (triple
.getEnvironment()) {
265 return XcodeSDK::MacOSX
;
266 case Triple::Simulator
:
267 return XcodeSDK::iPhoneSimulator
;
269 return XcodeSDK::iPhoneOS
;
272 if (triple
.getEnvironment() == Triple::Simulator
)
273 return XcodeSDK::AppleTVSimulator
;
274 return XcodeSDK::AppleTVOS
;
275 case Triple::WatchOS
:
276 if (triple
.getEnvironment() == Triple::Simulator
)
277 return XcodeSDK::WatchSimulator
;
278 return XcodeSDK::watchOS
;
280 return XcodeSDK::Linux
;
282 return XcodeSDK::unknown
;
286 std::string
XcodeSDK::FindXcodeContentsDirectoryInPath(llvm::StringRef path
) {
287 auto begin
= llvm::sys::path::begin(path
);
288 auto end
= llvm::sys::path::end(path
);
290 // Iterate over the path components until we find something that ends with
291 // .app. If the next component is Contents then we've found the Contents
293 for (auto it
= begin
; it
!= end
; ++it
) {
294 if (it
->endswith(".app")) {
296 if (++next
!= end
&& *next
== "Contents") {
297 llvm::SmallString
<128> buffer
;
298 llvm::sys::path::append(buffer
, begin
, ++next
,
299 llvm::sys::path::Style::posix
);
300 return buffer
.str().str();