|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | #include "llvm/TextAPI/InterfaceFile.h"
|
| 14 | +#include "llvm/TextAPI/TextAPIError.h" |
14 | 15 | #include <iomanip>
|
15 | 16 | #include <sstream>
|
16 | 17 |
|
@@ -81,6 +82,267 @@ void InterfaceFile::addDocument(std::shared_ptr<InterfaceFile> &&Document) {
|
81 | 82 | Documents.insert(Pos, Document);
|
82 | 83 | }
|
83 | 84 |
|
| 85 | +void InterfaceFile::inlineLibrary(std::shared_ptr<InterfaceFile> Library, |
| 86 | + bool Overwrite) { |
| 87 | + auto AddFwk = [&](std::shared_ptr<InterfaceFile> &&Reexport) { |
| 88 | + auto It = lower_bound( |
| 89 | + Documents, Reexport->getInstallName(), |
| 90 | + [](std::shared_ptr<InterfaceFile> &Lhs, const StringRef Rhs) { |
| 91 | + return Lhs->getInstallName() < Rhs; |
| 92 | + }); |
| 93 | + |
| 94 | + if (Overwrite && It != Documents.end() && |
| 95 | + Reexport->getInstallName() == (*It)->getInstallName()) { |
| 96 | + std::replace(Documents.begin(), Documents.end(), *It, |
| 97 | + std::move(Reexport)); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + if ((It != Documents.end()) && |
| 102 | + !(Reexport->getInstallName() < (*It)->getInstallName())) |
| 103 | + return; |
| 104 | + |
| 105 | + Documents.emplace(It, std::move(Reexport)); |
| 106 | + }; |
| 107 | + for (auto Doc : Library->documents()) |
| 108 | + AddFwk(std::move(Doc)); |
| 109 | + |
| 110 | + Library->Documents.clear(); |
| 111 | + AddFwk(std::move(Library)); |
| 112 | +} |
| 113 | + |
| 114 | +Expected<std::unique_ptr<InterfaceFile>> |
| 115 | +InterfaceFile::merge(const InterfaceFile *O) const { |
| 116 | + // Verify files can be merged. |
| 117 | + if (getInstallName() != O->getInstallName()) { |
| 118 | + return make_error<StringError>("install names do not match", |
| 119 | + inconvertibleErrorCode()); |
| 120 | + } |
| 121 | + |
| 122 | + if (getCurrentVersion() != O->getCurrentVersion()) { |
| 123 | + return make_error<StringError>("current versions do not match", |
| 124 | + inconvertibleErrorCode()); |
| 125 | + } |
| 126 | + |
| 127 | + if (getCompatibilityVersion() != O->getCompatibilityVersion()) { |
| 128 | + return make_error<StringError>("compatibility versions do not match", |
| 129 | + inconvertibleErrorCode()); |
| 130 | + } |
| 131 | + |
| 132 | + if ((getSwiftABIVersion() != 0) && (O->getSwiftABIVersion() != 0) && |
| 133 | + (getSwiftABIVersion() != O->getSwiftABIVersion())) { |
| 134 | + return make_error<StringError>("swift ABI versions do not match", |
| 135 | + inconvertibleErrorCode()); |
| 136 | + } |
| 137 | + |
| 138 | + if (isTwoLevelNamespace() != O->isTwoLevelNamespace()) { |
| 139 | + return make_error<StringError>("two level namespace flags do not match", |
| 140 | + inconvertibleErrorCode()); |
| 141 | + } |
| 142 | + |
| 143 | + if (isApplicationExtensionSafe() != O->isApplicationExtensionSafe()) { |
| 144 | + return make_error<StringError>( |
| 145 | + "application extension safe flags do not match", |
| 146 | + inconvertibleErrorCode()); |
| 147 | + } |
| 148 | + |
| 149 | + std::unique_ptr<InterfaceFile> IF(new InterfaceFile()); |
| 150 | + IF->setFileType(std::max(getFileType(), O->getFileType())); |
| 151 | + IF->setPath(getPath()); |
| 152 | + IF->setInstallName(getInstallName()); |
| 153 | + IF->setCurrentVersion(getCurrentVersion()); |
| 154 | + IF->setCompatibilityVersion(getCompatibilityVersion()); |
| 155 | + |
| 156 | + if (getSwiftABIVersion() == 0) |
| 157 | + IF->setSwiftABIVersion(O->getSwiftABIVersion()); |
| 158 | + else |
| 159 | + IF->setSwiftABIVersion(getSwiftABIVersion()); |
| 160 | + |
| 161 | + IF->setTwoLevelNamespace(isTwoLevelNamespace()); |
| 162 | + IF->setApplicationExtensionSafe(isApplicationExtensionSafe()); |
| 163 | + |
| 164 | + for (const auto &It : umbrellas()) { |
| 165 | + if (!It.second.empty()) |
| 166 | + IF->addParentUmbrella(It.first, It.second); |
| 167 | + } |
| 168 | + for (const auto &It : O->umbrellas()) { |
| 169 | + if (!It.second.empty()) |
| 170 | + IF->addParentUmbrella(It.first, It.second); |
| 171 | + } |
| 172 | + IF->addTargets(targets()); |
| 173 | + IF->addTargets(O->targets()); |
| 174 | + |
| 175 | + for (const auto &Lib : allowableClients()) |
| 176 | + for (const auto &Target : Lib.targets()) |
| 177 | + IF->addAllowableClient(Lib.getInstallName(), Target); |
| 178 | + |
| 179 | + for (const auto &Lib : O->allowableClients()) |
| 180 | + for (const auto &Target : Lib.targets()) |
| 181 | + IF->addAllowableClient(Lib.getInstallName(), Target); |
| 182 | + |
| 183 | + for (const auto &Lib : reexportedLibraries()) |
| 184 | + for (const auto &Target : Lib.targets()) |
| 185 | + IF->addReexportedLibrary(Lib.getInstallName(), Target); |
| 186 | + |
| 187 | + for (const auto &Lib : O->reexportedLibraries()) |
| 188 | + for (const auto &Target : Lib.targets()) |
| 189 | + IF->addReexportedLibrary(Lib.getInstallName(), Target); |
| 190 | + |
| 191 | + for (const auto &[Target, Path] : rpaths()) |
| 192 | + IF->addRPath(Target, Path); |
| 193 | + for (const auto &[Target, Path] : O->rpaths()) |
| 194 | + IF->addRPath(Target, Path); |
| 195 | + |
| 196 | + for (const auto *Sym : symbols()) { |
| 197 | + IF->addSymbol(Sym->getKind(), Sym->getName(), Sym->targets(), |
| 198 | + Sym->getFlags()); |
| 199 | + } |
| 200 | + |
| 201 | + for (const auto *Sym : O->symbols()) { |
| 202 | + IF->addSymbol(Sym->getKind(), Sym->getName(), Sym->targets(), |
| 203 | + Sym->getFlags()); |
| 204 | + } |
| 205 | + |
| 206 | + return std::move(IF); |
| 207 | +} |
| 208 | + |
| 209 | +Expected<std::unique_ptr<InterfaceFile>> |
| 210 | +InterfaceFile::remove(Architecture Arch) const { |
| 211 | + if (getArchitectures() == Arch) |
| 212 | + return make_error<StringError>("cannot remove last architecture slice '" + |
| 213 | + getArchitectureName(Arch) + "'", |
| 214 | + inconvertibleErrorCode()); |
| 215 | + |
| 216 | + if (!getArchitectures().has(Arch)) { |
| 217 | + bool Found = false; |
| 218 | + for (auto &Doc : Documents) { |
| 219 | + if (Doc->getArchitectures().has(Arch)) { |
| 220 | + Found = true; |
| 221 | + break; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + if (!Found) |
| 226 | + return make_error<TextAPIError>(TextAPIErrorCode::NoSuchArchitecture); |
| 227 | + } |
| 228 | + |
| 229 | + std::unique_ptr<InterfaceFile> IF(new InterfaceFile()); |
| 230 | + IF->setFileType(getFileType()); |
| 231 | + IF->setPath(getPath()); |
| 232 | + IF->addTargets(targets(ArchitectureSet::All().clear(Arch))); |
| 233 | + IF->setInstallName(getInstallName()); |
| 234 | + IF->setCurrentVersion(getCurrentVersion()); |
| 235 | + IF->setCompatibilityVersion(getCompatibilityVersion()); |
| 236 | + IF->setSwiftABIVersion(getSwiftABIVersion()); |
| 237 | + IF->setTwoLevelNamespace(isTwoLevelNamespace()); |
| 238 | + IF->setApplicationExtensionSafe(isApplicationExtensionSafe()); |
| 239 | + for (const auto &It : umbrellas()) |
| 240 | + if (It.first.Arch != Arch) |
| 241 | + IF->addParentUmbrella(It.first, It.second); |
| 242 | + |
| 243 | + for (const auto &Lib : allowableClients()) { |
| 244 | + for (const auto &Target : Lib.targets()) |
| 245 | + if (Target.Arch != Arch) |
| 246 | + IF->addAllowableClient(Lib.getInstallName(), Target); |
| 247 | + } |
| 248 | + |
| 249 | + for (const auto &Lib : reexportedLibraries()) { |
| 250 | + for (const auto &Target : Lib.targets()) |
| 251 | + if (Target.Arch != Arch) |
| 252 | + IF->addReexportedLibrary(Lib.getInstallName(), Target); |
| 253 | + } |
| 254 | + |
| 255 | + for (const auto *Sym : symbols()) { |
| 256 | + auto Archs = Sym->getArchitectures(); |
| 257 | + Archs.clear(Arch); |
| 258 | + if (Archs.empty()) |
| 259 | + continue; |
| 260 | + |
| 261 | + IF->addSymbol(Sym->getKind(), Sym->getName(), Sym->targets(Archs), |
| 262 | + Sym->getFlags()); |
| 263 | + } |
| 264 | + |
| 265 | + for (auto &Doc : Documents) { |
| 266 | + // Skip the inlined document if the to be removed architecture is the |
| 267 | + // only one left. |
| 268 | + if (Doc->getArchitectures() == Arch) |
| 269 | + continue; |
| 270 | + |
| 271 | + // If the document doesn't contain the arch, then no work is to be done |
| 272 | + // and it can be copied over. |
| 273 | + if (!Doc->getArchitectures().has(Arch)) { |
| 274 | + auto NewDoc = Doc; |
| 275 | + IF->addDocument(std::move(NewDoc)); |
| 276 | + continue; |
| 277 | + } |
| 278 | + |
| 279 | + auto Result = Doc->remove(Arch); |
| 280 | + if (!Result) |
| 281 | + return Result; |
| 282 | + |
| 283 | + IF->addDocument(std::move(Result.get())); |
| 284 | + } |
| 285 | + |
| 286 | + return std::move(IF); |
| 287 | +} |
| 288 | + |
| 289 | +Expected<std::unique_ptr<InterfaceFile>> |
| 290 | +InterfaceFile::extract(Architecture Arch) const { |
| 291 | + if (!getArchitectures().has(Arch)) { |
| 292 | + return make_error<StringError>("file doesn't have architecture '" + |
| 293 | + getArchitectureName(Arch) + "'", |
| 294 | + inconvertibleErrorCode()); |
| 295 | + } |
| 296 | + |
| 297 | + std::unique_ptr<InterfaceFile> IF(new InterfaceFile()); |
| 298 | + IF->setFileType(getFileType()); |
| 299 | + IF->setPath(getPath()); |
| 300 | + IF->addTargets(targets(Arch)); |
| 301 | + IF->setInstallName(getInstallName()); |
| 302 | + IF->setCurrentVersion(getCurrentVersion()); |
| 303 | + IF->setCompatibilityVersion(getCompatibilityVersion()); |
| 304 | + IF->setSwiftABIVersion(getSwiftABIVersion()); |
| 305 | + IF->setTwoLevelNamespace(isTwoLevelNamespace()); |
| 306 | + IF->setApplicationExtensionSafe(isApplicationExtensionSafe()); |
| 307 | + for (const auto &It : umbrellas()) |
| 308 | + if (It.first.Arch == Arch) |
| 309 | + IF->addParentUmbrella(It.first, It.second); |
| 310 | + |
| 311 | + for (const auto &It : rpaths()) |
| 312 | + if (It.first.Arch == Arch) |
| 313 | + IF->addRPath(It.first, It.second); |
| 314 | + |
| 315 | + for (const auto &Lib : allowableClients()) |
| 316 | + for (const auto &Target : Lib.targets()) |
| 317 | + if (Target.Arch == Arch) |
| 318 | + IF->addAllowableClient(Lib.getInstallName(), Target); |
| 319 | + |
| 320 | + for (const auto &Lib : reexportedLibraries()) |
| 321 | + for (const auto &Target : Lib.targets()) |
| 322 | + if (Target.Arch == Arch) |
| 323 | + IF->addReexportedLibrary(Lib.getInstallName(), Target); |
| 324 | + |
| 325 | + for (const auto *Sym : symbols()) { |
| 326 | + if (Sym->hasArchitecture(Arch)) |
| 327 | + IF->addSymbol(Sym->getKind(), Sym->getName(), Sym->targets(Arch), |
| 328 | + Sym->getFlags()); |
| 329 | + } |
| 330 | + |
| 331 | + for (auto &Doc : Documents) { |
| 332 | + // Skip documents that don't have the requested architecture. |
| 333 | + if (!Doc->getArchitectures().has(Arch)) |
| 334 | + continue; |
| 335 | + |
| 336 | + auto Result = Doc->extract(Arch); |
| 337 | + if (!Result) |
| 338 | + return Result; |
| 339 | + |
| 340 | + IF->addDocument(std::move(Result.get())); |
| 341 | + } |
| 342 | + |
| 343 | + return std::move(IF); |
| 344 | +} |
| 345 | + |
84 | 346 | static bool isYAMLTextStub(const FileType &Kind) {
|
85 | 347 | return (Kind >= FileType::TBD_V1) && (Kind < FileType::TBD_V5);
|
86 | 348 | }
|
|
0 commit comments