diff options
Diffstat (limited to 'tools/aapt2/link/TableMerger.cpp')
-rw-r--r-- | tools/aapt2/link/TableMerger.cpp | 254 |
1 files changed, 153 insertions, 101 deletions
diff --git a/tools/aapt2/link/TableMerger.cpp b/tools/aapt2/link/TableMerger.cpp index 7471e15db41a..eea430692936 100644 --- a/tools/aapt2/link/TableMerger.cpp +++ b/tools/aapt2/link/TableMerger.cpp @@ -55,19 +55,24 @@ bool TableMerger::mergeImpl(const Source& src, ResourceTable* table, bool error = false; for (auto& package : table->packages) { // Warn of packages with an unrelated ID. - if (package->id && package->id.value() != 0x0 && package->id.value() != desiredPackageId) { + const Maybe<ResourceId>& id = package->id; + if (id && id.value() != 0x0 && id.value() != desiredPackageId) { mContext->getDiagnostics()->warn(DiagMessage(src) << "ignoring package " << package->name); continue; } + // Only merge an empty package or the package we're building. + // Other packages may exist, which likely contain attribute definitions. + // This is because at compile time it is unknown if the attributes are simply + // uses of the attribute or definitions. if (package->name.empty() || mContext->getCompilationPackage() == package->name) { FileMergeCallback callback; if (collection) { callback = [&](const ResourceNameRef& name, const ConfigDescription& config, FileReference* newFile, FileReference* oldFile) -> bool { // The old file's path points inside the APK, so we can use it as is. - io::IFile* f = collection->findFile(util::utf16ToUtf8(*oldFile->path)); + io::IFile* f = collection->findFile(*oldFile->path); if (!f) { mContext->getDiagnostics()->error(DiagMessage(src) << "file '" << *oldFile->path @@ -85,8 +90,8 @@ bool TableMerger::mergeImpl(const Source& src, ResourceTable* table, // mangled, then looked up at resolution time. // Also, when linking, we convert references with no package name to use // the compilation package name. - error |= !doMerge(src, table, package.get(), - false /* mangle */, overlay, allowNew, callback); + error |= !doMerge(src, table, package.get(), false /* mangle */, overlay, allowNew, + callback); } } return !error; @@ -95,7 +100,7 @@ bool TableMerger::mergeImpl(const Source& src, ResourceTable* table, /** * This will merge and mangle resources from a static library. */ -bool TableMerger::mergeAndMangle(const Source& src, const StringPiece16& packageName, +bool TableMerger::mergeAndMangle(const Source& src, const StringPiece& packageName, ResourceTable* table, io::IFileCollection* collection) { bool error = false; for (auto& package : table->packages) { @@ -112,7 +117,7 @@ bool TableMerger::mergeAndMangle(const Source& src, const StringPiece16& package auto callback = [&](const ResourceNameRef& name, const ConfigDescription& config, FileReference* newFile, FileReference* oldFile) -> bool { // The old file's path points inside the APK, so we can use it as is. - io::IFile* f = collection->findFile(util::utf16ToUtf8(*oldFile->path)); + io::IFile* f = collection->findFile(*oldFile->path); if (!f) { mContext->getDiagnostics()->error(DiagMessage(src) << "file '" << *oldFile->path << "' not found"); @@ -129,126 +134,175 @@ bool TableMerger::mergeAndMangle(const Source& src, const StringPiece16& package return !error; } +static bool mergeType(IAaptContext* context, const Source& src, ResourceTableType* dstType, + ResourceTableType* srcType) { + if (dstType->symbolStatus.state < srcType->symbolStatus.state) { + // The incoming type's visibility is stronger, so we should override + // the visibility. + if (srcType->symbolStatus.state == SymbolState::kPublic) { + // Only copy the ID if the source is public, or else the ID is meaningless. + dstType->id = srcType->id; + } + dstType->symbolStatus = std::move(srcType->symbolStatus); + } else if (dstType->symbolStatus.state == SymbolState::kPublic + && srcType->symbolStatus.state == SymbolState::kPublic + && dstType->id && srcType->id + && dstType->id.value() != srcType->id.value()) { + // Both types are public and have different IDs. + context->getDiagnostics()->error(DiagMessage(src) + << "cannot merge type '" << srcType->type + << "': conflicting public IDs"); + return false; + } + return true; +} + +static bool mergeEntry(IAaptContext* context, const Source& src, ResourceEntry* dstEntry, + ResourceEntry* srcEntry) { + if (dstEntry->symbolStatus.state < srcEntry->symbolStatus.state) { + // The incoming type's visibility is stronger, so we should override + // the visibility. + if (srcEntry->symbolStatus.state == SymbolState::kPublic) { + // Only copy the ID if the source is public, or else the ID is meaningless. + dstEntry->id = srcEntry->id; + } + dstEntry->symbolStatus = std::move(srcEntry->symbolStatus); + } else if (srcEntry->symbolStatus.state == SymbolState::kPublic + && dstEntry->symbolStatus.state == SymbolState::kPublic + && dstEntry->id && srcEntry->id + && dstEntry->id.value() != srcEntry->id.value()) { + // Both entries are public and have different IDs. + context->getDiagnostics()->error(DiagMessage(src) + << "cannot merge entry '" << srcEntry->name + << "': conflicting public IDs"); + return false; + } + return true; +} + +/** + * Modified CollisionResolver which will merge Styleables. Used with overlays. + * + * Styleables are not actual resources, but they are treated as such during the + * compilation phase. Styleables don't simply overlay each other, their definitions merge + * and accumulate. If both values are Styleables, we just merge them into the existing value. + */ +static ResourceTable::CollisionResult resolveMergeCollision(Value* existing, Value* incoming) { + if (Styleable* existingStyleable = valueCast<Styleable>(existing)) { + if (Styleable* incomingStyleable = valueCast<Styleable>(incoming)) { + // Styleables get merged. + existingStyleable->mergeWith(incomingStyleable); + return ResourceTable::CollisionResult::kKeepOriginal; + } + } + // Delegate to the default handler. + return ResourceTable::resolveValueCollision(existing, incoming); +} + +static ResourceTable::CollisionResult mergeConfigValue(IAaptContext* context, + const ResourceNameRef& resName, + const bool overlay, + ResourceConfigValue* dstConfigValue, + ResourceConfigValue* srcConfigValue) { + using CollisionResult = ResourceTable::CollisionResult; + + Value* dstValue = dstConfigValue->value.get(); + Value* srcValue = srcConfigValue->value.get(); + + CollisionResult collisionResult; + if (overlay) { + collisionResult = resolveMergeCollision(dstValue, srcValue); + } else { + collisionResult = ResourceTable::resolveValueCollision(dstValue, srcValue); + } + + if (collisionResult == CollisionResult::kConflict) { + if (overlay) { + return CollisionResult::kTakeNew; + } + + // Error! + context->getDiagnostics()->error(DiagMessage(srcValue->getSource()) + << "resource '" << resName + << "' has a conflicting value for " + << "configuration (" + << srcConfigValue->config << ")"); + context->getDiagnostics()->note(DiagMessage(dstValue->getSource()) + << "originally defined here"); + return CollisionResult::kConflict; + } + return collisionResult; +} + bool TableMerger::doMerge(const Source& src, ResourceTable* srcTable, ResourceTablePackage* srcPackage, const bool manglePackage, const bool overlay, const bool allowNewResources, - FileMergeCallback callback) { + const FileMergeCallback& callback) { bool error = false; for (auto& srcType : srcPackage->types) { ResourceTableType* dstType = mMasterPackage->findOrCreateType(srcType->type); - if (srcType->symbolStatus.state == SymbolState::kPublic) { - if (dstType->symbolStatus.state == SymbolState::kPublic && dstType->id && srcType->id - && dstType->id.value() == srcType->id.value()) { - // Both types are public and have different IDs. - mContext->getDiagnostics()->error(DiagMessage(src) - << "can not merge type '" - << srcType->type - << "': conflicting public IDs"); - error = true; - continue; - } - - dstType->symbolStatus = std::move(srcType->symbolStatus); - dstType->id = srcType->id; + if (!mergeType(mContext, src, dstType, srcType.get())) { + error = true; + continue; } for (auto& srcEntry : srcType->entries) { - ResourceEntry* dstEntry; + std::string entryName = srcEntry->name; if (manglePackage) { - std::u16string mangledName = NameMangler::mangleEntry(srcPackage->name, - srcEntry->name); - if (allowNewResources) { - dstEntry = dstType->findOrCreateEntry(mangledName); - } else { - dstEntry = dstType->findEntry(mangledName); - } + entryName = NameMangler::mangleEntry(srcPackage->name, srcEntry->name); + } + + ResourceEntry* dstEntry; + if (allowNewResources) { + dstEntry = dstType->findOrCreateEntry(entryName); } else { - if (allowNewResources) { - dstEntry = dstType->findOrCreateEntry(srcEntry->name); - } else { - dstEntry = dstType->findEntry(srcEntry->name); - } + dstEntry = dstType->findEntry(entryName); } + const ResourceNameRef resName(srcPackage->name, srcType->type, srcEntry->name); + if (!dstEntry) { mContext->getDiagnostics()->error(DiagMessage(src) - << "resource " - << ResourceNameRef(srcPackage->name, - srcType->type, - srcEntry->name) + << "resource " << resName << " does not override an existing resource"); mContext->getDiagnostics()->note(DiagMessage(src) << "define an <add-resource> tag or use " - "--auto-add-overlay"); + << "--auto-add-overlay"); error = true; continue; } - if (srcEntry->symbolStatus.state != SymbolState::kUndefined) { - if (srcEntry->symbolStatus.state == SymbolState::kPublic) { - if (dstEntry->symbolStatus.state == SymbolState::kPublic && - dstEntry->id && srcEntry->id && - dstEntry->id.value() != srcEntry->id.value()) { - // Both entries are public and have different IDs. - mContext->getDiagnostics()->error(DiagMessage(src) - << "can not merge entry '" - << srcEntry->name - << "': conflicting public IDs"); - error = true; - continue; - } - - if (srcEntry->id) { - dstEntry->id = srcEntry->id; - } - } - - if (dstEntry->symbolStatus.state != SymbolState::kPublic && - dstEntry->symbolStatus.state != srcEntry->symbolStatus.state) { - dstEntry->symbolStatus = std::move(srcEntry->symbolStatus); - } + if (!mergeEntry(mContext, src, dstEntry, srcEntry.get())) { + error = true; + continue; } - ResourceNameRef resName(mMasterPackage->name, dstType->type, dstEntry->name); - - for (auto& srcValue : srcEntry->values) { - ResourceConfigValue* dstValue = dstEntry->findValue(srcValue->config, - srcValue->product); - if (dstValue) { - const int collisionResult = ResourceTable::resolveValueCollision( - dstValue->value.get(), srcValue->value.get()); - if (collisionResult == 0 && !overlay) { - // Error! - ResourceNameRef resourceName(srcPackage->name, - srcType->type, - srcEntry->name); - - mContext->getDiagnostics()->error(DiagMessage(srcValue->value->getSource()) - << "resource '" << resourceName - << "' has a conflicting value for " - << "configuration (" - << srcValue->config << ")"); - mContext->getDiagnostics()->note(DiagMessage(dstValue->value->getSource()) - << "originally defined here"); + for (auto& srcConfigValue : srcEntry->values) { + using CollisionResult = ResourceTable::CollisionResult; + + ResourceConfigValue* dstConfigValue = dstEntry->findValue(srcConfigValue->config, + srcConfigValue->product); + if (dstConfigValue) { + CollisionResult collisionResult = mergeConfigValue( + mContext, resName, overlay, dstConfigValue, srcConfigValue.get()); + if (collisionResult == CollisionResult::kConflict) { error = true; continue; - } else if (collisionResult < 0) { - // Keep our existing value. + } else if (collisionResult == CollisionResult::kKeepOriginal) { continue; } - + } else { + dstConfigValue = dstEntry->findOrCreateValue(srcConfigValue->config, + srcConfigValue->product); } - if (!dstValue) { - // Force create the entry if we didn't have it. - dstValue = dstEntry->findOrCreateValue(srcValue->config, srcValue->product); - } + // Continue if we're taking the new resource. - if (FileReference* f = valueCast<FileReference>(srcValue->value.get())) { + if (FileReference* f = valueCast<FileReference>(srcConfigValue->value.get())) { std::unique_ptr<FileReference> newFileRef; if (manglePackage) { newFileRef = cloneAndMangleFile(srcPackage->name, *f); @@ -258,15 +312,15 @@ bool TableMerger::doMerge(const Source& src, } if (callback) { - if (!callback(resName, srcValue->config, newFileRef.get(), f)) { + if (!callback(resName, srcConfigValue->config, newFileRef.get(), f)) { error = true; continue; } } - dstValue->value = std::move(newFileRef); + dstConfigValue->value = std::move(newFileRef); } else { - dstValue->value = std::unique_ptr<Value>(srcValue->value->clone( + dstConfigValue->value = std::unique_ptr<Value>(srcConfigValue->value->clone( &mMasterTable->stringPool)); } } @@ -275,13 +329,12 @@ bool TableMerger::doMerge(const Source& src, return !error; } -std::unique_ptr<FileReference> TableMerger::cloneAndMangleFile(const std::u16string& package, +std::unique_ptr<FileReference> TableMerger::cloneAndMangleFile(const std::string& package, const FileReference& fileRef) { - - StringPiece16 prefix, entry, suffix; + StringPiece prefix, entry, suffix; if (util::extractResFilePathParts(*fileRef.path, &prefix, &entry, &suffix)) { - std::u16string mangledEntry = NameMangler::mangleEntry(package, entry.toString()); - std::u16string newPath = prefix.toString() + mangledEntry + suffix.toString(); + std::string mangledEntry = NameMangler::mangleEntry(package, entry.toString()); + std::string newPath = prefix.toString() + mangledEntry + suffix.toString(); std::unique_ptr<FileReference> newFileRef = util::make_unique<FileReference>( mMasterTable->stringPool.makeRef(newPath)); newFileRef->setComment(fileRef.getComment()); @@ -293,8 +346,7 @@ std::unique_ptr<FileReference> TableMerger::cloneAndMangleFile(const std::u16str bool TableMerger::mergeFileImpl(const ResourceFile& fileDesc, io::IFile* file, bool overlay) { ResourceTable table; - std::u16string path = util::utf8ToUtf16(ResourceUtils::buildResourceFileName(fileDesc, - nullptr)); + std::string path = ResourceUtils::buildResourceFileName(fileDesc, nullptr); std::unique_ptr<FileReference> fileRef = util::make_unique<FileReference>( table.stringPool.makeRef(path)); fileRef->setSource(fileDesc.source); |