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 | |
Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 20 | #include <set> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <unordered_map> |
| 23 | #include <vector> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 24 | |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 25 | #include "ConfigDescription.h" |
| 26 | #include "Diagnostics.h" |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 27 | #include "util/Maybe.h" |
| 28 | |
| 29 | namespace aapt { |
| 30 | |
| 31 | namespace configuration { |
| 32 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 33 | /** Enumeration of currently supported ABIs. */ |
| 34 | enum class Abi { |
| 35 | kArmeV6, |
| 36 | kArmV7a, |
| 37 | kArm64V8a, |
| 38 | kX86, |
| 39 | kX86_64, |
| 40 | kMips, |
| 41 | kMips64, |
| 42 | kUniversal |
| 43 | }; |
| 44 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 45 | /** Helper method to convert an ABI to a string representing the path within the APK. */ |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 46 | const android::StringPiece& AbiToString(Abi abi); |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 47 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Represents an individual locale. When a locale is included, it must be |
| 50 | * declared from least specific to most specific, as a region does not make |
| 51 | * sense without a language. If neither the language or region are specified it |
| 52 | * acts as a special case for catch all. This can allow all locales to be kept, |
| 53 | * or compressed. |
| 54 | */ |
| 55 | struct Locale { |
| 56 | /** The ISO<?> standard locale language code. */ |
| 57 | Maybe<std::string> lang; |
| 58 | /** The ISO<?> standard locale region code. */ |
| 59 | Maybe<std::string> region; |
| 60 | |
| 61 | inline friend bool operator==(const Locale& lhs, const Locale& rhs) { |
| 62 | return lhs.lang == rhs.lang && lhs.region == rhs.region; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | // TODO: Encapsulate manifest modifications from the configuration file. |
| 67 | struct AndroidManifest { |
| 68 | inline friend bool operator==(const AndroidManifest& lhs, const AndroidManifest& rhs) { |
| 69 | return true; // nothing to compare yet. |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | struct AndroidSdk { |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 74 | Maybe<int> min_sdk_version; |
| 75 | Maybe<int> target_sdk_version; |
| 76 | Maybe<int> max_sdk_version; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 77 | Maybe<AndroidManifest> manifest; |
| 78 | |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 79 | static AndroidSdk ForMinSdk(int min_sdk) { |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 80 | AndroidSdk sdk; |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 81 | sdk.min_sdk_version = min_sdk; |
Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 82 | return sdk; |
| 83 | } |
| 84 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 85 | inline friend bool operator==(const AndroidSdk& lhs, const AndroidSdk& rhs) { |
| 86 | return lhs.min_sdk_version == rhs.min_sdk_version && |
| 87 | lhs.target_sdk_version == rhs.target_sdk_version && |
| 88 | lhs.max_sdk_version == rhs.max_sdk_version && |
| 89 | lhs.manifest == rhs.manifest; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | // TODO: Make device features more than just an arbitrary string? |
| 94 | using DeviceFeature = std::string; |
| 95 | |
| 96 | /** Represents a mapping of texture paths to a GL texture format. */ |
| 97 | struct GlTexture { |
| 98 | std::string name; |
| 99 | std::vector<std::string> texture_paths; |
| 100 | |
| 101 | inline friend bool operator==(const GlTexture& lhs, const GlTexture& rhs) { |
| 102 | return lhs.name == rhs.name && lhs.texture_paths == rhs.texture_paths; |
| 103 | } |
| 104 | }; |
| 105 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 106 | /** An artifact with all the details pulled from the PostProcessingConfiguration. */ |
| 107 | struct OutputArtifact { |
| 108 | std::string name; |
| 109 | int version; |
| 110 | std::vector<Abi> abis; |
| 111 | std::vector<ConfigDescription> screen_densities; |
| 112 | std::vector<ConfigDescription> locales; |
| 113 | Maybe<AndroidSdk> android_sdk; |
| 114 | std::vector<DeviceFeature> features; |
| 115 | std::vector<GlTexture> textures; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // namespace configuration |
| 119 | |
| 120 | // Forward declaration of classes used in the API. |
| 121 | struct IDiagnostics; |
| 122 | namespace xml { |
| 123 | class Element; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * XML configuration file parser for the split and optimize commands. |
| 128 | */ |
| 129 | class ConfigurationParser { |
| 130 | public: |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 131 | |
| 132 | /** Returns a ConfigurationParser for the file located at the provided path. */ |
| 133 | static Maybe<ConfigurationParser> ForPath(const std::string& path); |
| 134 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 135 | /** Returns a ConfigurationParser for the configuration in the provided file contents. */ |
| 136 | static ConfigurationParser ForContents(const std::string& contents) { |
| 137 | ConfigurationParser parser{contents}; |
| 138 | return parser; |
| 139 | } |
| 140 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 141 | /** Sets the diagnostics context to use when parsing. */ |
| 142 | ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) { |
| 143 | diag_ = diagnostics; |
| 144 | return *this; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Parses the configuration file and returns the results. If the configuration could not be parsed |
| 149 | * the result is empty and any errors will be displayed with the provided diagnostics context. |
| 150 | */ |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 151 | Maybe<std::vector<configuration::OutputArtifact>> Parse(const android::StringPiece& apk_path); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 152 | |
| 153 | protected: |
| 154 | /** |
| 155 | * Instantiates a new ConfigurationParser with the provided configuration file and a no-op |
| 156 | * diagnostics context. The default diagnostics context can be overridden with a call to |
| 157 | * WithDiagnostics(IDiagnostics *). |
| 158 | */ |
| 159 | explicit ConfigurationParser(std::string contents); |
| 160 | |
| 161 | /** Returns the current diagnostics context to any subclasses. */ |
| 162 | IDiagnostics* diagnostics() { |
| 163 | return diag_; |
| 164 | } |
| 165 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 166 | private: |
| 167 | /** The contents of the configuration file to parse. */ |
| 168 | const std::string contents_; |
| 169 | /** The diagnostics context to send messages to. */ |
| 170 | IDiagnostics* diag_; |
| 171 | }; |
| 172 | |
| 173 | } // namespace aapt |
| 174 | |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 175 | #endif // AAPT2_CONFIGURATION_H |