blob: e9375176f26b60d950a996476972a7f516537eab [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)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 : context_(context), master_table_(out_table), options_(options) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 // Create the desired package that all tables will be merged into.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070036 master_package_ =
37 master_table_->CreatePackage(context_->GetCompilationPackage(), context_->GetPackageId());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 CHECK(master_package_ != nullptr) << "package name or ID already taken";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039}
40
Adam Lesinski8780eb62017-10-31 17:44:39 -070041bool TableMerger::Merge(const Source& src, ResourceTable* table, bool overlay) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080042 TRACE_CALL();
Adam Lesinski00451162017-10-03 07:44:08 -070043 // We allow adding new resources if this is not an overlay, or if the options allow overlays
44 // to add new resources.
Adam Lesinski8780eb62017-10-31 17:44:39 -070045 return MergeImpl(src, table, overlay, options_.auto_add_overlay || !overlay /*allow_new*/);
Adam Lesinski64587af2016-02-18 18:33:06 -080046}
47
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070048// This will merge packages with the same package name (or no package name).
Adam Lesinski8780eb62017-10-31 17:44:39 -070049bool TableMerger::MergeImpl(const Source& src, ResourceTable* table, bool overlay, bool allow_new) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 bool error = false;
51 for (auto& package : table->packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 // Only merge an empty package or the package we're building.
53 // Other packages may exist, which likely contain attribute definitions.
54 // This is because at compile time it is unknown if the attributes are
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080055 // simply uses of the attribute or definitions.
56 if (package->name.empty() || context_->GetCompilationPackage() == package->name) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070057 // Merge here. Once the entries are merged and mangled, any references to them are still
58 // valid. This is because un-mangled references are mangled, then looked up at resolution
59 // time. Also, when linking, we convert references with no package name to use the compilation
60 // package name.
Adam Lesinski8780eb62017-10-31 17:44:39 -070061 error |= !DoMerge(src, table, package.get(), false /*mangle*/, overlay, allow_new);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
63 }
64 return !error;
Adam Lesinski83f22552015-11-07 11:51:23 -080065}
66
Adam Lesinski8780eb62017-10-31 17:44:39 -070067// This will merge and mangle resources from a static library. It is assumed that all FileReferences
68// have correctly set their io::IFile*.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070069bool TableMerger::MergeAndMangle(const Source& src, const StringPiece& package_name,
Adam Lesinski8780eb62017-10-31 17:44:39 -070070 ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 bool error = false;
72 for (auto& package : table->packages) {
73 // Warn of packages with an unrelated ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 if (package_name != package->name) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070075 context_->GetDiagnostics()->Warn(DiagMessage(src) << "ignoring package " << package->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 bool mangle = package_name != context_->GetCompilationPackage();
80 merged_packages_.insert(package->name);
Adam Lesinski8780eb62017-10-31 17:44:39 -070081 error |= !DoMerge(src, table, package.get(), mangle, false /*overlay*/, true /*allow_new*/);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 }
83 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084}
85
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070086static bool MergeType(IAaptContext* context, const Source& src, ResourceTableType* dst_type,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 ResourceTableType* src_type) {
Adam Lesinski71be7052017-12-12 16:48:07 -080088 if (src_type->visibility_level > dst_type->visibility_level) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070089 // The incoming type's visibility is stronger, so we should override the visibility.
Adam Lesinski71be7052017-12-12 16:48:07 -080090 if (src_type->visibility_level == Visibility::Level::kPublic) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070091 // Only copy the ID if the source is public, or else the ID is meaningless.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 dst_type->id = src_type->id;
Adam Lesinski5c3464c2016-08-24 16:03:48 -070093 }
Adam Lesinski71be7052017-12-12 16:48:07 -080094 dst_type->visibility_level = src_type->visibility_level;
95 } else if (dst_type->visibility_level == Visibility::Level::kPublic &&
96 src_type->visibility_level == Visibility::Level::kPublic && dst_type->id &&
97 src_type->id && dst_type->id.value() != src_type->id.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 // Both types are public and have different IDs.
Adam Lesinski71be7052017-12-12 16:48:07 -080099 context->GetDiagnostics()->Error(DiagMessage(src) << "cannot merge type '" << src_type->type
100 << "': conflicting public IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 return false;
102 }
103 return true;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700104}
105
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700106static bool MergeEntry(IAaptContext* context, const Source& src,
Izabela Orlowskad51efe82018-04-24 18:18:29 +0100107 ResourceEntry* dst_entry, ResourceEntry* src_entry,
108 bool strict_visibility) {
109 if (strict_visibility
110 && dst_entry->visibility.level != Visibility::Level::kUndefined
111 && src_entry->visibility.level != dst_entry->visibility.level) {
112 context->GetDiagnostics()->Error(
113 DiagMessage(src) << "cannot merge resource '" << dst_entry->name << "' with conflicting visibilities: "
114 << "public and private");
115 return false;
116 }
117
Adam Lesinski71be7052017-12-12 16:48:07 -0800118 // Copy over the strongest visibility.
119 if (src_entry->visibility.level > dst_entry->visibility.level) {
120 // Only copy the ID if the source is public, or else the ID is meaningless.
121 if (src_entry->visibility.level == Visibility::Level::kPublic) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 dst_entry->id = src_entry->id;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700123 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800124 dst_entry->visibility = std::move(src_entry->visibility);
125 } else if (src_entry->visibility.level == Visibility::Level::kPublic &&
126 dst_entry->visibility.level == Visibility::Level::kPublic && dst_entry->id &&
127 src_entry->id && src_entry->id != dst_entry->id) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 // Both entries are public and have different IDs.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700129 context->GetDiagnostics()->Error(DiagMessage(src) << "cannot merge entry '" << src_entry->name
130 << "': conflicting public IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 return false;
132 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800133
134 // Copy over the rest of the properties, if needed.
135 if (src_entry->allow_new) {
136 dst_entry->allow_new = std::move(src_entry->allow_new);
137 }
138
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800139 if (src_entry->overlayable_item) {
140 if (dst_entry->overlayable_item) {
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800141 // Do not allow a resource with an overlayable declaration to have that overlayable
142 // declaration redefined
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800143 context->GetDiagnostics()->Error(DiagMessage(src_entry->overlayable_item.value().source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800144 << "duplicate overlayable declaration for resource '"
145 << src_entry->name << "'");
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800146 context->GetDiagnostics()->Error(DiagMessage(dst_entry->overlayable_item.value().source)
Ryan Mitchell1bb1fe02018-11-16 11:21:41 -0800147 << "previous declaration here");
148 return false;
149 } else {
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800150 dst_entry->overlayable_item = std::move(src_entry->overlayable_item);
Adam Lesinski71be7052017-12-12 16:48:07 -0800151 }
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.
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700163static ResourceTable::CollisionResult ResolveMergeCollision(Value* existing, Value* incoming,
164 StringPool* pool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 if (Styleable* existing_styleable = ValueCast<Styleable>(existing)) {
166 if (Styleable* incoming_styleable = ValueCast<Styleable>(incoming)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 // Styleables get merged.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 existing_styleable->MergeWith(incoming_styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 return ResourceTable::CollisionResult::kKeepOriginal;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700170 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700171 } else if (Style* existing_style = ValueCast<Style>(existing)) {
172 if (Style* incoming_style = ValueCast<Style>(incoming)) {
173 // Styles get merged.
174 existing_style->MergeWith(incoming_style, pool);
175 return ResourceTable::CollisionResult::kKeepOriginal;
176 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 }
178 // Delegate to the default handler.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 return ResourceTable::ResolveValueCollision(existing, incoming);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700180}
181
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700182static ResourceTable::CollisionResult MergeConfigValue(IAaptContext* context,
183 const ResourceNameRef& res_name,
Adam Lesinski8780eb62017-10-31 17:44:39 -0700184 bool overlay,
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700185 ResourceConfigValue* dst_config_value,
186 ResourceConfigValue* src_config_value,
187 StringPool* pool) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 using CollisionResult = ResourceTable::CollisionResult;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700189
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 Value* dst_value = dst_config_value->value.get();
191 Value* src_value = src_config_value->value.get();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700192
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 CollisionResult collision_result;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 if (overlay) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700195 collision_result = ResolveMergeCollision(dst_value, src_value, pool);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700197 collision_result = ResourceTable::ResolveValueCollision(dst_value, src_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 }
199
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700200 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700201 if (overlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 return CollisionResult::kTakeNew;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700203 }
204
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 // Error!
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700206 context->GetDiagnostics()->Error(DiagMessage(src_value->GetSource())
207 << "resource '" << res_name << "' has a conflicting value for "
208 << "configuration (" << src_config_value->config << ")");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 context->GetDiagnostics()->Note(DiagMessage(dst_value->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 << "originally defined here");
211 return CollisionResult::kConflict;
212 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 return collision_result;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700214}
215
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216bool TableMerger::DoMerge(const Source& src, ResourceTable* src_table,
Adam Lesinski8780eb62017-10-31 17:44:39 -0700217 ResourceTablePackage* src_package, bool mangle_package, bool overlay,
218 bool allow_new_resources) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700220
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 for (auto& src_type : src_package->types) {
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700222 ResourceTableType* dst_type = master_package_->FindOrCreateType(src_type->type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 if (!MergeType(context_, src, dst_type, src_type.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 error = true;
225 continue;
226 }
227
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 for (auto& src_entry : src_type->entries) {
229 std::string entry_name = src_entry->name;
230 if (mangle_package) {
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700231 entry_name = NameMangler::MangleEntry(src_package->name, src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 }
233
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 ResourceEntry* dst_entry;
Adam Lesinski71be7052017-12-12 16:48:07 -0800235 if (allow_new_resources || src_entry->allow_new) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 dst_entry = dst_type->FindOrCreateEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 dst_entry = dst_type->FindEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 }
240
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700241 const ResourceNameRef res_name(src_package->name, src_type->type, src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 if (!dst_entry) {
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700244 context_->GetDiagnostics()->Error(DiagMessage(src)
245 << "resource " << res_name
246 << " does not override an existing resource");
247 context_->GetDiagnostics()->Note(DiagMessage(src) << "define an <add-resource> tag or use "
248 << "--auto-add-overlay");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 error = true;
250 continue;
251 }
252
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700253 if (!MergeEntry(context_, src, dst_entry, src_entry.get(), options_.strict_visibility)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 error = true;
255 continue;
256 }
257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 for (auto& src_config_value : src_entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 using CollisionResult = ResourceTable::CollisionResult;
260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 ResourceConfigValue* dst_config_value = dst_entry->FindValue(
262 src_config_value->config, src_config_value->product);
263 if (dst_config_value) {
264 CollisionResult collision_result =
265 MergeConfigValue(context_, res_name, overlay, dst_config_value,
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700266 src_config_value.get(), &master_table_->string_pool);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700268 error = true;
269 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 } else if (collision_result == CollisionResult::kKeepOriginal) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 continue;
272 }
273 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700274 dst_config_value =
275 dst_entry->FindOrCreateValue(src_config_value->config, src_config_value->product);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700276 }
277
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 // Continue if we're taking the new resource.
279
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700280 if (FileReference* f = ValueCast<FileReference>(src_config_value->value.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 std::unique_ptr<FileReference> new_file_ref;
282 if (mangle_package) {
283 new_file_ref = CloneAndMangleFile(src_package->name, *f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700285 new_file_ref = std::unique_ptr<FileReference>(f->Clone(&master_table_->string_pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 dst_config_value->value = std::move(new_file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800288
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 } else {
Ryan Mitchella9e31602018-06-28 16:41:38 -0700290 Maybe<std::string> original_comment = (dst_config_value->value)
291 ? dst_config_value->value->GetComment() : Maybe<std::string>();
292
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 dst_config_value->value = std::unique_ptr<Value>(
294 src_config_value->value->Clone(&master_table_->string_pool));
Ryan Mitchella9e31602018-06-28 16:41:38 -0700295
296 // Keep the comment from the original resource and ignore all comments from overlaying
297 // resources
298 if (overlay && original_comment) {
299 dst_config_value->value->SetComment(original_comment.value());
300 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700301 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700303 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 }
305 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700306}
307
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308std::unique_ptr<FileReference> TableMerger::CloneAndMangleFile(
309 const std::string& package, const FileReference& file_ref) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 StringPiece prefix, entry, suffix;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 if (util::ExtractResFilePathParts(*file_ref.path, &prefix, &entry, &suffix)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800312 std::string mangled_entry = NameMangler::MangleEntry(package, entry.to_string());
313 std::string newPath = prefix.to_string() + mangled_entry + suffix.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 std::unique_ptr<FileReference> new_file_ref =
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700315 util::make_unique<FileReference>(master_table_->string_pool.MakeRef(newPath));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700316 new_file_ref->SetComment(file_ref.GetComment());
317 new_file_ref->SetSource(file_ref.GetSource());
Adam Lesinski00451162017-10-03 07:44:08 -0700318 new_file_ref->type = file_ref.type;
319 new_file_ref->file = file_ref.file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 return new_file_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700322 return std::unique_ptr<FileReference>(file_ref.Clone(&master_table_->string_pool));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700323}
324
Adam Lesinski00451162017-10-03 07:44:08 -0700325bool TableMerger::MergeFile(const ResourceFile& file_desc, bool overlay, io::IFile* file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 std::string path = ResourceUtils::BuildResourceFileName(file_desc);
328 std::unique_ptr<FileReference> file_ref =
329 util::make_unique<FileReference>(table.string_pool.MakeRef(path));
330 file_ref->SetSource(file_desc.source);
Adam Lesinski00451162017-10-03 07:44:08 -0700331 file_ref->type = file_desc.type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 file_ref->file = file;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800333
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700334 ResourceTablePackage* pkg = table.CreatePackage(file_desc.name.package, 0x0);
335 pkg->FindOrCreateType(file_desc.name.type)
336 ->FindOrCreateEntry(file_desc.name.entry)
337 ->FindOrCreateValue(file_desc.config, {})
338 ->value = std::move(file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800339
Adam Lesinski8780eb62017-10-31 17:44:39 -0700340 return DoMerge(file->GetSource(), &table, pkg, false /*mangle*/, overlay /*overlay*/,
341 true /*allow_new*/);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700342}
343
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344} // namespace aapt