blob: ebf1c986a980d3a44a1d8ab10e504925f56b59e3 [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
Shane Farmerefe45392017-08-21 14:39:28 -070036/** A mapping of group label to a single configuration item. */
37template <class T>
38using Entry = std::unordered_map<std::string, T>;
39
Shane Farmer74cdea32017-05-12 16:22:36 -070040/** Output artifact configuration options. */
41struct Artifact {
42 /** Name to use for output of processing foo.apk -> foo.<name>.apk. */
Shane Farmer0a5b2012017-06-22 12:24:12 -070043 Maybe<std::string> name;
Shane Farmer74cdea32017-05-12 16:22:36 -070044 /** If present, uses the ABI group with this name. */
45 Maybe<std::string> abi_group;
46 /** If present, uses the screen density group with this name. */
47 Maybe<std::string> screen_density_group;
48 /** If present, uses the locale group with this name. */
49 Maybe<std::string> locale_group;
50 /** If present, uses the Android SDK group with this name. */
51 Maybe<std::string> android_sdk_group;
52 /** If present, uses the device feature group with this name. */
53 Maybe<std::string> device_feature_group;
54 /** If present, uses the OpenGL texture group with this name. */
55 Maybe<std::string> gl_texture_group;
Shane Farmer9f0e7f12017-06-22 12:26:44 -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> ToArtifactName(const android::StringPiece& format,
59 const android::StringPiece& apk_name, IDiagnostics* diag) const;
Shane Farmer0a5b2012017-06-22 12:24:12 -070060
61 /** Convert an artifact name template into a name string based on configuration contents. */
Shane Farmer9ecc0752017-08-24 15:55:36 -070062 Maybe<std::string> Name(const android::StringPiece& apk_name, IDiagnostics* diag) const;
Shane Farmer74cdea32017-05-12 16:22:36 -070063};
64
65/** Enumeration of currently supported ABIs. */
66enum class Abi {
67 kArmeV6,
68 kArmV7a,
69 kArm64V8a,
70 kX86,
71 kX86_64,
72 kMips,
73 kMips64,
74 kUniversal
75};
76
Shane Farmer57669432017-06-19 12:52:04 -070077/** Helper method to convert an ABI to a string representing the path within the APK. */
78const std::string& AbiToString(Abi abi);
79
Shane Farmer74cdea32017-05-12 16:22:36 -070080/**
81 * Represents an individual locale. When a locale is included, it must be
82 * declared from least specific to most specific, as a region does not make
83 * sense without a language. If neither the language or region are specified it
84 * acts as a special case for catch all. This can allow all locales to be kept,
85 * or compressed.
86 */
87struct Locale {
88 /** The ISO<?> standard locale language code. */
89 Maybe<std::string> lang;
90 /** The ISO<?> standard locale region code. */
91 Maybe<std::string> region;
92
93 inline friend bool operator==(const Locale& lhs, const Locale& rhs) {
94 return lhs.lang == rhs.lang && lhs.region == rhs.region;
95 }
96};
97
98// TODO: Encapsulate manifest modifications from the configuration file.
99struct AndroidManifest {
100 inline friend bool operator==(const AndroidManifest& lhs, const AndroidManifest& rhs) {
101 return true; // nothing to compare yet.
102 }
103};
104
105struct AndroidSdk {
Shane Farmer3edd4722017-09-01 14:34:22 -0700106 Maybe<int> min_sdk_version;
107 Maybe<int> target_sdk_version;
108 Maybe<int> max_sdk_version;
Shane Farmer74cdea32017-05-12 16:22:36 -0700109 Maybe<AndroidManifest> manifest;
110
Shane Farmer3edd4722017-09-01 14:34:22 -0700111 static AndroidSdk ForMinSdk(int min_sdk) {
Shane Farmerefe45392017-08-21 14:39:28 -0700112 AndroidSdk sdk;
Shane Farmer3edd4722017-09-01 14:34:22 -0700113 sdk.min_sdk_version = min_sdk;
Shane Farmerefe45392017-08-21 14:39:28 -0700114 return sdk;
115 }
116
Shane Farmer74cdea32017-05-12 16:22:36 -0700117 inline friend bool operator==(const AndroidSdk& lhs, const AndroidSdk& rhs) {
118 return lhs.min_sdk_version == rhs.min_sdk_version &&
119 lhs.target_sdk_version == rhs.target_sdk_version &&
120 lhs.max_sdk_version == rhs.max_sdk_version &&
121 lhs.manifest == rhs.manifest;
122 }
123};
124
125// TODO: Make device features more than just an arbitrary string?
126using DeviceFeature = std::string;
127
128/** Represents a mapping of texture paths to a GL texture format. */
129struct GlTexture {
130 std::string name;
131 std::vector<std::string> texture_paths;
132
133 inline friend bool operator==(const GlTexture& lhs, const GlTexture& rhs) {
134 return lhs.name == rhs.name && lhs.texture_paths == rhs.texture_paths;
135 }
136};
137
Shane Farmer280be342017-06-21 15:20:15 -0700138/** AAPT2 XML configuration file binary representation. */
139struct PostProcessingConfiguration {
Shane Farmer57669432017-06-19 12:52:04 -0700140 // TODO: Support named artifacts?
141 std::vector<Artifact> artifacts;
Shane Farmer74cdea32017-05-12 16:22:36 -0700142 Maybe<std::string> artifact_format;
143
144 Group<Abi> abi_groups;
145 Group<ConfigDescription> screen_density_groups;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700146 Group<ConfigDescription> locale_groups;
Shane Farmerefe45392017-08-21 14:39:28 -0700147 Entry<AndroidSdk> android_sdk_groups;
Shane Farmer74cdea32017-05-12 16:22:36 -0700148 Group<DeviceFeature> device_feature_groups;
149 Group<GlTexture> gl_texture_groups;
Shane Farmer9ecc0752017-08-24 15:55:36 -0700150
151 /** Helper method that generates a list of artifact names and returns true on success. */
152 bool AllArtifactNames(const android::StringPiece& apk_name,
153 std::vector<std::string>* artifact_names, IDiagnostics* diag) const;
Shane Farmer74cdea32017-05-12 16:22:36 -0700154};
155
156} // namespace configuration
157
158// Forward declaration of classes used in the API.
159struct IDiagnostics;
160namespace xml {
161class Element;
162}
163
164/**
165 * XML configuration file parser for the split and optimize commands.
166 */
167class ConfigurationParser {
168 public:
Shane Farmerb1027272017-06-14 09:10:28 -0700169
170 /** Returns a ConfigurationParser for the file located at the provided path. */
171 static Maybe<ConfigurationParser> ForPath(const std::string& path);
172
Shane Farmer74cdea32017-05-12 16:22:36 -0700173 /** Returns a ConfigurationParser for the configuration in the provided file contents. */
174 static ConfigurationParser ForContents(const std::string& contents) {
175 ConfigurationParser parser{contents};
176 return parser;
177 }
178
Shane Farmer74cdea32017-05-12 16:22:36 -0700179 /** Sets the diagnostics context to use when parsing. */
180 ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) {
181 diag_ = diagnostics;
182 return *this;
183 }
184
185 /**
186 * Parses the configuration file and returns the results. If the configuration could not be parsed
187 * the result is empty and any errors will be displayed with the provided diagnostics context.
188 */
Shane Farmer280be342017-06-21 15:20:15 -0700189 Maybe<configuration::PostProcessingConfiguration> Parse();
Shane Farmer74cdea32017-05-12 16:22:36 -0700190
191 protected:
192 /**
193 * Instantiates a new ConfigurationParser with the provided configuration file and a no-op
194 * diagnostics context. The default diagnostics context can be overridden with a call to
195 * WithDiagnostics(IDiagnostics *).
196 */
197 explicit ConfigurationParser(std::string contents);
198
199 /** Returns the current diagnostics context to any subclasses. */
200 IDiagnostics* diagnostics() {
201 return diag_;
202 }
203
204 /**
205 * An ActionHandler for processing XML elements in the XmlActionExecutor. Returns true if the
206 * element was successfully processed, otherwise returns false.
207 */
Shane Farmer280be342017-06-21 15:20:15 -0700208 using ActionHandler = std::function<bool(configuration::PostProcessingConfiguration* config,
209 xml::Element* element, IDiagnostics* diag)>;
Shane Farmer74cdea32017-05-12 16:22:36 -0700210
211 /** Handler for <artifact> tags. */
212 static ActionHandler artifact_handler_;
213 /** Handler for <artifact-format> tags. */
214 static ActionHandler artifact_format_handler_;
215 /** Handler for <abi-group> tags. */
216 static ActionHandler abi_group_handler_;
217 /** Handler for <screen-density-group> tags. */
218 static ActionHandler screen_density_group_handler_;
219 /** Handler for <locale-group> tags. */
220 static ActionHandler locale_group_handler_;
221 /** Handler for <android-sdk-group> tags. */
222 static ActionHandler android_sdk_group_handler_;
223 /** Handler for <gl-texture-group> tags. */
224 static ActionHandler gl_texture_group_handler_;
225 /** Handler for <device-feature-group> tags. */
226 static ActionHandler device_feature_group_handler_;
227
228 private:
229 /** The contents of the configuration file to parse. */
230 const std::string contents_;
231 /** The diagnostics context to send messages to. */
232 IDiagnostics* diag_;
233};
234
235} // namespace aapt
236
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700237#endif // AAPT2_CONFIGURATION_H