blob: d941b487e439771daa49f2dc0adc880cc816415a [file] [log] [blame]
Adam Lesinskifb6312f2016-06-28 14:40:32 -07001/*
2 * Copyright (C) 2016 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 Lesinskid48944a2017-02-21 14:22:30 -080017#include "optimize/VersionCollapser.h"
Adam Lesinskifb6312f2016-06-28 14:40:32 -070018
19#include <algorithm>
20#include <vector>
21
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "ResourceTable.h"
23
Adam Lesinskifb6312f2016-06-28 14:40:32 -070024namespace aapt {
25
26template <typename Iterator, typename Pred>
27class FilterIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 public:
29 FilterIterator(Iterator begin, Iterator end, Pred pred = Pred())
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 : current_(begin), end_(end), pred_(pred) {
31 Advance();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070033
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 bool HasNext() { return current_ != end_; }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 Iterator NextIter() {
37 Iterator iter = current_;
38 ++current_;
39 Advance();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 return iter;
41 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070042
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 typename Iterator::reference Next() { return *NextIter(); }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 void Advance() {
47 for (; current_ != end_; ++current_) {
48 if (pred_(*current_)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 return;
50 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070051 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -070053
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 Iterator current_, end_;
55 Pred pred_;
Adam Lesinskifb6312f2016-06-28 14:40:32 -070056};
57
58template <typename Iterator, typename Pred>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059FilterIterator<Iterator, Pred> make_filter_iterator(Iterator begin,
60 Iterator end = Iterator(),
61 Pred pred = Pred()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 return FilterIterator<Iterator, Pred>(begin, end, pred);
Adam Lesinskifb6312f2016-06-28 14:40:32 -070063}
64
65/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 * Every Configuration with an SDK version specified that is less than minSdk
67 * will be removed.
68 * The exception is when there is no exact matching resource for the minSdk. The
69 * next smallest
Adam Lesinskifb6312f2016-06-28 14:40:32 -070070 * one will be kept.
71 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072static void CollapseVersions(int min_sdk, ResourceEntry* entry) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 // First look for all sdks less than minSdk.
74 for (auto iter = entry->values.rbegin(); iter != entry->values.rend();
75 ++iter) {
76 // Check if the item was already marked for removal.
77 if (!(*iter)) {
78 continue;
Adam Lesinskifb6312f2016-06-28 14:40:32 -070079 }
80
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 const ConfigDescription& config = (*iter)->config;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 if (config.sdkVersion <= min_sdk) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 // This is the first configuration we've found with a smaller or equal SDK
84 // level
85 // to the minimum. We MUST keep this one, but remove all others we find,
86 // which get
87 // overridden by this one.
Adam Lesinski87675ad2016-07-15 17:03:03 -070088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 ConfigDescription config_without_sdk = config.CopyWithoutSdkVersion();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 auto pred = [&](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
91 // Check that the value hasn't already been marked for removal.
92 if (!val) {
93 return false;
Adam Lesinski87675ad2016-07-15 17:03:03 -070094 }
Adam Lesinski87675ad2016-07-15 17:03:03 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 // Only return Configs that differ in SDK version.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 config_without_sdk.sdkVersion = val->config.sdkVersion;
98 return config_without_sdk == val->config &&
99 val->config.sdkVersion <= min_sdk;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 };
101
102 // Remove the rest that match.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 auto filter_iter =
104 make_filter_iterator(iter + 1, entry->values.rend(), pred);
105 while (filter_iter.HasNext()) {
106 filter_iter.Next() = {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 }
Adam Lesinski87675ad2016-07-15 17:03:03 -0700108 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 }
110
111 // Now erase the nullptr values.
112 entry->values.erase(
113 std::remove_if(entry->values.begin(), entry->values.end(),
114 [](const std::unique_ptr<ResourceConfigValue>& val)
115 -> bool { return val == nullptr; }),
116 entry->values.end());
117
118 // Strip the version qualifiers for every resource with version <= minSdk.
119 // This will ensure
120 // that the resource entries are all packed together in the same ResTable_type
121 // struct
122 // and take up less space in the resources.arsc table.
123 bool modified = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
125 if (config_value->config.sdkVersion != 0 &&
126 config_value->config.sdkVersion <= min_sdk) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 // Override the resource with a Configuration without an SDK.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700128 std::unique_ptr<ResourceConfigValue> new_value =
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 util::make_unique<ResourceConfigValue>(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 config_value->config.CopyWithoutSdkVersion(),
131 config_value->product);
132 new_value->value = std::move(config_value->value);
133 config_value = std::move(new_value);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134
135 modified = true;
136 }
137 }
138
139 if (modified) {
140 // We've modified the keys (ConfigDescription) by changing the sdkVersion to
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 // 0. We MUST re-sort to ensure ordering guarantees hold.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 std::sort(entry->values.begin(), entry->values.end(),
143 [](const std::unique_ptr<ResourceConfigValue>& a,
144 const std::unique_ptr<ResourceConfigValue>& b) -> bool {
145 return a->config.compare(b->config) < 0;
146 });
147 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700148}
149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150bool VersionCollapser::Consume(IAaptContext* context, ResourceTable* table) {
151 const int min_sdk = context->GetMinSdkVersion();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 for (auto& package : table->packages) {
153 for (auto& type : package->types) {
154 for (auto& entry : type->entries) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 CollapseVersions(min_sdk, entry.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 }
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700157 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 }
159 return true;
Adam Lesinskifb6312f2016-06-28 14:40:32 -0700160}
161
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162} // namespace aapt