blob: 81518ffb244165a667bda2b821b5144dc17d75b4 [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
17#ifndef AAPT_TABLEMERGER_H
18#define AAPT_TABLEMERGER_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <functional>
21#include <map>
22
23#include "android-base/macros.h"
24
Adam Lesinskia6fe3452015-12-09 15:20:52 -080025#include "Resource.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "ResourceTable.h"
27#include "ResourceValues.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080028#include "filter/ConfigFilter.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080029#include "io/File.h"
30#include "process/IResourceTableConsumer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031#include "util/Util.h"
32
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033namespace aapt {
34
Adam Lesinskia6fe3452015-12-09 15:20:52 -080035struct TableMergerOptions {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070036 // If true, resources in overlays can be added without previously having existed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 bool auto_add_overlay = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038};
39
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070040// TableMerger takes resource tables and merges all packages within the tables that have the same
41// package ID.
42//
43// If a package has a different name, all the entries in that table have their names mangled
44// to include the package name. This way there are no collisions. In order to do this correctly,
45// the TableMerger needs to also mangle any FileReference paths. Once these are mangled, the
46// `IFile` pointer in `FileReference` will point to the original file.
47//
48// Once the merging is complete, a separate phase can go collect the files from the various
49// source APKs and either copy or process their XML and put them in the correct location in the
50// final APK.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051class TableMerger {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 public:
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070053 // Note: The out_table ResourceTable must live longer than this TableMerger.
54 // References are made to this ResourceTable for efficiency reasons.
55 TableMerger(IAaptContext* context, ResourceTable* out_table, const TableMergerOptions& options);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070057 inline const std::set<std::string>& merged_packages() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 return merged_packages_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070061 // Merges resources from the same or empty package. This is for local sources.
62 // An io::IFileCollection is optional and used to find the referenced Files and process them.
63 bool Merge(const Source& src, ResourceTable* table, io::IFileCollection* collection = nullptr);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080064
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070065 // Merges resources from an overlay ResourceTable.
66 // An io::IFileCollection is optional and used to find the referenced Files and process them.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 bool MergeOverlay(const Source& src, ResourceTable* table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 io::IFileCollection* collection = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070070 // Merges resources from the given package, mangling the name. This is for static libraries.
71 // An io::IFileCollection is needed in order to find the referenced Files and process them.
Adam Lesinskid5083f62017-01-16 15:07:21 -080072 bool MergeAndMangle(const Source& src, const android::StringPiece& package, ResourceTable* table,
73 io::IFileCollection* collection);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080074
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070075 // Merges a compiled file that belongs to this same or empty package. This is for local sources.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 bool MergeFile(const ResourceFile& fileDesc, io::IFile* file);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080077
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070078 // Merges a compiled file from an overlay, overriding an existing definition.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 bool MergeFileOverlay(const ResourceFile& fileDesc, io::IFile* file);
Adam Lesinski83f22552015-11-07 11:51:23 -080080
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 DISALLOW_COPY_AND_ASSIGN(TableMerger);
83
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 using FileMergeCallback = std::function<bool(const ResourceNameRef&,
85 const ConfigDescription& config,
86 FileReference*, FileReference*)>;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 IAaptContext* context_;
89 ResourceTable* master_table_;
90 TableMergerOptions options_;
91 ResourceTablePackage* master_package_;
92 std::set<std::string> merged_packages_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 bool MergeFileImpl(const ResourceFile& file_desc, io::IFile* file,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 bool overlay);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080096
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 bool MergeImpl(const Source& src, ResourceTable* src_table,
98 io::IFileCollection* collection, bool overlay, bool allow_new);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 bool DoMerge(const Source& src, ResourceTable* src_table,
101 ResourceTablePackage* src_package, const bool mangle_package,
102 const bool overlay, const bool allow_new_resources,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 const FileMergeCallback& callback);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 std::unique_ptr<FileReference> CloneAndMangleFile(const std::string& package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 const FileReference& value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700107};
108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110
111#endif /* AAPT_TABLEMERGER_H */