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 | #include "configuration/ConfigurationParser.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <functional> |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 21 | #include <map> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 22 | #include <memory> |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 23 | #include <string> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 24 | #include <utility> |
| 25 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 26 | #include "android-base/file.h" |
| 27 | #include "android-base/logging.h" |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 28 | #include "androidfw/ConfigDescription.h" |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 29 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 30 | #include "Diagnostics.h" |
Shane Farmer | 3edd472 | 2017-09-01 14:34:22 -0700 | [diff] [blame] | 31 | #include "ResourceUtils.h" |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 32 | #include "configuration/ConfigurationParser.internal.h" |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 33 | #include "io/File.h" |
| 34 | #include "io/FileSystem.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 35 | #include "io/StringStream.h" |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 36 | #include "util/Files.h" |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 37 | #include "util/Maybe.h" |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 38 | #include "util/Util.h" |
| 39 | #include "xml/XmlActionExecutor.h" |
| 40 | #include "xml/XmlDom.h" |
| 41 | #include "xml/XmlUtil.h" |
| 42 | |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 43 | using ::android::ConfigDescription; |
| 44 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 45 | namespace aapt { |
| 46 | |
| 47 | namespace { |
| 48 | |
| 49 | using ::aapt::configuration::Abi; |
| 50 | using ::aapt::configuration::AndroidManifest; |
| 51 | using ::aapt::configuration::AndroidSdk; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 52 | using ::aapt::configuration::ConfiguredArtifact; |
| 53 | using ::aapt::configuration::DeviceFeature; |
| 54 | using ::aapt::configuration::Entry; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 55 | using ::aapt::configuration::ExtractConfiguration; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 56 | using ::aapt::configuration::GlTexture; |
| 57 | using ::aapt::configuration::Group; |
| 58 | using ::aapt::configuration::Locale; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 59 | using ::aapt::configuration::OrderedEntry; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 60 | using ::aapt::configuration::OutputArtifact; |
| 61 | using ::aapt::configuration::PostProcessingConfiguration; |
| 62 | using ::aapt::configuration::handler::AbiGroupTagHandler; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 63 | using ::aapt::configuration::handler::AndroidSdkTagHandler; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 64 | using ::aapt::configuration::handler::ArtifactFormatTagHandler; |
| 65 | using ::aapt::configuration::handler::ArtifactTagHandler; |
| 66 | using ::aapt::configuration::handler::DeviceFeatureGroupTagHandler; |
| 67 | using ::aapt::configuration::handler::GlTextureGroupTagHandler; |
| 68 | using ::aapt::configuration::handler::LocaleGroupTagHandler; |
| 69 | using ::aapt::configuration::handler::ScreenDensityGroupTagHandler; |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 70 | using ::aapt::io::IFile; |
| 71 | using ::aapt::io::RegularFile; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 72 | using ::aapt::io::StringInputStream; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 73 | using ::aapt::util::TrimWhitespace; |
| 74 | using ::aapt::xml::Element; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 75 | using ::aapt::xml::NodeCast; |
| 76 | using ::aapt::xml::XmlActionExecutor; |
| 77 | using ::aapt::xml::XmlActionExecutorPolicy; |
| 78 | using ::aapt::xml::XmlNodeAction; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 79 | using ::android::StringPiece; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 80 | using ::android::base::ReadFileToString; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 81 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 82 | const std::unordered_map<StringPiece, Abi> kStringToAbiMap = { |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 83 | {"armeabi", Abi::kArmeV6}, {"armeabi-v7a", Abi::kArmV7a}, {"arm64-v8a", Abi::kArm64V8a}, |
| 84 | {"x86", Abi::kX86}, {"x86_64", Abi::kX86_64}, {"mips", Abi::kMips}, |
| 85 | {"mips64", Abi::kMips64}, {"universal", Abi::kUniversal}, |
| 86 | }; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 87 | const std::array<StringPiece, 8> kAbiToStringMap = { |
| 88 | {"armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64", "mips", "mips64", "universal"}}; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 89 | |
| 90 | constexpr const char* kAaptXmlNs = "http://schemas.android.com/tools/aapt"; |
| 91 | |
| 92 | /** A default noop diagnostics context. */ |
| 93 | class NoopDiagnostics : public IDiagnostics { |
| 94 | public: |
| 95 | void Log(Level level, DiagMessageActual& actualMsg) override {} |
| 96 | }; |
| 97 | NoopDiagnostics noop_; |
| 98 | |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 99 | /** Returns the value of the label attribute for a given element. */ |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 100 | std::string GetLabel(const Element* element, IDiagnostics* diag) { |
| 101 | std::string label; |
| 102 | for (const auto& attr : element->attributes) { |
| 103 | if (attr.name == "label") { |
| 104 | label = attr.value; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (label.empty()) { |
| 110 | diag->Error(DiagMessage() << "No label found for element " << element->name); |
| 111 | } |
| 112 | return label; |
| 113 | } |
| 114 | |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 115 | /** Returns the value of the version-code-order attribute for a given element. */ |
| 116 | Maybe<int32_t> GetVersionCodeOrder(const Element* element, IDiagnostics* diag) { |
| 117 | const xml::Attribute* version = element->FindAttribute("", "version-code-order"); |
| 118 | if (version == nullptr) { |
| 119 | std::string label = GetLabel(element, diag); |
| 120 | diag->Error(DiagMessage() << "No version-code-order found for element '" << element->name |
| 121 | << "' with label '" << label << "'"); |
| 122 | return {}; |
| 123 | } |
| 124 | return std::stoi(version->value); |
| 125 | } |
| 126 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 127 | /** XML node visitor that removes all of the namespace URIs from the node and all children. */ |
| 128 | class NamespaceVisitor : public xml::Visitor { |
| 129 | public: |
| 130 | void Visit(xml::Element* node) override { |
| 131 | node->namespace_uri.clear(); |
| 132 | VisitChildren(node); |
| 133 | } |
| 134 | }; |
| 135 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 136 | /** Copies the values referenced in a configuration group to the target list. */ |
| 137 | template <typename T> |
| 138 | bool CopyXmlReferences(const Maybe<std::string>& name, const Group<T>& groups, |
| 139 | std::vector<T>* target) { |
| 140 | // If there was no item configured, there is nothing to do and no error. |
| 141 | if (!name) { |
| 142 | return true; |
| 143 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 144 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 145 | // If the group could not be found, then something is wrong. |
| 146 | auto group = groups.find(name.value()); |
| 147 | if (group == groups.end()) { |
| 148 | return false; |
| 149 | } |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 150 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 151 | for (const T& item : group->second.entry) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 152 | target->push_back(item); |
| 153 | } |
| 154 | return true; |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Attempts to replace the placeholder in the name string with the provided value. Returns true on |
| 159 | * success, or false if the either the placeholder is not found in the name, or the value is not |
| 160 | * present and the placeholder was. |
| 161 | */ |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 162 | bool ReplacePlaceholder(const StringPiece& placeholder, const Maybe<StringPiece>& value, |
| 163 | std::string* name, IDiagnostics* diag) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 164 | size_t offset = name->find(placeholder.data()); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 165 | bool found = (offset != std::string::npos); |
| 166 | |
| 167 | // Make sure the placeholder was present if the desired value is present. |
| 168 | if (!found) { |
| 169 | if (value) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 170 | diag->Error(DiagMessage() << "Missing placeholder for artifact: " << placeholder); |
| 171 | return false; |
| 172 | } |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 176 | DCHECK(found) << "Missing return path for placeholder not found"; |
| 177 | |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 178 | // Make sure the placeholder was not present if the desired value was not present. |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 179 | if (!value) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 180 | diag->Error(DiagMessage() << "Placeholder present but no value for artifact: " << placeholder); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 181 | return false; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 182 | } |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 183 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 184 | name->replace(offset, placeholder.length(), value.value().data()); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 185 | |
| 186 | // Make sure there was only one instance of the placeholder. |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 187 | if (name->find(placeholder.data()) != std::string::npos) { |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 188 | diag->Error(DiagMessage() << "Placeholder present multiple times: " << placeholder); |
| 189 | return false; |
| 190 | } |
| 191 | return true; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 194 | /** |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 195 | * An ActionHandler for processing XML elements in the XmlActionExecutor. Returns true if the |
| 196 | * element was successfully processed, otherwise returns false. |
| 197 | */ |
| 198 | using ActionHandler = std::function<bool(configuration::PostProcessingConfiguration* config, |
| 199 | xml::Element* element, IDiagnostics* diag)>; |
| 200 | |
| 201 | /** Binds an ActionHandler to the current configuration being populated. */ |
| 202 | xml::XmlNodeAction::ActionFuncWithDiag Bind(configuration::PostProcessingConfiguration* config, |
| 203 | const ActionHandler& handler) { |
| 204 | return [config, handler](xml::Element* root_element, SourcePathDiagnostics* diag) { |
| 205 | return handler(config, root_element, diag); |
| 206 | }; |
| 207 | } |
| 208 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 209 | /** Converts a ConfiguredArtifact into an OutputArtifact. */ |
| 210 | Maybe<OutputArtifact> ToOutputArtifact(const ConfiguredArtifact& artifact, |
| 211 | const std::string& apk_name, |
| 212 | const PostProcessingConfiguration& config, |
| 213 | IDiagnostics* diag) { |
| 214 | if (!artifact.name && !config.artifact_format) { |
| 215 | diag->Error( |
| 216 | DiagMessage() << "Artifact does not have a name and no global name template defined"); |
| 217 | return {}; |
| 218 | } |
| 219 | |
| 220 | Maybe<std::string> artifact_name = |
| 221 | (artifact.name) ? artifact.Name(apk_name, diag) |
| 222 | : artifact.ToArtifactName(config.artifact_format.value(), apk_name, diag); |
| 223 | |
| 224 | if (!artifact_name) { |
| 225 | diag->Error(DiagMessage() << "Could not determine split APK artifact name"); |
| 226 | return {}; |
| 227 | } |
| 228 | |
| 229 | OutputArtifact output_artifact; |
| 230 | output_artifact.name = artifact_name.value(); |
| 231 | |
| 232 | SourcePathDiagnostics src_diag{{output_artifact.name}, diag}; |
| 233 | bool has_errors = false; |
| 234 | |
| 235 | if (!CopyXmlReferences(artifact.abi_group, config.abi_groups, &output_artifact.abis)) { |
| 236 | src_diag.Error(DiagMessage() << "Could not lookup required ABIs: " |
| 237 | << artifact.abi_group.value()); |
| 238 | has_errors = true; |
| 239 | } |
| 240 | |
| 241 | if (!CopyXmlReferences(artifact.locale_group, config.locale_groups, &output_artifact.locales)) { |
| 242 | src_diag.Error(DiagMessage() << "Could not lookup required locales: " |
| 243 | << artifact.locale_group.value()); |
| 244 | has_errors = true; |
| 245 | } |
| 246 | |
| 247 | if (!CopyXmlReferences(artifact.screen_density_group, config.screen_density_groups, |
| 248 | &output_artifact.screen_densities)) { |
| 249 | src_diag.Error(DiagMessage() << "Could not lookup required screen densities: " |
| 250 | << artifact.screen_density_group.value()); |
| 251 | has_errors = true; |
| 252 | } |
| 253 | |
| 254 | if (!CopyXmlReferences(artifact.device_feature_group, config.device_feature_groups, |
| 255 | &output_artifact.features)) { |
| 256 | src_diag.Error(DiagMessage() << "Could not lookup required device features: " |
| 257 | << artifact.device_feature_group.value()); |
| 258 | has_errors = true; |
| 259 | } |
| 260 | |
| 261 | if (!CopyXmlReferences(artifact.gl_texture_group, config.gl_texture_groups, |
| 262 | &output_artifact.textures)) { |
| 263 | src_diag.Error(DiagMessage() << "Could not lookup required OpenGL texture formats: " |
| 264 | << artifact.gl_texture_group.value()); |
| 265 | has_errors = true; |
| 266 | } |
| 267 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 268 | if (artifact.android_sdk) { |
| 269 | auto entry = config.android_sdks.find(artifact.android_sdk.value()); |
| 270 | if (entry == config.android_sdks.end()) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 271 | src_diag.Error(DiagMessage() << "Could not lookup required Android SDK version: " |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 272 | << artifact.android_sdk.value()); |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 273 | has_errors = true; |
| 274 | } else { |
| 275 | output_artifact.android_sdk = {entry->second}; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (has_errors) { |
| 280 | return {}; |
| 281 | } |
| 282 | return {output_artifact}; |
| 283 | } |
| 284 | |
| 285 | } // namespace |
| 286 | |
| 287 | namespace configuration { |
| 288 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 289 | /** Returns the binary reprasentation of the XML configuration. */ |
| 290 | Maybe<PostProcessingConfiguration> ExtractConfiguration(const std::string& contents, |
| 291 | const std::string& config_path, |
| 292 | IDiagnostics* diag) { |
| 293 | StringInputStream in(contents); |
| 294 | std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, diag, Source(config_path)); |
| 295 | if (!doc) { |
| 296 | return {}; |
| 297 | } |
| 298 | |
| 299 | // Strip any namespaces from the XML as the XmlActionExecutor ignores anything with a namespace. |
| 300 | Element* root = doc->root.get(); |
| 301 | if (root == nullptr) { |
| 302 | diag->Error(DiagMessage() << "Could not find the root element in the XML document"); |
| 303 | return {}; |
| 304 | } |
| 305 | |
| 306 | std::string& xml_ns = root->namespace_uri; |
| 307 | if (!xml_ns.empty()) { |
| 308 | if (xml_ns != kAaptXmlNs) { |
| 309 | diag->Error(DiagMessage() << "Unknown namespace found on root element: " << xml_ns); |
| 310 | return {}; |
| 311 | } |
| 312 | |
| 313 | xml_ns.clear(); |
| 314 | NamespaceVisitor visitor; |
| 315 | root->Accept(&visitor); |
| 316 | } |
| 317 | |
| 318 | XmlActionExecutor executor; |
| 319 | XmlNodeAction& root_action = executor["post-process"]; |
| 320 | XmlNodeAction& artifacts_action = root_action["artifacts"]; |
| 321 | |
| 322 | PostProcessingConfiguration config; |
| 323 | |
| 324 | // Parse the artifact elements. |
| 325 | artifacts_action["artifact"].Action(Bind(&config, ArtifactTagHandler)); |
| 326 | artifacts_action["artifact-format"].Action(Bind(&config, ArtifactFormatTagHandler)); |
| 327 | |
| 328 | // Parse the different configuration groups. |
| 329 | root_action["abi-groups"]["abi-group"].Action(Bind(&config, AbiGroupTagHandler)); |
| 330 | root_action["screen-density-groups"]["screen-density-group"].Action( |
| 331 | Bind(&config, ScreenDensityGroupTagHandler)); |
| 332 | root_action["locale-groups"]["locale-group"].Action(Bind(&config, LocaleGroupTagHandler)); |
| 333 | root_action["android-sdks"]["android-sdk"].Action(Bind(&config, AndroidSdkTagHandler)); |
| 334 | root_action["gl-texture-groups"]["gl-texture-group"].Action( |
| 335 | Bind(&config, GlTextureGroupTagHandler)); |
| 336 | root_action["device-feature-groups"]["device-feature-group"].Action( |
| 337 | Bind(&config, DeviceFeatureGroupTagHandler)); |
| 338 | |
| 339 | if (!executor.Execute(XmlActionExecutorPolicy::kNone, diag, doc.get())) { |
| 340 | diag->Error(DiagMessage() << "Could not process XML document"); |
| 341 | return {}; |
| 342 | } |
| 343 | |
| 344 | return {config}; |
| 345 | } |
| 346 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 347 | const StringPiece& AbiToString(Abi abi) { |
| 348 | return kAbiToStringMap.at(static_cast<size_t>(abi)); |
| 349 | } |
| 350 | |
| 351 | /** |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 352 | * Returns the common artifact base name from a template string. |
| 353 | */ |
| 354 | Maybe<std::string> ToBaseName(std::string result, const StringPiece& apk_name, IDiagnostics* diag) { |
| 355 | const StringPiece ext = file::GetExtension(apk_name); |
| 356 | size_t end_index = apk_name.to_string().rfind(ext.to_string()); |
| 357 | const std::string base_name = |
| 358 | (end_index != std::string::npos) ? std::string{apk_name.begin(), end_index} : ""; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 359 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 360 | // Base name is optional. |
| 361 | if (result.find("${basename}") != std::string::npos) { |
| 362 | Maybe<StringPiece> maybe_base_name = |
| 363 | base_name.empty() ? Maybe<StringPiece>{} : Maybe<StringPiece>{base_name}; |
| 364 | if (!ReplacePlaceholder("${basename}", maybe_base_name, &result, diag)) { |
| 365 | return {}; |
| 366 | } |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 369 | // Extension is optional. |
| 370 | if (result.find("${ext}") != std::string::npos) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 371 | // Make sure we disregard the '.' in the extension when replacing the placeholder. |
| 372 | if (!ReplacePlaceholder("${ext}", {ext.substr(1)}, &result, diag)) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 373 | return {}; |
| 374 | } |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 375 | } else { |
| 376 | // If no extension is specified, and the name template does not end in the current extension, |
| 377 | // add the existing extension. |
| 378 | if (!util::EndsWith(result, ext)) { |
| 379 | result.append(ext.to_string()); |
| 380 | } |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 383 | return result; |
| 384 | } |
| 385 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 386 | Maybe<std::string> ConfiguredArtifact::ToArtifactName(const StringPiece& format, |
| 387 | const StringPiece& apk_name, |
| 388 | IDiagnostics* diag) const { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 389 | Maybe<std::string> base = ToBaseName(format.to_string(), apk_name, diag); |
| 390 | if (!base) { |
| 391 | return {}; |
| 392 | } |
| 393 | std::string result = std::move(base.value()); |
| 394 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 395 | if (!ReplacePlaceholder("${abi}", abi_group, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 396 | return {}; |
| 397 | } |
| 398 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 399 | if (!ReplacePlaceholder("${density}", screen_density_group, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 400 | return {}; |
| 401 | } |
| 402 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 403 | if (!ReplacePlaceholder("${locale}", locale_group, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 404 | return {}; |
| 405 | } |
| 406 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 407 | if (!ReplacePlaceholder("${sdk}", android_sdk, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 408 | return {}; |
| 409 | } |
| 410 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 411 | if (!ReplacePlaceholder("${feature}", device_feature_group, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 412 | return {}; |
| 413 | } |
| 414 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 415 | if (!ReplacePlaceholder("${gl}", gl_texture_group, &result, diag)) { |
| 416 | return {}; |
| 417 | } |
| 418 | |
| 419 | return result; |
| 420 | } |
| 421 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 422 | Maybe<std::string> ConfiguredArtifact::Name(const StringPiece& apk_name, IDiagnostics* diag) const { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 423 | if (!name) { |
| 424 | return {}; |
| 425 | } |
| 426 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame] | 427 | return ToBaseName(name.value(), apk_name, diag); |
| 428 | } |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 429 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 430 | } // namespace configuration |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 431 | |
| 432 | /** Returns a ConfigurationParser for the file located at the provided path. */ |
| 433 | Maybe<ConfigurationParser> ConfigurationParser::ForPath(const std::string& path) { |
| 434 | std::string contents; |
| 435 | if (!ReadFileToString(path, &contents, true)) { |
| 436 | return {}; |
| 437 | } |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 438 | return ConfigurationParser(contents, path); |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 441 | ConfigurationParser::ConfigurationParser(std::string contents, const std::string& config_path) |
| 442 | : contents_(std::move(contents)), config_path_(config_path), diag_(&noop_) { |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 445 | Maybe<std::vector<OutputArtifact>> ConfigurationParser::Parse( |
| 446 | const android::StringPiece& apk_path) { |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 447 | Maybe<PostProcessingConfiguration> maybe_config = |
| 448 | ExtractConfiguration(contents_, config_path_, diag_); |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 449 | if (!maybe_config) { |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 450 | return {}; |
| 451 | } |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 452 | |
| 453 | // Convert from a parsed configuration to a list of artifacts for processing. |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 454 | const std::string& apk_name = file::GetFilename(apk_path).to_string(); |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 455 | std::vector<OutputArtifact> output_artifacts; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 456 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 457 | PostProcessingConfiguration& config = maybe_config.value(); |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 458 | |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 459 | bool valid = true; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 460 | int version = 1; |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 461 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 462 | for (const ConfiguredArtifact& artifact : config.artifacts) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 463 | Maybe<OutputArtifact> output_artifact = ToOutputArtifact(artifact, apk_name, config, diag_); |
| 464 | if (!output_artifact) { |
| 465 | // Defer return an error condition so that all errors are reported. |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 466 | valid = false; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 467 | } else { |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 468 | output_artifact.value().version = version++; |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 469 | output_artifacts.push_back(std::move(output_artifact.value())); |
| 470 | } |
| 471 | } |
| 472 | |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 473 | if (!config.ValidateVersionCodeOrdering(diag_)) { |
| 474 | diag_->Error(DiagMessage() << "could not validate post processing configuration"); |
| 475 | valid = false; |
| 476 | } |
| 477 | |
| 478 | if (valid) { |
| 479 | // Sorting artifacts requires that all references are valid as it uses them to determine order. |
| 480 | config.SortArtifacts(); |
| 481 | } |
| 482 | |
| 483 | if (!valid) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 484 | return {}; |
| 485 | } |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 486 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 487 | return {output_artifacts}; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 490 | namespace configuration { |
| 491 | namespace handler { |
| 492 | |
| 493 | bool ArtifactTagHandler(PostProcessingConfiguration* config, Element* root_element, |
| 494 | IDiagnostics* diag) { |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 495 | ConfiguredArtifact artifact{}; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 496 | for (const auto& attr : root_element->attributes) { |
| 497 | if (attr.name == "name") { |
| 498 | artifact.name = attr.value; |
| 499 | } else if (attr.name == "abi-group") { |
| 500 | artifact.abi_group = {attr.value}; |
| 501 | } else if (attr.name == "screen-density-group") { |
| 502 | artifact.screen_density_group = {attr.value}; |
| 503 | } else if (attr.name == "locale-group") { |
| 504 | artifact.locale_group = {attr.value}; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 505 | } else if (attr.name == "android-sdk") { |
| 506 | artifact.android_sdk = {attr.value}; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 507 | } else if (attr.name == "gl-texture-group") { |
| 508 | artifact.gl_texture_group = {attr.value}; |
| 509 | } else if (attr.name == "device-feature-group") { |
| 510 | artifact.device_feature_group = {attr.value}; |
| 511 | } else { |
| 512 | diag->Note(DiagMessage() << "Unknown artifact attribute: " << attr.name << " = " |
| 513 | << attr.value); |
| 514 | } |
| 515 | } |
| 516 | config->artifacts.push_back(artifact); |
| 517 | return true; |
| 518 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 519 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 520 | bool ArtifactFormatTagHandler(PostProcessingConfiguration* config, Element* root_element, |
| 521 | IDiagnostics* /* diag */) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 522 | for (auto& node : root_element->children) { |
| 523 | xml::Text* t; |
| 524 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 525 | config->artifact_format = TrimWhitespace(t->text).to_string(); |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | return true; |
| 530 | }; |
| 531 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 532 | bool AbiGroupTagHandler(PostProcessingConfiguration* config, Element* root_element, |
| 533 | IDiagnostics* diag) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 534 | std::string label = GetLabel(root_element, diag); |
| 535 | if (label.empty()) { |
| 536 | return false; |
| 537 | } |
| 538 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 539 | bool valid = true; |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 540 | OrderedEntry<Abi>& entry = config->abi_groups[label]; |
| 541 | Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag); |
| 542 | if (!order) { |
| 543 | valid = false; |
| 544 | } else { |
| 545 | entry.order = order.value(); |
| 546 | } |
| 547 | auto& group = entry.entry; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 548 | |
Shane Farmer | 39e474f | 2017-12-18 14:44:11 -0800 | [diff] [blame] | 549 | // Special case for empty abi-group tag. Label will be used as the ABI. |
| 550 | if (root_element->GetChildElements().empty()) { |
| 551 | auto abi = kStringToAbiMap.find(label); |
| 552 | if (abi == kStringToAbiMap.end()) { |
| 553 | return false; |
| 554 | } |
| 555 | group.push_back(abi->second); |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 556 | return valid; |
Shane Farmer | 39e474f | 2017-12-18 14:44:11 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 559 | for (auto* child : root_element->GetChildElements()) { |
| 560 | if (child->name != "abi") { |
| 561 | diag->Error(DiagMessage() << "Unexpected element in ABI group: " << child->name); |
| 562 | valid = false; |
| 563 | } else { |
| 564 | for (auto& node : child->children) { |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 565 | xml::Text* t; |
| 566 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
Shane Farmer | 39e474f | 2017-12-18 14:44:11 -0800 | [diff] [blame] | 567 | auto abi = kStringToAbiMap.find(TrimWhitespace(t->text).to_string()); |
| 568 | if (abi != kStringToAbiMap.end()) { |
| 569 | group.push_back(abi->second); |
| 570 | } else { |
| 571 | diag->Error(DiagMessage() << "Could not parse ABI value: " << t->text); |
| 572 | valid = false; |
| 573 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 574 | break; |
| 575 | } |
| 576 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 579 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 580 | return valid; |
| 581 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 582 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 583 | bool ScreenDensityGroupTagHandler(PostProcessingConfiguration* config, Element* root_element, |
| 584 | IDiagnostics* diag) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 585 | std::string label = GetLabel(root_element, diag); |
| 586 | if (label.empty()) { |
| 587 | return false; |
| 588 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 589 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 590 | bool valid = true; |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 591 | OrderedEntry<ConfigDescription>& entry = config->screen_density_groups[label]; |
| 592 | Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag); |
| 593 | if (!order) { |
| 594 | valid = false; |
| 595 | } else { |
| 596 | entry.order = order.value(); |
| 597 | } |
| 598 | auto& group = entry.entry; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 599 | |
Shane Farmer | 39e474f | 2017-12-18 14:44:11 -0800 | [diff] [blame] | 600 | // Special case for empty screen-density-group tag. Label will be used as the screen density. |
| 601 | if (root_element->GetChildElements().empty()) { |
| 602 | ConfigDescription config_descriptor; |
| 603 | bool parsed = ConfigDescription::Parse(label, &config_descriptor); |
| 604 | if (parsed && |
| 605 | (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) == |
| 606 | android::ResTable_config::CONFIG_DENSITY)) { |
| 607 | // Copy the density with the minimum SDK version stripped out. |
| 608 | group.push_back(config_descriptor.CopyWithoutSdkVersion()); |
| 609 | } else { |
| 610 | diag->Error(DiagMessage() |
| 611 | << "Could not parse config descriptor for empty screen-density-group: " |
| 612 | << label); |
| 613 | valid = false; |
| 614 | } |
| 615 | |
| 616 | return valid; |
| 617 | } |
| 618 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 619 | for (auto* child : root_element->GetChildElements()) { |
| 620 | if (child->name != "screen-density") { |
| 621 | diag->Error(DiagMessage() << "Unexpected root_element in screen density group: " |
| 622 | << child->name); |
| 623 | valid = false; |
| 624 | } else { |
| 625 | for (auto& node : child->children) { |
| 626 | xml::Text* t; |
| 627 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 628 | ConfigDescription config_descriptor; |
| 629 | const android::StringPiece& text = TrimWhitespace(t->text); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 630 | bool parsed = ConfigDescription::Parse(text, &config_descriptor); |
| 631 | if (parsed && |
| 632 | (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) == |
| 633 | android::ResTable_config::CONFIG_DENSITY)) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 634 | // Copy the density with the minimum SDK version stripped out. |
| 635 | group.push_back(config_descriptor.CopyWithoutSdkVersion()); |
| 636 | } else { |
| 637 | diag->Error(DiagMessage() |
| 638 | << "Could not parse config descriptor for screen-density: " << text); |
| 639 | valid = false; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 640 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 641 | break; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 646 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 647 | return valid; |
| 648 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 649 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 650 | bool LocaleGroupTagHandler(PostProcessingConfiguration* config, Element* root_element, |
| 651 | IDiagnostics* diag) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 652 | std::string label = GetLabel(root_element, diag); |
| 653 | if (label.empty()) { |
| 654 | return false; |
| 655 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 656 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 657 | bool valid = true; |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 658 | OrderedEntry<ConfigDescription>& entry = config->locale_groups[label]; |
| 659 | Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag); |
| 660 | if (!order) { |
| 661 | valid = false; |
| 662 | } else { |
| 663 | entry.order = order.value(); |
| 664 | } |
| 665 | auto& group = entry.entry; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 666 | |
Shane Farmer | 39e474f | 2017-12-18 14:44:11 -0800 | [diff] [blame] | 667 | // Special case to auto insert a locale for an empty group. Label will be used for locale. |
| 668 | if (root_element->GetChildElements().empty()) { |
| 669 | ConfigDescription config_descriptor; |
| 670 | bool parsed = ConfigDescription::Parse(label, &config_descriptor); |
| 671 | if (parsed && |
| 672 | (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) == |
| 673 | android::ResTable_config::CONFIG_LOCALE)) { |
| 674 | // Copy the locale with the minimum SDK version stripped out. |
| 675 | group.push_back(config_descriptor.CopyWithoutSdkVersion()); |
| 676 | } else { |
| 677 | diag->Error(DiagMessage() |
| 678 | << "Could not parse config descriptor for empty screen-density-group: " |
| 679 | << label); |
| 680 | valid = false; |
| 681 | } |
| 682 | |
| 683 | return valid; |
| 684 | } |
| 685 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 686 | for (auto* child : root_element->GetChildElements()) { |
| 687 | if (child->name != "locale") { |
| 688 | diag->Error(DiagMessage() << "Unexpected root_element in screen density group: " |
| 689 | << child->name); |
| 690 | valid = false; |
| 691 | } else { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 692 | for (auto& node : child->children) { |
| 693 | xml::Text* t; |
| 694 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 695 | ConfigDescription config_descriptor; |
| 696 | const android::StringPiece& text = TrimWhitespace(t->text); |
| 697 | bool parsed = ConfigDescription::Parse(text, &config_descriptor); |
| 698 | if (parsed && |
| 699 | (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) == |
| 700 | android::ResTable_config::CONFIG_LOCALE)) { |
| 701 | // Copy the locale with the minimum SDK version stripped out. |
| 702 | group.push_back(config_descriptor.CopyWithoutSdkVersion()); |
| 703 | } else { |
| 704 | diag->Error(DiagMessage() |
| 705 | << "Could not parse config descriptor for screen-density: " << text); |
| 706 | valid = false; |
| 707 | } |
| 708 | break; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 711 | } |
| 712 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 713 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 714 | return valid; |
| 715 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 716 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 717 | bool AndroidSdkTagHandler(PostProcessingConfiguration* config, Element* root_element, |
| 718 | IDiagnostics* diag) { |
| 719 | AndroidSdk entry = AndroidSdk::ForMinSdk(-1); |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 720 | bool valid = true; |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 721 | for (const auto& attr : root_element->attributes) { |
| 722 | bool valid_attr = false; |
| 723 | if (attr.name == "label") { |
| 724 | entry.label = attr.value; |
| 725 | valid_attr = true; |
| 726 | } else if (attr.name == "minSdkVersion") { |
| 727 | Maybe<int> version = ResourceUtils::ParseSdkVersion(attr.value); |
| 728 | if (version) { |
| 729 | valid_attr = true; |
| 730 | entry.min_sdk_version = version.value(); |
| 731 | } |
| 732 | } else if (attr.name == "targetSdkVersion") { |
| 733 | Maybe<int> version = ResourceUtils::ParseSdkVersion(attr.value); |
| 734 | if (version) { |
| 735 | valid_attr = true; |
| 736 | entry.target_sdk_version = version; |
| 737 | } |
| 738 | } else if (attr.name == "maxSdkVersion") { |
| 739 | Maybe<int> version = ResourceUtils::ParseSdkVersion(attr.value); |
| 740 | if (version) { |
| 741 | valid_attr = true; |
| 742 | entry.max_sdk_version = version; |
| 743 | } |
| 744 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 745 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 746 | if (!valid_attr) { |
| 747 | diag->Error(DiagMessage() << "Invalid attribute: " << attr.name << " = " << attr.value); |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 748 | valid = false; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Shane Farmer | 78c43d7 | 2017-12-04 09:08:38 -0800 | [diff] [blame] | 752 | if (entry.min_sdk_version == -1) { |
| 753 | diag->Error(DiagMessage() << "android-sdk is missing minSdkVersion attribute"); |
| 754 | valid = false; |
| 755 | } |
| 756 | |
| 757 | // TODO: Fill in the manifest details when they are finalised. |
| 758 | for (auto node : root_element->GetChildElements()) { |
| 759 | if (node->name == "manifest") { |
| 760 | if (entry.manifest) { |
| 761 | diag->Warn(DiagMessage() << "Found multiple manifest tags. Ignoring duplicates."); |
| 762 | continue; |
| 763 | } |
| 764 | entry.manifest = {AndroidManifest()}; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | config->android_sdks[entry.label] = entry; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 769 | return valid; |
| 770 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 771 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 772 | bool GlTextureGroupTagHandler(PostProcessingConfiguration* config, Element* root_element, |
| 773 | IDiagnostics* diag) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 774 | std::string label = GetLabel(root_element, diag); |
| 775 | if (label.empty()) { |
| 776 | return false; |
| 777 | } |
| 778 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 779 | bool valid = true; |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 780 | OrderedEntry<GlTexture>& entry = config->gl_texture_groups[label]; |
| 781 | Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag); |
| 782 | if (!order) { |
| 783 | valid = false; |
| 784 | } else { |
| 785 | entry.order = order.value(); |
| 786 | } |
| 787 | auto& group = entry.entry; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 788 | |
| 789 | GlTexture result; |
| 790 | for (auto* child : root_element->GetChildElements()) { |
| 791 | if (child->name != "gl-texture") { |
| 792 | diag->Error(DiagMessage() << "Unexpected element in GL texture group: " << child->name); |
| 793 | valid = false; |
| 794 | } else { |
| 795 | for (const auto& attr : child->attributes) { |
| 796 | if (attr.name == "name") { |
| 797 | result.name = attr.value; |
| 798 | break; |
| 799 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 802 | for (auto* element : child->GetChildElements()) { |
| 803 | if (element->name != "texture-path") { |
| 804 | diag->Error(DiagMessage() << "Unexpected element in gl-texture element: " << child->name); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 805 | valid = false; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 806 | continue; |
| 807 | } |
| 808 | for (auto& node : element->children) { |
| 809 | xml::Text* t; |
| 810 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 811 | result.texture_paths.push_back(TrimWhitespace(t->text).to_string()); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 814 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 815 | } |
| 816 | group.push_back(result); |
| 817 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 818 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 819 | return valid; |
| 820 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 821 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 822 | bool DeviceFeatureGroupTagHandler(PostProcessingConfiguration* config, Element* root_element, |
| 823 | IDiagnostics* diag) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 824 | std::string label = GetLabel(root_element, diag); |
| 825 | if (label.empty()) { |
| 826 | return false; |
| 827 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 828 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 829 | bool valid = true; |
Shane Farmer | 11cdc1c | 2018-01-31 16:43:24 -0800 | [diff] [blame] | 830 | OrderedEntry<DeviceFeature>& entry = config->device_feature_groups[label]; |
| 831 | Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag); |
| 832 | if (!order) { |
| 833 | valid = false; |
| 834 | } else { |
| 835 | entry.order = order.value(); |
| 836 | } |
| 837 | auto& group = entry.entry; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 838 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 839 | for (auto* child : root_element->GetChildElements()) { |
| 840 | if (child->name != "supports-feature") { |
| 841 | diag->Error(DiagMessage() << "Unexpected root_element in device feature group: " |
| 842 | << child->name); |
| 843 | valid = false; |
| 844 | } else { |
| 845 | for (auto& node : child->children) { |
| 846 | xml::Text* t; |
| 847 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 848 | group.push_back(TrimWhitespace(t->text).to_string()); |
| 849 | break; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 852 | } |
| 853 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 854 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 855 | return valid; |
| 856 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 857 | |
Shane Farmer | cb6c3f9 | 2017-11-27 13:19:36 -0800 | [diff] [blame] | 858 | } // namespace handler |
| 859 | } // namespace configuration |
| 860 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 861 | } // namespace aapt |