Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "ResourceTable.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 18 | #include "ResourceUtils.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 19 | #include "ResourceValues.h" |
| 20 | #include "ValueVisitor.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "link/TableMerger.h" |
| 22 | #include "util/Util.h" |
| 23 | |
| 24 | #include <cassert> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 28 | TableMerger::TableMerger(IAaptContext* context, ResourceTable* outTable, |
| 29 | const TableMergerOptions& options) : |
| 30 | mContext(context), mMasterTable(outTable), mOptions(options) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | // Create the desired package that all tables will be merged into. |
| 32 | mMasterPackage = mMasterTable->createPackage( |
| 33 | mContext->getCompilationPackage(), mContext->getPackageId()); |
| 34 | assert(mMasterPackage && "package name or ID already taken"); |
| 35 | } |
| 36 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 37 | /** |
| 38 | * This will merge packages with the same package name (or no package name). |
| 39 | */ |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 40 | bool TableMerger::mergeImpl(const Source& src, ResourceTable* table, |
| 41 | bool overlay, bool allowNew) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | const uint8_t desiredPackageId = mContext->getPackageId(); |
| 43 | |
| 44 | bool error = false; |
| 45 | for (auto& package : table->packages) { |
| 46 | // Warn of packages with an unrelated ID. |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 47 | if (package->id && package->id.value() != 0x0 && package->id.value() != desiredPackageId) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | mContext->getDiagnostics()->warn(DiagMessage(src) |
| 49 | << "ignoring package " << package->name); |
| 50 | continue; |
| 51 | } |
| 52 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 53 | if (package->name.empty() || mContext->getCompilationPackage() == package->name) { |
| 54 | // Merge here. Once the entries are merged and mangled, any references to |
| 55 | // them are still valid. This is because un-mangled references are |
| 56 | // mangled, then looked up at resolution time. |
| 57 | // Also, when linking, we convert references with no package name to use |
| 58 | // the compilation package name. |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 59 | error |= !doMerge(src, table, package.get(), |
| 60 | false /* mangle */, overlay, allowNew, {}); |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | return !error; |
| 64 | } |
| 65 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 66 | bool TableMerger::merge(const Source& src, ResourceTable* table) { |
| 67 | return mergeImpl(src, table, false /* overlay */, true /* allow new */); |
| 68 | } |
| 69 | |
| 70 | bool TableMerger::mergeOverlay(const Source& src, ResourceTable* table) { |
| 71 | return mergeImpl(src, table, true /* overlay */, mOptions.autoAddOverlay); |
| 72 | } |
| 73 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 74 | /** |
| 75 | * This will merge and mangle resources from a static library. |
| 76 | */ |
| 77 | bool TableMerger::mergeAndMangle(const Source& src, const StringPiece16& packageName, |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 78 | ResourceTable* table, io::IFileCollection* collection) { |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 79 | bool error = false; |
| 80 | for (auto& package : table->packages) { |
| 81 | // Warn of packages with an unrelated ID. |
| 82 | if (packageName != package->name) { |
| 83 | mContext->getDiagnostics()->warn(DiagMessage(src) |
| 84 | << "ignoring package " << package->name); |
| 85 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 88 | bool mangle = packageName != mContext->getCompilationPackage(); |
| 89 | mMergedPackages.insert(package->name); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 90 | |
| 91 | auto callback = [&](const ResourceNameRef& name, const ConfigDescription& config, |
| 92 | FileReference* newFile, FileReference* oldFile) -> bool { |
| 93 | // The old file's path points inside the APK, so we can use it as is. |
| 94 | io::IFile* f = collection->findFile(util::utf16ToUtf8(*oldFile->path)); |
| 95 | if (!f) { |
| 96 | mContext->getDiagnostics()->error(DiagMessage(src) << "file '" << *oldFile->path |
| 97 | << "' not found"); |
| 98 | return false; |
| 99 | } |
| 100 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 101 | newFile->file = f; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 102 | return true; |
| 103 | }; |
| 104 | |
| 105 | error |= !doMerge(src, table, package.get(), |
| 106 | mangle, false /* overlay */, true /* allow new */, callback); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | } |
| 108 | return !error; |
| 109 | } |
| 110 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 111 | bool TableMerger::doMerge(const Source& src, |
| 112 | ResourceTable* srcTable, |
| 113 | ResourceTablePackage* srcPackage, |
| 114 | const bool manglePackage, |
| 115 | const bool overlay, |
| 116 | const bool allowNewResources, |
| 117 | FileMergeCallback callback) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 118 | bool error = false; |
| 119 | |
| 120 | for (auto& srcType : srcPackage->types) { |
| 121 | ResourceTableType* dstType = mMasterPackage->findOrCreateType(srcType->type); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 122 | if (srcType->symbolStatus.state == SymbolState::kPublic) { |
| 123 | if (dstType->symbolStatus.state == SymbolState::kPublic && dstType->id && srcType->id |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | && dstType->id.value() == srcType->id.value()) { |
| 125 | // Both types are public and have different IDs. |
| 126 | mContext->getDiagnostics()->error(DiagMessage(src) |
| 127 | << "can not merge type '" |
| 128 | << srcType->type |
| 129 | << "': conflicting public IDs"); |
| 130 | error = true; |
| 131 | continue; |
| 132 | } |
| 133 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 134 | dstType->symbolStatus = std::move(srcType->symbolStatus); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 135 | dstType->id = srcType->id; |
| 136 | } |
| 137 | |
| 138 | for (auto& srcEntry : srcType->entries) { |
| 139 | ResourceEntry* dstEntry; |
| 140 | if (manglePackage) { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 141 | std::u16string mangledName = NameMangler::mangleEntry(srcPackage->name, |
| 142 | srcEntry->name); |
| 143 | if (allowNewResources) { |
| 144 | dstEntry = dstType->findOrCreateEntry(mangledName); |
| 145 | } else { |
| 146 | dstEntry = dstType->findEntry(mangledName); |
| 147 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 148 | } else { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 149 | if (allowNewResources) { |
| 150 | dstEntry = dstType->findOrCreateEntry(srcEntry->name); |
| 151 | } else { |
| 152 | dstEntry = dstType->findEntry(srcEntry->name); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (!dstEntry) { |
| 157 | mContext->getDiagnostics()->error(DiagMessage(src) |
| 158 | << "resource " |
| 159 | << ResourceNameRef(srcPackage->name, |
| 160 | srcType->type, |
| 161 | srcEntry->name) |
| 162 | << " does not override an existing resource"); |
| 163 | mContext->getDiagnostics()->note(DiagMessage(src) |
| 164 | << "define an <add-resource> tag or use " |
| 165 | "--auto-add-overlay"); |
| 166 | error = true; |
| 167 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 170 | if (srcEntry->symbolStatus.state != SymbolState::kUndefined) { |
| 171 | if (srcEntry->symbolStatus.state == SymbolState::kPublic) { |
| 172 | if (dstEntry->symbolStatus.state == SymbolState::kPublic && |
| 173 | dstEntry->id && srcEntry->id && |
| 174 | dstEntry->id.value() != srcEntry->id.value()) { |
| 175 | // Both entries are public and have different IDs. |
| 176 | mContext->getDiagnostics()->error(DiagMessage(src) |
| 177 | << "can not merge entry '" |
| 178 | << srcEntry->name |
| 179 | << "': conflicting public IDs"); |
| 180 | error = true; |
| 181 | continue; |
| 182 | } |
| 183 | |
| 184 | if (srcEntry->id) { |
| 185 | dstEntry->id = srcEntry->id; |
| 186 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 189 | if (dstEntry->symbolStatus.state != SymbolState::kPublic && |
| 190 | dstEntry->symbolStatus.state != srcEntry->symbolStatus.state) { |
| 191 | dstEntry->symbolStatus = std::move(srcEntry->symbolStatus); |
| 192 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 195 | ResourceNameRef resName(mMasterPackage->name, dstType->type, dstEntry->name); |
| 196 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 197 | for (auto& srcValue : srcEntry->values) { |
| 198 | ResourceConfigValue* dstValue = dstEntry->findValue(srcValue->config, |
| 199 | srcValue->product); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 200 | if (dstValue) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 201 | const int collisionResult = ResourceTable::resolveValueCollision( |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 202 | dstValue->value.get(), srcValue->value.get()); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 203 | if (collisionResult == 0 && !overlay) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 204 | // Error! |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 205 | ResourceNameRef resourceName(srcPackage->name, |
| 206 | srcType->type, |
| 207 | srcEntry->name); |
| 208 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 209 | mContext->getDiagnostics()->error(DiagMessage(srcValue->value->getSource()) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 210 | << "resource '" << resourceName |
| 211 | << "' has a conflicting value for " |
| 212 | << "configuration (" |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 213 | << srcValue->config << ")"); |
| 214 | mContext->getDiagnostics()->note(DiagMessage(dstValue->value->getSource()) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 215 | << "originally defined here"); |
| 216 | error = true; |
| 217 | continue; |
| 218 | } else if (collisionResult < 0) { |
| 219 | // Keep our existing value. |
| 220 | continue; |
| 221 | } |
| 222 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 225 | if (!dstValue) { |
| 226 | // Force create the entry if we didn't have it. |
| 227 | dstValue = dstEntry->findOrCreateValue(srcValue->config, srcValue->product); |
| 228 | } |
| 229 | |
| 230 | if (FileReference* f = valueCast<FileReference>(srcValue->value.get())) { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 231 | std::unique_ptr<FileReference> newFileRef; |
| 232 | if (manglePackage) { |
| 233 | newFileRef = cloneAndMangleFile(srcPackage->name, *f); |
| 234 | } else { |
| 235 | newFileRef = std::unique_ptr<FileReference>(f->clone( |
| 236 | &mMasterTable->stringPool)); |
| 237 | } |
| 238 | |
| 239 | if (callback) { |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 240 | if (!callback(resName, srcValue->config, newFileRef.get(), f)) { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 241 | error = true; |
| 242 | continue; |
| 243 | } |
| 244 | } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 245 | dstValue->value = std::move(newFileRef); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 246 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 247 | } else { |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 248 | dstValue->value = std::unique_ptr<Value>(srcValue->value->clone( |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 249 | &mMasterTable->stringPool)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | return !error; |
| 255 | } |
| 256 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 257 | std::unique_ptr<FileReference> TableMerger::cloneAndMangleFile(const std::u16string& package, |
| 258 | const FileReference& fileRef) { |
| 259 | |
| 260 | StringPiece16 prefix, entry, suffix; |
| 261 | if (util::extractResFilePathParts(*fileRef.path, &prefix, &entry, &suffix)) { |
| 262 | std::u16string mangledEntry = NameMangler::mangleEntry(package, entry.toString()); |
| 263 | std::u16string newPath = prefix.toString() + mangledEntry + suffix.toString(); |
| 264 | std::unique_ptr<FileReference> newFileRef = util::make_unique<FileReference>( |
| 265 | mMasterTable->stringPool.makeRef(newPath)); |
| 266 | newFileRef->setComment(fileRef.getComment()); |
| 267 | newFileRef->setSource(fileRef.getSource()); |
| 268 | return newFileRef; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 269 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 270 | return std::unique_ptr<FileReference>(fileRef.clone(&mMasterTable->stringPool)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 273 | bool TableMerger::mergeFileImpl(const ResourceFile& fileDesc, io::IFile* file, bool overlay) { |
| 274 | ResourceTable table; |
| 275 | std::u16string path = util::utf8ToUtf16(ResourceUtils::buildResourceFileName(fileDesc, |
| 276 | nullptr)); |
| 277 | std::unique_ptr<FileReference> fileRef = util::make_unique<FileReference>( |
| 278 | table.stringPool.makeRef(path)); |
| 279 | fileRef->setSource(fileDesc.source); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 280 | fileRef->file = file; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 281 | |
| 282 | ResourceTablePackage* pkg = table.createPackage(fileDesc.name.package, 0x0); |
| 283 | pkg->findOrCreateType(fileDesc.name.type) |
| 284 | ->findOrCreateEntry(fileDesc.name.entry) |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 285 | ->findOrCreateValue(fileDesc.config, {}) |
| 286 | ->value = std::move(fileRef); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 287 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 288 | return doMerge(file->getSource(), &table, pkg, |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 289 | false /* mangle */, overlay /* overlay */, true /* allow new */, {}); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | bool TableMerger::mergeFile(const ResourceFile& fileDesc, io::IFile* file) { |
| 293 | return mergeFileImpl(fileDesc, file, false /* overlay */); |
| 294 | } |
| 295 | |
| 296 | bool TableMerger::mergeFileOverlay(const ResourceFile& fileDesc, io::IFile* file) { |
| 297 | return mergeFileImpl(fileDesc, file, true /* overlay */); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | } // namespace aapt |