blob: a58685e3b8a66a6cabd0ac244f2c84f3aa599427 [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#ifndef AAPT2_CONFIGURATION_H
18#define AAPT2_CONFIGURATION_H
19
20#include <string>
21#include <unordered_map>
22#include <vector>
Shane Farmer74cdea32017-05-12 16:22:36 -070023
Shane Farmer9f0e7f12017-06-22 12:26:44 -070024#include "ConfigDescription.h"
25#include "Diagnostics.h"
Shane Farmer74cdea32017-05-12 16:22:36 -070026#include "util/Maybe.h"
27
28namespace aapt {
29
30namespace configuration {
31
32/** A mapping of group labels to group of configuration items. */
33template<class T>
34using Group = std::unordered_map<std::string, std::vector<T>>;
35
36/** Output artifact configuration options. */
37struct Artifact {
38 /** Name to use for output of processing foo.apk -> foo.<name>.apk. */
Shane Farmer0a5b2012017-06-22 12:24:12 -070039 Maybe<std::string> name;
Shane Farmer74cdea32017-05-12 16:22:36 -070040 /** If present, uses the ABI group with this name. */
41 Maybe<std::string> abi_group;
42 /** If present, uses the screen density group with this name. */
43 Maybe<std::string> screen_density_group;
44 /** If present, uses the locale group with this name. */
45 Maybe<std::string> locale_group;
46 /** If present, uses the Android SDK group with this name. */
47 Maybe<std::string> android_sdk_group;
48 /** If present, uses the device feature group with this name. */
49 Maybe<std::string> device_feature_group;
50 /** If present, uses the OpenGL texture group with this name. */
51 Maybe<std::string> gl_texture_group;
Shane Farmer9f0e7f12017-06-22 12:26:44 -070052
53 /** Convert an artifact name template into a name string based on configuration contents. */
Shane Farmer9ecc0752017-08-24 15:55:36 -070054 Maybe<std::string> ToArtifactName(const android::StringPiece& format,
55 const android::StringPiece& apk_name, IDiagnostics* diag) const;
Shane Farmer0a5b2012017-06-22 12:24:12 -070056
57 /** Convert an artifact name template into a name string based on configuration contents. */
Shane Farmer9ecc0752017-08-24 15:55:36 -070058 Maybe<std::string> Name(const android::StringPiece& apk_name, IDiagnostics* diag) const;
Shane Farmer74cdea32017-05-12 16:22:36 -070059};
60
61/** Enumeration of currently supported ABIs. */
62enum class Abi {
63 kArmeV6,
64 kArmV7a,
65 kArm64V8a,
66 kX86,
67 kX86_64,
68 kMips,
69 kMips64,
70 kUniversal
71};
72
Shane Farmer57669432017-06-19 12:52:04 -070073/** Helper method to convert an ABI to a string representing the path within the APK. */
74const std::string& AbiToString(Abi abi);
75
Shane Farmer74cdea32017-05-12 16:22:36 -070076/**
77 * Represents an individual locale. When a locale is included, it must be
78 * declared from least specific to most specific, as a region does not make
79 * sense without a language. If neither the language or region are specified it
80 * acts as a special case for catch all. This can allow all locales to be kept,
81 * or compressed.
82 */
83struct Locale {
84 /** The ISO<?> standard locale language code. */
85 Maybe<std::string> lang;
86 /** The ISO<?> standard locale region code. */
87 Maybe<std::string> region;
88
89 inline friend bool operator==(const Locale& lhs, const Locale& rhs) {
90 return lhs.lang == rhs.lang && lhs.region == rhs.region;
91 }
92};
93
94// TODO: Encapsulate manifest modifications from the configuration file.
95struct AndroidManifest {
96 inline friend bool operator==(const AndroidManifest& lhs, const AndroidManifest& rhs) {
97 return true; // nothing to compare yet.
98 }
99};
100
101struct AndroidSdk {
102 Maybe<std::string> min_sdk_version;
103 Maybe<std::string> target_sdk_version;
104 Maybe<std::string> max_sdk_version;
105 Maybe<AndroidManifest> manifest;
106
107 inline friend bool operator==(const AndroidSdk& lhs, const AndroidSdk& rhs) {
108 return lhs.min_sdk_version == rhs.min_sdk_version &&
109 lhs.target_sdk_version == rhs.target_sdk_version &&
110 lhs.max_sdk_version == rhs.max_sdk_version &&
111 lhs.manifest == rhs.manifest;
112 }
113};
114
115// TODO: Make device features more than just an arbitrary string?
116using DeviceFeature = std::string;
117
118/** Represents a mapping of texture paths to a GL texture format. */
119struct GlTexture {
120 std::string name;
121 std::vector<std::string> texture_paths;
122
123 inline friend bool operator==(const GlTexture& lhs, const GlTexture& rhs) {
124 return lhs.name == rhs.name && lhs.texture_paths == rhs.texture_paths;
125 }
126};
127
Shane Farmer280be342017-06-21 15:20:15 -0700128/** AAPT2 XML configuration file binary representation. */
129struct PostProcessingConfiguration {
Shane Farmer57669432017-06-19 12:52:04 -0700130 // TODO: Support named artifacts?
131 std::vector<Artifact> artifacts;
Shane Farmer74cdea32017-05-12 16:22:36 -0700132 Maybe<std::string> artifact_format;
133
134 Group<Abi> abi_groups;
135 Group<ConfigDescription> screen_density_groups;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700136 Group<ConfigDescription> locale_groups;
Shane Farmer74cdea32017-05-12 16:22:36 -0700137 Group<AndroidSdk> android_sdk_groups;
138 Group<DeviceFeature> device_feature_groups;
139 Group<GlTexture> gl_texture_groups;
Shane Farmer9ecc0752017-08-24 15:55:36 -0700140
141 /** Helper method that generates a list of artifact names and returns true on success. */
142 bool AllArtifactNames(const android::StringPiece& apk_name,
143 std::vector<std::string>* artifact_names, IDiagnostics* diag) const;
Shane Farmer74cdea32017-05-12 16:22:36 -0700144};
145
146} // namespace configuration
147
148// Forward declaration of classes used in the API.
149struct IDiagnostics;
150namespace xml {
151class Element;
152}
153
154/**
155 * XML configuration file parser for the split and optimize commands.
156 */
157class ConfigurationParser {
158 public:
Shane Farmerb1027272017-06-14 09:10:28 -0700159
160 /** Returns a ConfigurationParser for the file located at the provided path. */
161 static Maybe<ConfigurationParser> ForPath(const std::string& path);
162
Shane Farmer74cdea32017-05-12 16:22:36 -0700163 /** Returns a ConfigurationParser for the configuration in the provided file contents. */
164 static ConfigurationParser ForContents(const std::string& contents) {
165 ConfigurationParser parser{contents};
166 return parser;
167 }
168
Shane Farmer74cdea32017-05-12 16:22:36 -0700169 /** Sets the diagnostics context to use when parsing. */
170 ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) {
171 diag_ = diagnostics;
172 return *this;
173 }
174
175 /**
176 * Parses the configuration file and returns the results. If the configuration could not be parsed
177 * the result is empty and any errors will be displayed with the provided diagnostics context.
178 */
Shane Farmer280be342017-06-21 15:20:15 -0700179 Maybe<configuration::PostProcessingConfiguration> Parse();
Shane Farmer74cdea32017-05-12 16:22:36 -0700180
181 protected:
182 /**
183 * Instantiates a new ConfigurationParser with the provided configuration file and a no-op
184 * diagnostics context. The default diagnostics context can be overridden with a call to
185 * WithDiagnostics(IDiagnostics *).
186 */
187 explicit ConfigurationParser(std::string contents);
188
189 /** Returns the current diagnostics context to any subclasses. */
190 IDiagnostics* diagnostics() {
191 return diag_;
192 }
193
194 /**
195 * An ActionHandler for processing XML elements in the XmlActionExecutor. Returns true if the
196 * element was successfully processed, otherwise returns false.
197 */
Shane Farmer280be342017-06-21 15:20:15 -0700198 using ActionHandler = std::function<bool(configuration::PostProcessingConfiguration* config,
199 xml::Element* element, IDiagnostics* diag)>;
Shane Farmer74cdea32017-05-12 16:22:36 -0700200
201 /** Handler for <artifact> tags. */
202 static ActionHandler artifact_handler_;
203 /** Handler for <artifact-format> tags. */
204 static ActionHandler artifact_format_handler_;
205 /** Handler for <abi-group> tags. */
206 static ActionHandler abi_group_handler_;
207 /** Handler for <screen-density-group> tags. */
208 static ActionHandler screen_density_group_handler_;
209 /** Handler for <locale-group> tags. */
210 static ActionHandler locale_group_handler_;
211 /** Handler for <android-sdk-group> tags. */
212 static ActionHandler android_sdk_group_handler_;
213 /** Handler for <gl-texture-group> tags. */
214 static ActionHandler gl_texture_group_handler_;
215 /** Handler for <device-feature-group> tags. */
216 static ActionHandler device_feature_group_handler_;
217
218 private:
219 /** The contents of the configuration file to parse. */
220 const std::string contents_;
221 /** The diagnostics context to send messages to. */
222 IDiagnostics* diag_;
223};
224
225} // namespace aapt
226
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700227#endif // AAPT2_CONFIGURATION_H