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