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