Debian Codegraph @

clang/unittests/Basic/DarwinSDKInfoTest.cpp

raw
1 //===- unittests/Basic/DarwinSDKInfoTest.cpp -- SDKSettings.json test -----===//
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 "clang/Basic/DarwinSDKInfo.h"
10 #include "llvm/Support/JSON.h"
11 #include "gtest/gtest.h"
12 #include <optional>
13
14 using namespace llvm;
15 using namespace clang;
16
17 // Check the version mapping logic in DarwinSDKInfo.
18 TEST(DarwinSDKInfo, VersionMapping) {
19 llvm::json::Object Obj({{"3.0", "1.0"}, {"3.1", "1.2"}});
20 std::optional<DarwinSDKInfo::RelatedTargetVersionMapping> Mapping =
21 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj,
22 VersionTuple());
23 EXPECT_TRUE(Mapping);
24 EXPECT_EQ(Mapping->getMinimumValue(), VersionTuple(1));
25
26 // Exact mapping.
27 EXPECT_EQ(Mapping->map(VersionTuple(3), VersionTuple(0, 1), std::nullopt),
28 VersionTuple(1));
29 EXPECT_EQ(Mapping->map(VersionTuple(3, 0), VersionTuple(0, 1), std::nullopt),
30 VersionTuple(1));
31 EXPECT_EQ(
32 Mapping->map(VersionTuple(3, 0, 0), VersionTuple(0, 1), std::nullopt),
33 VersionTuple(1));
34 EXPECT_EQ(Mapping->map(VersionTuple(3, 1), VersionTuple(0, 1), std::nullopt),
35 VersionTuple(1, 2));
36 EXPECT_EQ(
37 Mapping->map(VersionTuple(3, 1, 0), VersionTuple(0, 1), std::nullopt),
38 VersionTuple(1, 2));
39
40 // Missing mapping - fallback to major.
41 EXPECT_EQ(
42 Mapping->map(VersionTuple(3, 0, 1), VersionTuple(0, 1), std::nullopt),
43 VersionTuple(1));
44
45 // Minimum
46 EXPECT_EQ(Mapping->map(VersionTuple(2), VersionTuple(0, 1), std::nullopt),
47 VersionTuple(0, 1));
48
49 // Maximum
50 EXPECT_EQ(
51 Mapping->map(VersionTuple(4), VersionTuple(0, 1), VersionTuple(100)),
52 VersionTuple(100));
53 }
54
55 // Check the version mapping logic in DarwinSDKInfo.
56 TEST(DarwinSDKInfo, VersionMappingMissingKey) {
57 llvm::json::Object Obj({{"3.0", "1.0"}, {"5.0", "1.2"}});
58 std::optional<DarwinSDKInfo::RelatedTargetVersionMapping> Mapping =
59 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj,
60 VersionTuple());
61 EXPECT_TRUE(Mapping);
62 EXPECT_EQ(
63 Mapping->map(VersionTuple(4), VersionTuple(0, 1), VersionTuple(100)),
64 std::nullopt);
65 }
66
67 TEST(DarwinSDKInfo, VersionMappingParseEmpty) {
68 llvm::json::Object Obj({});
69 EXPECT_FALSE(
70 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj, VersionTuple())
71 .has_value());
72 }
73
74 TEST(DarwinSDKInfo, VersionMappingParseError) {
75 llvm::json::Object Obj({{"test", "1.2"}});
76 EXPECT_FALSE(
77 DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(Obj, VersionTuple())
78 .has_value());
79 }
80
81 TEST(DarwinSDKInfo, PlatformPrefix) {
82 llvm::json::Object SDKSettings({{"CanonicalName", "macosx26.0"},
83 {"Version", "26.0"},
84 {"MaximumDeploymentTarget", "26.0.99"}});
85 llvm::json::Object SupportedTargets;
86 llvm::json::Object MacOS({{"Archs", {"x86_64", "arm64"}},
87 {"LLVMTargetTripleVendor", "apple"},
88 {"LLVMTargetTripleSys", "macos"},
89 {"LLVMTargetTripleEnvironment", ""},
90 {"SystemPrefix", "/System/macOSSupport"}});
91 llvm::json::Object MacCatalyst({{"Archs", {"x86_64", "arm64"}},
92 {"LLVMTargetTripleVendor", "apple"},
93 {"LLVMTargetTripleSys", "ios"},
94 {"LLVMTargetTripleEnvironment", "macabi"},
95 {"SystemPrefix", "/System/iOSSupport"}});
96 llvm::json::Object DriverKit({{"Archs", {"x86_64", "arm64"}},
97 {"LLVMTargetTripleVendor", "apple"},
98 {"LLVMTargetTripleSys", "driverkit"},
99 {"LLVMTargetTripleEnvironment", ""},
100 {"SystemPrefix", ""}});
101 SupportedTargets["macosx"] = std::move(MacOS);
102 SupportedTargets["iosmac"] = std::move(MacCatalyst);
103 SupportedTargets["driverkit"] = std::move(DriverKit);
104 SDKSettings["SupportedTargets"] = std::move(SupportedTargets);
105
106 auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(&SDKSettings);
107 ASSERT_TRUE(SDKInfo);
108 EXPECT_EQ(SDKInfo->getPlatformPrefix(Triple("arm64-apple-macos26.0")),
109 "/System/macOSSupport");
110 // The triple's architecture doesn't matter.
111 EXPECT_EQ(SDKInfo->getPlatformPrefix(Triple("ppc-apple-macos26.0")),
112 "/System/macOSSupport");
113 // OSes that aren't specified in the SDK never get a system prefix.
114 EXPECT_EQ(SDKInfo->getPlatformPrefix(Triple("arm64-apple-ios26.0")), "");
115 // /System/iOSSupport is not a system prefix, and is ignored.
116 EXPECT_EQ(SDKInfo->getPlatformPrefix(Triple("arm64-apple-ios26.0-macabi")),
117 "");
118 // Should future versions of DriverKit drop the system prefix, that will be
119 // respected.
120 EXPECT_EQ(SDKInfo->getPlatformPrefix(Triple("arm64-apple-driverkit")), "");
121 }
122
123 TEST(DarwinSDKInfoTest, ParseAndTestMappingMacCatalyst) {
124 llvm::json::Object Obj;
125 Obj["CanonicalName"] = "macosx11.0";
126 Obj["Version"] = "11.0";
127 Obj["MaximumDeploymentTarget"] = "11.99";
128 llvm::json::Object VersionMap;
129 VersionMap["10.15"] = "13.1";
130 VersionMap["11.0"] = "14.0";
131 VersionMap["11.2"] = "14.2";
132 llvm::json::Object MacOS2iOSMac;
133 MacOS2iOSMac["macOS_iOSMac"] = std::move(VersionMap);
134 Obj["VersionMap"] = std::move(MacOS2iOSMac);
135
136 auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj);
137 ASSERT_TRUE(SDKInfo);
138 EXPECT_EQ(SDKInfo->getVersion(), VersionTuple(11, 0));
139
140 auto Mapping = SDKInfo->getVersionMapping(
141 DarwinSDKInfo::OSEnvPair::macOStoMacCatalystPair());
142 ASSERT_TRUE(Mapping);
143 // Verify that the macOS versions that are present in the map are translated
144 // directly to their corresponding Mac Catalyst versions.
145 EXPECT_EQ(*Mapping->map(VersionTuple(10, 15), VersionTuple(), std::nullopt),
146 VersionTuple(13, 1));
147 EXPECT_EQ(*Mapping->map(VersionTuple(11, 0), VersionTuple(), std::nullopt),
148 VersionTuple(14, 0));
149 EXPECT_EQ(*Mapping->map(VersionTuple(11, 2), VersionTuple(), std::nullopt),
150 VersionTuple(14, 2));
151
152 // Verify that a macOS version that's not present in the map is translated
153 // like the nearest major OS version.
154 EXPECT_EQ(*Mapping->map(VersionTuple(11, 1), VersionTuple(), std::nullopt),
155 VersionTuple(14, 0));
156
157 // Verify that the macOS versions that are outside of the mapped version
158 // range map to the min/max values passed to the `map` call.
159 EXPECT_EQ(
160 *Mapping->map(VersionTuple(10, 14), VersionTuple(99, 99), std::nullopt),
161 VersionTuple(99, 99));
162 EXPECT_EQ(
163 *Mapping->map(VersionTuple(11, 5), VersionTuple(), VersionTuple(99, 99)),
164 VersionTuple(99, 99));
165 EXPECT_EQ(*Mapping->map(VersionTuple(11, 5), VersionTuple(99, 98),
166 VersionTuple(99, 99)),
167 VersionTuple(99, 99));
168 }
169
170 TEST(DarwinSDKInfoTest, ParseAndTestMappingIOSDerived) {
171 llvm::json::Object Obj;
172 Obj["CanonicalName"] = "appletvos15.0";
173 Obj["Version"] = "15.0";
174 Obj["MaximumDeploymentTarget"] = "15.0.99";
175 llvm::json::Object VersionMap;
176 VersionMap["10.0"] = "10.0";
177 VersionMap["10.3.1"] = "10.2";
178 VersionMap["11.0"] = "11.0";
179 llvm::json::Object IOSToTvOS;
180 IOSToTvOS["iOS_tvOS"] = std::move(VersionMap);
181 Obj["VersionMap"] = std::move(IOSToTvOS);
182
183 auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj);
184 ASSERT_TRUE(SDKInfo);
185 EXPECT_EQ(SDKInfo->getVersion(), VersionTuple(15, 0));
186
187 // Verify that mapping is present for platforms that derive from iOS.
188 const auto *Mapping = SDKInfo->getVersionMapping(DarwinSDKInfo::OSEnvPair(
189 llvm::Triple::IOS, llvm::Triple::UnknownEnvironment, llvm::Triple::TvOS,
190 llvm::Triple::UnknownEnvironment));
191 ASSERT_TRUE(Mapping);
192
193 // Verify that the iOS versions that are present in the map are translated
194 // directly to their corresponding tvOS versions.
195 EXPECT_EQ(*Mapping->map(VersionTuple(10, 0), VersionTuple(), std::nullopt),
196 VersionTuple(10, 0));
197 EXPECT_EQ(*Mapping->map(VersionTuple(10, 3, 1), VersionTuple(), std::nullopt),
198 VersionTuple(10, 2));
199 EXPECT_EQ(*Mapping->map(VersionTuple(11, 0), VersionTuple(), std::nullopt),
200 VersionTuple(11, 0));
201
202 // Verify that an iOS version that's not present in the map is translated
203 // like the nearest major OS version.
204 EXPECT_EQ(*Mapping->map(VersionTuple(10, 1), VersionTuple(), std::nullopt),
205 VersionTuple(10, 0));
206
207 // Verify that the iOS versions that are outside of the mapped version
208 // range map to the min/max values passed to the `map` call.
209 EXPECT_EQ(
210 *Mapping->map(VersionTuple(9, 0), VersionTuple(99, 99), std::nullopt),
211 VersionTuple(99, 99));
212 EXPECT_EQ(
213 *Mapping->map(VersionTuple(13, 0), VersionTuple(), VersionTuple(99, 99)),
214 VersionTuple(99, 99));
215
216 // Verify introduced, deprecated, and obsoleted mappings.
217 EXPECT_EQ(Mapping->mapIntroducedAvailabilityVersion(VersionTuple(10, 1)),
218 VersionTuple(10.0));
219 EXPECT_EQ(Mapping->mapDeprecatedObsoletedAvailabilityVersion(
220 VersionTuple(100000, 0)),
221 VersionTuple(100000));
222 EXPECT_EQ(
223 Mapping->mapDeprecatedObsoletedAvailabilityVersion(VersionTuple(13.0)),
224 VersionTuple(15, 0, 99));
225 }
226
227 TEST(DarwinSDKInfoTest, MissingKeys) {
228 llvm::json::Object Obj;
229 ASSERT_FALSE(DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj));
230 Obj["Version"] = "11.0";
231 ASSERT_FALSE(DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj));
232 }