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 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "link/TableMerger.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include "android-base/logging.h" |
| 20 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "ResourceTable.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 22 | #include "ResourceUtils.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | #include "ResourceValues.h" |
| 24 | #include "ValueVisitor.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "util/Util.h" |
| 26 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | using android::StringPiece; |
| 28 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | TableMerger::TableMerger(IAaptContext* context, ResourceTable* out_table, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 32 | const TableMergerOptions& options) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | : context_(context), master_table_(out_table), options_(options) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 34 | // Create the desired package that all tables will be merged into. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | master_package_ = master_table_->CreatePackage( |
| 36 | context_->GetCompilationPackage(), context_->GetPackageId()); |
| 37 | CHECK(master_package_ != nullptr) << "package name or ID already taken"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | bool TableMerger::Merge(const Source& src, ResourceTable* table, |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 41 | io::IFileCollection* collection) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 42 | return MergeImpl(src, table, collection, false /* overlay */, true /* allow new */); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | bool TableMerger::MergeOverlay(const Source& src, ResourceTable* table, |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 46 | io::IFileCollection* collection) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 47 | return MergeImpl(src, table, collection, true /* overlay */, options_.auto_add_overlay); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 50 | /** |
| 51 | * This will merge packages with the same package name (or no package name). |
| 52 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | bool TableMerger::MergeImpl(const Source& src, ResourceTable* table, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | io::IFileCollection* collection, bool overlay, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | bool allow_new) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | bool error = false; |
| 57 | for (auto& package : table->packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | // Only merge an empty package or the package we're building. |
| 59 | // Other packages may exist, which likely contain attribute definitions. |
| 60 | // This is because at compile time it is unknown if the attributes are |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 61 | // simply uses of the attribute or definitions. |
| 62 | if (package->name.empty() || context_->GetCompilationPackage() == package->name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | FileMergeCallback callback; |
| 64 | if (collection) { |
| 65 | callback = [&](const ResourceNameRef& name, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | const ConfigDescription& config, FileReference* new_file, |
| 67 | FileReference* old_file) -> bool { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 68 | // The old file's path points inside the APK, so we can use it as is. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | io::IFile* f = collection->FindFile(*old_file->path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | if (!f) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | context_->GetDiagnostics()->Error(DiagMessage(src) |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 72 | << "file '" << *old_file->path << "' not found"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | new_file->file = f; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | return true; |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | // Merge here. Once the entries are merged and mangled, any references to |
| 82 | // them are still valid. This is because un-mangled references are |
| 83 | // mangled, then looked up at resolution time. |
| 84 | // Also, when linking, we convert references with no package name to use |
| 85 | // the compilation package name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | error |= !DoMerge(src, table, package.get(), false /* mangle */, overlay, |
| 87 | allow_new, callback); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | return !error; |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * This will merge and mangle resources from a static library. |
| 95 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | bool TableMerger::MergeAndMangle(const Source& src, |
| 97 | const StringPiece& package_name, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | ResourceTable* table, |
| 99 | io::IFileCollection* collection) { |
| 100 | bool error = false; |
| 101 | for (auto& package : table->packages) { |
| 102 | // Warn of packages with an unrelated ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | if (package_name != package->name) { |
| 104 | context_->GetDiagnostics()->Warn(DiagMessage(src) << "ignoring package " |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | << package->name); |
| 106 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | bool mangle = package_name != context_->GetCompilationPackage(); |
| 110 | merged_packages_.insert(package->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | auto callback = [&]( |
| 113 | const ResourceNameRef& name, const ConfigDescription& config, |
| 114 | FileReference* new_file, FileReference* old_file) -> bool { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | // The old file's path points inside the APK, so we can use it as is. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | io::IFile* f = collection->FindFile(*old_file->path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | if (!f) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | context_->GetDiagnostics()->Error( |
| 119 | DiagMessage(src) << "file '" << *old_file->path << "' not found"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 123 | new_file->file = f; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 124 | return true; |
| 125 | }; |
| 126 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 127 | error |= !DoMerge(src, table, package.get(), mangle, false /* overlay */, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | true /* allow new */, callback); |
| 129 | } |
| 130 | return !error; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | static bool MergeType(IAaptContext* context, const Source& src, |
| 134 | ResourceTableType* dst_type, |
| 135 | ResourceTableType* src_type) { |
| 136 | if (dst_type->symbol_status.state < src_type->symbol_status.state) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | // The incoming type's visibility is stronger, so we should override |
| 138 | // the visibility. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 139 | if (src_type->symbol_status.state == SymbolState::kPublic) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | // Only copy the ID if the source is public, or else the ID is |
| 141 | // meaningless. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | dst_type->id = src_type->id; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 143 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | dst_type->symbol_status = std::move(src_type->symbol_status); |
| 145 | } else if (dst_type->symbol_status.state == SymbolState::kPublic && |
| 146 | src_type->symbol_status.state == SymbolState::kPublic && |
| 147 | dst_type->id && src_type->id && |
| 148 | dst_type->id.value() != src_type->id.value()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | // Both types are public and have different IDs. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | context->GetDiagnostics()->Error(DiagMessage(src) |
| 151 | << "cannot merge type '" << src_type->type |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | << "': conflicting public IDs"); |
| 153 | return false; |
| 154 | } |
| 155 | return true; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | static bool MergeEntry(IAaptContext* context, const Source& src, |
| 159 | ResourceEntry* dst_entry, ResourceEntry* src_entry) { |
| 160 | if (dst_entry->symbol_status.state < src_entry->symbol_status.state) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | // The incoming type's visibility is stronger, so we should override |
| 162 | // the visibility. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | if (src_entry->symbol_status.state == SymbolState::kPublic) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 164 | // Only copy the ID if the source is public, or else the ID is |
| 165 | // meaningless. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 166 | dst_entry->id = src_entry->id; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 167 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | dst_entry->symbol_status = std::move(src_entry->symbol_status); |
| 169 | } else if (src_entry->symbol_status.state == SymbolState::kPublic && |
| 170 | dst_entry->symbol_status.state == SymbolState::kPublic && |
| 171 | dst_entry->id && src_entry->id && |
| 172 | dst_entry->id.value() != src_entry->id.value()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | // Both entries are public and have different IDs. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 174 | context->GetDiagnostics()->Error( |
| 175 | DiagMessage(src) << "cannot merge entry '" << src_entry->name |
| 176 | << "': conflicting public IDs"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | return true; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Modified CollisionResolver which will merge Styleables. Used with overlays. |
| 184 | * |
| 185 | * Styleables are not actual resources, but they are treated as such during the |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | * compilation phase. Styleables don't simply overlay each other, their |
| 187 | * definitions merge |
| 188 | * and accumulate. If both values are Styleables, we just merge them into the |
| 189 | * existing value. |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 190 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | static ResourceTable::CollisionResult ResolveMergeCollision(Value* existing, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | Value* incoming) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | if (Styleable* existing_styleable = ValueCast<Styleable>(existing)) { |
| 194 | if (Styleable* incoming_styleable = ValueCast<Styleable>(incoming)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | // Styleables get merged. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | existing_styleable->MergeWith(incoming_styleable); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | return ResourceTable::CollisionResult::kKeepOriginal; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 198 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | } |
| 200 | // Delegate to the default handler. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 201 | return ResourceTable::ResolveValueCollision(existing, incoming); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | static ResourceTable::CollisionResult MergeConfigValue( |
| 205 | IAaptContext* context, const ResourceNameRef& res_name, const bool overlay, |
| 206 | ResourceConfigValue* dst_config_value, |
| 207 | ResourceConfigValue* src_config_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | using CollisionResult = ResourceTable::CollisionResult; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 209 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | Value* dst_value = dst_config_value->value.get(); |
| 211 | Value* src_value = src_config_value->value.get(); |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 212 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 213 | CollisionResult collision_result; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 214 | if (overlay) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | collision_result = ResolveMergeCollision(dst_value, src_value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 217 | collision_result = |
| 218 | ResourceTable::ResolveValueCollision(dst_value, src_value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 221 | if (collision_result == CollisionResult::kConflict) { |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 222 | if (overlay) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | return CollisionResult::kTakeNew; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 226 | // Error! |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | context->GetDiagnostics()->Error( |
| 228 | DiagMessage(src_value->GetSource()) |
| 229 | << "resource '" << res_name << "' has a conflicting value for " |
| 230 | << "configuration (" << src_config_value->config << ")"); |
| 231 | context->GetDiagnostics()->Note(DiagMessage(dst_value->GetSource()) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | << "originally defined here"); |
| 233 | return CollisionResult::kConflict; |
| 234 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | return collision_result; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 238 | bool TableMerger::DoMerge(const Source& src, ResourceTable* src_table, |
| 239 | ResourceTablePackage* src_package, |
| 240 | const bool mangle_package, const bool overlay, |
| 241 | const bool allow_new_resources, |
Chih-Hung Hsieh | 9b8528f | 2016-08-10 14:15:30 -0700 | [diff] [blame] | 242 | const FileMergeCallback& callback) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 243 | bool error = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | for (auto& src_type : src_package->types) { |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame^] | 246 | ResourceTableType* dst_type = master_package_->FindOrCreateType(src_type->type); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | if (!MergeType(context_, src, dst_type, src_type.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | error = true; |
| 249 | continue; |
| 250 | } |
| 251 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 252 | for (auto& src_entry : src_type->entries) { |
| 253 | std::string entry_name = src_entry->name; |
| 254 | if (mangle_package) { |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame^] | 255 | entry_name = NameMangler::MangleEntry(src_package->name, src_entry->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 258 | ResourceEntry* dst_entry; |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame^] | 259 | if (allow_new_resources || src_entry->symbol_status.allow_new) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | dst_entry = dst_type->FindOrCreateEntry(entry_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 261 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | dst_entry = dst_type->FindEntry(entry_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame^] | 265 | const ResourceNameRef res_name(src_package->name, src_type->type, src_entry->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 267 | if (!dst_entry) { |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame^] | 268 | context_->GetDiagnostics()->Error(DiagMessage(src) |
| 269 | << "resource " << res_name |
| 270 | << " does not override an existing resource"); |
| 271 | context_->GetDiagnostics()->Note(DiagMessage(src) << "define an <add-resource> tag or use " |
| 272 | << "--auto-add-overlay"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | error = true; |
| 274 | continue; |
| 275 | } |
| 276 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | if (!MergeEntry(context_, src, dst_entry, src_entry.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 278 | error = true; |
| 279 | continue; |
| 280 | } |
| 281 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 282 | for (auto& src_config_value : src_entry->values) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 283 | using CollisionResult = ResourceTable::CollisionResult; |
| 284 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 285 | ResourceConfigValue* dst_config_value = dst_entry->FindValue( |
| 286 | src_config_value->config, src_config_value->product); |
| 287 | if (dst_config_value) { |
| 288 | CollisionResult collision_result = |
| 289 | MergeConfigValue(context_, res_name, overlay, dst_config_value, |
| 290 | src_config_value.get()); |
| 291 | if (collision_result == CollisionResult::kConflict) { |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 292 | error = true; |
| 293 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | } else if (collision_result == CollisionResult::kKeepOriginal) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 295 | continue; |
| 296 | } |
| 297 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 298 | dst_config_value = dst_entry->FindOrCreateValue( |
| 299 | src_config_value->config, src_config_value->product); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 302 | // Continue if we're taking the new resource. |
| 303 | |
| 304 | if (FileReference* f = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 305 | ValueCast<FileReference>(src_config_value->value.get())) { |
| 306 | std::unique_ptr<FileReference> new_file_ref; |
| 307 | if (mangle_package) { |
| 308 | new_file_ref = CloneAndMangleFile(src_package->name, *f); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 309 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | new_file_ref = std::unique_ptr<FileReference>( |
| 311 | f->Clone(&master_table_->string_pool)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (callback) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 315 | if (!callback(res_name, src_config_value->config, |
| 316 | new_file_ref.get(), f)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 317 | error = true; |
| 318 | continue; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 319 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 320 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 321 | dst_config_value->value = std::move(new_file_ref); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 322 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 323 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | dst_config_value->value = std::unique_ptr<Value>( |
| 325 | src_config_value->value->Clone(&master_table_->string_pool)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 326 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 327 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 328 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 329 | } |
| 330 | return !error; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | std::unique_ptr<FileReference> TableMerger::CloneAndMangleFile( |
| 334 | const std::string& package, const FileReference& file_ref) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 335 | StringPiece prefix, entry, suffix; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 336 | if (util::ExtractResFilePathParts(*file_ref.path, &prefix, &entry, &suffix)) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 337 | std::string mangled_entry = NameMangler::MangleEntry(package, entry.to_string()); |
| 338 | std::string newPath = prefix.to_string() + mangled_entry + suffix.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 339 | std::unique_ptr<FileReference> new_file_ref = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 340 | util::make_unique<FileReference>( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 341 | master_table_->string_pool.MakeRef(newPath)); |
| 342 | new_file_ref->SetComment(file_ref.GetComment()); |
| 343 | new_file_ref->SetSource(file_ref.GetSource()); |
| 344 | return new_file_ref; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 345 | } |
| 346 | return std::unique_ptr<FileReference>( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 347 | file_ref.Clone(&master_table_->string_pool)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 350 | bool TableMerger::MergeFileImpl(const ResourceFile& file_desc, io::IFile* file, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 351 | bool overlay) { |
| 352 | ResourceTable table; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 353 | std::string path = ResourceUtils::BuildResourceFileName(file_desc); |
| 354 | std::unique_ptr<FileReference> file_ref = |
| 355 | util::make_unique<FileReference>(table.string_pool.MakeRef(path)); |
| 356 | file_ref->SetSource(file_desc.source); |
| 357 | file_ref->file = file; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 358 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 359 | ResourceTablePackage* pkg = table.CreatePackage(file_desc.name.package, 0x0); |
| 360 | pkg->FindOrCreateType(file_desc.name.type) |
| 361 | ->FindOrCreateEntry(file_desc.name.entry) |
| 362 | ->FindOrCreateValue(file_desc.config, {}) |
| 363 | ->value = std::move(file_ref); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 364 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 365 | return DoMerge(file->GetSource(), &table, pkg, false /* mangle */, |
| 366 | overlay /* overlay */, true /* allow_new */, {}); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 369 | bool TableMerger::MergeFile(const ResourceFile& file_desc, io::IFile* file) { |
| 370 | return MergeFileImpl(file_desc, file, false /* overlay */); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 373 | bool TableMerger::MergeFileOverlay(const ResourceFile& file_desc, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 374 | io::IFile* file) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 375 | return MergeFileImpl(file_desc, file, true /* overlay */); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 378 | } // namespace aapt |