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 | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <map> |
| 22 | |
| 23 | #include "android-base/macros.h" |
| 24 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 25 | #include "Resource.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "ResourceTable.h" |
| 27 | #include "ResourceValues.h" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 28 | #include "filter/ConfigFilter.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 29 | #include "io/File.h" |
| 30 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | #include "util/Util.h" |
| 32 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | namespace aapt { |
| 34 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 35 | struct TableMergerOptions { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 36 | // If true, resources in overlays can be added without previously having existed. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | bool auto_add_overlay = false; |
Izabela Orlowska | d51efe8 | 2018-04-24 18:18:29 +0100 | [diff] [blame] | 38 | // If true, resource overlays with conflicting visibility are not allowed. |
| 39 | bool strict_visibility = false; |
Donald Chai | 121c6e8 | 2019-06-12 12:51:57 -0700 | [diff] [blame] | 40 | // If true, styles specified via "aapt2 link -R" completely replace any previously-seen resources. |
| 41 | bool override_styles_instead_of_overlaying = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 44 | // TableMerger takes resource tables and merges all packages within the tables that have the same |
| 45 | // package ID. |
| 46 | // |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 47 | // It is assumed that any FileReference values have their io::IFile pointer set to point to the |
| 48 | // file they represent. |
| 49 | // |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 50 | // If a package has a different name, all the entries in that table have their names mangled |
| 51 | // to include the package name. This way there are no collisions. In order to do this correctly, |
| 52 | // the TableMerger needs to also mangle any FileReference paths. Once these are mangled, the |
| 53 | // `IFile` pointer in `FileReference` will point to the original file. |
| 54 | // |
| 55 | // Once the merging is complete, a separate phase can go collect the files from the various |
| 56 | // source APKs and either copy or process their XML and put them in the correct location in the |
| 57 | // final APK. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | class TableMerger { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | public: |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 60 | // Note: The out_table ResourceTable must live longer than this TableMerger. |
| 61 | // References are made to this ResourceTable for efficiency reasons. |
| 62 | TableMerger(IAaptContext* context, ResourceTable* out_table, const TableMergerOptions& options); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 64 | inline const std::set<std::string, std::less<>>& merged_packages() const { |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | return merged_packages_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 68 | // Merges resources from the same or empty package. This is for local sources. |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 69 | // If overlay is true, the resources are treated as overlays. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 70 | bool Merge(const android::Source& src, ResourceTable* table, bool overlay); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 72 | // Merges resources from the given package, mangling the name. This is for static libraries. |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 73 | // All FileReference values must have their io::IFile set. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 74 | bool MergeAndMangle(const android::Source& src, android::StringPiece package, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 75 | ResourceTable* table); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 76 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 77 | // Merges a compiled file that belongs to this same or empty package. |
| 78 | bool MergeFile(const ResourceFile& fileDesc, bool overlay, io::IFile* file); |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 79 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | private: |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | DISALLOW_COPY_AND_ASSIGN(TableMerger); |
| 82 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | IAaptContext* context_; |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 84 | ResourceTable* main_table_; |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | TableMergerOptions options_; |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 86 | ResourceTablePackage* main_package_; |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 87 | std::set<std::string, std::less<>> merged_packages_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 88 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 89 | bool MergeImpl(const android::Source& src, ResourceTable* src_table, bool overlay, |
| 90 | bool allow_new); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 91 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 92 | bool DoMerge(const android::Source& src, ResourceTablePackage* src_package, bool mangle_package, |
Fabien Sanglard | 2369f54 | 2019-03-19 08:32:46 -0700 | [diff] [blame] | 93 | bool overlay, bool allow_new_resources); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 95 | std::unique_ptr<FileReference> CloneAndMangleFile(const std::string& package, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | const FileReference& value); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | |
| 101 | #endif /* AAPT_TABLEMERGER_H */ |