blob: 655268bc73f9fe1a4d8c4fa177c8639e3ce8ae70 [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
Shane Farmer39e474f2017-12-18 14:44:11 -0800529 // Special case for empty abi-group tag. Label will be used as the ABI.
530 if (root_element->GetChildElements().empty()) {
531 auto abi = kStringToAbiMap.find(label);
532 if (abi == kStringToAbiMap.end()) {
533 return false;
534 }
535 group.push_back(abi->second);
536 return true;
537 }
538
Shane Farmer280be342017-06-21 15:20:15 -0700539 for (auto* child : root_element->GetChildElements()) {
540 if (child->name != "abi") {
541 diag->Error(DiagMessage() << "Unexpected element in ABI group: " << child->name);
542 valid = false;
543 } else {
544 for (auto& node : child->children) {
Shane Farmer74cdea32017-05-12 16:22:36 -0700545 xml::Text* t;
546 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
Shane Farmer39e474f2017-12-18 14:44:11 -0800547 auto abi = kStringToAbiMap.find(TrimWhitespace(t->text).to_string());
548 if (abi != kStringToAbiMap.end()) {
549 group.push_back(abi->second);
550 } else {
551 diag->Error(DiagMessage() << "Could not parse ABI value: " << t->text);
552 valid = false;
553 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700554 break;
555 }
556 }
Shane Farmer280be342017-06-21 15:20:15 -0700557 }
558 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700559
Shane Farmer280be342017-06-21 15:20:15 -0700560 return valid;
561};
Shane Farmer74cdea32017-05-12 16:22:36 -0700562
Shane Farmercb6c3f92017-11-27 13:19:36 -0800563bool ScreenDensityGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
564 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700565 std::string label = GetLabel(root_element, diag);
566 if (label.empty()) {
567 return false;
568 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700569
Shane Farmer280be342017-06-21 15:20:15 -0700570 auto& group = config->screen_density_groups[label];
571 bool valid = true;
Shane Farmer74cdea32017-05-12 16:22:36 -0700572
Shane Farmer39e474f2017-12-18 14:44:11 -0800573 // Special case for empty screen-density-group tag. Label will be used as the screen density.
574 if (root_element->GetChildElements().empty()) {
575 ConfigDescription config_descriptor;
576 bool parsed = ConfigDescription::Parse(label, &config_descriptor);
577 if (parsed &&
578 (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) ==
579 android::ResTable_config::CONFIG_DENSITY)) {
580 // Copy the density with the minimum SDK version stripped out.
581 group.push_back(config_descriptor.CopyWithoutSdkVersion());
582 } else {
583 diag->Error(DiagMessage()
584 << "Could not parse config descriptor for empty screen-density-group: "
585 << label);
586 valid = false;
587 }
588
589 return valid;
590 }
591
Shane Farmer280be342017-06-21 15:20:15 -0700592 for (auto* child : root_element->GetChildElements()) {
593 if (child->name != "screen-density") {
594 diag->Error(DiagMessage() << "Unexpected root_element in screen density group: "
595 << child->name);
596 valid = false;
597 } else {
598 for (auto& node : child->children) {
599 xml::Text* t;
600 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
601 ConfigDescription config_descriptor;
602 const android::StringPiece& text = TrimWhitespace(t->text);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700603 bool parsed = ConfigDescription::Parse(text, &config_descriptor);
604 if (parsed &&
605 (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) ==
606 android::ResTable_config::CONFIG_DENSITY)) {
Shane Farmer280be342017-06-21 15:20:15 -0700607 // Copy the density with the minimum SDK version stripped out.
608 group.push_back(config_descriptor.CopyWithoutSdkVersion());
609 } else {
610 diag->Error(DiagMessage()
611 << "Could not parse config descriptor for screen-density: " << text);
612 valid = false;
Shane Farmer74cdea32017-05-12 16:22:36 -0700613 }
Shane Farmer280be342017-06-21 15:20:15 -0700614 break;
Shane Farmer74cdea32017-05-12 16:22:36 -0700615 }
616 }
Shane Farmer280be342017-06-21 15:20:15 -0700617 }
618 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700619
Shane Farmer280be342017-06-21 15:20:15 -0700620 return valid;
621};
Shane Farmer74cdea32017-05-12 16:22:36 -0700622
Shane Farmercb6c3f92017-11-27 13:19:36 -0800623bool LocaleGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
624 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700625 std::string label = GetLabel(root_element, diag);
626 if (label.empty()) {
627 return false;
628 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700629
Shane Farmer280be342017-06-21 15:20:15 -0700630 auto& group = config->locale_groups[label];
631 bool valid = true;
Shane Farmer74cdea32017-05-12 16:22:36 -0700632
Shane Farmer39e474f2017-12-18 14:44:11 -0800633 // Special case to auto insert a locale for an empty group. Label will be used for locale.
634 if (root_element->GetChildElements().empty()) {
635 ConfigDescription config_descriptor;
636 bool parsed = ConfigDescription::Parse(label, &config_descriptor);
637 if (parsed &&
638 (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) ==
639 android::ResTable_config::CONFIG_LOCALE)) {
640 // Copy the locale with the minimum SDK version stripped out.
641 group.push_back(config_descriptor.CopyWithoutSdkVersion());
642 } else {
643 diag->Error(DiagMessage()
644 << "Could not parse config descriptor for empty screen-density-group: "
645 << label);
646 valid = false;
647 }
648
649 return valid;
650 }
651
Shane Farmer280be342017-06-21 15:20:15 -0700652 for (auto* child : root_element->GetChildElements()) {
653 if (child->name != "locale") {
654 diag->Error(DiagMessage() << "Unexpected root_element in screen density group: "
655 << child->name);
656 valid = false;
657 } else {
Shane Farmer0a5b2012017-06-22 12:24:12 -0700658 for (auto& node : child->children) {
659 xml::Text* t;
660 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
661 ConfigDescription config_descriptor;
662 const android::StringPiece& text = TrimWhitespace(t->text);
663 bool parsed = ConfigDescription::Parse(text, &config_descriptor);
664 if (parsed &&
665 (config_descriptor.CopyWithoutSdkVersion().diff(ConfigDescription::DefaultConfig()) ==
666 android::ResTable_config::CONFIG_LOCALE)) {
667 // Copy the locale with the minimum SDK version stripped out.
668 group.push_back(config_descriptor.CopyWithoutSdkVersion());
669 } else {
670 diag->Error(DiagMessage()
671 << "Could not parse config descriptor for screen-density: " << text);
672 valid = false;
673 }
674 break;
Shane Farmer74cdea32017-05-12 16:22:36 -0700675 }
676 }
Shane Farmer280be342017-06-21 15:20:15 -0700677 }
678 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700679
Shane Farmer280be342017-06-21 15:20:15 -0700680 return valid;
681};
Shane Farmer74cdea32017-05-12 16:22:36 -0700682
Shane Farmercb6c3f92017-11-27 13:19:36 -0800683bool AndroidSdkGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
684 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700685 std::string label = GetLabel(root_element, diag);
686 if (label.empty()) {
687 return false;
688 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700689
Shane Farmer280be342017-06-21 15:20:15 -0700690 bool valid = true;
Shane Farmerefe45392017-08-21 14:39:28 -0700691 bool found = false;
Shane Farmer74cdea32017-05-12 16:22:36 -0700692
Shane Farmer280be342017-06-21 15:20:15 -0700693 for (auto* child : root_element->GetChildElements()) {
694 if (child->name != "android-sdk") {
695 diag->Error(DiagMessage() << "Unexpected root_element in ABI group: " << child->name);
696 valid = false;
697 } else {
698 AndroidSdk entry;
699 for (const auto& attr : child->attributes) {
Shane Farmer67e8a302017-12-06 14:39:10 -0800700 Maybe<int>* target = nullptr;
Shane Farmer280be342017-06-21 15:20:15 -0700701 if (attr.name == "minSdkVersion") {
Shane Farmer67e8a302017-12-06 14:39:10 -0800702 target = &entry.min_sdk_version;
Shane Farmer280be342017-06-21 15:20:15 -0700703 } else if (attr.name == "targetSdkVersion") {
Shane Farmer67e8a302017-12-06 14:39:10 -0800704 target = &entry.target_sdk_version;
Shane Farmer280be342017-06-21 15:20:15 -0700705 } else if (attr.name == "maxSdkVersion") {
Shane Farmer67e8a302017-12-06 14:39:10 -0800706 target = &entry.max_sdk_version;
Shane Farmer74cdea32017-05-12 16:22:36 -0700707 } else {
Shane Farmer280be342017-06-21 15:20:15 -0700708 diag->Warn(DiagMessage() << "Unknown attribute: " << attr.name << " = " << attr.value);
Shane Farmer67e8a302017-12-06 14:39:10 -0800709 continue;
710 }
711
712 *target = ResourceUtils::ParseSdkVersion(attr.value);
713 if (!*target) {
714 diag->Error(DiagMessage() << "Invalid attribute: " << attr.name << " = " << attr.value);
715 valid = false;
Shane Farmer74cdea32017-05-12 16:22:36 -0700716 }
717 }
718
Shane Farmer280be342017-06-21 15:20:15 -0700719 // TODO: Fill in the manifest details when they are finalised.
720 for (auto node : child->GetChildElements()) {
721 if (node->name == "manifest") {
722 if (entry.manifest) {
723 diag->Warn(DiagMessage() << "Found multiple manifest tags. Ignoring duplicates.");
724 continue;
725 }
726 entry.manifest = {AndroidManifest()};
727 }
728 }
729
Shane Farmerefe45392017-08-21 14:39:28 -0700730 config->android_sdk_groups[label] = entry;
731 if (found) {
732 valid = false;
733 }
734 found = true;
Shane Farmer280be342017-06-21 15:20:15 -0700735 }
736 }
737
738 return valid;
739};
Shane Farmer74cdea32017-05-12 16:22:36 -0700740
Shane Farmercb6c3f92017-11-27 13:19:36 -0800741bool GlTextureGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
742 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700743 std::string label = GetLabel(root_element, diag);
744 if (label.empty()) {
745 return false;
746 }
747
748 auto& group = config->gl_texture_groups[label];
749 bool valid = true;
750
751 GlTexture result;
752 for (auto* child : root_element->GetChildElements()) {
753 if (child->name != "gl-texture") {
754 diag->Error(DiagMessage() << "Unexpected element in GL texture group: " << child->name);
755 valid = false;
756 } else {
757 for (const auto& attr : child->attributes) {
758 if (attr.name == "name") {
759 result.name = attr.value;
760 break;
761 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700762 }
763
Shane Farmer280be342017-06-21 15:20:15 -0700764 for (auto* element : child->GetChildElements()) {
765 if (element->name != "texture-path") {
766 diag->Error(DiagMessage() << "Unexpected element in gl-texture element: " << child->name);
Shane Farmer74cdea32017-05-12 16:22:36 -0700767 valid = false;
Shane Farmer280be342017-06-21 15:20:15 -0700768 continue;
769 }
770 for (auto& node : element->children) {
771 xml::Text* t;
772 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
773 result.texture_paths.push_back(TrimWhitespace(t->text).to_string());
Shane Farmer74cdea32017-05-12 16:22:36 -0700774 }
775 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700776 }
Shane Farmer280be342017-06-21 15:20:15 -0700777 }
778 group.push_back(result);
779 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700780
Shane Farmer280be342017-06-21 15:20:15 -0700781 return valid;
782};
Shane Farmer74cdea32017-05-12 16:22:36 -0700783
Shane Farmercb6c3f92017-11-27 13:19:36 -0800784bool DeviceFeatureGroupTagHandler(PostProcessingConfiguration* config, Element* root_element,
785 IDiagnostics* diag) {
Shane Farmer280be342017-06-21 15:20:15 -0700786 std::string label = GetLabel(root_element, diag);
787 if (label.empty()) {
788 return false;
789 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700790
Shane Farmer280be342017-06-21 15:20:15 -0700791 auto& group = config->device_feature_groups[label];
792 bool valid = true;
Shane Farmer74cdea32017-05-12 16:22:36 -0700793
Shane Farmer280be342017-06-21 15:20:15 -0700794 for (auto* child : root_element->GetChildElements()) {
795 if (child->name != "supports-feature") {
796 diag->Error(DiagMessage() << "Unexpected root_element in device feature group: "
797 << child->name);
798 valid = false;
799 } else {
800 for (auto& node : child->children) {
801 xml::Text* t;
802 if ((t = NodeCast<xml::Text>(node.get())) != nullptr) {
803 group.push_back(TrimWhitespace(t->text).to_string());
804 break;
Shane Farmer74cdea32017-05-12 16:22:36 -0700805 }
806 }
Shane Farmer280be342017-06-21 15:20:15 -0700807 }
808 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700809
Shane Farmer280be342017-06-21 15:20:15 -0700810 return valid;
811};
Shane Farmer74cdea32017-05-12 16:22:36 -0700812
Shane Farmercb6c3f92017-11-27 13:19:36 -0800813} // namespace handler
814} // namespace configuration
815
Shane Farmer74cdea32017-05-12 16:22:36 -0700816} // namespace aapt