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