blob: 21c6b111b85524d01a928873081a2e4fe49905e3 [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"
24#include "ValueVisitor.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "util/Util.h"
26
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070027using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080028
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029namespace aapt {
30
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031TableMerger::TableMerger(IAaptContext* context, ResourceTable* out_table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 const TableMergerOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 : context_(context), master_table_(out_table), options_(options) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 // Create the desired package that all tables will be merged into.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070035 master_package_ =
36 master_table_->CreatePackage(context_->GetCompilationPackage(), context_->GetPackageId());
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 CHECK(master_package_ != nullptr) << "package name or ID already taken";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038}
39
Adam Lesinski00451162017-10-03 07:44:08 -070040bool TableMerger::Merge(const Source& src, ResourceTable* table, bool overlay,
41 io::IFileCollection* collection) {
42 // We allow adding new resources if this is not an overlay, or if the options allow overlays
43 // to add new resources.
44 return MergeImpl(src, table, collection, overlay,
45 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 Lesinskice5e56e2016-10-21 17:56:45 -070049bool TableMerger::MergeImpl(const Source& src, ResourceTable* table,
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070050 io::IFileCollection* collection, bool overlay, bool allow_new) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 bool error = false;
52 for (auto& package : table->packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 // Only merge an empty package or the package we're building.
54 // Other packages may exist, which likely contain attribute definitions.
55 // This is because at compile time it is unknown if the attributes are
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080056 // simply uses of the attribute or definitions.
57 if (package->name.empty() || context_->GetCompilationPackage() == package->name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 FileMergeCallback callback;
59 if (collection) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070060 callback = [&](const ResourceNameRef& name, const ConfigDescription& config,
61 FileReference* new_file, FileReference* old_file) -> bool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 // The old file's path points inside the APK, so we can use it as is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 io::IFile* f = collection->FindFile(*old_file->path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 if (!f) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080066 << "file '" << *old_file->path << "' not found");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 return false;
68 }
69
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 new_file->file = f;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 return true;
72 };
73 }
74
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070075 // Merge here. Once the entries are merged and mangled, any references to them are still
76 // valid. This is because un-mangled references are mangled, then looked up at resolution
77 // time. Also, when linking, we convert references with no package name to use the compilation
78 // package name.
79 error |=
80 !DoMerge(src, table, package.get(), false /* mangle */, overlay, allow_new, callback);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 }
82 }
83 return !error;
Adam Lesinski83f22552015-11-07 11:51:23 -080084}
85
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070086// This will merge and mangle resources from a static library.
87bool TableMerger::MergeAndMangle(const Source& src, const StringPiece& package_name,
88 ResourceTable* table, io::IFileCollection* collection) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 bool error = false;
90 for (auto& package : table->packages) {
91 // Warn of packages with an unrelated ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 if (package_name != package->name) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070093 context_->GetDiagnostics()->Warn(DiagMessage(src) << "ignoring package " << package->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 bool mangle = package_name != context_->GetCompilationPackage();
98 merged_packages_.insert(package->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700100 auto callback = [&](const ResourceNameRef& name, const ConfigDescription& config,
101 FileReference* new_file, FileReference* old_file) -> bool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 // The old file's path points inside the APK, so we can use it as is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 io::IFile* f = collection->FindFile(*old_file->path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 if (!f) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700105 context_->GetDiagnostics()->Error(DiagMessage(src)
106 << "file '" << *old_file->path << "' not found");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 return false;
108 }
109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 new_file->file = f;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 return true;
112 };
113
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700114 error |= !DoMerge(src, table, package.get(), mangle, false /*overlay*/, true /*allow_new*/,
115 callback);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 }
117 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118}
119
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700120static bool MergeType(IAaptContext* context, const Source& src, ResourceTableType* dst_type,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 ResourceTableType* src_type) {
122 if (dst_type->symbol_status.state < src_type->symbol_status.state) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700123 // The incoming type's visibility is stronger, so we should override the visibility.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 if (src_type->symbol_status.state == SymbolState::kPublic) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700125 // Only copy the ID if the source is public, or else the ID is meaningless.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 dst_type->id = src_type->id;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700127 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 dst_type->symbol_status = std::move(src_type->symbol_status);
129 } else if (dst_type->symbol_status.state == SymbolState::kPublic &&
130 src_type->symbol_status.state == SymbolState::kPublic &&
131 dst_type->id && src_type->id &&
132 dst_type->id.value() != src_type->id.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 // Both types are public and have different IDs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 context->GetDiagnostics()->Error(DiagMessage(src)
135 << "cannot merge type '" << src_type->type
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 << "': conflicting public IDs");
137 return false;
138 }
139 return true;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700140}
141
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700142static bool MergeEntry(IAaptContext* context, const Source& src, ResourceEntry* dst_entry,
143 ResourceEntry* src_entry) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 if (dst_entry->symbol_status.state < src_entry->symbol_status.state) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700145 // The incoming type's visibility is stronger, so we should override the visibility.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 if (src_entry->symbol_status.state == SymbolState::kPublic) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700147 // Only copy the ID if the source is public, or else the ID is meaningless.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 dst_entry->id = src_entry->id;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700149 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 dst_entry->symbol_status = std::move(src_entry->symbol_status);
151 } else if (src_entry->symbol_status.state == SymbolState::kPublic &&
152 dst_entry->symbol_status.state == SymbolState::kPublic &&
153 dst_entry->id && src_entry->id &&
154 dst_entry->id.value() != src_entry->id.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 // Both entries are public and have different IDs.
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700156 context->GetDiagnostics()->Error(DiagMessage(src) << "cannot merge entry '" << src_entry->name
157 << "': conflicting public IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 return false;
159 }
160 return true;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700161}
162
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700163// Modified CollisionResolver which will merge Styleables and Styles. Used with overlays.
164//
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700165// Styleables are not actual resources, but they are treated as such during the compilation phase.
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700166//
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700167// Styleables and Styles don't simply overlay each other, their definitions merge and accumulate.
168// If both values are Styleables/Styles, we just merge them into the existing value.
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700169static ResourceTable::CollisionResult ResolveMergeCollision(Value* existing, Value* incoming,
170 StringPool* pool) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 if (Styleable* existing_styleable = ValueCast<Styleable>(existing)) {
172 if (Styleable* incoming_styleable = ValueCast<Styleable>(incoming)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 // Styleables get merged.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 existing_styleable->MergeWith(incoming_styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 return ResourceTable::CollisionResult::kKeepOriginal;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700176 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700177 } else if (Style* existing_style = ValueCast<Style>(existing)) {
178 if (Style* incoming_style = ValueCast<Style>(incoming)) {
179 // Styles get merged.
180 existing_style->MergeWith(incoming_style, pool);
181 return ResourceTable::CollisionResult::kKeepOriginal;
182 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 }
184 // Delegate to the default handler.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 return ResourceTable::ResolveValueCollision(existing, incoming);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700186}
187
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700188static ResourceTable::CollisionResult MergeConfigValue(IAaptContext* context,
189 const ResourceNameRef& res_name,
190 const bool overlay,
191 ResourceConfigValue* dst_config_value,
192 ResourceConfigValue* src_config_value,
193 StringPool* pool) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 using CollisionResult = ResourceTable::CollisionResult;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700195
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 Value* dst_value = dst_config_value->value.get();
197 Value* src_value = src_config_value->value.get();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 CollisionResult collision_result;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 if (overlay) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700201 collision_result = ResolveMergeCollision(dst_value, src_value, pool);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700203 collision_result = ResourceTable::ResolveValueCollision(dst_value, src_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 }
205
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700207 if (overlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 return CollisionResult::kTakeNew;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700209 }
210
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 // Error!
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700212 context->GetDiagnostics()->Error(DiagMessage(src_value->GetSource())
213 << "resource '" << res_name << "' has a conflicting value for "
214 << "configuration (" << src_config_value->config << ")");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 context->GetDiagnostics()->Note(DiagMessage(dst_value->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 << "originally defined here");
217 return CollisionResult::kConflict;
218 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 return collision_result;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700220}
221
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222bool TableMerger::DoMerge(const Source& src, ResourceTable* src_table,
223 ResourceTablePackage* src_package,
224 const bool mangle_package, const bool overlay,
225 const bool allow_new_resources,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700226 const FileMergeCallback& callback) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700228
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 for (auto& src_type : src_package->types) {
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700230 ResourceTableType* dst_type = master_package_->FindOrCreateType(src_type->type);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700231 if (!MergeType(context_, src, dst_type, src_type.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 error = true;
233 continue;
234 }
235
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 for (auto& src_entry : src_type->entries) {
237 std::string entry_name = src_entry->name;
238 if (mangle_package) {
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700239 entry_name = NameMangler::MangleEntry(src_package->name, src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 }
241
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242 ResourceEntry* dst_entry;
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700243 if (allow_new_resources || src_entry->symbol_status.allow_new) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 dst_entry = dst_type->FindOrCreateEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 dst_entry = dst_type->FindEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 }
248
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700249 const ResourceNameRef res_name(src_package->name, src_type->type, src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251 if (!dst_entry) {
Adam Lesinski4488f1c2017-05-26 17:33:38 -0700252 context_->GetDiagnostics()->Error(DiagMessage(src)
253 << "resource " << res_name
254 << " does not override an existing resource");
255 context_->GetDiagnostics()->Note(DiagMessage(src) << "define an <add-resource> tag or use "
256 << "--auto-add-overlay");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 error = true;
258 continue;
259 }
260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 if (!MergeEntry(context_, src, dst_entry, src_entry.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 error = true;
263 continue;
264 }
265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266 for (auto& src_config_value : src_entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 using CollisionResult = ResourceTable::CollisionResult;
268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 ResourceConfigValue* dst_config_value = dst_entry->FindValue(
270 src_config_value->config, src_config_value->product);
271 if (dst_config_value) {
272 CollisionResult collision_result =
273 MergeConfigValue(context_, res_name, overlay, dst_config_value,
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700274 src_config_value.get(), &master_table_->string_pool);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700276 error = true;
277 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 } else if (collision_result == CollisionResult::kKeepOriginal) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 continue;
280 }
281 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700282 dst_config_value =
283 dst_entry->FindOrCreateValue(src_config_value->config, src_config_value->product);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700284 }
285
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 // Continue if we're taking the new resource.
287
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700288 if (FileReference* f = ValueCast<FileReference>(src_config_value->value.get())) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 std::unique_ptr<FileReference> new_file_ref;
290 if (mangle_package) {
291 new_file_ref = CloneAndMangleFile(src_package->name, *f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 } else {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700293 new_file_ref = std::unique_ptr<FileReference>(f->Clone(&master_table_->string_pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 }
295
296 if (callback) {
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700297 if (!callback(res_name, src_config_value->config, new_file_ref.get(), f)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 error = true;
299 continue;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800300 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 dst_config_value->value = std::move(new_file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800303
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 dst_config_value->value = std::unique_ptr<Value>(
306 src_config_value->value->Clone(&master_table_->string_pool));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700307 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700309 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 }
311 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700312}
313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314std::unique_ptr<FileReference> TableMerger::CloneAndMangleFile(
315 const std::string& package, const FileReference& file_ref) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 StringPiece prefix, entry, suffix;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700317 if (util::ExtractResFilePathParts(*file_ref.path, &prefix, &entry, &suffix)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800318 std::string mangled_entry = NameMangler::MangleEntry(package, entry.to_string());
319 std::string newPath = prefix.to_string() + mangled_entry + suffix.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700320 std::unique_ptr<FileReference> new_file_ref =
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700321 util::make_unique<FileReference>(master_table_->string_pool.MakeRef(newPath));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 new_file_ref->SetComment(file_ref.GetComment());
323 new_file_ref->SetSource(file_ref.GetSource());
Adam Lesinski00451162017-10-03 07:44:08 -0700324 new_file_ref->type = file_ref.type;
325 new_file_ref->file = file_ref.file;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700326 return new_file_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 }
Adam Lesinski5924d8c2017-05-30 15:15:58 -0700328 return std::unique_ptr<FileReference>(file_ref.Clone(&master_table_->string_pool));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700329}
330
Adam Lesinski00451162017-10-03 07:44:08 -0700331bool TableMerger::MergeFile(const ResourceFile& file_desc, bool overlay, io::IFile* file) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 std::string path = ResourceUtils::BuildResourceFileName(file_desc);
334 std::unique_ptr<FileReference> file_ref =
335 util::make_unique<FileReference>(table.string_pool.MakeRef(path));
336 file_ref->SetSource(file_desc.source);
Adam Lesinski00451162017-10-03 07:44:08 -0700337 file_ref->type = file_desc.type;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 file_ref->file = file;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800339
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 ResourceTablePackage* pkg = table.CreatePackage(file_desc.name.package, 0x0);
341 pkg->FindOrCreateType(file_desc.name.type)
342 ->FindOrCreateEntry(file_desc.name.entry)
343 ->FindOrCreateValue(file_desc.config, {})
344 ->value = std::move(file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800345
Adam Lesinski00451162017-10-03 07:44:08 -0700346 return DoMerge(file->GetSource(), &table, pkg, false /* mangle */, overlay /* overlay */,
347 true /* allow_new */, {});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348}
349
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700350} // namespace aapt