blob: ebc523f096dbdacce54780b405875a1bafee9870 [file] [log] [blame]
Shane Farmer74cdea32017-05-12 16:22:36 -07001/*
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 Farmer57669432017-06-19 12:52:04 -070021#include <map>
Shane Farmer74cdea32017-05-12 16:22:36 -070022#include <memory>
23#include <utility>
24
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070025#include "android-base/file.h"
26#include "android-base/logging.h"
Shane Farmer74cdea32017-05-12 16:22:36 -070027
28#include "ConfigDescription.h"
29#include "Diagnostics.h"
Shane Farmer3edd4722017-09-01 14:34:22 -070030#include "ResourceUtils.h"
Shane Farmercb6c3f92017-11-27 13:19:36 -080031#include "configuration/ConfigurationParser.internal.h"
Shane Farmerb1027272017-06-14 09:10:28 -070032#include "io/File.h"
33#include "io/FileSystem.h"
Adam Lesinski00451162017-10-03 07:44:08 -070034#include "io/StringStream.h"
Shane Farmer9ecc0752017-08-24 15:55:36 -070035#include "util/Files.h"
Shane Farmer9f0e7f12017-06-22 12:26:44 -070036#include "util/Maybe.h"
Shane Farmer74cdea32017-05-12 16:22:36 -070037#include "util/Util.h"
38#include "xml/XmlActionExecutor.h"
39#include "xml/XmlDom.h"
40#include "xml/XmlUtil.h"
41
42namespace aapt {
43
44namespace {
45
46using ::aapt::configuration::Abi;
47using ::aapt::configuration::AndroidManifest;
48using ::aapt::configuration::AndroidSdk;
Shane Farmercb6c3f92017-11-27 13:19:36 -080049using ::aapt::configuration::ConfiguredArtifact;
50using ::aapt::configuration::DeviceFeature;
51using ::aapt::configuration::Entry;
Shane Farmer74cdea32017-05-12 16:22:36 -070052using ::aapt::configuration::GlTexture;
53using ::aapt::configuration::Group;
54using ::aapt::configuration::Locale;
Shane Farmercb6c3f92017-11-27 13:19:36 -080055using ::aapt::configuration::OutputArtifact;
56using ::aapt::configuration::PostProcessingConfiguration;
57using ::aapt::configuration::handler::AbiGroupTagHandler;
58using ::aapt::configuration::handler::AndroidSdkGroupTagHandler;
59using ::aapt::configuration::handler::ArtifactFormatTagHandler;
60using ::aapt::configuration::handler::ArtifactTagHandler;
61using ::aapt::configuration::handler::DeviceFeatureGroupTagHandler;
62using ::aapt::configuration::handler::GlTextureGroupTagHandler;
63using ::aapt::configuration::handler::LocaleGroupTagHandler;
64using ::aapt::configuration::handler::ScreenDensityGroupTagHandler;
Shane Farmerb1027272017-06-14 09:10:28 -070065using ::aapt::io::IFile;
66using ::aapt::io::RegularFile;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070067using ::aapt::io::StringInputStream;
Shane Farmer74cdea32017-05-12 16:22:36 -070068using ::aapt::util::TrimWhitespace;
69using ::aapt::xml::Element;
Shane Farmer74cdea32017-05-12 16:22:36 -070070using ::aapt::xml::NodeCast;
71using ::aapt::xml::XmlActionExecutor;
72using ::aapt::xml::XmlActionExecutorPolicy;
73using ::aapt::xml::XmlNodeAction;
Shane Farmer0a5b2012017-06-22 12:24:12 -070074using ::android::StringPiece;
Shane Farmercb6c3f92017-11-27 13:19:36 -080075using ::android::base::ReadFileToString;
Shane Farmer74cdea32017-05-12 16:22:36 -070076
Shane Farmercb6c3f92017-11-27 13:19:36 -080077const std::unordered_map<StringPiece, Abi> kStringToAbiMap = {
Shane Farmer57669432017-06-19 12:52:04 -070078 {"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 Farmercb6c3f92017-11-27 13:19:36 -080082const std::array<StringPiece, 8> kAbiToStringMap = {
83 {"armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64", "mips", "mips64", "universal"}};
Shane Farmer74cdea32017-05-12 16:22:36 -070084
85constexpr const char* kAaptXmlNs = "http://schemas.android.com/tools/aapt";
86
87/** A default noop diagnostics context. */
88class NoopDiagnostics : public IDiagnostics {
89 public:
90 void Log(Level level, DiagMessageActual& actualMsg) override {}
91};
92NoopDiagnostics noop_;
93
94std::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. */
110class 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 Farmercb6c3f92017-11-27 13:19:36 -0800118/** Copies the values referenced in a configuration group to the target list. */
119template <typename T>
120bool 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 Farmer74cdea32017-05-12 16:22:36 -0700126
Shane Farmercb6c3f92017-11-27 13:19:36 -0800127 // 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 Farmerb1027272017-06-14 09:10:28 -0700132
Shane Farmercb6c3f92017-11-27 13:19:36 -0800133 for (const T& item : group->second) {
134 target->push_back(item);
135 }
136 return true;
Shane Farmer57669432017-06-19 12:52:04 -0700137}
138
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700139/**
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 Farmercb6c3f92017-11-27 13:19:36 -0800144bool ReplacePlaceholder(const StringPiece& placeholder, const Maybe<StringPiece>& value,
145 std::string* name, IDiagnostics* diag) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700146 size_t offset = name->find(placeholder.data());
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700147 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 Farmer9f0e7f12017-06-22 12:26:44 -0700152 diag->Error(DiagMessage() << "Missing placeholder for artifact: " << placeholder);
153 return false;
154 }
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700155 return true;
156 }
157
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700158 DCHECK(found) << "Missing return path for placeholder not found";
159
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700160 // Make sure the placeholder was not present if the desired value was not present.
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700161 if (!value) {
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700162 diag->Error(DiagMessage() << "Placeholder present but no value for artifact: " << placeholder);
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700163 return false;
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700164 }
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700165
Shane Farmer0a5b2012017-06-22 12:24:12 -0700166 name->replace(offset, placeholder.length(), value.value().data());
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700167
168 // Make sure there was only one instance of the placeholder.
Shane Farmer0a5b2012017-06-22 12:24:12 -0700169 if (name->find(placeholder.data()) != std::string::npos) {
Shane Farmer1a21b8c2017-07-21 09:42:42 -0700170 diag->Error(DiagMessage() << "Placeholder present multiple times: " << placeholder);
171 return false;
172 }
173 return true;
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700174}
175
Shane Farmer9ecc0752017-08-24 15:55:36 -0700176/**
Shane Farmercb6c3f92017-11-27 13:19:36 -0800177 * An ActionHandler for processing XML elements in the XmlActionExecutor. Returns true if the
178 * element was successfully processed, otherwise returns false.
179 */
180using ActionHandler = std::function<bool(configuration::PostProcessingConfiguration* config,
181 xml::Element* element, IDiagnostics* diag)>;
182
183/** Binds an ActionHandler to the current configuration being populated. */
184xml::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. */
192Maybe<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. */
247Maybe<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
324namespace configuration {
325
326const StringPiece& AbiToString(Abi abi) {
327 return kAbiToStringMap.at(static_cast<size_t>(abi));
328}
329
330/**
Shane Farmer9ecc0752017-08-24 15:55:36 -0700331 * Returns the common artifact base name from a template string.
332 */
333Maybe<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 Farmer9f0e7f12017-06-22 12:26:44 -0700338
Shane Farmer9ecc0752017-08-24 15:55:36 -0700339 // 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 Farmer9f0e7f12017-06-22 12:26:44 -0700346 }
347
Shane Farmer0a5b2012017-06-22 12:24:12 -0700348 // Extension is optional.
349 if (result.find("${ext}") != std::string::npos) {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700350 // Make sure we disregard the '.' in the extension when replacing the placeholder.
351 if (!ReplacePlaceholder("${ext}", {ext.substr(1)}, &result, diag)) {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700352 return {};
353 }
Shane Farmer9ecc0752017-08-24 15:55:36 -0700354 } 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 Farmer0a5b2012017-06-22 12:24:12 -0700360 }
361
Shane Farmer9ecc0752017-08-24 15:55:36 -0700362 return result;
363}
364
Shane Farmercb6c3f92017-11-27 13:19:36 -0800365Maybe<std::string> ConfiguredArtifact::ToArtifactName(const StringPiece& format,
366 const StringPiece& apk_name,
367 IDiagnostics* diag) const {
Shane Farmer9ecc0752017-08-24 15:55:36 -0700368 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 Farmer0a5b2012017-06-22 12:24:12 -0700374 if (!ReplacePlaceholder("${abi}", abi_group, &result, diag)) {
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700375 return {};
376 }
377
Shane Farmer0a5b2012017-06-22 12:24:12 -0700378 if (!ReplacePlaceholder("${density}", screen_density_group, &result, diag)) {
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700379 return {};
380 }
381
Shane Farmer0a5b2012017-06-22 12:24:12 -0700382 if (!ReplacePlaceholder("${locale}", locale_group, &result, diag)) {
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700383 return {};
384 }
385
Shane Farmer0a5b2012017-06-22 12:24:12 -0700386 if (!ReplacePlaceholder("${sdk}", android_sdk_group, &result, diag)) {
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700387 return {};
388 }
389
Shane Farmer0a5b2012017-06-22 12:24:12 -0700390 if (!ReplacePlaceholder("${feature}", device_feature_group, &result, diag)) {
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700391 return {};
392 }
393
Shane Farmer0a5b2012017-06-22 12:24:12 -0700394 if (!ReplacePlaceholder("${gl}", gl_texture_group, &result, diag)) {
395 return {};
396 }
397
398 return result;
399}
400
Shane Farmercb6c3f92017-11-27 13:19:36 -0800401Maybe<std::string> ConfiguredArtifact::Name(const StringPiece& apk_name, IDiagnostics* diag) const {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700402 if (!name) {
403 return {};
404 }
405
Shane Farmer9ecc0752017-08-24 15:55:36 -0700406 return ToBaseName(name.value(), apk_name, diag);
407}
Shane Farmer0a5b2012017-06-22 12:24:12 -0700408
Shane Farmer57669432017-06-19 12:52:04 -0700409} // namespace configuration
Shane Farmerb1027272017-06-14 09:10:28 -0700410
411/** Returns a ConfigurationParser for the file located at the provided path. */
412Maybe<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 Farmer74cdea32017-05-12 16:22:36 -0700420ConfigurationParser::ConfigurationParser(std::string contents)
421 : contents_(std::move(contents)),
422 diag_(&noop_) {
423}
424
Shane Farmercb6c3f92017-11-27 13:19:36 -0800425Maybe<std::vector<OutputArtifact>> ConfigurationParser::Parse(
426 const android::StringPiece& apk_path) {
427 Maybe<PostProcessingConfiguration> maybe_config = ExtractConfiguration(contents_, diag_);
428 if (!maybe_config) {
Shane Farmer74cdea32017-05-12 16:22:36 -0700429 return {};
430 }
Shane Farmercb6c3f92017-11-27 13:19:36 -0800431 const PostProcessingConfiguration& config = maybe_config.value();
Shane Farmer57669432017-06-19 12:52:04 -0700432
Shane Farmer810fd182017-09-21 14:37:44 -0700433 // 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 Farmercb6c3f92017-11-27 13:19:36 -0800437 std::vector<ConfiguredArtifact> artifacts = config.artifacts;
Shane Farmer810fd182017-09-21 14:37:44 -0700438 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 Farmercb6c3f92017-11-27 13:19:36 -0800444 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 Farmer74cdea32017-05-12 16:22:36 -0700466}
467
Shane Farmercb6c3f92017-11-27 13:19:36 -0800468namespace configuration {
469namespace handler {
470
471bool ArtifactTagHandler(PostProcessingConfiguration* config, Element* root_element,
472 IDiagnostics* diag) {
Shane Farmer810fd182017-09-21 14:37:44 -0700473 // 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 Farmercb6c3f92017-11-27 13:19:36 -0800476 ConfiguredArtifact artifact{};
Shane Farmer810fd182017-09-21 14:37:44 -0700477 Maybe<int> version;
Shane Farmer280be342017-06-21 15:20:15 -0700478 for (const auto& attr : root_element->attributes) {
479 if (attr.name == "name") {
480 artifact.name = attr.value;
Shane Farmer810fd182017-09-21 14:37:44 -0700481 } else if (attr.name == "version") {
482 version = std::stoi(attr.value);
Shane Farmer280be342017-06-21 15:20:15 -0700483 } 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 Farmer810fd182017-09-21 14:37:44 -0700500
501 artifact.version = (version) ? version.value() : current_version + 1;
502
Shane Farmer280be342017-06-21 15:20:15 -0700503 config->artifacts.push_back(artifact);
504 return true;
505};
Shane Farmer74cdea32017-05-12 16:22:36 -0700506
Shane Farmercb6c3f92017-11-27 13:19:36 -0800507bool ArtifactFormatTagHandler(PostProcessingConfiguration* config, Element* root_element,
508 IDiagnostics* /* diag */) {
Shane Farmer280be342017-06-21 15:20:15 -0700509 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 Farmercb6c3f92017-11-27 13:19:36 -0800519bool AbiGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
520 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700521 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
529 for (auto* child : root_element->GetChildElements()) {
530 if (child->name != "abi") {
531 diag->Error(DiagMessage() << "Unexpected element in ABI group: " << child->name);
532 valid = false;
533 } else {
534 for (auto& node : child->children) {
Shane Farmer74cdea32017-05-12 16:22:36 -0700535 xml::Text* t;
536 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
Shane Farmer280be342017-06-21 15:20:15 -0700537 group.push_back(kStringToAbiMap.at(TrimWhitespace(t->text).to_string()));
Shane Farmer74cdea32017-05-12 16:22:36 -0700538 break;
539 }
540 }
Shane Farmer280be342017-06-21 15:20:15 -0700541 }
542 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700543
Shane Farmer280be342017-06-21 15:20:15 -0700544 return valid;
545};
Shane Farmer74cdea32017-05-12 16:22:36 -0700546
Shane Farmercb6c3f92017-11-27 13:19:36 -0800547bool ScreenDensityGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
548 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700549 std::string label = GetLabel(root_element, diag);
550 if (label.empty()) {
551 return false;
552 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700553
Shane Farmer280be342017-06-21 15:20:15 -0700554 auto& group = config->screen_density_groups[label];
555 bool valid = true;
Shane Farmer74cdea32017-05-12 16:22:36 -0700556
Shane Farmer280be342017-06-21 15:20:15 -0700557 for (auto* child : root_element->GetChildElements()) {
558 if (child->name != "screen-density") {
559 diag->Error(DiagMessage() << "Unexpected root_element in screen density group: "
560 << child->name);
561 valid = false;
562 } else {
563 for (auto& node : child->children) {
564 xml::Text* t;
565 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
566 ConfigDescription config_descriptor;
567 const android::StringPiece& text = TrimWhitespace(t->text);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700568 bool parsed = ConfigDescription::Parse(text, &config_descriptor);
569 if (parsed &&
570 (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) ==
571 android::ResTable_config::CONFIG_DENSITY)) {
Shane Farmer280be342017-06-21 15:20:15 -0700572 // Copy the density with the minimum SDK version stripped out.
573 group.push_back(config_descriptor.CopyWithoutSdkVersion());
574 } else {
575 diag->Error(DiagMessage()
576 << "Could not parse config descriptor for screen-density: " << text);
577 valid = false;
Shane Farmer74cdea32017-05-12 16:22:36 -0700578 }
Shane Farmer280be342017-06-21 15:20:15 -0700579 break;
Shane Farmer74cdea32017-05-12 16:22:36 -0700580 }
581 }
Shane Farmer280be342017-06-21 15:20:15 -0700582 }
583 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700584
Shane Farmer280be342017-06-21 15:20:15 -0700585 return valid;
586};
Shane Farmer74cdea32017-05-12 16:22:36 -0700587
Shane Farmercb6c3f92017-11-27 13:19:36 -0800588bool LocaleGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
589 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700590 std::string label = GetLabel(root_element, diag);
591 if (label.empty()) {
592 return false;
593 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700594
Shane Farmer280be342017-06-21 15:20:15 -0700595 auto& group = config->locale_groups[label];
596 bool valid = true;
Shane Farmer74cdea32017-05-12 16:22:36 -0700597
Shane Farmer280be342017-06-21 15:20:15 -0700598 for (auto* child : root_element->GetChildElements()) {
599 if (child->name != "locale") {
600 diag->Error(DiagMessage() << "Unexpected root_element in screen density group: "
601 << child->name);
602 valid = false;
603 } else {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700604 for (auto& node : child->children) {
605 xml::Text* t;
606 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
607 ConfigDescription config_descriptor;
608 const android::StringPiece& text = TrimWhitespace(t->text);
609 bool parsed = ConfigDescription::Parse(text, &config_descriptor);
610 if (parsed &&
611 (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) ==
612 android::ResTable_config::CONFIG_LOCALE)) {
613 // Copy the locale with the minimum SDK version stripped out.
614 group.push_back(config_descriptor.CopyWithoutSdkVersion());
615 } else {
616 diag->Error(DiagMessage()
617 << "Could not parse config descriptor for screen-density: " << text);
618 valid = false;
619 }
620 break;
Shane Farmer74cdea32017-05-12 16:22:36 -0700621 }
622 }
Shane Farmer280be342017-06-21 15:20:15 -0700623 }
624 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700625
Shane Farmer280be342017-06-21 15:20:15 -0700626 return valid;
627};
Shane Farmer74cdea32017-05-12 16:22:36 -0700628
Shane Farmercb6c3f92017-11-27 13:19:36 -0800629bool AndroidSdkGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
630 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700631 std::string label = GetLabel(root_element, diag);
632 if (label.empty()) {
633 return false;
634 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700635
Shane Farmer280be342017-06-21 15:20:15 -0700636 bool valid = true;
Shane Farmerefe45392017-08-21 14:39:28 -0700637 bool found = false;
Shane Farmer74cdea32017-05-12 16:22:36 -0700638
Shane Farmer280be342017-06-21 15:20:15 -0700639 for (auto* child : root_element->GetChildElements()) {
640 if (child->name != "android-sdk") {
641 diag->Error(DiagMessage() << "Unexpected root_element in ABI group: " << child->name);
642 valid = false;
643 } else {
644 AndroidSdk entry;
645 for (const auto& attr : child->attributes) {
Shane Farmer67e8a302017-12-06 14:39:10 -0800646 Maybe<int>* target = nullptr;
Shane Farmer280be342017-06-21 15:20:15 -0700647 if (attr.name == "minSdkVersion") {
Shane Farmer67e8a302017-12-06 14:39:10 -0800648 target = &entry.min_sdk_version;
Shane Farmer280be342017-06-21 15:20:15 -0700649 } else if (attr.name == "targetSdkVersion") {
Shane Farmer67e8a302017-12-06 14:39:10 -0800650 target = &entry.target_sdk_version;
Shane Farmer280be342017-06-21 15:20:15 -0700651 } else if (attr.name == "maxSdkVersion") {
Shane Farmer67e8a302017-12-06 14:39:10 -0800652 target = &entry.max_sdk_version;
Shane Farmer74cdea32017-05-12 16:22:36 -0700653 } else {
Shane Farmer280be342017-06-21 15:20:15 -0700654 diag->Warn(DiagMessage() << "Unknown attribute: " << attr.name << " = " << attr.value);
Shane Farmer67e8a302017-12-06 14:39:10 -0800655 continue;
656 }
657
658 *target = ResourceUtils::ParseSdkVersion(attr.value);
659 if (!*target) {
660 diag->Error(DiagMessage() << "Invalid attribute: " << attr.name << " = " << attr.value);
661 valid = false;
Shane Farmer74cdea32017-05-12 16:22:36 -0700662 }
663 }
664
Shane Farmer280be342017-06-21 15:20:15 -0700665 // TODO: Fill in the manifest details when they are finalised.
666 for (auto node : child->GetChildElements()) {
667 if (node->name == "manifest") {
668 if (entry.manifest) {
669 diag->Warn(DiagMessage() << "Found multiple manifest tags. Ignoring duplicates.");
670 continue;
671 }
672 entry.manifest = {AndroidManifest()};
673 }
674 }
675
Shane Farmerefe45392017-08-21 14:39:28 -0700676 config->android_sdk_groups[label] = entry;
677 if (found) {
678 valid = false;
679 }
680 found = true;
Shane Farmer280be342017-06-21 15:20:15 -0700681 }
682 }
683
684 return valid;
685};
Shane Farmer74cdea32017-05-12 16:22:36 -0700686
Shane Farmercb6c3f92017-11-27 13:19:36 -0800687bool GlTextureGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
688 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700689 std::string label = GetLabel(root_element, diag);
690 if (label.empty()) {
691 return false;
692 }
693
694 auto& group = config->gl_texture_groups[label];
695 bool valid = true;
696
697 GlTexture result;
698 for (auto* child : root_element->GetChildElements()) {
699 if (child->name != "gl-texture") {
700 diag->Error(DiagMessage() << "Unexpected element in GL texture group: " << child->name);
701 valid = false;
702 } else {
703 for (const auto& attr : child->attributes) {
704 if (attr.name == "name") {
705 result.name = attr.value;
706 break;
707 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700708 }
709
Shane Farmer280be342017-06-21 15:20:15 -0700710 for (auto* element : child->GetChildElements()) {
711 if (element->name != "texture-path") {
712 diag->Error(DiagMessage() << "Unexpected element in gl-texture element: " << child->name);
Shane Farmer74cdea32017-05-12 16:22:36 -0700713 valid = false;
Shane Farmer280be342017-06-21 15:20:15 -0700714 continue;
715 }
716 for (auto& node : element->children) {
717 xml::Text* t;
718 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
719 result.texture_paths.push_back(TrimWhitespace(t->text).to_string());
Shane Farmer74cdea32017-05-12 16:22:36 -0700720 }
721 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700722 }
Shane Farmer280be342017-06-21 15:20:15 -0700723 }
724 group.push_back(result);
725 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700726
Shane Farmer280be342017-06-21 15:20:15 -0700727 return valid;
728};
Shane Farmer74cdea32017-05-12 16:22:36 -0700729
Shane Farmercb6c3f92017-11-27 13:19:36 -0800730bool DeviceFeatureGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
731 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700732 std::string label = GetLabel(root_element, diag);
733 if (label.empty()) {
734 return false;
735 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700736
Shane Farmer280be342017-06-21 15:20:15 -0700737 auto& group = config->device_feature_groups[label];
738 bool valid = true;
Shane Farmer74cdea32017-05-12 16:22:36 -0700739
Shane Farmer280be342017-06-21 15:20:15 -0700740 for (auto* child : root_element->GetChildElements()) {
741 if (child->name != "supports-feature") {
742 diag->Error(DiagMessage() << "Unexpected root_element in device feature group: "
743 << child->name);
744 valid = false;
745 } else {
746 for (auto& node : child->children) {
747 xml::Text* t;
748 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
749 group.push_back(TrimWhitespace(t->text).to_string());
750 break;
Shane Farmer74cdea32017-05-12 16:22:36 -0700751 }
752 }
Shane Farmer280be342017-06-21 15:20:15 -0700753 }
754 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700755
Shane Farmer280be342017-06-21 15:20:15 -0700756 return valid;
757};
Shane Farmer74cdea32017-05-12 16:22:36 -0700758
Shane Farmercb6c3f92017-11-27 13:19:36 -0800759} // namespace handler
760} // namespace configuration
761
Shane Farmer74cdea32017-05-12 16:22:36 -0700762} // namespace aapt