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