Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include "link/Linkers.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | |
| 21 | #include "android-base/logging.h" |
| 22 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | #include "ConfigDescription.h" |
| 24 | #include "ResourceTable.h" |
| 25 | #include "SdkConstants.h" |
| 26 | #include "ValueVisitor.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | |
| 28 | namespace aapt { |
| 29 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 30 | bool ShouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config, |
| 31 | const ApiVersion sdk_version_to_generate) { |
| 32 | // We assume the caller is trying to generate a version greater than the current configuration. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | CHECK(sdk_version_to_generate > config.sdkVersion); |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 34 | return sdk_version_to_generate < FindNextApiVersionForConfig(entry, config); |
| 35 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 37 | ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry, |
| 38 | const ConfigDescription& config) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | const auto end_iter = entry->values.end(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | auto iter = entry->values.begin(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | for (; iter != end_iter; ++iter) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | if ((*iter)->config == config) { |
| 43 | break; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 44 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | // The source config came from this list, so it should be here. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | CHECK(iter != entry->values.end()); |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame^] | 49 | ++iter; |
| 50 | |
| 51 | // The next configuration either only varies in sdkVersion, or it is completely different |
| 52 | // and therefore incompatible. If it is incompatible, we must generate the versioned resource. |
| 53 | |
| 54 | // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other |
| 55 | // qualifiers, so we need to iterate through the entire list to be sure there |
| 56 | // are no higher sdk level versions of this resource. |
| 57 | ConfigDescription temp_config(config); |
| 58 | for (; iter != end_iter; ++iter) { |
| 59 | temp_config.sdkVersion = (*iter)->config.sdkVersion; |
| 60 | if (temp_config == (*iter)->config) { |
| 61 | // The two configs are the same, return the sdkVersion. |
| 62 | return (*iter)->config.sdkVersion; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Didn't find another config with a different sdk version, so return the highest possible value. |
| 67 | return std::numeric_limits<ApiVersion>::max(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | bool AutoVersioner::Consume(IAaptContext* context, ResourceTable* table) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | for (auto& package : table->packages) { |
| 72 | for (auto& type : package->types) { |
| 73 | if (type->type != ResourceType::kStyle) { |
| 74 | continue; |
| 75 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | for (auto& entry : type->entries) { |
| 78 | for (size_t i = 0; i < entry->values.size(); i++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | ResourceConfigValue* config_value = entry->values[i].get(); |
| 80 | if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 81 | // If this configuration is only used on L-MR1 then we don't need |
| 82 | // to do anything since we use private attributes since that |
| 83 | // version. |
| 84 | continue; |
| 85 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | if (Style* style = ValueCast<Style>(config_value->value.get())) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 88 | Maybe<ApiVersion> min_sdk_stripped; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | std::vector<Style::Entry> stripped; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | auto iter = style->entries.begin(); |
| 92 | while (iter != style->entries.end()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | CHECK(bool(iter->key.id)) << "IDs must be assigned and linked"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | // Find the SDK level that is higher than the configuration |
| 96 | // allows. |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 97 | const ApiVersion sdk_level = FindAttributeSdkLevel(iter->key.id.value()); |
| 98 | if (sdk_level > std::max<ApiVersion>(config_value->config.sdkVersion, 1)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | // Record that we are about to strip this. |
| 100 | stripped.emplace_back(std::move(*iter)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | // We use the smallest SDK level to generate the new style. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | if (min_sdk_stripped) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 104 | min_sdk_stripped = std::min(min_sdk_stripped.value(), sdk_level); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | min_sdk_stripped = sdk_level; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | |
| 109 | // Erase this from this style. |
| 110 | iter = style->entries.erase(iter); |
| 111 | continue; |
| 112 | } |
| 113 | ++iter; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 114 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | if (min_sdk_stripped && !stripped.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | // We found attributes from a higher SDK level. Check that |
| 118 | // there is no other defined resource for the version we want to |
| 119 | // generate. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | if (ShouldGenerateVersionedResource(entry.get(), |
| 121 | config_value->config, |
| 122 | min_sdk_stripped.value())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | // Let's create a new Style for this versioned resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | ConfigDescription new_config(config_value->config); |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 125 | new_config.sdkVersion = static_cast<uint16_t>(min_sdk_stripped.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 127 | std::unique_ptr<Style> new_style(style->Clone(&table->string_pool)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 128 | new_style->SetComment(style->GetComment()); |
| 129 | new_style->SetSource(style->GetSource()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | |
| 131 | // Move the previously stripped attributes into this style. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | new_style->entries.insert( |
| 133 | new_style->entries.end(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | std::make_move_iterator(stripped.begin()), |
| 135 | std::make_move_iterator(stripped.end())); |
| 136 | |
| 137 | // Insert the new Resource into the correct place. |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 138 | entry->FindOrCreateValue(new_config, {})->value = std::move(new_style); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 144 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | } |
| 146 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | } // namespace aapt |