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