| 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 |  | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 20 | #include <optional> | 
| Shane Farmer | 810fd18 | 2017-09-21 14:37:44 -0700 | [diff] [blame] | 21 | #include <set> | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 22 | #include <string> | 
|  | 23 | #include <unordered_map> | 
|  | 24 | #include <vector> | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 25 |  | 
| Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 26 | #include "androidfw/ConfigDescription.h" | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 27 | #include "androidfw/IDiagnostics.h" | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 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. */ | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 57 | std::optional<std::string> lang; | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 58 | /** The ISO<?> standard locale region code. */ | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 59 | std::optional<std::string> region; | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 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 | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 74 | std::string label; | 
|  | 75 | int min_sdk_version;  // min_sdk_version is mandatory if splitting by SDK. | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 76 | std::optional<int> target_sdk_version; | 
|  | 77 | std::optional<int> max_sdk_version; | 
|  | 78 | std::optional<AndroidManifest> manifest; | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 79 |  | 
| Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 80 | static AndroidSdk ForMinSdk(int min_sdk) { | 
| Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 81 | AndroidSdk sdk; | 
| Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 82 | sdk.min_sdk_version = min_sdk; | 
| Shane Farmer | efe4539 | 2017-08-21 14:39:28 -0700 | [diff] [blame] | 83 | return sdk; | 
|  | 84 | } | 
|  | 85 |  | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 86 | inline friend bool operator==(const AndroidSdk& lhs, const AndroidSdk& rhs) { | 
|  | 87 | return lhs.min_sdk_version == rhs.min_sdk_version && | 
|  | 88 | lhs.target_sdk_version == rhs.target_sdk_version && | 
|  | 89 | lhs.max_sdk_version == rhs.max_sdk_version && | 
|  | 90 | lhs.manifest == rhs.manifest; | 
|  | 91 | } | 
|  | 92 | }; | 
|  | 93 |  | 
|  | 94 | // TODO: Make device features more than just an arbitrary string? | 
|  | 95 | using DeviceFeature = std::string; | 
|  | 96 |  | 
|  | 97 | /** Represents a mapping of texture paths to a GL texture format. */ | 
|  | 98 | struct GlTexture { | 
|  | 99 | std::string name; | 
|  | 100 | std::vector<std::string> texture_paths; | 
|  | 101 |  | 
|  | 102 | inline friend bool operator==(const GlTexture& lhs, const GlTexture& rhs) { | 
|  | 103 | return lhs.name == rhs.name && lhs.texture_paths == rhs.texture_paths; | 
|  | 104 | } | 
|  | 105 | }; | 
|  | 106 |  | 
| Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 107 | /** An artifact with all the details pulled from the PostProcessingConfiguration. */ | 
|  | 108 | struct OutputArtifact { | 
|  | 109 | std::string name; | 
|  | 110 | int version; | 
|  | 111 | std::vector<Abi> abis; | 
| Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 112 | std::vector<android::ConfigDescription> screen_densities; | 
|  | 113 | std::vector<android::ConfigDescription> locales; | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 114 | std::optional<AndroidSdk> android_sdk; | 
| Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 115 | std::vector<DeviceFeature> features; | 
|  | 116 | std::vector<GlTexture> textures; | 
| Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 117 |  | 
|  | 118 | inline int GetMinSdk(int default_value = -1) const { | 
|  | 119 | if (!android_sdk) { | 
|  | 120 | return default_value; | 
|  | 121 | } | 
|  | 122 | return android_sdk.value().min_sdk_version; | 
|  | 123 | } | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 124 | }; | 
|  | 125 |  | 
|  | 126 | }  // namespace configuration | 
|  | 127 |  | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 128 | /** | 
|  | 129 | * XML configuration file parser for the split and optimize commands. | 
|  | 130 | */ | 
|  | 131 | class ConfigurationParser { | 
|  | 132 | public: | 
| Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | /** Returns a ConfigurationParser for the file located at the provided path. */ | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 135 | static std::optional<ConfigurationParser> ForPath(const std::string& path); | 
| Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 136 |  | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 137 | /** Returns a ConfigurationParser for the configuration in the provided file contents. */ | 
| Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 138 | static ConfigurationParser ForContents(const std::string& contents, const std::string& path) { | 
|  | 139 | ConfigurationParser parser{contents, path}; | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 140 | return parser; | 
|  | 141 | } | 
|  | 142 |  | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 143 | /** Sets the diagnostics context to use when parsing. */ | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 144 | ConfigurationParser& WithDiagnostics(android::IDiagnostics* diagnostics) { | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 145 | diag_ = diagnostics; | 
|  | 146 | return *this; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | /** | 
|  | 150 | * Parses the configuration file and returns the results. If the configuration could not be parsed | 
|  | 151 | * the result is empty and any errors will be displayed with the provided diagnostics context. | 
|  | 152 | */ | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 153 | std::optional<std::vector<configuration::OutputArtifact>> Parse( | 
|  | 154 | const android::StringPiece& apk_path); | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 155 |  | 
|  | 156 | protected: | 
|  | 157 | /** | 
|  | 158 | * Instantiates a new ConfigurationParser with the provided configuration file and a no-op | 
|  | 159 | * diagnostics context. The default diagnostics context can be overridden with a call to | 
|  | 160 | * WithDiagnostics(IDiagnostics *). | 
|  | 161 | */ | 
| Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 162 | ConfigurationParser(std::string contents, const std::string& config_path); | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 163 |  | 
|  | 164 | /** Returns the current diagnostics context to any subclasses. */ | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 165 | android::IDiagnostics* diagnostics() { | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 166 | return diag_; | 
|  | 167 | } | 
|  | 168 |  | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 169 | private: | 
|  | 170 | /** The contents of the configuration file to parse. */ | 
|  | 171 | const std::string contents_; | 
| Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 172 | /** Path to the input configuration. */ | 
|  | 173 | const std::string config_path_; | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 174 | /** The diagnostics context to send messages to. */ | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 175 | android::IDiagnostics* diag_; | 
| Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 176 | }; | 
|  | 177 |  | 
|  | 178 | }  // namespace aapt | 
|  | 179 |  | 
| Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 180 | #endif  // AAPT2_CONFIGURATION_H |