Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #ifndef AAPT2_CONFIGURATIONPARSER_INTERNAL_H |
| 18 | #define AAPT2_CONFIGURATIONPARSER_INTERNAL_H |
| 19 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame^] | 20 | #include "androidfw/ConfigDescription.h" |
| 21 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 22 | #include "configuration/ConfigurationParser.h" |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <limits> |
| 26 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 27 | namespace aapt { |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 28 | |
| 29 | // Forward declaration of classes used in the API. |
| 30 | namespace xml { |
| 31 | class Element; |
| 32 | } |
| 33 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 34 | namespace configuration { |
| 35 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 36 | template <typename T> |
| 37 | struct OrderedEntry { |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 38 | int32_t order; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 39 | std::vector<T> entry; |
| 40 | }; |
| 41 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 42 | /** A mapping of group label to a single configuration item. */ |
| 43 | template <class T> |
| 44 | using Entry = std::unordered_map<std::string, T>; |
| 45 | |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 46 | /** A mapping of group labels to group of configuration items. */ |
| 47 | template <class T> |
| 48 | using Group = Entry<OrderedEntry<T>>; |
| 49 | |
| 50 | template<typename T> |
| 51 | bool IsGroupValid(const Group<T>& group, const std::string& name, IDiagnostics* diag) { |
| 52 | std::set<int32_t> orders; |
| 53 | for (const auto& p : group) { |
| 54 | orders.insert(p.second.order); |
| 55 | } |
| 56 | bool valid = orders.size() == group.size(); |
| 57 | if (!valid) { |
| 58 | diag->Error(DiagMessage() << name << " have overlapping version-code-order attributes"); |
| 59 | } |
| 60 | return valid; |
| 61 | } |
| 62 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 63 | /** Retrieves an entry from the provided Group, creating a new instance if one does not exist. */ |
| 64 | template <typename T> |
| 65 | std::vector<T>& GetOrCreateGroup(std::string label, Group<T>* group) { |
| 66 | OrderedEntry<T>& entry = (*group)[label]; |
| 67 | // If this is a new entry, set the order. |
| 68 | if (entry.order == 0) { |
| 69 | entry.order = group->size(); |
| 70 | } |
| 71 | return entry.entry; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * A ComparisonChain is a grouping of comparisons to perform when sorting groups that have a well |
| 76 | * defined order of precedence. Comparisons are only made if none of the previous comparisons had a |
| 77 | * definite result. A comparison has a result if at least one of the items has an entry for that |
| 78 | * value and that they are not equal. |
| 79 | */ |
| 80 | class ComparisonChain { |
| 81 | public: |
| 82 | /** |
| 83 | * Adds a new comparison of items in a group to the chain. The new comparison is only used if we |
| 84 | * have not been able to determine the sort order with the previous comparisons. |
| 85 | */ |
| 86 | template <typename T> |
| 87 | ComparisonChain& Add(const Group<T>& groups, const Maybe<std::string>& lhs, |
| 88 | const Maybe<std::string>& rhs) { |
| 89 | return Add(GetGroupOrder(groups, lhs), GetGroupOrder(groups, rhs)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Adds a new comparison to the chain. The new comparison is only used if we have not been able to |
| 94 | * determine the sort order with the previous comparisons. |
| 95 | */ |
| 96 | ComparisonChain& Add(int lhs, int rhs) { |
| 97 | if (!has_result_) { |
| 98 | has_result_ = (lhs != rhs); |
| 99 | result_ = (lhs < rhs); |
| 100 | } |
| 101 | return *this; |
| 102 | } |
| 103 | |
| 104 | /** Returns true if the left hand side should come before the right hand side. */ |
| 105 | bool Compare() { |
| 106 | return result_; |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | template <typename T> |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 111 | inline size_t GetGroupOrder(const Entry<T>& groups, const Maybe<std::string>& label) { |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 112 | if (!label) { |
| 113 | return std::numeric_limits<size_t>::max(); |
| 114 | } |
| 115 | return groups.at(label.value()).order; |
| 116 | } |
| 117 | |
| 118 | bool has_result_ = false; |
| 119 | bool result_ = false; |
| 120 | }; |
| 121 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 122 | /** Output artifact configuration options. */ |
| 123 | struct ConfiguredArtifact { |
| 124 | /** Name to use for output of processing foo.apk -> foo.<name>.apk. */ |
| 125 | Maybe<std::string> name; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 126 | /** If present, uses the ABI group with this name. */ |
| 127 | Maybe<std::string> abi_group; |
| 128 | /** If present, uses the screen density group with this name. */ |
| 129 | Maybe<std::string> screen_density_group; |
| 130 | /** If present, uses the locale group with this name. */ |
| 131 | Maybe<std::string> locale_group; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 132 | /** If present, uses the Android SDK with this name. */ |
| 133 | Maybe<std::string> android_sdk; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 134 | /** If present, uses the device feature group with this name. */ |
| 135 | Maybe<std::string> device_feature_group; |
| 136 | /** If present, uses the OpenGL texture group with this name. */ |
| 137 | Maybe<std::string> gl_texture_group; |
| 138 | |
| 139 | /** Convert an artifact name template into a name string based on configuration contents. */ |
| 140 | Maybe<std::string> ToArtifactName(const android::StringPiece& format, |
| 141 | const android::StringPiece& apk_name, IDiagnostics* diag) const; |
| 142 | |
| 143 | /** Convert an artifact name template into a name string based on configuration contents. */ |
| 144 | Maybe<std::string> Name(const android::StringPiece& apk_name, IDiagnostics* diag) const; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | /** AAPT2 XML configuration file binary representation. */ |
| 148 | struct PostProcessingConfiguration { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 149 | std::vector<ConfiguredArtifact> artifacts; |
| 150 | Maybe<std::string> artifact_format; |
| 151 | |
| 152 | Group<Abi> abi_groups; |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame^] | 153 | Group<android::ConfigDescription> screen_density_groups; |
| 154 | Group<android::ConfigDescription> locale_groups; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 155 | Group<DeviceFeature> device_feature_groups; |
| 156 | Group<GlTexture> gl_texture_groups; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 157 | Entry<AndroidSdk> android_sdks; |
| 158 | |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 159 | bool ValidateVersionCodeOrdering(IDiagnostics* diag) { |
| 160 | bool valid = IsGroupValid(abi_groups, "abi-groups", diag); |
| 161 | valid &= IsGroupValid(screen_density_groups, "screen-density-groups", diag); |
| 162 | valid &= IsGroupValid(locale_groups, "locale-groups", diag); |
| 163 | valid &= IsGroupValid(device_feature_groups, "device-feature-groups", diag); |
| 164 | valid &= IsGroupValid(gl_texture_groups, "gl-texture-groups", diag); |
| 165 | return valid; |
| 166 | } |
| 167 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Sorts the configured artifacts based on the ordering of the groups in the configuration file. |
| 170 | * The only exception to this rule is Android SDK versions. Larger SDK versions will have a larger |
| 171 | * versionCode to ensure users get the correct APK when they upgrade their OS. |
| 172 | */ |
| 173 | void SortArtifacts() { |
| 174 | std::sort(artifacts.begin(), artifacts.end(), *this); |
| 175 | } |
| 176 | |
| 177 | /** Comparator that ensures artifacts are in the preferred order for versionCode rewriting. */ |
| 178 | bool operator()(const ConfiguredArtifact& lhs, const ConfiguredArtifact& rhs) { |
| 179 | // Split dimensions are added in the order of precedence. Items higher in the list result in |
| 180 | // higher version codes. |
| 181 | return ComparisonChain() |
| 182 | // All splits with a minSdkVersion specified must be last to ensure the application will be |
| 183 | // updated if a user upgrades the version of Android on their device. |
| 184 | .Add(GetMinSdk(lhs), GetMinSdk(rhs)) |
| 185 | // ABI version is important, especially on x86 phones where they may begin to run in ARM |
| 186 | // emulation mode on newer Android versions. This allows us to ensure that the x86 version |
| 187 | // is installed on these devices rather than ARM. |
| 188 | .Add(abi_groups, lhs.abi_group, rhs.abi_group) |
| 189 | // The rest are in arbitrary order based on estimated usage. |
| 190 | .Add(screen_density_groups, lhs.screen_density_group, rhs.screen_density_group) |
| 191 | .Add(locale_groups, lhs.locale_group, rhs.locale_group) |
| 192 | .Add(gl_texture_groups, lhs.gl_texture_group, rhs.gl_texture_group) |
| 193 | .Add(device_feature_groups, lhs.device_feature_group, rhs.device_feature_group) |
| 194 | .Compare(); |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | /** |
| 199 | * Returns the min_sdk_version from the provided artifact or 0 if none is present. This allows |
| 200 | * artifacts that have an Android SDK version to have a higher versionCode than those that do not. |
| 201 | */ |
| 202 | inline int GetMinSdk(const ConfiguredArtifact& artifact) { |
| 203 | if (!artifact.android_sdk) { |
| 204 | return 0; |
| 205 | } |
| 206 | const auto& entry = android_sdks.find(artifact.android_sdk.value()); |
| 207 | if (entry == android_sdks.end()) { |
| 208 | return 0; |
| 209 | } |
| 210 | return entry->second.min_sdk_version; |
| 211 | } |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 214 | /** Parses the provided XML document returning the post processing configuration. */ |
| 215 | Maybe<PostProcessingConfiguration> ExtractConfiguration(const std::string& contents, |
| 216 | const std::string& config_path, |
| 217 | IDiagnostics* diag); |
| 218 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 219 | namespace handler { |
| 220 | |
| 221 | /** Handler for <artifact> tags. */ |
| 222 | bool ArtifactTagHandler(configuration::PostProcessingConfiguration* config, xml::Element* element, |
| 223 | IDiagnostics* diag); |
| 224 | |
| 225 | /** Handler for <artifact-format> tags. */ |
| 226 | bool ArtifactFormatTagHandler(configuration::PostProcessingConfiguration* config, |
| 227 | xml::Element* element, IDiagnostics* diag); |
| 228 | |
| 229 | /** Handler for <abi-group> tags. */ |
| 230 | bool AbiGroupTagHandler(configuration::PostProcessingConfiguration* config, xml::Element* element, |
| 231 | IDiagnostics* diag); |
| 232 | |
| 233 | /** Handler for <screen-density-group> tags. */ |
| 234 | bool ScreenDensityGroupTagHandler(configuration::PostProcessingConfiguration* config, |
| 235 | xml::Element* element, IDiagnostics* diag); |
| 236 | |
| 237 | /** Handler for <locale-group> tags. */ |
| 238 | bool LocaleGroupTagHandler(configuration::PostProcessingConfiguration* config, |
| 239 | xml::Element* element, IDiagnostics* diag); |
| 240 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 241 | /** Handler for <android-sdk> tags. */ |
| 242 | bool AndroidSdkTagHandler(configuration::PostProcessingConfiguration* config, xml::Element* element, |
| 243 | IDiagnostics* diag); |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 244 | |
| 245 | /** Handler for <gl-texture-group> tags. */ |
| 246 | bool GlTextureGroupTagHandler(configuration::PostProcessingConfiguration* config, |
| 247 | xml::Element* element, IDiagnostics* diag); |
| 248 | |
| 249 | /** Handler for <device-feature-group> tags. */ |
| 250 | bool DeviceFeatureGroupTagHandler(configuration::PostProcessingConfiguration* config, |
| 251 | xml::Element* element, IDiagnostics* diag); |
| 252 | |
| 253 | } // namespace handler |
| 254 | } // namespace configuration |
| 255 | } // namespace aapt |
| 256 | #endif // AAPT2_CONFIGURATIONPARSER_INTERNAL_H |