blob: 328ac97090a864b174b58ddea6be5e441946e57f [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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include "link/Linkers.h"
18
19#include <algorithm>
20
21#include "android-base/logging.h"
22
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include "ResourceTable.h"
24#include "SdkConstants.h"
25#include "ValueVisitor.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080026#include "trace/TraceBuffer.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020028using android::ConfigDescription;
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030namespace aapt {
31
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020032bool ShouldGenerateVersionedResource(const ResourceEntry* entry,
33 const ConfigDescription& config,
Adam Lesinskic744ae82017-05-17 19:28:38 -070034 const ApiVersion sdk_version_to_generate) {
35 // We assume the caller is trying to generate a version greater than the current configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 CHECK(sdk_version_to_generate > config.sdkVersion);
Adam Lesinskic744ae82017-05-17 19:28:38 -070037 return sdk_version_to_generate < FindNextApiVersionForConfig(entry, config);
38}
Adam Lesinskifb6312f2016-06-28 14:40:32 -070039
Adam Lesinskic744ae82017-05-17 19:28:38 -070040ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry,
41 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 const auto end_iter = entry->values.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 auto iter = entry->values.begin();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 for (; iter != end_iter; ++iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 if ((*iter)->config == config) {
46 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080047 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 // The source config came from this list, so it should be here.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 CHECK(iter != entry->values.end());
Adam Lesinskibb94f322017-07-12 07:41:55 -070052 ++iter;
53
54 // The next configuration either only varies in sdkVersion, or it is completely different
55 // and therefore incompatible. If it is incompatible, we must generate the versioned resource.
56
57 // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other
58 // qualifiers, so we need to iterate through the entire list to be sure there
59 // are no higher sdk level versions of this resource.
60 ConfigDescription temp_config(config);
61 for (; iter != end_iter; ++iter) {
62 temp_config.sdkVersion = (*iter)->config.sdkVersion;
63 if (temp_config == (*iter)->config) {
64 // The two configs are the same, return the sdkVersion.
65 return (*iter)->config.sdkVersion;
66 }
67 }
68
69 // Didn't find another config with a different sdk version, so return the highest possible value.
70 return std::numeric_limits<ApiVersion>::max();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071}
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073bool AutoVersioner::Consume(IAaptContext* context, ResourceTable* table) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080074 TRACE_NAME("AutoVersioner::Consume");
Ryan Mitchellefcdb952021-04-14 17:31:37 -070075 CloningValueTransformer cloner(&table->string_pool);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 for (auto& package : table->packages) {
77 for (auto& type : package->types) {
78 if (type->type != ResourceType::kStyle) {
79 continue;
80 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 for (auto& entry : type->entries) {
83 for (size_t i = 0; i < entry->values.size(); i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 ResourceConfigValue* config_value = entry->values[i].get();
85 if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 // If this configuration is only used on L-MR1 then we don't need
87 // to do anything since we use private attributes since that
88 // version.
89 continue;
90 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 if (Style* style = ValueCast<Style>(config_value->value.get())) {
Ryan Mitchell4382e442021-07-14 12:53:01 -070093 std::optional<ApiVersion> min_sdk_stripped;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 std::vector<Style::Entry> stripped;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 auto iter = style->entries.begin();
97 while (iter != style->entries.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 CHECK(bool(iter->key.id)) << "IDs must be assigned and linked";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 // Find the SDK level that is higher than the configuration
101 // allows.
Adam Lesinskic744ae82017-05-17 19:28:38 -0700102 const ApiVersion sdk_level = FindAttributeSdkLevel(iter->key.id.value());
103 if (sdk_level > std::max<ApiVersion>(config_value->config.sdkVersion, 1)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 // Record that we are about to strip this.
105 stripped.emplace_back(std::move(*iter));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 // We use the smallest SDK level to generate the new style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 if (min_sdk_stripped) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700109 min_sdk_stripped = std::min(min_sdk_stripped.value(), sdk_level);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 min_sdk_stripped = sdk_level;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113
114 // Erase this from this style.
115 iter = style->entries.erase(iter);
116 continue;
117 }
118 ++iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 if (min_sdk_stripped && !stripped.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 // We found attributes from a higher SDK level. Check that
123 // there is no other defined resource for the version we want to
124 // generate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 if (ShouldGenerateVersionedResource(entry.get(),
126 config_value->config,
127 min_sdk_stripped.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 // Let's create a new Style for this versioned resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700129 ConfigDescription new_config(config_value->config);
Adam Lesinskic744ae82017-05-17 19:28:38 -0700130 new_config.sdkVersion = static_cast<uint16_t>(min_sdk_stripped.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700132 std::unique_ptr<Style> new_style(style->Transform(cloner));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133 new_style->SetComment(style->GetComment());
134 new_style->SetSource(style->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135
136 // Move the previously stripped attributes into this style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 new_style->entries.insert(
138 new_style->entries.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 std::make_move_iterator(stripped.begin()),
140 std::make_move_iterator(stripped.end()));
141
142 // Insert the new Resource into the correct place.
Adam Lesinskic744ae82017-05-17 19:28:38 -0700143 entry->FindOrCreateValue(new_config, {})->value = std::move(new_style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 }
145 }
146 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 }
151 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152}
153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154} // namespace aapt