blob: 4ab83c3f2de3601a15ce781511e2fedac99b8632 [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 Lesinskicacb28f2016-10-19 12:18:14 -070036 /**
37 * If true, resources in overlays can be added without previously having
38 * existed.
39 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 bool auto_add_overlay = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041};
42
43/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 * TableMerger takes resource tables and merges all packages within the tables
45 * that have the same
Adam Lesinski1ab598f2015-08-14 14:26:04 -070046 * package ID.
47 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 * If a package has a different name, all the entries in that table have their
49 * names mangled
50 * to include the package name. This way there are no collisions. In order to do
51 * this correctly,
52 * the TableMerger needs to also mangle any FileReference paths. Once these are
53 * mangled,
54 * the original source path of the file, along with the new destination path is
55 * recorded in the
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056 * queue returned from getFileMergeQueue().
57 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 * Once the merging is complete, a separate process can go collect the files
59 * from the various
60 * source APKs and either copy or process their XML and put them in the correct
61 * location in
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062 * the final APK.
63 */
64class TableMerger {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 public:
66 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 * Note: The out_table ResourceTable must live longer than this TableMerger.
68 * References are made to this ResourceTable for efficiency reasons.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 TableMerger(IAaptContext* context, ResourceTable* out_table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 const TableMergerOptions& options);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 const std::set<std::string>& merged_packages() const {
74 return merged_packages_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 /**
78 * Merges resources from the same or empty package. This is for local sources.
79 * An io::IFileCollection is optional and used to find the referenced Files
80 * and process them.
81 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 bool Merge(const Source& src, ResourceTable* table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 io::IFileCollection* collection = nullptr);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 /**
86 * Merges resources from an overlay ResourceTable.
87 * An io::IFileCollection is optional and used to find the referenced Files
88 * and process them.
89 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 bool MergeOverlay(const Source& src, ResourceTable* table,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 io::IFileCollection* collection = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 /**
94 * Merges resources from the given package, mangling the name. This is for
95 * static libraries.
96 * An io::IFileCollection is needed in order to find the referenced Files and
97 * process them.
98 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 bool MergeAndMangle(const Source& src, const StringPiece& package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 ResourceTable* table, io::IFileCollection* collection);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800101
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 /**
103 * Merges a compiled file that belongs to this same or empty package. This is
104 * for local sources.
105 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 bool MergeFile(const ResourceFile& fileDesc, io::IFile* file);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800107
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 /**
109 * Merges a compiled file from an overlay, overriding an existing definition.
110 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 bool MergeFileOverlay(const ResourceFile& fileDesc, io::IFile* file);
Adam Lesinski83f22552015-11-07 11:51:23 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 DISALLOW_COPY_AND_ASSIGN(TableMerger);
115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 using FileMergeCallback = std::function<bool(const ResourceNameRef&,
117 const ConfigDescription& config,
118 FileReference*, FileReference*)>;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 IAaptContext* context_;
121 ResourceTable* master_table_;
122 TableMergerOptions options_;
123 ResourceTablePackage* master_package_;
124 std::set<std::string> merged_packages_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 bool MergeFileImpl(const ResourceFile& file_desc, io::IFile* file,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 bool overlay);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800128
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 bool MergeImpl(const Source& src, ResourceTable* src_table,
130 io::IFileCollection* collection, bool overlay, bool allow_new);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 bool DoMerge(const Source& src, ResourceTable* src_table,
133 ResourceTablePackage* src_package, const bool mangle_package,
134 const bool overlay, const bool allow_new_resources,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 const FileMergeCallback& callback);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 std::unique_ptr<FileReference> CloneAndMangleFile(const std::string& package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 const FileReference& value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139};
140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142
143#endif /* AAPT_TABLEMERGER_H */