blob: d808da31d3b3633975266fa675f2c0be4b145b61 [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 Lesinski1ab598f2015-08-14 14:26:04 -070027namespace aapt {
28
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029TableMerger::TableMerger(IAaptContext* context, ResourceTable* out_table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 const TableMergerOptions& options)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031 : context_(context), master_table_(out_table), options_(options) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 // Create the desired package that all tables will be merged into.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033 master_package_ = master_table_->CreatePackage(
34 context_->GetCompilationPackage(), context_->GetPackageId());
35 CHECK(master_package_ != nullptr) << "package name or ID already taken";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036}
37
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038bool TableMerger::Merge(const Source& src, ResourceTable* table,
Adam Lesinski64587af2016-02-18 18:33:06 -080039 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 return MergeImpl(src, table, collection, false /* overlay */,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 true /* allow new */);
Adam Lesinski64587af2016-02-18 18:33:06 -080042}
43
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044bool TableMerger::MergeOverlay(const Source& src, ResourceTable* table,
Adam Lesinski64587af2016-02-18 18:33:06 -080045 io::IFileCollection* collection) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 return MergeImpl(src, table, collection, true /* overlay */,
47 options_.auto_add_overlay);
Adam Lesinski64587af2016-02-18 18:33:06 -080048}
49
Adam Lesinski83f22552015-11-07 11:51:23 -080050/**
51 * This will merge packages with the same package name (or no package name).
52 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053bool TableMerger::MergeImpl(const Source& src, ResourceTable* table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 io::IFileCollection* collection, bool overlay,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 bool allow_new) {
56 const uint8_t desired_package_id = context_->GetPackageId();
Adam Lesinski085f4952016-08-30 14:25:51 -070057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 bool error = false;
59 for (auto& package : table->packages) {
60 // Warn of packages with an unrelated ID.
61 const Maybe<ResourceId>& id = package->id;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 if (id && id.value() != 0x0 && id.value() != desired_package_id) {
63 context_->GetDiagnostics()->Warn(DiagMessage(src) << "ignoring package "
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 << package->name);
65 continue;
Adam Lesinski83f22552015-11-07 11:51:23 -080066 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067
68 // Only merge an empty package or the package we're building.
69 // Other packages may exist, which likely contain attribute definitions.
70 // This is because at compile time it is unknown if the attributes are
71 // simply
72 // uses of the attribute or definitions.
73 if (package->name.empty() ||
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 context_->GetCompilationPackage() == package->name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 FileMergeCallback callback;
76 if (collection) {
77 callback = [&](const ResourceNameRef& name,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 const ConfigDescription& config, FileReference* new_file,
79 FileReference* old_file) -> bool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 // The old file's path points inside the APK, so we can use it as is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 io::IFile* f = collection->FindFile(*old_file->path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 if (!f) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 context_->GetDiagnostics()->Error(DiagMessage(src)
84 << "file '" << *old_file->path
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 << "' not found");
86 return false;
87 }
88
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 new_file->file = f;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 return true;
91 };
92 }
93
94 // Merge here. Once the entries are merged and mangled, any references to
95 // them are still valid. This is because un-mangled references are
96 // mangled, then looked up at resolution time.
97 // Also, when linking, we convert references with no package name to use
98 // the compilation package name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 error |= !DoMerge(src, table, package.get(), false /* mangle */, overlay,
100 allow_new, callback);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 }
102 }
103 return !error;
Adam Lesinski83f22552015-11-07 11:51:23 -0800104}
105
106/**
107 * This will merge and mangle resources from a static library.
108 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109bool TableMerger::MergeAndMangle(const Source& src,
110 const StringPiece& package_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 ResourceTable* table,
112 io::IFileCollection* collection) {
113 bool error = false;
114 for (auto& package : table->packages) {
115 // Warn of packages with an unrelated ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 if (package_name != package->name) {
117 context_->GetDiagnostics()->Warn(DiagMessage(src) << "ignoring package "
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 << package->name);
119 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 bool mangle = package_name != context_->GetCompilationPackage();
123 merged_packages_.insert(package->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 auto callback = [&](
126 const ResourceNameRef& name, const ConfigDescription& config,
127 FileReference* new_file, FileReference* old_file) -> bool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 // The old file's path points inside the APK, so we can use it as is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 io::IFile* f = collection->FindFile(*old_file->path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 if (!f) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 context_->GetDiagnostics()->Error(
132 DiagMessage(src) << "file '" << *old_file->path << "' not found");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 return false;
134 }
135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 new_file->file = f;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 return true;
138 };
139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 error |= !DoMerge(src, table, package.get(), mangle, false /* overlay */,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 true /* allow new */, callback);
142 }
143 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144}
145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146static bool MergeType(IAaptContext* context, const Source& src,
147 ResourceTableType* dst_type,
148 ResourceTableType* src_type) {
149 if (dst_type->symbol_status.state < src_type->symbol_status.state) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 // The incoming type's visibility is stronger, so we should override
151 // the visibility.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 if (src_type->symbol_status.state == SymbolState::kPublic) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 // Only copy the ID if the source is public, or else the ID is
154 // meaningless.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 dst_type->id = src_type->id;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700156 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 dst_type->symbol_status = std::move(src_type->symbol_status);
158 } else if (dst_type->symbol_status.state == SymbolState::kPublic &&
159 src_type->symbol_status.state == SymbolState::kPublic &&
160 dst_type->id && src_type->id &&
161 dst_type->id.value() != src_type->id.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 // Both types are public and have different IDs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 context->GetDiagnostics()->Error(DiagMessage(src)
164 << "cannot merge type '" << src_type->type
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 << "': conflicting public IDs");
166 return false;
167 }
168 return true;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700169}
170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171static bool MergeEntry(IAaptContext* context, const Source& src,
172 ResourceEntry* dst_entry, ResourceEntry* src_entry) {
173 if (dst_entry->symbol_status.state < src_entry->symbol_status.state) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 // The incoming type's visibility is stronger, so we should override
175 // the visibility.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 if (src_entry->symbol_status.state == SymbolState::kPublic) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 // Only copy the ID if the source is public, or else the ID is
178 // meaningless.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 dst_entry->id = src_entry->id;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700180 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 dst_entry->symbol_status = std::move(src_entry->symbol_status);
182 } else if (src_entry->symbol_status.state == SymbolState::kPublic &&
183 dst_entry->symbol_status.state == SymbolState::kPublic &&
184 dst_entry->id && src_entry->id &&
185 dst_entry->id.value() != src_entry->id.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 // Both entries are public and have different IDs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 context->GetDiagnostics()->Error(
188 DiagMessage(src) << "cannot merge entry '" << src_entry->name
189 << "': conflicting public IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 return false;
191 }
192 return true;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700193}
194
195/**
196 * Modified CollisionResolver which will merge Styleables. Used with overlays.
197 *
198 * Styleables are not actual resources, but they are treated as such during the
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 * compilation phase. Styleables don't simply overlay each other, their
200 * definitions merge
201 * and accumulate. If both values are Styleables, we just merge them into the
202 * existing value.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700203 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204static ResourceTable::CollisionResult ResolveMergeCollision(Value* existing,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 Value* incoming) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700206 if (Styleable* existing_styleable = ValueCast<Styleable>(existing)) {
207 if (Styleable* incoming_styleable = ValueCast<Styleable>(incoming)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 // Styleables get merged.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 existing_styleable->MergeWith(incoming_styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 return ResourceTable::CollisionResult::kKeepOriginal;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700211 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 }
213 // Delegate to the default handler.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700214 return ResourceTable::ResolveValueCollision(existing, incoming);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700215}
216
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217static ResourceTable::CollisionResult MergeConfigValue(
218 IAaptContext* context, const ResourceNameRef& res_name, const bool overlay,
219 ResourceConfigValue* dst_config_value,
220 ResourceConfigValue* src_config_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 using CollisionResult = ResourceTable::CollisionResult;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 Value* dst_value = dst_config_value->value.get();
224 Value* src_value = src_config_value->value.get();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700225
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700226 CollisionResult collision_result;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227 if (overlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 collision_result = ResolveMergeCollision(dst_value, src_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 collision_result =
231 ResourceTable::ResolveValueCollision(dst_value, src_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 }
233
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700235 if (overlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 return CollisionResult::kTakeNew;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700237 }
238
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 // Error!
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 context->GetDiagnostics()->Error(
241 DiagMessage(src_value->GetSource())
242 << "resource '" << res_name << "' has a conflicting value for "
243 << "configuration (" << src_config_value->config << ")");
244 context->GetDiagnostics()->Note(DiagMessage(dst_value->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 << "originally defined here");
246 return CollisionResult::kConflict;
247 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 return collision_result;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700249}
250
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700251bool TableMerger::DoMerge(const Source& src, ResourceTable* src_table,
252 ResourceTablePackage* src_package,
253 const bool mangle_package, const bool overlay,
254 const bool allow_new_resources,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700255 const FileMergeCallback& callback) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258 for (auto& src_type : src_package->types) {
259 ResourceTableType* dst_type =
260 master_package_->FindOrCreateType(src_type->type);
261 if (!MergeType(context_, src, dst_type, src_type.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_entry : src_type->entries) {
267 std::string entry_name = src_entry->name;
268 if (mangle_package) {
269 entry_name =
270 NameMangler::MangleEntry(src_package->name, src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 }
272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 ResourceEntry* dst_entry;
274 if (allow_new_resources) {
275 dst_entry = dst_type->FindOrCreateEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700277 dst_entry = dst_type->FindEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 }
279
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 const ResourceNameRef res_name(src_package->name, src_type->type,
281 src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283 if (!dst_entry) {
284 context_->GetDiagnostics()->Error(
285 DiagMessage(src) << "resource " << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 << " does not override an existing resource");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 DiagMessage(src) << "define an <add-resource> tag or use "
289 << "--auto-add-overlay");
290 error = true;
291 continue;
292 }
293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294 if (!MergeEntry(context_, src, dst_entry, src_entry.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 error = true;
296 continue;
297 }
298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 for (auto& src_config_value : src_entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 using CollisionResult = ResourceTable::CollisionResult;
301
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 ResourceConfigValue* dst_config_value = dst_entry->FindValue(
303 src_config_value->config, src_config_value->product);
304 if (dst_config_value) {
305 CollisionResult collision_result =
306 MergeConfigValue(context_, res_name, overlay, dst_config_value,
307 src_config_value.get());
308 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700309 error = true;
310 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 } else if (collision_result == CollisionResult::kKeepOriginal) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 continue;
313 }
314 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 dst_config_value = dst_entry->FindOrCreateValue(
316 src_config_value->config, src_config_value->product);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700317 }
318
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319 // Continue if we're taking the new resource.
320
321 if (FileReference* f =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700322 ValueCast<FileReference>(src_config_value->value.get())) {
323 std::unique_ptr<FileReference> new_file_ref;
324 if (mangle_package) {
325 new_file_ref = CloneAndMangleFile(src_package->name, *f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 new_file_ref = std::unique_ptr<FileReference>(
328 f->Clone(&master_table_->string_pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329 }
330
331 if (callback) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332 if (!callback(res_name, src_config_value->config,
333 new_file_ref.get(), f)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700334 error = true;
335 continue;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800336 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700338 dst_config_value->value = std::move(new_file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800339
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700341 dst_config_value->value = std::unique_ptr<Value>(
342 src_config_value->value->Clone(&master_table_->string_pool));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700343 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700345 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700346 }
347 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700348}
349
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700350std::unique_ptr<FileReference> TableMerger::CloneAndMangleFile(
351 const std::string& package, const FileReference& file_ref) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 StringPiece prefix, entry, suffix;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700353 if (util::ExtractResFilePathParts(*file_ref.path, &prefix, &entry, &suffix)) {
354 std::string mangled_entry =
355 NameMangler::MangleEntry(package, entry.ToString());
356 std::string newPath = prefix.ToString() + mangled_entry + suffix.ToString();
357 std::unique_ptr<FileReference> new_file_ref =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700358 util::make_unique<FileReference>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 master_table_->string_pool.MakeRef(newPath));
360 new_file_ref->SetComment(file_ref.GetComment());
361 new_file_ref->SetSource(file_ref.GetSource());
362 return new_file_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700363 }
364 return std::unique_ptr<FileReference>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700365 file_ref.Clone(&master_table_->string_pool));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700366}
367
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700368bool TableMerger::MergeFileImpl(const ResourceFile& file_desc, io::IFile* file,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 bool overlay) {
370 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700371 std::string path = ResourceUtils::BuildResourceFileName(file_desc);
372 std::unique_ptr<FileReference> file_ref =
373 util::make_unique<FileReference>(table.string_pool.MakeRef(path));
374 file_ref->SetSource(file_desc.source);
375 file_ref->file = file;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377 ResourceTablePackage* pkg = table.CreatePackage(file_desc.name.package, 0x0);
378 pkg->FindOrCreateType(file_desc.name.type)
379 ->FindOrCreateEntry(file_desc.name.entry)
380 ->FindOrCreateValue(file_desc.config, {})
381 ->value = std::move(file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800382
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700383 return DoMerge(file->GetSource(), &table, pkg, false /* mangle */,
384 overlay /* overlay */, true /* allow_new */, {});
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800385}
386
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700387bool TableMerger::MergeFile(const ResourceFile& file_desc, io::IFile* file) {
388 return MergeFileImpl(file_desc, file, false /* overlay */);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800389}
390
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391bool TableMerger::MergeFileOverlay(const ResourceFile& file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700392 io::IFile* file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700393 return MergeFileImpl(file_desc, file, true /* overlay */);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700394}
395
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700396} // namespace aapt