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