blob: bc93ec6908e7fcbdd2634b0131cb9ab97cc97c1e [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinskicacb28f2016-10-19 12:18:14 -070017#include "link/TableMerger.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include "android-base/logging.h"
20
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "ResourceTable.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080022#include "ResourceUtils.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "ResourceValues.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080024#include "trace/TraceBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "ValueVisitor.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "util/Util.h"
27
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070028using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080029
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030namespace aapt {
31
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032TableMerger::TableMerger(IAaptContext* context, ResourceTable* out_table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 const TableMergerOptions& options)
Ryan Mitchell4ea90752020-07-31 08:21:43 -070034 : context_(context), main_table_(out_table), options_(options) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 // Create the desired package that all tables will be merged into.
Ryan Mitchell9634efb2021-03-19 14:53:17 -070036 main_package_ = main_table_->FindOrCreatePackage(context_->GetCompilationPackage());
Ryan Mitchell4ea90752020-07-31 08:21:43 -070037 CHECK(main_package_ != nullptr) << "package name or ID already taken";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038}
39
Adam Lesinski8780eb62017-10-31 17:44:39 -070040bool TableMerger::Merge(const Source& src, ResourceTable* table, bool overlay) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080041 TRACE_CALL();
Adam Lesinski00451162017-10-03 07:44:08 -070042 // We allow adding new resources if this is not an overlay, or if the options allow overlays
43 // to add new resources.
Adam Lesinski8780eb62017-10-31 17:44:39 -070044 return MergeImpl(src, table, overlay, options_.auto_add_overlay || !overlay /*allow_new*/);
Adam Lesinski64587af2016-02-18 18:33:06 -080045}
46
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070047// This will merge packages with the same package name (or no package name).
Adam Lesinski8780eb62017-10-31 17:44:39 -070048bool TableMerger::MergeImpl(const Source& src, ResourceTable* table, bool overlay, bool allow_new) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 bool error = false;
50 for (auto& package : table->packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 // Only merge an empty package or the package we're building.
52 // Other packages may exist, which likely contain attribute definitions.
53 // This is because at compile time it is unknown if the attributes are
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080054 // simply uses of the attribute or definitions.
55 if (package->name.empty() || context_->GetCompilationPackage() == package->name) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070056 // Merge here. Once the entries are merged and mangled, any references to them are still
57 // valid. This is because un-mangled references are mangled, then looked up at resolution
58 // time. Also, when linking, we convert references with no package name to use the compilation
59 // package name.
Fabien Sanglard2369f542019-03-19 08:32:46 -070060 error |= !DoMerge(src, package.get(), false /*mangle*/, overlay, allow_new);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 }
62 }
63 return !error;
Adam Lesinski83f22552015-11-07 11:51:23 -080064}
65
Adam Lesinski8780eb62017-10-31 17:44:39 -070066// This will merge and mangle resources from a static library. It is assumed that all FileReferences
67// have correctly set their io::IFile*.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070068bool TableMerger::MergeAndMangle(const Source& src, const StringPiece& package_name,
Adam Lesinski8780eb62017-10-31 17:44:39 -070069 ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 bool error = false;
71 for (auto& package : table->packages) {
72 // Warn of packages with an unrelated ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 if (package_name != package->name) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070074 context_->GetDiagnostics()->Warn(DiagMessage(src) << "ignoring package " << package->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 bool mangle = package_name != context_->GetCompilationPackage();
79 merged_packages_.insert(package->name);
Fabien Sanglard2369f542019-03-19 08:32:46 -070080 error |= !DoMerge(src, package.get(), mangle, false /*overlay*/, true /*allow_new*/);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 }
82 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083}
84
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070085static bool MergeType(IAaptContext* context, const Source& src, ResourceTableType* dst_type,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 ResourceTableType* src_type) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -070087 if (src_type->visibility_level >= dst_type->visibility_level) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070088 // The incoming type's visibility is stronger, so we should override the visibility.
Adam Lesinski71be7052017-12-12 16:48:07 -080089 dst_type->visibility_level = src_type->visibility_level;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 }
91 return true;
Adam Lesinski5c3464c2016-08-24 16:03:48 -070092}
93
Ryan Mitchelle4e989c2018-10-29 02:21:50 -070094static bool MergeEntry(IAaptContext* context, const Source& src,
Izabela Orlowskad51efe82018-04-24 18:18:29 +010095 ResourceEntry* dst_entry, ResourceEntry* src_entry,
96 bool strict_visibility) {
97 if (strict_visibility
98 && dst_entry->visibility.level != Visibility::Level::kUndefined
99 && src_entry->visibility.level != dst_entry->visibility.level) {
100 context->GetDiagnostics()->Error(
101 DiagMessage(src) << "cannot merge resource '" << dst_entry->name << "' with conflicting visibilities: "
102 << "public and private");
103 return false;
104 }
105
Adam Lesinski71be7052017-12-12 16:48:07 -0800106 // Copy over the strongest visibility.
107 if (src_entry->visibility.level > dst_entry->visibility.level) {
108 // Only copy the ID if the source is public, or else the ID is meaningless.
109 if (src_entry->visibility.level == Visibility::Level::kPublic) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 dst_entry->id = src_entry->id;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700111 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800112 dst_entry->visibility = std::move(src_entry->visibility);
113 } else if (src_entry->visibility.level == Visibility::Level::kPublic &&
114 dst_entry->visibility.level == Visibility::Level::kPublic && dst_entry->id &&
115 src_entry->id && src_entry->id != dst_entry->id) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 // Both entries are public and have different IDs.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700117 context->GetDiagnostics()->Error(DiagMessage(src) << "cannot merge entry '" << src_entry->name
118 << "': conflicting public IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 return false;
120 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800121
122 // Copy over the rest of the properties, if needed.
123 if (src_entry->allow_new) {
124 dst_entry->allow_new = std::move(src_entry->allow_new);
125 }
126
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800127 if (src_entry->overlayable_item) {
128 if (dst_entry->overlayable_item) {
Ryan Mitchellced9a5c2019-04-05 10:44:16 -0700129 CHECK(src_entry->overlayable_item.value().overlayable != nullptr);
130 Overlayable* src_overlayable = src_entry->overlayable_item.value().overlayable.get();
131
132 CHECK(dst_entry->overlayable_item.value().overlayable != nullptr);
133 Overlayable* dst_overlayable = dst_entry->overlayable_item.value().overlayable.get();
134
135 if (src_overlayable->name != dst_overlayable->name
136 || src_overlayable->actor != dst_overlayable->actor
137 || src_entry->overlayable_item.value().policies !=
138 dst_entry->overlayable_item.value().policies) {
139
140 // Do not allow a resource with an overlayable declaration to have that overlayable
141 // declaration redefined.
142 context->GetDiagnostics()->Error(DiagMessage(src_entry->overlayable_item.value().source)
143 << "duplicate overlayable declaration for resource '"
144 << src_entry->name << "'");
145 context->GetDiagnostics()->Error(DiagMessage(dst_entry->overlayable_item.value().source)
146 << "previous declaration here");
147 return false;
148 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800149 }
Ryan Mitchellced9a5c2019-04-05 10:44:16 -0700150
151 dst_entry->overlayable_item = std::move(src_entry->overlayable_item);
Adam Lesinski71be7052017-12-12 16:48:07 -0800152 }
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 return true;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700155}
156
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700157// Modified CollisionResolver which will merge Styleables and Styles. Used with overlays.
158//
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700159// Styleables are not actual resources, but they are treated as such during the compilation phase.
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700160//
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700161// Styleables and Styles don't simply overlay each other, their definitions merge and accumulate.
162// If both values are Styleables/Styles, we just merge them into the existing value.
Donald Chai121c6e82019-06-12 12:51:57 -0700163static ResourceTable::CollisionResult ResolveMergeCollision(
164 bool override_styles_instead_of_overlaying, Value* existing, Value* incoming,
165 StringPool* pool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 if (Styleable* existing_styleable = ValueCast<Styleable>(existing)) {
167 if (Styleable* incoming_styleable = ValueCast<Styleable>(incoming)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 // Styleables get merged.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 existing_styleable->MergeWith(incoming_styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 return ResourceTable::CollisionResult::kKeepOriginal;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700171 }
Donald Chai121c6e82019-06-12 12:51:57 -0700172 } else if (!override_styles_instead_of_overlaying) {
173 if (Style* existing_style = ValueCast<Style>(existing)) {
174 if (Style* incoming_style = ValueCast<Style>(incoming)) {
175 // Styles get merged.
176 existing_style->MergeWith(incoming_style, pool);
177 return ResourceTable::CollisionResult::kKeepOriginal;
178 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700179 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 }
181 // Delegate to the default handler.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 return ResourceTable::ResolveValueCollision(existing, incoming);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700183}
184
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700185static ResourceTable::CollisionResult MergeConfigValue(IAaptContext* context,
186 const ResourceNameRef& res_name,
Adam Lesinski8780eb62017-10-31 17:44:39 -0700187 bool overlay,
Donald Chai121c6e82019-06-12 12:51:57 -0700188 bool override_styles_instead_of_overlaying,
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700189 ResourceConfigValue* dst_config_value,
190 ResourceConfigValue* src_config_value,
191 StringPool* pool) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 using CollisionResult = ResourceTable::CollisionResult;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700193
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 Value* dst_value = dst_config_value->value.get();
195 Value* src_value = src_config_value->value.get();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 CollisionResult collision_result;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 if (overlay) {
Donald Chai121c6e82019-06-12 12:51:57 -0700199 collision_result =
200 ResolveMergeCollision(override_styles_instead_of_overlaying, dst_value, src_value, pool);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700202 collision_result = ResourceTable::ResolveValueCollision(dst_value, src_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700206 if (overlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 return CollisionResult::kTakeNew;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700208 }
209
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 // Error!
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700211 context->GetDiagnostics()->Error(DiagMessage(src_value->GetSource())
212 << "resource '" << res_name << "' has a conflicting value for "
213 << "configuration (" << src_config_value->config << ")");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 context->GetDiagnostics()->Note(DiagMessage(dst_value->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 << "originally defined here");
216 return CollisionResult::kConflict;
217 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700218 return collision_result;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700219}
220
Fabien Sanglard2369f542019-03-19 08:32:46 -0700221bool TableMerger::DoMerge(const Source& src, ResourceTablePackage* src_package, bool mangle_package,
222 bool overlay, bool allow_new_resources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700224
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700225 for (auto& src_type : src_package->types) {
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700226 ResourceTableType* dst_type = main_package_->FindOrCreateType(src_type->type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 if (!MergeType(context_, src, dst_type, src_type.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 error = true;
229 continue;
230 }
231
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 for (auto& src_entry : src_type->entries) {
233 std::string entry_name = src_entry->name;
234 if (mangle_package) {
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700235 entry_name = NameMangler::MangleEntry(src_package->name, src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 }
237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 ResourceEntry* dst_entry;
Adam Lesinski71be7052017-12-12 16:48:07 -0800239 if (allow_new_resources || src_entry->allow_new) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 dst_entry = dst_type->FindOrCreateEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 dst_entry = dst_type->FindEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 }
244
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700245 const ResourceNameRef res_name(src_package->name, src_type->type, src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 if (!dst_entry) {
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700248 context_->GetDiagnostics()->Error(DiagMessage(src)
249 << "resource " << res_name
250 << " does not override an existing resource");
251 context_->GetDiagnostics()->Note(DiagMessage(src) << "define an <add-resource> tag or use "
252 << "--auto-add-overlay");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 error = true;
254 continue;
255 }
256
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700257 if (!MergeEntry(context_, src, dst_entry, src_entry.get(), options_.strict_visibility)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 error = true;
259 continue;
260 }
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 for (auto& src_config_value : src_entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 using CollisionResult = ResourceTable::CollisionResult;
264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 ResourceConfigValue* dst_config_value = dst_entry->FindValue(
266 src_config_value->config, src_config_value->product);
267 if (dst_config_value) {
Donald Chai121c6e82019-06-12 12:51:57 -0700268 CollisionResult collision_result = MergeConfigValue(
269 context_, res_name, overlay, options_.override_styles_instead_of_overlaying,
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700270 dst_config_value, src_config_value.get(), &main_table_->string_pool);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700272 error = true;
273 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 } else if (collision_result == CollisionResult::kKeepOriginal) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 continue;
276 }
277 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700278 dst_config_value =
279 dst_entry->FindOrCreateValue(src_config_value->config, src_config_value->product);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700280 }
281
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 // Continue if we're taking the new resource.
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700283 CloningValueTransformer cloner(&main_table_->string_pool);
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700284 if (FileReference* f = ValueCast<FileReference>(src_config_value->value.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285 std::unique_ptr<FileReference> new_file_ref;
286 if (mangle_package) {
287 new_file_ref = CloneAndMangleFile(src_package->name, *f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 } else {
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700289 new_file_ref = std::unique_ptr<FileReference>(f->Transform(cloner));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700291 dst_config_value->value = std::move(new_file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800292
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 } else {
Ryan Mitchella9e31602018-06-28 16:41:38 -0700294 Maybe<std::string> original_comment = (dst_config_value->value)
295 ? dst_config_value->value->GetComment() : Maybe<std::string>();
296
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700297 dst_config_value->value = src_config_value->value->Transform(cloner);
Ryan Mitchella9e31602018-06-28 16:41:38 -0700298
299 // Keep the comment from the original resource and ignore all comments from overlaying
300 // resources
301 if (overlay && original_comment) {
302 dst_config_value->value->SetComment(original_comment.value());
303 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700306 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 }
308 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700309}
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311std::unique_ptr<FileReference> TableMerger::CloneAndMangleFile(
312 const std::string& package, const FileReference& file_ref) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 StringPiece prefix, entry, suffix;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 if (util::ExtractResFilePathParts(*file_ref.path, &prefix, &entry, &suffix)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800315 std::string mangled_entry = NameMangler::MangleEntry(package, entry.to_string());
316 std::string newPath = prefix.to_string() + mangled_entry + suffix.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 std::unique_ptr<FileReference> new_file_ref =
Ryan Mitchell4ea90752020-07-31 08:21:43 -0700318 util::make_unique<FileReference>(main_table_->string_pool.MakeRef(newPath));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 new_file_ref->SetComment(file_ref.GetComment());
320 new_file_ref->SetSource(file_ref.GetSource());
Adam Lesinski00451162017-10-03 07:44:08 -0700321 new_file_ref->type = file_ref.type;
322 new_file_ref->file = file_ref.file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 return new_file_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 }
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700325
326 CloningValueTransformer cloner(&main_table_->string_pool);
327 return std::unique_ptr<FileReference>(file_ref.Transform(cloner));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700328}
329
Adam Lesinski00451162017-10-03 07:44:08 -0700330bool TableMerger::MergeFile(const ResourceFile& file_desc, bool overlay, io::IFile* file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 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);
Adam Lesinski00451162017-10-03 07:44:08 -0700336 file_ref->type = file_desc.type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337 file_ref->file = file;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800338
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700339 ResourceTablePackage* pkg = table.FindOrCreatePackage(file_desc.name.package);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 pkg->FindOrCreateType(file_desc.name.type)
341 ->FindOrCreateEntry(file_desc.name.entry)
342 ->FindOrCreateValue(file_desc.config, {})
343 ->value = std::move(file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800344
Fabien Sanglard2369f542019-03-19 08:32:46 -0700345 return DoMerge(file->GetSource(), pkg, false /*mangle*/, overlay /*overlay*/, true /*allow_new*/);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700346}
347
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700348} // namespace aapt