Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 20 | #include "Resource.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "ResourceTable.h" |
| 22 | #include "ResourceValues.h" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame^] | 23 | #include "filter/ConfigFilter.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 24 | #include "io/File.h" |
| 25 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "util/Util.h" |
| 27 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 28 | #include <functional> |
| 29 | #include <map> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | |
| 31 | namespace aapt { |
| 32 | |
| 33 | struct FileToMerge { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 34 | /** |
| 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 | |
| 50 | struct TableMergerOptions { |
| 51 | /** |
| 52 | * If true, resources in overlays can be added without previously having existed. |
| 53 | */ |
| 54 | bool autoAddOverlay = false; |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame^] | 55 | |
| 56 | /** |
| 57 | * A filter that removes resources whose configurations don't match. |
| 58 | */ |
| 59 | IConfigFilter* filter = nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | }; |
| 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 | */ |
| 76 | class TableMerger { |
| 77 | public: |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 78 | /** |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 84 | const std::map<ResourceKeyRef, FileToMerge>& getFilesToMerge() { |
| 85 | return mFilesToMerge; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 88 | const std::set<std::u16string>& getMergedPackages() const { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 89 | return mMergedPackages; |
| 90 | } |
| 91 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Merges resources from the same or empty package. This is for local sources. |
| 94 | */ |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 95 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 102 | /** |
| 103 | * Merges resources from the given package, mangling the name. This is for static libraries. |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 104 | * An io::IFileCollection is needed in order to find the referenced Files and process them. |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 105 | */ |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 106 | 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 Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 118 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | private: |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 120 | using FileMergeCallback = std::function<bool(const ResourceNameRef&, |
| 121 | const ConfigDescription& config, |
| 122 | FileReference*, |
| 123 | FileReference*)>; |
| 124 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | IAaptContext* mContext; |
| 126 | ResourceTable* mMasterTable; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 127 | TableMergerOptions mOptions; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 128 | ResourceTablePackage* mMasterPackage; |
| 129 | |
| 130 | std::set<std::u16string> mMergedPackages; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 131 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | |
| 138 | bool doMerge(const Source& src, ResourceTable* srcTable, ResourceTablePackage* srcPackage, |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 139 | const bool manglePackage, |
| 140 | const bool overlay, |
| 141 | const bool allowNewResources, |
| 142 | FileMergeCallback callback); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 144 | std::unique_ptr<FileReference> cloneAndMangleFile(const std::u16string& package, |
| 145 | const FileReference& value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | } // namespace aapt |
| 149 | |
| 150 | #endif /* AAPT_TABLEMERGER_H */ |