blob: e1be5d52e9cfe06a68b1c80aadefd20ec864a4db [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 Lesinskia6fe3452015-12-09 15:20:52 -080023#include "io/File.h"
24#include "process/IResourceTableConsumer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include "util/Util.h"
26
Adam Lesinskia6fe3452015-12-09 15:20:52 -080027#include <functional>
28#include <map>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029
30namespace aapt {
31
32struct FileToMerge {
Adam Lesinskia6fe3452015-12-09 15:20:52 -080033 /**
34 * The compiled file from which to read the data.
35 */
36 io::IFile* file;
37
38 /**
39 * Where the original, uncompiled file came from.
40 */
41 Source originalSource;
42
43 /**
44 * The destination path within the APK/archive.
45 */
46 std::string dstPath;
47};
48
49struct TableMergerOptions {
50 /**
51 * If true, resources in overlays can be added without previously having existed.
52 */
53 bool autoAddOverlay = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054};
55
56/**
57 * TableMerger takes resource tables and merges all packages within the tables that have the same
58 * package ID.
59 *
60 * If a package has a different name, all the entries in that table have their names mangled
61 * to include the package name. This way there are no collisions. In order to do this correctly,
62 * the TableMerger needs to also mangle any FileReference paths. Once these are mangled,
63 * the original source path of the file, along with the new destination path is recorded in the
64 * queue returned from getFileMergeQueue().
65 *
66 * Once the merging is complete, a separate process can go collect the files from the various
67 * source APKs and either copy or process their XML and put them in the correct location in
68 * the final APK.
69 */
70class TableMerger {
71public:
Adam Lesinskia6fe3452015-12-09 15:20:52 -080072 /**
73 * Note: The outTable ResourceTable must live longer than this TableMerger. References
74 * are made to this ResourceTable for efficiency reasons.
75 */
76 TableMerger(IAaptContext* context, ResourceTable* outTable, const TableMergerOptions& options);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinskia6fe3452015-12-09 15:20:52 -080078 const std::map<ResourceKeyRef, FileToMerge>& getFilesToMerge() {
79 return mFilesToMerge;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080 }
81
Adam Lesinskia6fe3452015-12-09 15:20:52 -080082 const std::set<std::u16string>& getMergedPackages() const {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083 return mMergedPackages;
84 }
85
Adam Lesinski83f22552015-11-07 11:51:23 -080086 /**
87 * Merges resources from the same or empty package. This is for local sources.
88 */
Adam Lesinskia6fe3452015-12-09 15:20:52 -080089 bool merge(const Source& src, ResourceTable* table);
90
91 /**
92 * Merges resources from an overlay ResourceTable.
93 */
94 bool mergeOverlay(const Source& src, ResourceTable* table);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095
Adam Lesinski83f22552015-11-07 11:51:23 -080096 /**
97 * Merges resources from the given package, mangling the name. This is for static libraries.
Adam Lesinskia6fe3452015-12-09 15:20:52 -080098 * An io::IFileCollection is needed in order to find the referenced Files and process them.
Adam Lesinski83f22552015-11-07 11:51:23 -080099 */
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800100 bool mergeAndMangle(const Source& src, const StringPiece16& package, ResourceTable* table,
101 io::IFileCollection* collection);
102
103 /**
104 * Merges a compiled file that belongs to this same or empty package. This is for local sources.
105 */
106 bool mergeFile(const ResourceFile& fileDesc, io::IFile* file);
107
108 /**
109 * Merges a compiled file from an overlay, overriding an existing definition.
110 */
111 bool mergeFileOverlay(const ResourceFile& fileDesc, io::IFile* file);
Adam Lesinski83f22552015-11-07 11:51:23 -0800112
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700113private:
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800114 using FileMergeCallback = std::function<bool(const ResourceNameRef&,
115 const ConfigDescription& config,
116 FileReference*,
117 FileReference*)>;
118
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119 IAaptContext* mContext;
120 ResourceTable* mMasterTable;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800121 TableMergerOptions mOptions;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122 ResourceTablePackage* mMasterPackage;
123
124 std::set<std::u16string> mMergedPackages;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800125 std::map<ResourceKeyRef, FileToMerge> mFilesToMerge;
126
127 bool mergeFileImpl(const ResourceFile& fileDesc, io::IFile* file, bool overlay);
128
129 bool mergeImpl(const Source& src, ResourceTable* srcTable,
130 bool overlay, bool allowNew);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131
132 bool doMerge(const Source& src, ResourceTable* srcTable, ResourceTablePackage* srcPackage,
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800133 const bool manglePackage,
134 const bool overlay,
135 const bool allowNewResources,
136 FileMergeCallback callback);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800138 std::unique_ptr<FileReference> cloneAndMangleFile(const std::u16string& package,
139 const FileReference& value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140};
141
142} // namespace aapt
143
144#endif /* AAPT_TABLEMERGER_H */