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