Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 17 | #include "optimize/VersionCollapser.h" |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | #include <vector> |
| 21 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "ResourceTable.h" |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 23 | #include "trace/TraceBuffer.h" |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 25 | using android::ConfigDescription; |
| 26 | |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 27 | namespace aapt { |
| 28 | |
| 29 | template <typename Iterator, typename Pred> |
| 30 | class FilterIterator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 31 | public: |
| 32 | FilterIterator(Iterator begin, Iterator end, Pred pred = Pred()) |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | : current_(begin), end_(end), pred_(pred) { |
| 34 | Advance(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | bool HasNext() { return current_ != end_; } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 38 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | Iterator NextIter() { |
| 40 | Iterator iter = current_; |
| 41 | ++current_; |
| 42 | Advance(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | return iter; |
| 44 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 45 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | typename Iterator::reference Next() { return *NextIter(); } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | private: |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | void Advance() { |
| 50 | for (; current_ != end_; ++current_) { |
| 51 | if (pred_(*current_)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 54 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | Iterator current_, end_; |
| 58 | Pred pred_; |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | template <typename Iterator, typename Pred> |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | FilterIterator<Iterator, Pred> make_filter_iterator(Iterator begin, |
| 63 | Iterator end = Iterator(), |
| 64 | Pred pred = Pred()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | return FilterIterator<Iterator, Pred>(begin, end, pred); |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 69 | * Every Configuration with an SDK version specified that is less than minSdk will be removed. The |
| 70 | * exception is when there is no exact matching resource for the minSdk. The next smallest one will |
| 71 | * be kept. |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 72 | */ |
Brandon Liu | 6064447 | 2024-03-14 00:58:18 +0000 | [diff] [blame] | 73 | static void CollapseVersions(IAaptContext* context, int min_sdk, ResourceEntry* entry) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | // First look for all sdks less than minSdk. |
| 75 | for (auto iter = entry->values.rbegin(); iter != entry->values.rend(); |
| 76 | ++iter) { |
| 77 | // Check if the item was already marked for removal. |
| 78 | if (!(*iter)) { |
| 79 | continue; |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | const ConfigDescription& config = (*iter)->config; |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | if (config.sdkVersion <= min_sdk) { |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 84 | // This is the first configuration we've found with a smaller or equal SDK level to the |
| 85 | // minimum. We MUST keep this one, but remove all others we find, which get overridden by this |
| 86 | // one. |
Adam Lesinski | 87675ad | 2016-07-15 17:03:03 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | ConfigDescription config_without_sdk = config.CopyWithoutSdkVersion(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | auto pred = [&](const std::unique_ptr<ResourceConfigValue>& val) -> bool { |
| 90 | // Check that the value hasn't already been marked for removal. |
| 91 | if (!val) { |
| 92 | return false; |
Adam Lesinski | 87675ad | 2016-07-15 17:03:03 -0700 | [diff] [blame] | 93 | } |
Adam Lesinski | 87675ad | 2016-07-15 17:03:03 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | // Only return Configs that differ in SDK version. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | config_without_sdk.sdkVersion = val->config.sdkVersion; |
| 97 | return config_without_sdk == val->config && |
| 98 | val->config.sdkVersion <= min_sdk; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | // Remove the rest that match. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | auto filter_iter = |
| 103 | make_filter_iterator(iter + 1, entry->values.rend(), pred); |
| 104 | while (filter_iter.HasNext()) { |
Brandon Liu | 6064447 | 2024-03-14 00:58:18 +0000 | [diff] [blame] | 105 | auto& next = filter_iter.Next(); |
| 106 | if (context->IsVerbose()) { |
| 107 | context->GetDiagnostics()->Note(android::DiagMessage() |
| 108 | << "removing configuration " << next->config.to_string() |
| 109 | << " for entry: " << entry->name |
| 110 | << ", because its SDK version is smaller than minSdk"); |
| 111 | } |
| 112 | next = {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | } |
Adam Lesinski | 87675ad | 2016-07-15 17:03:03 -0700 | [diff] [blame] | 114 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Now erase the nullptr values. |
| 118 | entry->values.erase( |
| 119 | std::remove_if(entry->values.begin(), entry->values.end(), |
| 120 | [](const std::unique_ptr<ResourceConfigValue>& val) |
| 121 | -> bool { return val == nullptr; }), |
| 122 | entry->values.end()); |
| 123 | |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 124 | // Strip the version qualifiers for every resource with version <= minSdk. This will ensure that |
| 125 | // the resource entries are all packed together in the same ResTable_type struct and take up less |
| 126 | // space in the resources.arsc table. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | bool modified = false; |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) { |
| 129 | if (config_value->config.sdkVersion != 0 && |
| 130 | config_value->config.sdkVersion <= min_sdk) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | // Override the resource with a Configuration without an SDK. |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | std::unique_ptr<ResourceConfigValue> new_value = |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | util::make_unique<ResourceConfigValue>( |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 134 | config_value->config.CopyWithoutSdkVersion(), |
| 135 | config_value->product); |
Brandon Liu | 6064447 | 2024-03-14 00:58:18 +0000 | [diff] [blame] | 136 | if (context->IsVerbose()) { |
| 137 | context->GetDiagnostics()->Note(android::DiagMessage() |
| 138 | << "overriding resource: " << entry->name |
| 139 | << ", removing SDK version from configuration " |
| 140 | << config_value->config.to_string()); |
| 141 | } |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | new_value->value = std::move(config_value->value); |
| 143 | config_value = std::move(new_value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | |
| 145 | modified = true; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (modified) { |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 150 | // We've modified the keys (ConfigDescription) by changing the sdkVersion to 0. We MUST re-sort |
| 151 | // to ensure ordering guarantees hold. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | std::sort(entry->values.begin(), entry->values.end(), |
| 153 | [](const std::unique_ptr<ResourceConfigValue>& a, |
| 154 | const std::unique_ptr<ResourceConfigValue>& b) -> bool { |
| 155 | return a->config.compare(b->config) < 0; |
| 156 | }); |
| 157 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | bool VersionCollapser::Consume(IAaptContext* context, ResourceTable* table) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 161 | TRACE_NAME("VersionCollapser::Consume"); |
Adam Lesinski | ce5e56e2 | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | const int min_sdk = context->GetMinSdkVersion(); |
Brandon Liu | 6064447 | 2024-03-14 00:58:18 +0000 | [diff] [blame] | 163 | if (context->IsVerbose()) { |
| 164 | context->GetDiagnostics()->Note(android::DiagMessage() |
| 165 | << "Running VersionCollapser with minSdk = " << min_sdk); |
| 166 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 167 | for (auto& package : table->packages) { |
| 168 | for (auto& type : package->types) { |
| 169 | for (auto& entry : type->entries) { |
Brandon Liu | 6064447 | 2024-03-14 00:58:18 +0000 | [diff] [blame] | 170 | CollapseVersions(context, min_sdk, entry.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 172 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | } |
| 174 | return true; |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 177 | } // namespace aapt |