blob: b3c22dd736f4cbf9840d0f1d3e9cde8cd59cf902 [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 {
34 /**
35 * If true, resources in overlays can be added without previously having existed.
36 */
37 bool autoAddOverlay = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038};
39
40/**
41 * TableMerger takes resource tables and merges all packages within the tables that have the same
42 * package ID.
43 *
44 * If a package has a different name, all the entries in that table have their names mangled
45 * to include the package name. This way there are no collisions. In order to do this correctly,
46 * the TableMerger needs to also mangle any FileReference paths. Once these are mangled,
47 * the original source path of the file, along with the new destination path is recorded in the
48 * queue returned from getFileMergeQueue().
49 *
50 * Once the merging is complete, a separate process can go collect the files from the various
51 * source APKs and either copy or process their XML and put them in the correct location in
52 * the final APK.
53 */
54class TableMerger {
55public:
Adam Lesinskia6fe3452015-12-09 15:20:52 -080056 /**
57 * Note: The outTable ResourceTable must live longer than this TableMerger. References
58 * are made to this ResourceTable for efficiency reasons.
59 */
60 TableMerger(IAaptContext* context, ResourceTable* outTable, const TableMergerOptions& options);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061
Adam Lesinskia6fe3452015-12-09 15:20:52 -080062 const std::set<std::u16string>& getMergedPackages() const {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063 return mMergedPackages;
64 }
65
Adam Lesinski83f22552015-11-07 11:51:23 -080066 /**
67 * Merges resources from the same or empty package. This is for local sources.
68 */
Adam Lesinskia6fe3452015-12-09 15:20:52 -080069 bool merge(const Source& src, ResourceTable* table);
70
71 /**
72 * Merges resources from an overlay ResourceTable.
73 */
74 bool mergeOverlay(const Source& src, ResourceTable* table);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075
Adam Lesinski83f22552015-11-07 11:51:23 -080076 /**
77 * Merges resources from the given package, mangling the name. This is for static libraries.
Adam Lesinskia6fe3452015-12-09 15:20:52 -080078 * An io::IFileCollection is needed in order to find the referenced Files and process them.
Adam Lesinski83f22552015-11-07 11:51:23 -080079 */
Adam Lesinskia6fe3452015-12-09 15:20:52 -080080 bool mergeAndMangle(const Source& src, const StringPiece16& package, ResourceTable* table,
81 io::IFileCollection* collection);
82
83 /**
84 * Merges a compiled file that belongs to this same or empty package. This is for local sources.
85 */
86 bool mergeFile(const ResourceFile& fileDesc, io::IFile* file);
87
88 /**
89 * Merges a compiled file from an overlay, overriding an existing definition.
90 */
91 bool mergeFileOverlay(const ResourceFile& fileDesc, io::IFile* file);
Adam Lesinski83f22552015-11-07 11:51:23 -080092
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093private:
Adam Lesinskia6fe3452015-12-09 15:20:52 -080094 using FileMergeCallback = std::function<bool(const ResourceNameRef&,
95 const ConfigDescription& config,
Adam Lesinski355f2852016-02-13 20:26:45 -080096 FileReference*, FileReference*)>;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080097
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098 IAaptContext* mContext;
99 ResourceTable* mMasterTable;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800100 TableMergerOptions mOptions;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700101 ResourceTablePackage* mMasterPackage;
102
103 std::set<std::u16string> mMergedPackages;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800104
105 bool mergeFileImpl(const ResourceFile& fileDesc, io::IFile* file, bool overlay);
106
107 bool mergeImpl(const Source& src, ResourceTable* srcTable,
108 bool overlay, bool allowNew);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109
110 bool doMerge(const Source& src, ResourceTable* srcTable, ResourceTablePackage* srcPackage,
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800111 const bool manglePackage,
112 const bool overlay,
113 const bool allowNewResources,
114 FileMergeCallback callback);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800116 std::unique_ptr<FileReference> cloneAndMangleFile(const std::u16string& package,
117 const FileReference& value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118};
119
120} // namespace aapt
121
122#endif /* AAPT_TABLEMERGER_H */