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