Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [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_CONFIGURATION_H |
| 18 | #define AAPT2_CONFIGURATION_H |
| 19 | |
| 20 | #include <string> |
| 21 | #include <unordered_map> |
| 22 | #include <vector> |
| 23 | #include <ConfigDescription.h> |
| 24 | |
| 25 | #include "util/Maybe.h" |
| 26 | |
| 27 | namespace aapt { |
| 28 | |
| 29 | namespace configuration { |
| 30 | |
| 31 | /** A mapping of group labels to group of configuration items. */ |
| 32 | template<class T> |
| 33 | using Group = std::unordered_map<std::string, std::vector<T>>; |
| 34 | |
| 35 | /** Output artifact configuration options. */ |
| 36 | struct Artifact { |
| 37 | /** Name to use for output of processing foo.apk -> foo.<name>.apk. */ |
| 38 | std::string name; |
| 39 | /** If present, uses the ABI group with this name. */ |
| 40 | Maybe<std::string> abi_group; |
| 41 | /** If present, uses the screen density group with this name. */ |
| 42 | Maybe<std::string> screen_density_group; |
| 43 | /** If present, uses the locale group with this name. */ |
| 44 | Maybe<std::string> locale_group; |
| 45 | /** If present, uses the Android SDK group with this name. */ |
| 46 | Maybe<std::string> android_sdk_group; |
| 47 | /** If present, uses the device feature group with this name. */ |
| 48 | Maybe<std::string> device_feature_group; |
| 49 | /** If present, uses the OpenGL texture group with this name. */ |
| 50 | Maybe<std::string> gl_texture_group; |
| 51 | }; |
| 52 | |
| 53 | /** Enumeration of currently supported ABIs. */ |
| 54 | enum class Abi { |
| 55 | kArmeV6, |
| 56 | kArmV7a, |
| 57 | kArm64V8a, |
| 58 | kX86, |
| 59 | kX86_64, |
| 60 | kMips, |
| 61 | kMips64, |
| 62 | kUniversal |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * Represents an individual locale. When a locale is included, it must be |
| 67 | * declared from least specific to most specific, as a region does not make |
| 68 | * sense without a language. If neither the language or region are specified it |
| 69 | * acts as a special case for catch all. This can allow all locales to be kept, |
| 70 | * or compressed. |
| 71 | */ |
| 72 | struct Locale { |
| 73 | /** The ISO<?> standard locale language code. */ |
| 74 | Maybe<std::string> lang; |
| 75 | /** The ISO<?> standard locale region code. */ |
| 76 | Maybe<std::string> region; |
| 77 | |
| 78 | inline friend bool operator==(const Locale& lhs, const Locale& rhs) { |
| 79 | return lhs.lang == rhs.lang && lhs.region == rhs.region; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | // TODO: Encapsulate manifest modifications from the configuration file. |
| 84 | struct AndroidManifest { |
| 85 | inline friend bool operator==(const AndroidManifest& lhs, const AndroidManifest& rhs) { |
| 86 | return true; // nothing to compare yet. |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | struct AndroidSdk { |
| 91 | Maybe<std::string> min_sdk_version; |
| 92 | Maybe<std::string> target_sdk_version; |
| 93 | Maybe<std::string> max_sdk_version; |
| 94 | Maybe<AndroidManifest> manifest; |
| 95 | |
| 96 | inline friend bool operator==(const AndroidSdk& lhs, const AndroidSdk& rhs) { |
| 97 | return lhs.min_sdk_version == rhs.min_sdk_version && |
| 98 | lhs.target_sdk_version == rhs.target_sdk_version && |
| 99 | lhs.max_sdk_version == rhs.max_sdk_version && |
| 100 | lhs.manifest == rhs.manifest; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | // TODO: Make device features more than just an arbitrary string? |
| 105 | using DeviceFeature = std::string; |
| 106 | |
| 107 | /** Represents a mapping of texture paths to a GL texture format. */ |
| 108 | struct GlTexture { |
| 109 | std::string name; |
| 110 | std::vector<std::string> texture_paths; |
| 111 | |
| 112 | inline friend bool operator==(const GlTexture& lhs, const GlTexture& rhs) { |
| 113 | return lhs.name == rhs.name && lhs.texture_paths == rhs.texture_paths; |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * AAPT2 XML configuration binary representation. |
| 119 | */ |
| 120 | struct Configuration { |
| 121 | std::unordered_map<std::string, Artifact> artifacts; |
| 122 | Maybe<std::string> artifact_format; |
| 123 | |
| 124 | Group<Abi> abi_groups; |
| 125 | Group<ConfigDescription> screen_density_groups; |
| 126 | Group<Locale> locale_groups; |
| 127 | Group<AndroidSdk> android_sdk_groups; |
| 128 | Group<DeviceFeature> device_feature_groups; |
| 129 | Group<GlTexture> gl_texture_groups; |
| 130 | }; |
| 131 | |
| 132 | } // namespace configuration |
| 133 | |
| 134 | // Forward declaration of classes used in the API. |
| 135 | struct IDiagnostics; |
| 136 | namespace xml { |
| 137 | class Element; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * XML configuration file parser for the split and optimize commands. |
| 142 | */ |
| 143 | class ConfigurationParser { |
| 144 | public: |
| 145 | /** Returns a ConfigurationParser for the configuration in the provided file contents. */ |
| 146 | static ConfigurationParser ForContents(const std::string& contents) { |
| 147 | ConfigurationParser parser{contents}; |
| 148 | return parser; |
| 149 | } |
| 150 | |
| 151 | /** Returns a ConfigurationParser for the file located at the provided path. */ |
| 152 | static ConfigurationParser ForPath(const std::string& path) { |
| 153 | // TODO: Read XML file into memory. |
| 154 | return ForContents(path); |
| 155 | } |
| 156 | |
| 157 | /** Sets the diagnostics context to use when parsing. */ |
| 158 | ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) { |
| 159 | diag_ = diagnostics; |
| 160 | return *this; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Parses the configuration file and returns the results. If the configuration could not be parsed |
| 165 | * the result is empty and any errors will be displayed with the provided diagnostics context. |
| 166 | */ |
| 167 | Maybe<configuration::Configuration> Parse(); |
| 168 | |
| 169 | protected: |
| 170 | /** |
| 171 | * Instantiates a new ConfigurationParser with the provided configuration file and a no-op |
| 172 | * diagnostics context. The default diagnostics context can be overridden with a call to |
| 173 | * WithDiagnostics(IDiagnostics *). |
| 174 | */ |
| 175 | explicit ConfigurationParser(std::string contents); |
| 176 | |
| 177 | /** Returns the current diagnostics context to any subclasses. */ |
| 178 | IDiagnostics* diagnostics() { |
| 179 | return diag_; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * An ActionHandler for processing XML elements in the XmlActionExecutor. Returns true if the |
| 184 | * element was successfully processed, otherwise returns false. |
| 185 | */ |
| 186 | using ActionHandler = std::function<bool(configuration::Configuration* config, |
| 187 | xml::Element* element, |
| 188 | IDiagnostics* diag)>; |
| 189 | |
| 190 | /** Handler for <artifact> tags. */ |
| 191 | static ActionHandler artifact_handler_; |
| 192 | /** Handler for <artifact-format> tags. */ |
| 193 | static ActionHandler artifact_format_handler_; |
| 194 | /** Handler for <abi-group> tags. */ |
| 195 | static ActionHandler abi_group_handler_; |
| 196 | /** Handler for <screen-density-group> tags. */ |
| 197 | static ActionHandler screen_density_group_handler_; |
| 198 | /** Handler for <locale-group> tags. */ |
| 199 | static ActionHandler locale_group_handler_; |
| 200 | /** Handler for <android-sdk-group> tags. */ |
| 201 | static ActionHandler android_sdk_group_handler_; |
| 202 | /** Handler for <gl-texture-group> tags. */ |
| 203 | static ActionHandler gl_texture_group_handler_; |
| 204 | /** Handler for <device-feature-group> tags. */ |
| 205 | static ActionHandler device_feature_group_handler_; |
| 206 | |
| 207 | private: |
| 208 | /** The contents of the configuration file to parse. */ |
| 209 | const std::string contents_; |
| 210 | /** The diagnostics context to send messages to. */ |
| 211 | IDiagnostics* diag_; |
| 212 | }; |
| 213 | |
| 214 | } // namespace aapt |
| 215 | |
| 216 | #endif //AAPT2_CONFIGURATION_H |