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