blob: 37daf42f51e5817a875efa38d38b7e04e9f8bb35 [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 Lesinskice5e56e22016-10-21 17:56:45 -070020#include <functional>
21#include <map>
22
23#include "android-base/macros.h"
24
Adam Lesinskia6fe3452015-12-09 15:20:52 -080025#include "Resource.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "ResourceTable.h"
27#include "ResourceValues.h"
Adam Lesinski6a008172016-02-02 17:02:58 -080028#include "filter/ConfigFilter.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080029#include "io/File.h"
30#include "process/IResourceTableConsumer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031#include "util/Util.h"
32
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033namespace aapt {
34
Adam Lesinskia6fe3452015-12-09 15:20:52 -080035struct TableMergerOptions {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070036 // If true, resources in overlays can be added without previously having existed.
Adam Lesinskice5e56e22016-10-21 17:56:45 -070037 bool auto_add_overlay = false;
Izabela Orlowskad51efe82018-04-24 18:18:29 +010038 // If true, resource overlays with conflicting visibility are not allowed.
39 bool strict_visibility = false;
Donald Chai121c6e82019-06-12 12:51:57 -070040 // If true, styles specified via "aapt2 link -R" completely replace any previously-seen resources.
41 bool override_styles_instead_of_overlaying = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042};
43
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070044// TableMerger takes resource tables and merges all packages within the tables that have the same
45// package ID.
46//
Adam Lesinski8780eb62017-10-31 17:44:39 -070047// It is assumed that any FileReference values have their io::IFile pointer set to point to the
48// file they represent.
49//
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070050// 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 Lesinski1ab598f2015-08-14 14:26:04 -070058class TableMerger {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 public:
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070060 // 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 Lesinski1ab598f2015-08-14 14:26:04 -070063
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070064 inline const std::set<std::string, std::less<>>& merged_packages() const {
Adam Lesinskice5e56e22016-10-21 17:56:45 -070065 return merged_packages_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070068 // Merges resources from the same or empty package. This is for local sources.
Adam Lesinski00451162017-10-03 07:44:08 -070069 // If overlay is true, the resources are treated as overlays.
Jeremy Meyer56f36e82022-05-20 20:35:42 +000070 bool Merge(const android::Source& src, ResourceTable* table, bool overlay);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071
Adam Lesinski1ef0fa92017-08-15 21:32:49 -070072 // Merges resources from the given package, mangling the name. This is for static libraries.
Adam Lesinski8780eb62017-10-31 17:44:39 -070073 // All FileReference values must have their io::IFile set.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070074 bool MergeAndMangle(const android::Source& src, android::StringPiece package,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000075 ResourceTable* table);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080076
Adam Lesinski00451162017-10-03 07:44:08 -070077 // Merges a compiled file that belongs to this same or empty package.
78 bool MergeFile(const ResourceFile& fileDesc, bool overlay, io::IFile* file);
Adam Lesinski83f22552015-11-07 11:51:23 -080079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 private:
Adam Lesinskice5e56e22016-10-21 17:56:45 -070081 DISALLOW_COPY_AND_ASSIGN(TableMerger);
82
Adam Lesinskice5e56e22016-10-21 17:56:45 -070083 IAaptContext* context_;
Ryan Mitchell4ea90752020-07-31 08:21:43 -070084 ResourceTable* main_table_;
Adam Lesinskice5e56e22016-10-21 17:56:45 -070085 TableMergerOptions options_;
Ryan Mitchell4ea90752020-07-31 08:21:43 -070086 ResourceTablePackage* main_package_;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070087 std::set<std::string, std::less<>> merged_packages_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088
Jeremy Meyer56f36e82022-05-20 20:35:42 +000089 bool MergeImpl(const android::Source& src, ResourceTable* src_table, bool overlay,
90 bool allow_new);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091
Jeremy Meyer56f36e82022-05-20 20:35:42 +000092 bool DoMerge(const android::Source& src, ResourceTablePackage* src_package, bool mangle_package,
Fabien Sanglard2369f542019-03-19 08:32:46 -070093 bool overlay, bool allow_new_resources);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094
Adam Lesinskice5e56e22016-10-21 17:56:45 -070095 std::unique_ptr<FileReference> CloneAndMangleFile(const std::string& package,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 const FileReference& value);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097};
98
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100
101#endif /* AAPT_TABLEMERGER_H */