blob: c2e7181692d44e85d4feac650af2ac0c81da0f2f [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 Lesinskia6fe3452015-12-09 15:20:52 -080020#include "Resource.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "ResourceTable.h"
22#include "ResourceValues.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080023#include "filter/ConfigFilter.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080024#include "io/File.h"
25#include "process/IResourceTableConsumer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "util/Util.h"
27
Adam Lesinskia6fe3452015-12-09 15:20:52 -080028#include <functional>
29#include <map>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030
31namespace aapt {
32
Adam Lesinskia6fe3452015-12-09 15:20:52 -080033struct TableMergerOptions {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 /**
35 * If true, resources in overlays can be added without previously having
36 * existed.
37 */
38 bool autoAddOverlay = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039};
40
41/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 * TableMerger takes resource tables and merges all packages within the tables
43 * that have the same
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044 * package ID.
45 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 * If a package has a different name, all the entries in that table have their
47 * names mangled
48 * to include the package name. This way there are no collisions. In order to do
49 * this correctly,
50 * the TableMerger needs to also mangle any FileReference paths. Once these are
51 * mangled,
52 * the original source path of the file, along with the new destination path is
53 * recorded in the
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054 * queue returned from getFileMergeQueue().
55 *
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 * Once the merging is complete, a separate process can go collect the files
57 * from the various
58 * source APKs and either copy or process their XML and put them in the correct
59 * location in
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060 * the final APK.
61 */
62class TableMerger {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 public:
64 /**
65 * Note: The outTable ResourceTable must live longer than this TableMerger.
66 * References
67 * are made to this ResourceTable for efficiency reasons.
68 */
69 TableMerger(IAaptContext* context, ResourceTable* outTable,
70 const TableMergerOptions& options);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 const std::set<std::string>& getMergedPackages() const {
73 return mMergedPackages;
74 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 /**
77 * Merges resources from the same or empty package. This is for local sources.
78 * An io::IFileCollection is optional and used to find the referenced Files
79 * and process them.
80 */
81 bool merge(const Source& src, ResourceTable* table,
82 io::IFileCollection* collection = nullptr);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 /**
85 * Merges resources from an overlay ResourceTable.
86 * An io::IFileCollection is optional and used to find the referenced Files
87 * and process them.
88 */
89 bool mergeOverlay(const Source& src, ResourceTable* table,
90 io::IFileCollection* collection = nullptr);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 /**
93 * Merges resources from the given package, mangling the name. This is for
94 * static libraries.
95 * An io::IFileCollection is needed in order to find the referenced Files and
96 * process them.
97 */
98 bool mergeAndMangle(const Source& src, const StringPiece& package,
99 ResourceTable* table, io::IFileCollection* collection);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 /**
102 * Merges a compiled file that belongs to this same or empty package. This is
103 * for local sources.
104 */
105 bool mergeFile(const ResourceFile& fileDesc, io::IFile* file);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 /**
108 * Merges a compiled file from an overlay, overriding an existing definition.
109 */
110 bool mergeFileOverlay(const ResourceFile& fileDesc, io::IFile* file);
Adam Lesinski83f22552015-11-07 11:51:23 -0800111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 private:
113 using FileMergeCallback = std::function<bool(const ResourceNameRef&,
114 const ConfigDescription& config,
115 FileReference*, FileReference*)>;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 IAaptContext* mContext;
118 ResourceTable* mMasterTable;
119 TableMergerOptions mOptions;
120 ResourceTablePackage* mMasterPackage;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 std::set<std::string> mMergedPackages;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800123
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 bool mergeFileImpl(const ResourceFile& fileDesc, io::IFile* file,
125 bool overlay);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 bool mergeImpl(const Source& src, ResourceTable* srcTable,
128 io::IFileCollection* collection, bool overlay, bool allowNew);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 bool doMerge(const Source& src, ResourceTable* srcTable,
131 ResourceTablePackage* srcPackage, const bool manglePackage,
132 const bool overlay, const bool allowNewResources,
133 const FileMergeCallback& callback);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 std::unique_ptr<FileReference> cloneAndMangleFile(const std::string& package,
136 const FileReference& value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137};
138
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140
141#endif /* AAPT_TABLEMERGER_H */