blob: 4539679fa7699fca74870a47525cf03f67b3bfc2 [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
33struct FileToMerge {
Adam Lesinskia6fe3452015-12-09 15:20:52 -080034 /**
35 * The compiled file from which to read the data.
36 */
37 io::IFile* file;
38
39 /**
40 * Where the original, uncompiled file came from.
41 */
42 Source originalSource;
43
44 /**
45 * The destination path within the APK/archive.
46 */
47 std::string dstPath;
48};
49
50struct TableMergerOptions {
51 /**
52 * If true, resources in overlays can be added without previously having existed.
53 */
54 bool autoAddOverlay = false;
Adam Lesinski6a008172016-02-02 17:02:58 -080055
56 /**
57 * A filter that removes resources whose configurations don't match.
58 */
59 IConfigFilter* filter = nullptr;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060};
61
62/**
63 * TableMerger takes resource tables and merges all packages within the tables that have the same
64 * package ID.
65 *
66 * If a package has a different name, all the entries in that table have their names mangled
67 * to include the package name. This way there are no collisions. In order to do this correctly,
68 * the TableMerger needs to also mangle any FileReference paths. Once these are mangled,
69 * the original source path of the file, along with the new destination path is recorded in the
70 * queue returned from getFileMergeQueue().
71 *
72 * Once the merging is complete, a separate process can go collect the files from the various
73 * source APKs and either copy or process their XML and put them in the correct location in
74 * the final APK.
75 */
76class TableMerger {
77public:
Adam Lesinskia6fe3452015-12-09 15:20:52 -080078 /**
79 * Note: The outTable ResourceTable must live longer than this TableMerger. References
80 * are made to this ResourceTable for efficiency reasons.
81 */
82 TableMerger(IAaptContext* context, ResourceTable* outTable, const TableMergerOptions& options);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083
Adam Lesinskia6fe3452015-12-09 15:20:52 -080084 const std::map<ResourceKeyRef, FileToMerge>& getFilesToMerge() {
85 return mFilesToMerge;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070086 }
87
Adam Lesinskia6fe3452015-12-09 15:20:52 -080088 const std::set<std::u16string>& getMergedPackages() const {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089 return mMergedPackages;
90 }
91
Adam Lesinski83f22552015-11-07 11:51:23 -080092 /**
93 * Merges resources from the same or empty package. This is for local sources.
94 */
Adam Lesinskia6fe3452015-12-09 15:20:52 -080095 bool merge(const Source& src, ResourceTable* table);
96
97 /**
98 * Merges resources from an overlay ResourceTable.
99 */
100 bool mergeOverlay(const Source& src, ResourceTable* table);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101
Adam Lesinski83f22552015-11-07 11:51:23 -0800102 /**
103 * Merges resources from the given package, mangling the name. This is for static libraries.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800104 * An io::IFileCollection is needed in order to find the referenced Files and process them.
Adam Lesinski83f22552015-11-07 11:51:23 -0800105 */
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800106 bool mergeAndMangle(const Source& src, const StringPiece16& package, ResourceTable* table,
107 io::IFileCollection* collection);
108
109 /**
110 * Merges a compiled file that belongs to this same or empty package. This is for local sources.
111 */
112 bool mergeFile(const ResourceFile& fileDesc, io::IFile* file);
113
114 /**
115 * Merges a compiled file from an overlay, overriding an existing definition.
116 */
117 bool mergeFileOverlay(const ResourceFile& fileDesc, io::IFile* file);
Adam Lesinski83f22552015-11-07 11:51:23 -0800118
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119private:
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800120 using FileMergeCallback = std::function<bool(const ResourceNameRef&,
121 const ConfigDescription& config,
122 FileReference*,
123 FileReference*)>;
124
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700125 IAaptContext* mContext;
126 ResourceTable* mMasterTable;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800127 TableMergerOptions mOptions;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128 ResourceTablePackage* mMasterPackage;
129
130 std::set<std::u16string> mMergedPackages;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800131 std::map<ResourceKeyRef, FileToMerge> mFilesToMerge;
132
133 bool mergeFileImpl(const ResourceFile& fileDesc, io::IFile* file, bool overlay);
134
135 bool mergeImpl(const Source& src, ResourceTable* srcTable,
136 bool overlay, bool allowNew);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700137
138 bool doMerge(const Source& src, ResourceTable* srcTable, ResourceTablePackage* srcPackage,
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800139 const bool manglePackage,
140 const bool overlay,
141 const bool allowNewResources,
142 FileMergeCallback callback);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800144 std::unique_ptr<FileReference> cloneAndMangleFile(const std::u16string& package,
145 const FileReference& value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146};
147
148} // namespace aapt
149
150#endif /* AAPT_TABLEMERGER_H */