blob: 93110912526313d7ee94ada2164a2df3ceb67dc9 [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 Lesinskid5083f62017-01-16 15:07:21 -080027using android::StringPiece;
28
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 Lesinskice5e56e2016-10-21 17:56:45 -070035 master_package_ = master_table_->CreatePackage(
36 context_->GetCompilationPackage(), context_->GetPackageId());
37 CHECK(master_package_ != nullptr) << "package name or ID already taken";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038}
39
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040bool TableMerger::Merge(const Source& src, ResourceTable* table,
Adam Lesinski64587af2016-02-18 18:33:06 -080041 io::IFileCollection* collection) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080042 return MergeImpl(src, table, collection, false /* overlay */, true /* allow new */);
Adam Lesinski64587af2016-02-18 18:33:06 -080043}
44
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045bool TableMerger::MergeOverlay(const Source& src, ResourceTable* table,
Adam Lesinski64587af2016-02-18 18:33:06 -080046 io::IFileCollection* collection) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080047 return MergeImpl(src, table, collection, true /* overlay */, 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) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 bool error = false;
57 for (auto& package : table->packages) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 // Only merge an empty package or the package we're building.
59 // Other packages may exist, which likely contain attribute definitions.
60 // This is because at compile time it is unknown if the attributes are
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080061 // simply uses of the attribute or definitions.
62 if (package->name.empty() || context_->GetCompilationPackage() == package->name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 FileMergeCallback callback;
64 if (collection) {
65 callback = [&](const ResourceNameRef& name,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 const ConfigDescription& config, FileReference* new_file,
67 FileReference* old_file) -> bool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 // The old file's path points inside the APK, so we can use it as is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 io::IFile* f = collection->FindFile(*old_file->path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 if (!f) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 context_->GetDiagnostics()->Error(DiagMessage(src)
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -080072 << "file '" << *old_file->path << "' not found");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 return false;
74 }
75
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 new_file->file = f;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 return true;
78 };
79 }
80
81 // Merge here. Once the entries are merged and mangled, any references to
82 // them are still valid. This is because un-mangled references are
83 // mangled, then looked up at resolution time.
84 // Also, when linking, we convert references with no package name to use
85 // the compilation package name.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 error |= !DoMerge(src, table, package.get(), false /* mangle */, overlay,
87 allow_new, callback);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 }
89 }
90 return !error;
Adam Lesinski83f22552015-11-07 11:51:23 -080091}
92
93/**
94 * This will merge and mangle resources from a static library.
95 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096bool TableMerger::MergeAndMangle(const Source& src,
97 const StringPiece& package_name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 ResourceTable* table,
99 io::IFileCollection* collection) {
100 bool error = false;
101 for (auto& package : table->packages) {
102 // Warn of packages with an unrelated ID.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 if (package_name != package->name) {
104 context_->GetDiagnostics()->Warn(DiagMessage(src) << "ignoring package "
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 << package->name);
106 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 bool mangle = package_name != context_->GetCompilationPackage();
110 merged_packages_.insert(package->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 auto callback = [&](
113 const ResourceNameRef& name, const ConfigDescription& config,
114 FileReference* new_file, FileReference* old_file) -> bool {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 // The old file's path points inside the APK, so we can use it as is.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 io::IFile* f = collection->FindFile(*old_file->path);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 if (!f) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 context_->GetDiagnostics()->Error(
119 DiagMessage(src) << "file '" << *old_file->path << "' not found");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 return false;
121 }
122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 new_file->file = f;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 return true;
125 };
126
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 error |= !DoMerge(src, table, package.get(), mangle, false /* overlay */,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 true /* allow new */, callback);
129 }
130 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131}
132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133static bool MergeType(IAaptContext* context, const Source& src,
134 ResourceTableType* dst_type,
135 ResourceTableType* src_type) {
136 if (dst_type->symbol_status.state < src_type->symbol_status.state) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 // The incoming type's visibility is stronger, so we should override
138 // the visibility.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 if (src_type->symbol_status.state == SymbolState::kPublic) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 // Only copy the ID if the source is public, or else the ID is
141 // meaningless.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 dst_type->id = src_type->id;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700143 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 dst_type->symbol_status = std::move(src_type->symbol_status);
145 } else if (dst_type->symbol_status.state == SymbolState::kPublic &&
146 src_type->symbol_status.state == SymbolState::kPublic &&
147 dst_type->id && src_type->id &&
148 dst_type->id.value() != src_type->id.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 // Both types are public and have different IDs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 context->GetDiagnostics()->Error(DiagMessage(src)
151 << "cannot merge type '" << src_type->type
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 << "': conflicting public IDs");
153 return false;
154 }
155 return true;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700156}
157
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158static bool MergeEntry(IAaptContext* context, const Source& src,
159 ResourceEntry* dst_entry, ResourceEntry* src_entry) {
160 if (dst_entry->symbol_status.state < src_entry->symbol_status.state) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 // The incoming type's visibility is stronger, so we should override
162 // the visibility.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 if (src_entry->symbol_status.state == SymbolState::kPublic) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 // Only copy the ID if the source is public, or else the ID is
165 // meaningless.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 dst_entry->id = src_entry->id;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700167 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 dst_entry->symbol_status = std::move(src_entry->symbol_status);
169 } else if (src_entry->symbol_status.state == SymbolState::kPublic &&
170 dst_entry->symbol_status.state == SymbolState::kPublic &&
171 dst_entry->id && src_entry->id &&
172 dst_entry->id.value() != src_entry->id.value()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 // Both entries are public and have different IDs.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 context->GetDiagnostics()->Error(
175 DiagMessage(src) << "cannot merge entry '" << src_entry->name
176 << "': conflicting public IDs");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 return false;
178 }
179 return true;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700180}
181
182/**
183 * Modified CollisionResolver which will merge Styleables. Used with overlays.
184 *
185 * Styleables are not actual resources, but they are treated as such during the
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 * compilation phase. Styleables don't simply overlay each other, their
187 * definitions merge
188 * and accumulate. If both values are Styleables, we just merge them into the
189 * existing value.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700190 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191static ResourceTable::CollisionResult ResolveMergeCollision(Value* existing,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 Value* incoming) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193 if (Styleable* existing_styleable = ValueCast<Styleable>(existing)) {
194 if (Styleable* incoming_styleable = ValueCast<Styleable>(incoming)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 // Styleables get merged.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 existing_styleable->MergeWith(incoming_styleable);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 return ResourceTable::CollisionResult::kKeepOriginal;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700198 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 }
200 // Delegate to the default handler.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 return ResourceTable::ResolveValueCollision(existing, incoming);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700202}
203
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700204static ResourceTable::CollisionResult MergeConfigValue(
205 IAaptContext* context, const ResourceNameRef& res_name, const bool overlay,
206 ResourceConfigValue* dst_config_value,
207 ResourceConfigValue* src_config_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208 using CollisionResult = ResourceTable::CollisionResult;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700209
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 Value* dst_value = dst_config_value->value.get();
211 Value* src_value = src_config_value->value.get();
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700212
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 CollisionResult collision_result;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 if (overlay) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 collision_result = ResolveMergeCollision(dst_value, src_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 collision_result =
218 ResourceTable::ResolveValueCollision(dst_value, src_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 }
220
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700222 if (overlay) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 return CollisionResult::kTakeNew;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700224 }
225
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 // Error!
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700227 context->GetDiagnostics()->Error(
228 DiagMessage(src_value->GetSource())
229 << "resource '" << res_name << "' has a conflicting value for "
230 << "configuration (" << src_config_value->config << ")");
231 context->GetDiagnostics()->Note(DiagMessage(dst_value->GetSource())
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 << "originally defined here");
233 return CollisionResult::kConflict;
234 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 return collision_result;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700236}
237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238bool TableMerger::DoMerge(const Source& src, ResourceTable* src_table,
239 ResourceTablePackage* src_package,
240 const bool mangle_package, const bool overlay,
241 const bool allow_new_resources,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700242 const FileMergeCallback& callback) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 bool error = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 for (auto& src_type : src_package->types) {
246 ResourceTableType* dst_type =
247 master_package_->FindOrCreateType(src_type->type);
248 if (!MergeType(context_, src, dst_type, src_type.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 error = true;
250 continue;
251 }
252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 for (auto& src_entry : src_type->entries) {
254 std::string entry_name = src_entry->name;
255 if (mangle_package) {
256 entry_name =
257 NameMangler::MangleEntry(src_package->name, src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 }
259
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 ResourceEntry* dst_entry;
261 if (allow_new_resources) {
262 dst_entry = dst_type->FindOrCreateEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 dst_entry = dst_type->FindEntry(entry_name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 }
266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 const ResourceNameRef res_name(src_package->name, src_type->type,
268 src_entry->name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700270 if (!dst_entry) {
271 context_->GetDiagnostics()->Error(
272 DiagMessage(src) << "resource " << res_name
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 << " does not override an existing resource");
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274 context_->GetDiagnostics()->Note(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 DiagMessage(src) << "define an <add-resource> tag or use "
276 << "--auto-add-overlay");
277 error = true;
278 continue;
279 }
280
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700281 if (!MergeEntry(context_, src, dst_entry, src_entry.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 error = true;
283 continue;
284 }
285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 for (auto& src_config_value : src_entry->values) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 using CollisionResult = ResourceTable::CollisionResult;
288
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 ResourceConfigValue* dst_config_value = dst_entry->FindValue(
290 src_config_value->config, src_config_value->product);
291 if (dst_config_value) {
292 CollisionResult collision_result =
293 MergeConfigValue(context_, res_name, overlay, dst_config_value,
294 src_config_value.get());
295 if (collision_result == CollisionResult::kConflict) {
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700296 error = true;
297 continue;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 } else if (collision_result == CollisionResult::kKeepOriginal) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 continue;
300 }
301 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700302 dst_config_value = dst_entry->FindOrCreateValue(
303 src_config_value->config, src_config_value->product);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700304 }
305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 // Continue if we're taking the new resource.
307
308 if (FileReference* f =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 ValueCast<FileReference>(src_config_value->value.get())) {
310 std::unique_ptr<FileReference> new_file_ref;
311 if (mangle_package) {
312 new_file_ref = CloneAndMangleFile(src_package->name, *f);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 new_file_ref = std::unique_ptr<FileReference>(
315 f->Clone(&master_table_->string_pool));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 }
317
318 if (callback) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 if (!callback(res_name, src_config_value->config,
320 new_file_ref.get(), f)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321 error = true;
322 continue;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800323 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 dst_config_value->value = std::move(new_file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800326
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700327 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328 dst_config_value->value = std::unique_ptr<Value>(
329 src_config_value->value->Clone(&master_table_->string_pool));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700330 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700331 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700332 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333 }
334 return !error;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700335}
336
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700337std::unique_ptr<FileReference> TableMerger::CloneAndMangleFile(
338 const std::string& package, const FileReference& file_ref) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700339 StringPiece prefix, entry, suffix;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700340 if (util::ExtractResFilePathParts(*file_ref.path, &prefix, &entry, &suffix)) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800341 std::string mangled_entry = NameMangler::MangleEntry(package, entry.to_string());
342 std::string newPath = prefix.to_string() + mangled_entry + suffix.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700343 std::unique_ptr<FileReference> new_file_ref =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 util::make_unique<FileReference>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700345 master_table_->string_pool.MakeRef(newPath));
346 new_file_ref->SetComment(file_ref.GetComment());
347 new_file_ref->SetSource(file_ref.GetSource());
348 return new_file_ref;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349 }
350 return std::unique_ptr<FileReference>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700351 file_ref.Clone(&master_table_->string_pool));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700352}
353
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700354bool TableMerger::MergeFileImpl(const ResourceFile& file_desc, io::IFile* file,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 bool overlay) {
356 ResourceTable table;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700357 std::string path = ResourceUtils::BuildResourceFileName(file_desc);
358 std::unique_ptr<FileReference> file_ref =
359 util::make_unique<FileReference>(table.string_pool.MakeRef(path));
360 file_ref->SetSource(file_desc.source);
361 file_ref->file = file;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800362
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700363 ResourceTablePackage* pkg = table.CreatePackage(file_desc.name.package, 0x0);
364 pkg->FindOrCreateType(file_desc.name.type)
365 ->FindOrCreateEntry(file_desc.name.entry)
366 ->FindOrCreateValue(file_desc.config, {})
367 ->value = std::move(file_ref);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800368
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700369 return DoMerge(file->GetSource(), &table, pkg, false /* mangle */,
370 overlay /* overlay */, true /* allow_new */, {});
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800371}
372
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700373bool TableMerger::MergeFile(const ResourceFile& file_desc, io::IFile* file) {
374 return MergeFileImpl(file_desc, file, false /* overlay */);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800375}
376
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700377bool TableMerger::MergeFileOverlay(const ResourceFile& file_desc,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700378 io::IFile* file) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379 return MergeFileImpl(file_desc, file, true /* overlay */);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700380}
381
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700382} // namespace aapt