blob: 960c7d46cc987e1e49f5c93cbe6477bd3cd77e3b [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"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020027using android::ConfigDescription;
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029namespace aapt {
30
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020031bool ShouldGenerateVersionedResource(const ResourceEntry* entry,
32 const ConfigDescription& config,
Adam Lesinskic744ae82017-05-17 19:28:38 -070033 const ApiVersion sdk_version_to_generate) {
34 // We assume the caller is trying to generate a version greater than the current configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 CHECK(sdk_version_to_generate > config.sdkVersion);
Adam Lesinskic744ae82017-05-17 19:28:38 -070036 return sdk_version_to_generate < FindNextApiVersionForConfig(entry, config);
37}
Adam Lesinskifb6312f2016-06-28 14:40:32 -070038
Adam Lesinskic744ae82017-05-17 19:28:38 -070039ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry,
40 const ConfigDescription& config) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 const auto end_iter = entry->values.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 auto iter = entry->values.begin();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 for (; iter != end_iter; ++iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 if ((*iter)->config == config) {
45 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080046 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 // The source config came from this list, so it should be here.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 CHECK(iter != entry->values.end());
Adam Lesinskibb94f322017-07-12 07:41:55 -070051 ++iter;
52
53 // The next configuration either only varies in sdkVersion, or it is completely different
54 // and therefore incompatible. If it is incompatible, we must generate the versioned resource.
55
56 // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other
57 // qualifiers, so we need to iterate through the entire list to be sure there
58 // are no higher sdk level versions of this resource.
59 ConfigDescription temp_config(config);
60 for (; iter != end_iter; ++iter) {
61 temp_config.sdkVersion = (*iter)->config.sdkVersion;
62 if (temp_config == (*iter)->config) {
63 // The two configs are the same, return the sdkVersion.
64 return (*iter)->config.sdkVersion;
65 }
66 }
67
68 // Didn't find another config with a different sdk version, so return the highest possible value.
69 return std::numeric_limits<ApiVersion>::max();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070}
71
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072bool AutoVersioner::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 for (auto& package : table->packages) {
74 for (auto& type : package->types) {
75 if (type->type != ResourceType::kStyle) {
76 continue;
77 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 for (auto& entry : type->entries) {
80 for (size_t i = 0; i < entry->values.size(); i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 ResourceConfigValue* config_value = entry->values[i].get();
82 if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 // If this configuration is only used on L-MR1 then we don't need
84 // to do anything since we use private attributes since that
85 // version.
86 continue;
87 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 if (Style* style = ValueCast<Style>(config_value->value.get())) {
Adam Lesinskic744ae82017-05-17 19:28:38 -070090 Maybe<ApiVersion> min_sdk_stripped;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 std::vector<Style::Entry> stripped;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 auto iter = style->entries.begin();
94 while (iter != style->entries.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 CHECK(bool(iter->key.id)) << "IDs must be assigned and linked";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 // Find the SDK level that is higher than the configuration
98 // allows.
Adam Lesinskic744ae82017-05-17 19:28:38 -070099 const ApiVersion sdk_level = FindAttributeSdkLevel(iter->key.id.value());
100 if (sdk_level > std::max<ApiVersion>(config_value->config.sdkVersion, 1)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 // Record that we are about to strip this.
102 stripped.emplace_back(std::move(*iter));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 // We use the smallest SDK level to generate the new style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 if (min_sdk_stripped) {
Adam Lesinskic744ae82017-05-17 19:28:38 -0700106 min_sdk_stripped = std::min(min_sdk_stripped.value(), sdk_level);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 } else {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 min_sdk_stripped = sdk_level;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110
111 // Erase this from this style.
112 iter = style->entries.erase(iter);
113 continue;
114 }
115 ++iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 if (min_sdk_stripped && !stripped.empty()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119 // We found attributes from a higher SDK level. Check that
120 // there is no other defined resource for the version we want to
121 // generate.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 if (ShouldGenerateVersionedResource(entry.get(),
123 config_value->config,
124 min_sdk_stripped.value())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 // Let's create a new Style for this versioned resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 ConfigDescription new_config(config_value->config);
Adam Lesinskic744ae82017-05-17 19:28:38 -0700127 new_config.sdkVersion = static_cast<uint16_t>(min_sdk_stripped.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128
Adam Lesinskic744ae82017-05-17 19:28:38 -0700129 std::unique_ptr<Style> new_style(style->Clone(&table->string_pool));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 new_style->SetComment(style->GetComment());
131 new_style->SetSource(style->GetSource());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132
133 // Move the previously stripped attributes into this style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 new_style->entries.insert(
135 new_style->entries.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 std::make_move_iterator(stripped.begin()),
137 std::make_move_iterator(stripped.end()));
138
139 // Insert the new Resource into the correct place.
Adam Lesinskic744ae82017-05-17 19:28:38 -0700140 entry->FindOrCreateValue(new_config, {})->value = std::move(new_style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 }
142 }
143 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 }
148 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149}
150
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151} // namespace aapt