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 | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 30 | #include "io/File.h" |
| 31 | #include "io/FileSystem.h" |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 32 | #include "io/StringInputStream.h" |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 33 | #include "util/Files.h" |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 34 | #include "util/Maybe.h" |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 35 | #include "util/Util.h" |
| 36 | #include "xml/XmlActionExecutor.h" |
| 37 | #include "xml/XmlDom.h" |
| 38 | #include "xml/XmlUtil.h" |
| 39 | |
| 40 | namespace aapt { |
| 41 | |
| 42 | namespace { |
| 43 | |
| 44 | using ::aapt::configuration::Abi; |
| 45 | using ::aapt::configuration::AndroidManifest; |
| 46 | using ::aapt::configuration::AndroidSdk; |
| 47 | using ::aapt::configuration::Artifact; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 48 | using ::aapt::configuration::PostProcessingConfiguration; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 49 | using ::aapt::configuration::GlTexture; |
| 50 | using ::aapt::configuration::Group; |
| 51 | using ::aapt::configuration::Locale; |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 52 | using ::aapt::io::IFile; |
| 53 | using ::aapt::io::RegularFile; |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 54 | using ::aapt::io::StringInputStream; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 55 | using ::aapt::util::TrimWhitespace; |
| 56 | using ::aapt::xml::Element; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 57 | using ::aapt::xml::NodeCast; |
| 58 | using ::aapt::xml::XmlActionExecutor; |
| 59 | using ::aapt::xml::XmlActionExecutorPolicy; |
| 60 | using ::aapt::xml::XmlNodeAction; |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 61 | using ::android::base::ReadFileToString; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 62 | using ::android::StringPiece; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 63 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 64 | const std::unordered_map<std::string, Abi> kStringToAbiMap = { |
| 65 | {"armeabi", Abi::kArmeV6}, {"armeabi-v7a", Abi::kArmV7a}, {"arm64-v8a", Abi::kArm64V8a}, |
| 66 | {"x86", Abi::kX86}, {"x86_64", Abi::kX86_64}, {"mips", Abi::kMips}, |
| 67 | {"mips64", Abi::kMips64}, {"universal", Abi::kUniversal}, |
| 68 | }; |
| 69 | const std::map<Abi, std::string> kAbiToStringMap = { |
| 70 | {Abi::kArmeV6, "armeabi"}, {Abi::kArmV7a, "armeabi-v7a"}, {Abi::kArm64V8a, "arm64-v8a"}, |
| 71 | {Abi::kX86, "x86"}, {Abi::kX86_64, "x86_64"}, {Abi::kMips, "mips"}, |
| 72 | {Abi::kMips64, "mips64"}, {Abi::kUniversal, "universal"}, |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | constexpr const char* kAaptXmlNs = "http://schemas.android.com/tools/aapt"; |
| 76 | |
| 77 | /** A default noop diagnostics context. */ |
| 78 | class NoopDiagnostics : public IDiagnostics { |
| 79 | public: |
| 80 | void Log(Level level, DiagMessageActual& actualMsg) override {} |
| 81 | }; |
| 82 | NoopDiagnostics noop_; |
| 83 | |
| 84 | std::string GetLabel(const Element* element, IDiagnostics* diag) { |
| 85 | std::string label; |
| 86 | for (const auto& attr : element->attributes) { |
| 87 | if (attr.name == "label") { |
| 88 | label = attr.value; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (label.empty()) { |
| 94 | diag->Error(DiagMessage() << "No label found for element " << element->name); |
| 95 | } |
| 96 | return label; |
| 97 | } |
| 98 | |
| 99 | /** XML node visitor that removes all of the namespace URIs from the node and all children. */ |
| 100 | class NamespaceVisitor : public xml::Visitor { |
| 101 | public: |
| 102 | void Visit(xml::Element* node) override { |
| 103 | node->namespace_uri.clear(); |
| 104 | VisitChildren(node); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | } // namespace |
| 109 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 110 | namespace configuration { |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 111 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 112 | const std::string& AbiToString(Abi abi) { |
| 113 | return kAbiToStringMap.find(abi)->second; |
| 114 | } |
| 115 | |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Attempts to replace the placeholder in the name string with the provided value. Returns true on |
| 118 | * success, or false if the either the placeholder is not found in the name, or the value is not |
| 119 | * present and the placeholder was. |
| 120 | */ |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 121 | static bool ReplacePlaceholder(const StringPiece& placeholder, const Maybe<StringPiece>& value, |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 122 | std::string* name, IDiagnostics* diag) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 123 | size_t offset = name->find(placeholder.data()); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 124 | bool found = (offset != std::string::npos); |
| 125 | |
| 126 | // Make sure the placeholder was present if the desired value is present. |
| 127 | if (!found) { |
| 128 | if (value) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 129 | diag->Error(DiagMessage() << "Missing placeholder for artifact: " << placeholder); |
| 130 | return false; |
| 131 | } |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 135 | DCHECK(found) << "Missing return path for placeholder not found"; |
| 136 | |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 137 | // 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] | 138 | if (!value) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 139 | diag->Error(DiagMessage() << "Placeholder present but no value for artifact: " << placeholder); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 140 | return false; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 141 | } |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 142 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 143 | name->replace(offset, placeholder.length(), value.value().data()); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 144 | |
| 145 | // Make sure there was only one instance of the placeholder. |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 146 | if (name->find(placeholder.data()) != std::string::npos) { |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame] | 147 | diag->Error(DiagMessage() << "Placeholder present multiple times: " << placeholder); |
| 148 | return false; |
| 149 | } |
| 150 | return true; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 153 | /** |
| 154 | * Returns the common artifact base name from a template string. |
| 155 | */ |
| 156 | Maybe<std::string> ToBaseName(std::string result, const StringPiece& apk_name, IDiagnostics* diag) { |
| 157 | const StringPiece ext = file::GetExtension(apk_name); |
| 158 | size_t end_index = apk_name.to_string().rfind(ext.to_string()); |
| 159 | const std::string base_name = |
| 160 | (end_index != std::string::npos) ? std::string{apk_name.begin(), end_index} : ""; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 161 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 162 | // Base name is optional. |
| 163 | if (result.find("${basename}") != std::string::npos) { |
| 164 | Maybe<StringPiece> maybe_base_name = |
| 165 | base_name.empty() ? Maybe<StringPiece>{} : Maybe<StringPiece>{base_name}; |
| 166 | if (!ReplacePlaceholder("${basename}", maybe_base_name, &result, diag)) { |
| 167 | return {}; |
| 168 | } |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 171 | // Extension is optional. |
| 172 | if (result.find("${ext}") != std::string::npos) { |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 173 | // Make sure we disregard the '.' in the extension when replacing the placeholder. |
| 174 | if (!ReplacePlaceholder("${ext}", {ext.substr(1)}, &result, diag)) { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 175 | return {}; |
| 176 | } |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 177 | } else { |
| 178 | // If no extension is specified, and the name template does not end in the current extension, |
| 179 | // add the existing extension. |
| 180 | if (!util::EndsWith(result, ext)) { |
| 181 | result.append(ext.to_string()); |
| 182 | } |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 185 | return result; |
| 186 | } |
| 187 | |
| 188 | Maybe<std::string> Artifact::ToArtifactName(const StringPiece& format, const StringPiece& apk_name, |
| 189 | IDiagnostics* diag) const { |
| 190 | Maybe<std::string> base = ToBaseName(format.to_string(), apk_name, diag); |
| 191 | if (!base) { |
| 192 | return {}; |
| 193 | } |
| 194 | std::string result = std::move(base.value()); |
| 195 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 196 | if (!ReplacePlaceholder("${abi}", abi_group, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 197 | return {}; |
| 198 | } |
| 199 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 200 | if (!ReplacePlaceholder("${density}", screen_density_group, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 201 | return {}; |
| 202 | } |
| 203 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 204 | if (!ReplacePlaceholder("${locale}", locale_group, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 205 | return {}; |
| 206 | } |
| 207 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 208 | if (!ReplacePlaceholder("${sdk}", android_sdk_group, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 209 | return {}; |
| 210 | } |
| 211 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 212 | if (!ReplacePlaceholder("${feature}", device_feature_group, &result, diag)) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 213 | return {}; |
| 214 | } |
| 215 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 216 | if (!ReplacePlaceholder("${gl}", gl_texture_group, &result, diag)) { |
| 217 | return {}; |
| 218 | } |
| 219 | |
| 220 | return result; |
| 221 | } |
| 222 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 223 | Maybe<std::string> Artifact::Name(const StringPiece& apk_name, IDiagnostics* diag) const { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 224 | if (!name) { |
| 225 | return {}; |
| 226 | } |
| 227 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 228 | return ToBaseName(name.value(), apk_name, diag); |
| 229 | } |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 230 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 231 | bool PostProcessingConfiguration::AllArtifactNames(const StringPiece& apk_name, |
| 232 | std::vector<std::string>* artifact_names, |
| 233 | IDiagnostics* diag) const { |
| 234 | for (const auto& artifact : artifacts) { |
| 235 | Maybe<std::string> name; |
| 236 | if (artifact.name) { |
| 237 | name = artifact.Name(apk_name, diag); |
| 238 | } else { |
| 239 | if (!artifact_format) { |
| 240 | diag->Error(DiagMessage() << "No global artifact template and an artifact name is missing"); |
| 241 | return false; |
| 242 | } |
| 243 | name = artifact.ToArtifactName(artifact_format.value(), apk_name, diag); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 244 | } |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 245 | |
| 246 | if (!name) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | artifact_names->push_back(std::move(name.value())); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Shane Farmer | 9ecc075 | 2017-08-24 15:55:36 -0700 | [diff] [blame^] | 253 | return true; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 256 | } // namespace configuration |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 257 | |
| 258 | /** Returns a ConfigurationParser for the file located at the provided path. */ |
| 259 | Maybe<ConfigurationParser> ConfigurationParser::ForPath(const std::string& path) { |
| 260 | std::string contents; |
| 261 | if (!ReadFileToString(path, &contents, true)) { |
| 262 | return {}; |
| 263 | } |
| 264 | return ConfigurationParser(contents); |
| 265 | } |
| 266 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 267 | ConfigurationParser::ConfigurationParser(std::string contents) |
| 268 | : contents_(std::move(contents)), |
| 269 | diag_(&noop_) { |
| 270 | } |
| 271 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 272 | Maybe<PostProcessingConfiguration> ConfigurationParser::Parse() { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 273 | StringInputStream in(contents_); |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 274 | std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, diag_, Source("config.xml")); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 275 | if (!doc) { |
| 276 | return {}; |
| 277 | } |
| 278 | |
| 279 | // Strip any namespaces from the XML as the XmlActionExecutor ignores anything with a namespace. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 280 | Element* root = doc->root.get(); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 281 | if (root == nullptr) { |
| 282 | diag_->Error(DiagMessage() << "Could not find the root element in the XML document"); |
| 283 | return {}; |
| 284 | } |
| 285 | |
| 286 | std::string& xml_ns = root->namespace_uri; |
| 287 | if (!xml_ns.empty()) { |
| 288 | if (xml_ns != kAaptXmlNs) { |
| 289 | diag_->Error(DiagMessage() << "Unknown namespace found on root element: " << xml_ns); |
| 290 | return {}; |
| 291 | } |
| 292 | |
| 293 | xml_ns.clear(); |
| 294 | NamespaceVisitor visitor; |
| 295 | root->Accept(&visitor); |
| 296 | } |
| 297 | |
| 298 | XmlActionExecutor executor; |
| 299 | XmlNodeAction& root_action = executor["post-process"]; |
| 300 | XmlNodeAction& artifacts_action = root_action["artifacts"]; |
| 301 | XmlNodeAction& groups_action = root_action["groups"]; |
| 302 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 303 | PostProcessingConfiguration config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 304 | |
| 305 | // Helper to bind a static method to an action handler in the DOM executor. |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 306 | auto bind_handler = |
| 307 | [&config](std::function<bool(PostProcessingConfiguration*, Element*, IDiagnostics*)> h) |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 308 | -> XmlNodeAction::ActionFuncWithDiag { |
| 309 | return std::bind(h, &config, std::placeholders::_1, std::placeholders::_2); |
| 310 | }; |
| 311 | |
| 312 | // Parse the artifact elements. |
| 313 | artifacts_action["artifact"].Action(bind_handler(artifact_handler_)); |
| 314 | artifacts_action["artifact-format"].Action(bind_handler(artifact_format_handler_)); |
| 315 | |
| 316 | // Parse the different configuration groups. |
| 317 | groups_action["abi-group"].Action(bind_handler(abi_group_handler_)); |
| 318 | groups_action["screen-density-group"].Action(bind_handler(screen_density_group_handler_)); |
| 319 | groups_action["locale-group"].Action(bind_handler(locale_group_handler_)); |
| 320 | groups_action["android-sdk-group"].Action(bind_handler(android_sdk_group_handler_)); |
| 321 | groups_action["gl-texture-group"].Action(bind_handler(gl_texture_group_handler_)); |
| 322 | groups_action["device-feature-group"].Action(bind_handler(device_feature_group_handler_)); |
| 323 | |
| 324 | if (!executor.Execute(XmlActionExecutorPolicy::kNone, diag_, doc.get())) { |
| 325 | diag_->Error(DiagMessage() << "Could not process XML document"); |
| 326 | return {}; |
| 327 | } |
| 328 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 329 | // TODO: Validate all references in the configuration are valid. It should be safe to assume from |
| 330 | // this point on that any references from one section to another will be present. |
| 331 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 332 | return {config}; |
| 333 | } |
| 334 | |
| 335 | ConfigurationParser::ActionHandler ConfigurationParser::artifact_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 336 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 337 | Artifact artifact{}; |
| 338 | for (const auto& attr : root_element->attributes) { |
| 339 | if (attr.name == "name") { |
| 340 | artifact.name = attr.value; |
| 341 | } else if (attr.name == "abi-group") { |
| 342 | artifact.abi_group = {attr.value}; |
| 343 | } else if (attr.name == "screen-density-group") { |
| 344 | artifact.screen_density_group = {attr.value}; |
| 345 | } else if (attr.name == "locale-group") { |
| 346 | artifact.locale_group = {attr.value}; |
| 347 | } else if (attr.name == "android-sdk-group") { |
| 348 | artifact.android_sdk_group = {attr.value}; |
| 349 | } else if (attr.name == "gl-texture-group") { |
| 350 | artifact.gl_texture_group = {attr.value}; |
| 351 | } else if (attr.name == "device-feature-group") { |
| 352 | artifact.device_feature_group = {attr.value}; |
| 353 | } else { |
| 354 | diag->Note(DiagMessage() << "Unknown artifact attribute: " << attr.name << " = " |
| 355 | << attr.value); |
| 356 | } |
| 357 | } |
| 358 | config->artifacts.push_back(artifact); |
| 359 | return true; |
| 360 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 361 | |
| 362 | ConfigurationParser::ActionHandler ConfigurationParser::artifact_format_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 363 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 364 | for (auto& node : root_element->children) { |
| 365 | xml::Text* t; |
| 366 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 367 | config->artifact_format = TrimWhitespace(t->text).to_string(); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | return true; |
| 372 | }; |
| 373 | |
| 374 | ConfigurationParser::ActionHandler ConfigurationParser::abi_group_handler_ = |
| 375 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 376 | std::string label = GetLabel(root_element, diag); |
| 377 | if (label.empty()) { |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | auto& group = config->abi_groups[label]; |
| 382 | bool valid = true; |
| 383 | |
| 384 | for (auto* child : root_element->GetChildElements()) { |
| 385 | if (child->name != "abi") { |
| 386 | diag->Error(DiagMessage() << "Unexpected element in ABI group: " << child->name); |
| 387 | valid = false; |
| 388 | } else { |
| 389 | for (auto& node : child->children) { |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 390 | xml::Text* t; |
| 391 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 392 | group.push_back(kStringToAbiMap.at(TrimWhitespace(t->text).to_string())); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 393 | break; |
| 394 | } |
| 395 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 398 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 399 | return valid; |
| 400 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 401 | |
| 402 | ConfigurationParser::ActionHandler ConfigurationParser::screen_density_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 403 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 404 | std::string label = GetLabel(root_element, diag); |
| 405 | if (label.empty()) { |
| 406 | return false; |
| 407 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 408 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 409 | auto& group = config->screen_density_groups[label]; |
| 410 | bool valid = true; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 411 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 412 | for (auto* child : root_element->GetChildElements()) { |
| 413 | if (child->name != "screen-density") { |
| 414 | diag->Error(DiagMessage() << "Unexpected root_element in screen density group: " |
| 415 | << child->name); |
| 416 | valid = false; |
| 417 | } else { |
| 418 | for (auto& node : child->children) { |
| 419 | xml::Text* t; |
| 420 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 421 | ConfigDescription config_descriptor; |
| 422 | const android::StringPiece& text = TrimWhitespace(t->text); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 423 | bool parsed = ConfigDescription::Parse(text, &config_descriptor); |
| 424 | if (parsed && |
| 425 | (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) == |
| 426 | android::ResTable_config::CONFIG_DENSITY)) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 427 | // Copy the density with the minimum SDK version stripped out. |
| 428 | group.push_back(config_descriptor.CopyWithoutSdkVersion()); |
| 429 | } else { |
| 430 | diag->Error(DiagMessage() |
| 431 | << "Could not parse config descriptor for screen-density: " << text); |
| 432 | valid = false; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 433 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 434 | break; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 439 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 440 | return valid; |
| 441 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 442 | |
| 443 | ConfigurationParser::ActionHandler ConfigurationParser::locale_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 444 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 445 | std::string label = GetLabel(root_element, diag); |
| 446 | if (label.empty()) { |
| 447 | return false; |
| 448 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 449 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 450 | auto& group = config->locale_groups[label]; |
| 451 | bool valid = true; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 452 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 453 | for (auto* child : root_element->GetChildElements()) { |
| 454 | if (child->name != "locale") { |
| 455 | diag->Error(DiagMessage() << "Unexpected root_element in screen density group: " |
| 456 | << child->name); |
| 457 | valid = false; |
| 458 | } else { |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 459 | for (auto& node : child->children) { |
| 460 | xml::Text* t; |
| 461 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 462 | ConfigDescription config_descriptor; |
| 463 | const android::StringPiece& text = TrimWhitespace(t->text); |
| 464 | bool parsed = ConfigDescription::Parse(text, &config_descriptor); |
| 465 | if (parsed && |
| 466 | (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) == |
| 467 | android::ResTable_config::CONFIG_LOCALE)) { |
| 468 | // Copy the locale with the minimum SDK version stripped out. |
| 469 | group.push_back(config_descriptor.CopyWithoutSdkVersion()); |
| 470 | } else { |
| 471 | diag->Error(DiagMessage() |
| 472 | << "Could not parse config descriptor for screen-density: " << text); |
| 473 | valid = false; |
| 474 | } |
| 475 | break; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 480 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 481 | return valid; |
| 482 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 483 | |
| 484 | ConfigurationParser::ActionHandler ConfigurationParser::android_sdk_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 485 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 486 | std::string label = GetLabel(root_element, diag); |
| 487 | if (label.empty()) { |
| 488 | return false; |
| 489 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 490 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 491 | auto& group = config->android_sdk_groups[label]; |
| 492 | bool valid = true; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 493 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 494 | for (auto* child : root_element->GetChildElements()) { |
| 495 | if (child->name != "android-sdk") { |
| 496 | diag->Error(DiagMessage() << "Unexpected root_element in ABI group: " << child->name); |
| 497 | valid = false; |
| 498 | } else { |
| 499 | AndroidSdk entry; |
| 500 | for (const auto& attr : child->attributes) { |
| 501 | if (attr.name == "minSdkVersion") { |
| 502 | entry.min_sdk_version = {attr.value}; |
| 503 | } else if (attr.name == "targetSdkVersion") { |
| 504 | entry.target_sdk_version = {attr.value}; |
| 505 | } else if (attr.name == "maxSdkVersion") { |
| 506 | entry.max_sdk_version = {attr.value}; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 507 | } else { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 508 | diag->Warn(DiagMessage() << "Unknown attribute: " << attr.name << " = " << attr.value); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 512 | // TODO: Fill in the manifest details when they are finalised. |
| 513 | for (auto node : child->GetChildElements()) { |
| 514 | if (node->name == "manifest") { |
| 515 | if (entry.manifest) { |
| 516 | diag->Warn(DiagMessage() << "Found multiple manifest tags. Ignoring duplicates."); |
| 517 | continue; |
| 518 | } |
| 519 | entry.manifest = {AndroidManifest()}; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | group.push_back(entry); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return valid; |
| 528 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 529 | |
| 530 | ConfigurationParser::ActionHandler ConfigurationParser::gl_texture_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 531 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 532 | std::string label = GetLabel(root_element, diag); |
| 533 | if (label.empty()) { |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | auto& group = config->gl_texture_groups[label]; |
| 538 | bool valid = true; |
| 539 | |
| 540 | GlTexture result; |
| 541 | for (auto* child : root_element->GetChildElements()) { |
| 542 | if (child->name != "gl-texture") { |
| 543 | diag->Error(DiagMessage() << "Unexpected element in GL texture group: " << child->name); |
| 544 | valid = false; |
| 545 | } else { |
| 546 | for (const auto& attr : child->attributes) { |
| 547 | if (attr.name == "name") { |
| 548 | result.name = attr.value; |
| 549 | break; |
| 550 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 553 | for (auto* element : child->GetChildElements()) { |
| 554 | if (element->name != "texture-path") { |
| 555 | diag->Error(DiagMessage() << "Unexpected element in gl-texture element: " << child->name); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 556 | valid = false; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 557 | continue; |
| 558 | } |
| 559 | for (auto& node : element->children) { |
| 560 | xml::Text* t; |
| 561 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 562 | result.texture_paths.push_back(TrimWhitespace(t->text).to_string()); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 565 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 566 | } |
| 567 | group.push_back(result); |
| 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 | return valid; |
| 571 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 572 | |
| 573 | ConfigurationParser::ActionHandler ConfigurationParser::device_feature_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 574 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 575 | std::string label = GetLabel(root_element, diag); |
| 576 | if (label.empty()) { |
| 577 | return false; |
| 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 | auto& group = config->device_feature_groups[label]; |
| 581 | bool valid = true; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 582 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 583 | for (auto* child : root_element->GetChildElements()) { |
| 584 | if (child->name != "supports-feature") { |
| 585 | diag->Error(DiagMessage() << "Unexpected root_element in device feature group: " |
| 586 | << child->name); |
| 587 | valid = false; |
| 588 | } else { |
| 589 | for (auto& node : child->children) { |
| 590 | xml::Text* t; |
| 591 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 592 | group.push_back(TrimWhitespace(t->text).to_string()); |
| 593 | break; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 594 | } |
| 595 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 598 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 599 | return valid; |
| 600 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 601 | |
| 602 | } // namespace aapt |