1//===-- XcodeSDKTest.cpp --------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "gtest/gtest.h"
10
11#include "lldb/Utility/FileSpec.h"
12#include "lldb/Utility/XcodeSDK.h"
13
14#include "llvm/ADT/StringRef.h"
15#include "llvm/TargetParser/Triple.h"
16
17#include <tuple>
18
19using namespace lldb_private;
20
21TEST(XcodeSDKTest, ParseTest) {
22 EXPECT_EQ(XcodeSDK::GetAnyMacOS().GetType(), XcodeSDK::MacOSX);
23 EXPECT_EQ(XcodeSDK("MacOSX.sdk").GetType(), XcodeSDK::MacOSX);
24 EXPECT_EQ(XcodeSDK("iPhoneSimulator.sdk").GetType(), XcodeSDK::iPhoneSimulator);
25 EXPECT_EQ(XcodeSDK("iPhoneOS.sdk").GetType(), XcodeSDK::iPhoneOS);
26 EXPECT_EQ(XcodeSDK("AppleTVSimulator.sdk").GetType(), XcodeSDK::AppleTVSimulator);
27 EXPECT_EQ(XcodeSDK("AppleTVOS.sdk").GetType(), XcodeSDK::AppleTVOS);
28 EXPECT_EQ(XcodeSDK("WatchSimulator.sdk").GetType(), XcodeSDK::WatchSimulator);
29 EXPECT_EQ(XcodeSDK("WatchOS.sdk").GetType(), XcodeSDK::watchOS);
30 EXPECT_EQ(XcodeSDK("XRSimulator.sdk").GetType(), XcodeSDK::XRSimulator);
31 EXPECT_EQ(XcodeSDK("XROS.sdk").GetType(), XcodeSDK::XROS);
32 EXPECT_EQ(XcodeSDK("Linux.sdk").GetType(), XcodeSDK::Linux);
33 EXPECT_EQ(XcodeSDK("MacOSX.sdk").GetVersion(), llvm::VersionTuple());
34 EXPECT_EQ(XcodeSDK("MacOSX10.9.sdk").GetVersion(), llvm::VersionTuple(10, 9));
35 EXPECT_EQ(XcodeSDK("MacOSX10.15.4.sdk").GetVersion(), llvm::VersionTuple(10, 15));
36 EXPECT_EQ(XcodeSDK("MacOSX.sdk").IsAppleInternalSDK(), false);
37 EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").GetType(), XcodeSDK::MacOSX);
38 EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").GetVersion(),
39 llvm::VersionTuple(10, 15));
40 EXPECT_EQ(XcodeSDK("MacOSX10.15.Internal.sdk").IsAppleInternalSDK(), true);
41 EXPECT_EQ(XcodeSDK().GetType(), XcodeSDK::unknown);
42 EXPECT_EQ(XcodeSDK().GetVersion(), llvm::VersionTuple());
43}
44
45TEST(XcodeSDKTest, MergeTest) {
46 XcodeSDK sdk("MacOSX.sdk");
47 sdk.Merge(other: XcodeSDK("WatchOS.sdk"));
48 // This doesn't make any particular sense and shouldn't happen in practice, we
49 // just want to guarantee a well-defined behavior when choosing one
50 // SDK to fit all CUs in an lldb::Module.
51 // -> The higher number wins.
52 EXPECT_EQ(sdk.GetType(), XcodeSDK::watchOS);
53 sdk.Merge(other: XcodeSDK("WatchOS1.1.sdk"));
54 EXPECT_EQ(sdk.GetVersion(), llvm::VersionTuple(1, 1));
55 sdk.Merge(other: XcodeSDK("WatchOS2.0.sdk"));
56 EXPECT_EQ(sdk.GetVersion(), llvm::VersionTuple(2, 0));
57 sdk.Merge(other: XcodeSDK("WatchOS1.1.Internal.sdk"));
58 EXPECT_EQ(sdk.GetVersion(), llvm::VersionTuple(2, 0));
59 EXPECT_EQ(sdk.IsAppleInternalSDK(), true);
60 XcodeSDK empty;
61 empty.Merge(other: XcodeSDK("MacOSX10.14.Internal.sdk"));
62 EXPECT_EQ(empty.GetString(), llvm::StringRef("MacOSX10.14.Internal.sdk"));
63}
64
65#ifndef _WIN32
66TEST(XcodeSDKTest, SDKSupportsModules) {
67 std::string base = "/Applications/Xcode.app/Contents/Developer/Platforms/";
68 EXPECT_TRUE(XcodeSDK::SDKSupportsModules(
69 XcodeSDK::Type::iPhoneSimulator,
70 FileSpec(
71 base +
72 "iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.0.sdk")));
73 EXPECT_TRUE(XcodeSDK::SDKSupportsModules(
74 XcodeSDK::Type::iPhoneSimulator,
75 FileSpec(base + "iPhoneSimulator.platform/Developer/SDKs/"
76 "iPhoneSimulator12.0.Internal.sdk")));
77 EXPECT_FALSE(XcodeSDK::SDKSupportsModules(
78 XcodeSDK::Type::iPhoneSimulator,
79 FileSpec(
80 base +
81 "iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.2.sdk")));
82 EXPECT_TRUE(XcodeSDK::SDKSupportsModules(
83 XcodeSDK::Type::MacOSX,
84 FileSpec(base + "MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk")));
85 EXPECT_FALSE(XcodeSDK::SDKSupportsModules(
86 XcodeSDK::Type::MacOSX,
87 FileSpec(base + "MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk")));
88}
89#endif
90
91TEST(XcodeSDKTest, SDKSupportsSwift) {
92 EXPECT_TRUE(XcodeSDK("iPhoneSimulator12.0.sdk").SupportsSwift());
93 EXPECT_TRUE(XcodeSDK("iPhoneSimulator12.0.Internal.sdk").SupportsSwift());
94 EXPECT_FALSE(XcodeSDK("iPhoneSimulator7.2.sdk").SupportsSwift());
95 EXPECT_TRUE(XcodeSDK("MacOSX10.10.sdk").SupportsSwift());
96 EXPECT_FALSE(XcodeSDK("MacOSX10.9.sdk").SupportsSwift());
97 EXPECT_TRUE(XcodeSDK("Linux.sdk").SupportsSwift());
98 EXPECT_TRUE(XcodeSDK("MacOSX.sdk").SupportsSwift());
99 EXPECT_FALSE(XcodeSDK("EverythingElse.sdk").SupportsSwift());
100}
101
102TEST(XcodeSDKTest, GetCanonicalNameAndConstruct) {
103 XcodeSDK::Info info;
104 info.type = XcodeSDK::Type::MacOSX;
105 EXPECT_EQ("macosx", XcodeSDK::GetCanonicalName(info));
106 EXPECT_EQ(XcodeSDK(info).Parse(), info);
107
108 info.type = XcodeSDK::Type::iPhoneSimulator;
109 EXPECT_EQ("iphonesimulator", XcodeSDK::GetCanonicalName(info));
110 EXPECT_EQ(XcodeSDK(info).Parse(), info);
111
112 info.type = XcodeSDK::Type::iPhoneOS;
113 EXPECT_EQ("iphoneos", XcodeSDK::GetCanonicalName(info));
114 EXPECT_EQ(XcodeSDK(info).Parse(), info);
115
116 info.type = XcodeSDK::Type::AppleTVSimulator;
117 EXPECT_EQ("appletvsimulator", XcodeSDK::GetCanonicalName(info));
118 EXPECT_EQ(XcodeSDK(info).Parse(), info);
119
120 info.type = XcodeSDK::Type::AppleTVOS;
121 EXPECT_EQ("appletvos", XcodeSDK::GetCanonicalName(info));
122 EXPECT_EQ(XcodeSDK(info).Parse(), info);
123
124 info.type = XcodeSDK::Type::WatchSimulator;
125 EXPECT_EQ("watchsimulator", XcodeSDK::GetCanonicalName(info));
126 EXPECT_EQ(XcodeSDK(info).Parse(), info);
127
128 info.type = XcodeSDK::Type::watchOS;
129 EXPECT_EQ("watchos", XcodeSDK::GetCanonicalName(info));
130 EXPECT_EQ(XcodeSDK(info).Parse(), info);
131
132 info.type = XcodeSDK::Type::XRSimulator;
133 EXPECT_EQ("xrsimulator", XcodeSDK::GetCanonicalName(info));
134 EXPECT_EQ(XcodeSDK(info).Parse(), info);
135
136 info.type = XcodeSDK::Type::XROS;
137 EXPECT_EQ("xros", XcodeSDK::GetCanonicalName(info));
138 EXPECT_EQ(XcodeSDK(info).Parse(), info);
139
140 info.type = XcodeSDK::Type::Linux;
141 EXPECT_EQ("linux", XcodeSDK::GetCanonicalName(info));
142 EXPECT_EQ(XcodeSDK(info).Parse(), info);
143
144 info.type = XcodeSDK::Type::unknown;
145 EXPECT_EQ("", XcodeSDK::GetCanonicalName(info));
146 EXPECT_EQ(XcodeSDK(info).Parse(), info);
147
148 info.internal = true;
149 info.type = XcodeSDK::Type::MacOSX;
150 EXPECT_EQ("macosx.internal", XcodeSDK::GetCanonicalName(info));
151 EXPECT_EQ(XcodeSDK(info).Parse(), info);
152
153 info.type = XcodeSDK::Type::iPhoneSimulator;
154 EXPECT_EQ("iphonesimulator.internal", XcodeSDK::GetCanonicalName(info));
155 EXPECT_EQ(XcodeSDK(info).Parse(), info);
156
157 info.type = XcodeSDK::Type::iPhoneOS;
158 EXPECT_EQ("iphoneos.internal", XcodeSDK::GetCanonicalName(info));
159 EXPECT_EQ(XcodeSDK(info).Parse(), info);
160
161 info.type = XcodeSDK::Type::AppleTVSimulator;
162 EXPECT_EQ("appletvsimulator.internal", XcodeSDK::GetCanonicalName(info));
163 EXPECT_EQ(XcodeSDK(info).Parse(), info);
164
165 info.type = XcodeSDK::Type::AppleTVOS;
166 EXPECT_EQ("appletvos.internal", XcodeSDK::GetCanonicalName(info));
167 EXPECT_EQ(XcodeSDK(info).Parse(), info);
168
169 info.type = XcodeSDK::Type::WatchSimulator;
170 EXPECT_EQ("watchsimulator.internal", XcodeSDK::GetCanonicalName(info));
171 EXPECT_EQ(XcodeSDK(info).Parse(), info);
172
173 info.type = XcodeSDK::Type::watchOS;
174 EXPECT_EQ("watchos.internal", XcodeSDK::GetCanonicalName(info));
175 EXPECT_EQ(XcodeSDK(info).Parse(), info);
176
177 info.type = XcodeSDK::Type::XRSimulator;
178 EXPECT_EQ("xrsimulator.internal", XcodeSDK::GetCanonicalName(info));
179 EXPECT_EQ(XcodeSDK(info).Parse(), info);
180
181 info.type = XcodeSDK::Type::XROS;
182 EXPECT_EQ("xros.internal", XcodeSDK::GetCanonicalName(info));
183 EXPECT_EQ(XcodeSDK(info).Parse(), info);
184 info.type = XcodeSDK::Type::MacOSX;
185 info.version = llvm::VersionTuple(10, 9);
186 EXPECT_EQ("macosx10.9.internal", XcodeSDK::GetCanonicalName(info));
187 EXPECT_EQ(XcodeSDK(info).Parse(), info);
188
189 info.type = XcodeSDK::Type::iPhoneOS;
190 info.version = llvm::VersionTuple(7, 0);
191 EXPECT_EQ("iphoneos7.0.internal", XcodeSDK::GetCanonicalName(info));
192 EXPECT_EQ(XcodeSDK(info).Parse(), info);
193}
194
195TEST(XcodeSDKTest, GetSDKTypeForTriple) {
196 EXPECT_EQ(
197 XcodeSDK::GetSDKTypeForTriple(llvm::Triple("x86_64-apple-macosx10.14")),
198 XcodeSDK::Type::MacOSX);
199 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("x86_64-apple-darwin")),
200 XcodeSDK::Type::MacOSX);
201 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(
202 llvm::Triple("x86_64-apple-ios13.4-simulator")),
203 XcodeSDK::Type::iPhoneSimulator);
204 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("arm64-apple-ios13.4")),
205 XcodeSDK::Type::iPhoneOS);
206 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(
207 llvm::Triple("x86_64-apple-ios13.4-macabi")),
208 XcodeSDK::Type::MacOSX);
209 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(
210 llvm::Triple("x86_64-apple-tvos-simulator")),
211 XcodeSDK::Type::AppleTVSimulator);
212 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("arm64-apple-tvos")),
213 XcodeSDK::Type::AppleTVOS);
214 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(
215 llvm::Triple("x86_64-apple-watchos-simulator")),
216 XcodeSDK::Type::WatchSimulator);
217 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("arm64-apple-watchos")),
218 XcodeSDK::Type::watchOS);
219 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(
220 llvm::Triple("arm64e-apple-xros-simulator")),
221 XcodeSDK::Type::XRSimulator);
222 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("arm64e-apple-xros")),
223 XcodeSDK::Type::XROS);
224 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("x86_64-unknown-linux")),
225 XcodeSDK::Type::Linux);
226 EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("i386-unknown-netbsd")),
227 XcodeSDK::Type::unknown);
228}
229
230TEST(XcodeSDKTest, FindXcodeContentsDirectoryInPath) {
231 std::string standard =
232 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/"
233 "Developer/SDKs/MacOSX.sdk";
234 EXPECT_EQ("/Applications/Xcode.app/Contents",
235 XcodeSDK::FindXcodeContentsDirectoryInPath(standard));
236
237 std::string standard_version =
238 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/"
239 "Developer/SDKs/MacOSX10.15.sdk";
240 EXPECT_EQ("/Applications/Xcode.app/Contents",
241 XcodeSDK::FindXcodeContentsDirectoryInPath(standard_version));
242
243 std::string beta = "/Applications/Xcode-beta.app/Contents/Developer/"
244 "Platforms/MacOSX.platform/"
245 "Developer/SDKs/MacOSX10.15.sdk";
246 EXPECT_EQ("/Applications/Xcode-beta.app/Contents",
247 XcodeSDK::FindXcodeContentsDirectoryInPath(beta));
248
249 std::string no_app =
250 "/Applications/Xcode/Contents/Developer/Platforms/MacOSX.platform/"
251 "Developer/SDKs/MacOSX10.15.sdk";
252 EXPECT_EQ("", XcodeSDK::FindXcodeContentsDirectoryInPath(no_app));
253
254 std::string no_contents =
255 "/Applications/Xcode.app/Developer/Platforms/MacOSX.platform/"
256 "Developer/SDKs/MacOSX10.15.sdk";
257 EXPECT_EQ("", XcodeSDK::FindXcodeContentsDirectoryInPath(no_contents));
258
259 std::string no_capitalization =
260 "/Applications/Xcode.app/contents/Developer/Platforms/MacOSX.platform/"
261 "Developer/SDKs/MacOSX10.15.sdk";
262 EXPECT_EQ("", XcodeSDK::FindXcodeContentsDirectoryInPath(no_capitalization));
263}
264

source code of lldb/unittests/Utility/XcodeSDKTest.cpp