blob: 77471ea5d0dade2239e16cacbe6beff668a51491 [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 "ConfigDescription.h"
24#include "ResourceTable.h"
25#include "SdkConstants.h"
26#include "ValueVisitor.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027
28namespace aapt {
29
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030bool ShouldGenerateVersionedResource(const ResourceEntry* entry,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 const ConfigDescription& config,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032 const int sdk_version_to_generate) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 // We assume the caller is trying to generate a version greater than the
34 // current configuration.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 CHECK(sdk_version_to_generate > config.sdkVersion);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 const auto end_iter = entry->values.end();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 auto iter = entry->values.begin();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 for (; iter != end_iter; ++iter) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 if ((*iter)->config == config) {
41 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080042 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 // The source config came from this list, so it should be here.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 CHECK(iter != entry->values.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 ++iter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 // The next configuration either only varies in sdkVersion, or it is
50 // completely different
51 // and therefore incompatible. If it is incompatible, we must generate the
52 // versioned resource.
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 // NOTE: The ordering of configurations takes sdkVersion as higher precedence
55 // than other
56 // qualifiers, so we need to iterate through the entire list to be sure there
57 // are no higher sdk level versions of this resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 ConfigDescription temp_config(config);
59 for (; iter != end_iter; ++iter) {
60 temp_config.sdkVersion = (*iter)->config.sdkVersion;
61 if (temp_config == (*iter)->config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 // The two configs are the same, check the sdk version.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 return sdk_version_to_generate < (*iter)->config.sdkVersion;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 // No match was found, so we should generate the versioned resource.
68 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069}
70
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071bool AutoVersioner::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 for (auto& package : table->packages) {
73 for (auto& type : package->types) {
74 if (type->type != ResourceType::kStyle) {
75 continue;
76 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 for (auto& entry : type->entries) {
79 for (size_t i = 0; i < entry->values.size(); i++) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 ResourceConfigValue* config_value = entry->values[i].get();
81 if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 // If this configuration is only used on L-MR1 then we don't need
83 // to do anything since we use private attributes since that
84 // version.
85 continue;
86 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 if (Style* style = ValueCast<Style>(config_value->value.get())) {
89 Maybe<size_t> min_sdk_stripped;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 std::vector<Style::Entry> stripped;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 auto iter = style->entries.begin();
93 while (iter != style->entries.end()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 CHECK(bool(iter->key.id)) << "IDs must be assigned and linked";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 // Find the SDK level that is higher than the configuration
97 // allows.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 const size_t sdk_level =
99 FindAttributeSdkLevel(iter->key.id.value());
100 if (sdk_level >
101 std::max<size_t>(config_value->config.sdkVersion, 1)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 // Record that we are about to strip this.
103 stripped.emplace_back(std::move(*iter));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 // We use the smallest SDK level to generate the new style.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 if (min_sdk_stripped) {
107 min_sdk_stripped =
108 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);
129 new_config.sdkVersion = min_sdk_stripped.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 std::unique_ptr<Style> new_style(
132 style->Clone(&table->string_pool));
133 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 Lesinskice5e56e2016-10-21 17:56:45 -0700143 entry->FindOrCreateValue(new_config, {})->value =
144 std::move(new_style);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 }
146 }
147 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 }
152 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153}
154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155} // namespace aapt