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 | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 23 | #include "io/File.h" |
| 24 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include "util/Util.h" |
| 26 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 27 | #include <functional> |
| 28 | #include <map> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | |
| 30 | namespace aapt { |
| 31 | |
| 32 | struct FileToMerge { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 33 | /** |
| 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 | |
| 49 | struct TableMergerOptions { |
| 50 | /** |
| 51 | * If true, resources in overlays can be added without previously having existed. |
| 52 | */ |
| 53 | bool autoAddOverlay = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | }; |
| 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 | */ |
| 70 | class TableMerger { |
| 71 | public: |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 72 | /** |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 78 | const std::map<ResourceKeyRef, FileToMerge>& getFilesToMerge() { |
| 79 | return mFilesToMerge; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 82 | const std::set<std::u16string>& getMergedPackages() const { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | return mMergedPackages; |
| 84 | } |
| 85 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 86 | /** |
| 87 | * Merges resources from the same or empty package. This is for local sources. |
| 88 | */ |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 89 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 96 | /** |
| 97 | * 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] | 98 | * 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] | 99 | */ |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 100 | 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 Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 113 | private: |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 114 | using FileMergeCallback = std::function<bool(const ResourceNameRef&, |
| 115 | const ConfigDescription& config, |
| 116 | FileReference*, |
| 117 | FileReference*)>; |
| 118 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | IAaptContext* mContext; |
| 120 | ResourceTable* mMasterTable; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 121 | TableMergerOptions mOptions; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | ResourceTablePackage* mMasterPackage; |
| 123 | |
| 124 | std::set<std::u16string> mMergedPackages; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 125 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | |
| 132 | bool doMerge(const Source& src, ResourceTable* srcTable, ResourceTablePackage* srcPackage, |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 133 | const bool manglePackage, |
| 134 | const bool overlay, |
| 135 | const bool allowNewResources, |
| 136 | FileMergeCallback callback); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 138 | std::unique_ptr<FileReference> cloneAndMangleFile(const std::u16string& package, |
| 139 | const FileReference& value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | } // namespace aapt |
| 143 | |
| 144 | #endif /* AAPT_TABLEMERGER_H */ |