blob: 73b92542a755fbcbc1f5c541926920fe8ec88781 [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");
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 for (auto& package : table->packages) {
76 for (auto& type : package->types) {
77 if (type->type != ResourceType::kStyle) {
78 continue;
79 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 for (auto& entry : type->entries) {
82 for (size_t i = 0; i < entry->values.size(); i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 ResourceConfigValue* config_value = entry->values[i].get();
84 if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 // If this configuration is only used on L-MR1 then we don't need
86 // to do anything since we use private attributes since that
87 // version.
88 continue;
89 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 if (Style* style = ValueCast<Style>(config_value->value.get())) {
Adam Lesinskic744ae82017-05-17 19:28:38 -070092 Maybe<ApiVersion> min_sdk_stripped;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 std::vector<Style::Entry> stripped;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 auto iter = style->entries.begin();
96 while (iter != style->entries.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 CHECK(bool(iter->key.id)) << "IDs must be assigned and linked";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 // Find the SDK level that is higher than the configuration
100 // allows.
Adam Lesinskic744ae82017-05-17 19:28:38 -0700101 const ApiVersion sdk_level = FindAttributeSdkLevel(iter->key.id.value());
102 if (sdk_level > std::max<ApiVersion>(config_value->config.sdkVersion, 1)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 // Record that we are about to strip this.
104 stripped.emplace_back(std::move(*iter));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 // We use the smallest SDK level to generate the new style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 if (min_sdk_stripped) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700108 min_sdk_stripped = std::min(min_sdk_stripped.value(), sdk_level);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 min_sdk_stripped = sdk_level;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112
113 // Erase this from this style.
114 iter = style->entries.erase(iter);
115 continue;
116 }
117 ++iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 if (min_sdk_stripped && !stripped.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 // We found attributes from a higher SDK level. Check that
122 // there is no other defined resource for the version we want to
123 // generate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 if (ShouldGenerateVersionedResource(entry.get(),
125 config_value->config,
126 min_sdk_stripped.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 // Let's create a new Style for this versioned resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 ConfigDescription new_config(config_value->config);
Adam Lesinskic744ae82017-05-17 19:28:38 -0700129 new_config.sdkVersion = static_cast<uint16_t>(min_sdk_stripped.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130
Adam Lesinskic744ae82017-05-17 19:28:38 -0700131 std::unique_ptr<Style> new_style(style->Clone(&table->string_pool));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 new_style->SetComment(style->GetComment());
133 new_style->SetSource(style->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134
135 // Move the previously stripped attributes into this style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 new_style->entries.insert(
137 new_style->entries.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 std::make_move_iterator(stripped.begin()),
139 std::make_move_iterator(stripped.end()));
140
141 // Insert the new Resource into the correct place.
Adam Lesinskic744ae82017-05-17 19:28:38 -0700142 entry->FindOrCreateValue(new_config, {})->value = std::move(new_style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 }
144 }
145 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 }
150 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151}
152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153} // namespace aapt