blob: 6259ce8e28ea1f941361d62d5f6022e1db756e1a [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 Farmer0a5b2012017-06-22 12:24:12 -070054 Maybe<std::string> ToArtifactName(const android::StringPiece& format, IDiagnostics* diag,
55 const android::StringPiece& base_name = "",
56 const android::StringPiece& ext = "apk") const;
57
58 /** Convert an artifact name template into a name string based on configuration contents. */
59 Maybe<std::string> Name(const android::StringPiece& base_name, const android::StringPiece& ext,
60 IDiagnostics* diag) const;
Shane Farmer74cdea32017-05-12 16:22:36 -070061};
62
63/** Enumeration of currently supported ABIs. */
64enum class Abi {
65 kArmeV6,
66 kArmV7a,
67 kArm64V8a,
68 kX86,
69 kX86_64,
70 kMips,
71 kMips64,
72 kUniversal
73};
74
Shane Farmer57669432017-06-19 12:52:04 -070075/** Helper method to convert an ABI to a string representing the path within the APK. */
76const std::string& AbiToString(Abi abi);
77
Shane Farmer74cdea32017-05-12 16:22:36 -070078/**
79 * Represents an individual locale. When a locale is included, it must be
80 * declared from least specific to most specific, as a region does not make
81 * sense without a language. If neither the language or region are specified it
82 * acts as a special case for catch all. This can allow all locales to be kept,
83 * or compressed.
84 */
85struct Locale {
86 /** The ISO<?> standard locale language code. */
87 Maybe<std::string> lang;
88 /** The ISO<?> standard locale region code. */
89 Maybe<std::string> region;
90
91 inline friend bool operator==(const Locale& lhs, const Locale& rhs) {
92 return lhs.lang == rhs.lang && lhs.region == rhs.region;
93 }
94};
95
96// TODO: Encapsulate manifest modifications from the configuration file.
97struct AndroidManifest {
98 inline friend bool operator==(const AndroidManifest& lhs, const AndroidManifest& rhs) {
99 return true; // nothing to compare yet.
100 }
101};
102
103struct AndroidSdk {
104 Maybe<std::string> min_sdk_version;
105 Maybe<std::string> target_sdk_version;
106 Maybe<std::string> max_sdk_version;
107 Maybe<AndroidManifest> manifest;
108
109 inline friend bool operator==(const AndroidSdk& lhs, const AndroidSdk& rhs) {
110 return lhs.min_sdk_version == rhs.min_sdk_version &&
111 lhs.target_sdk_version == rhs.target_sdk_version &&
112 lhs.max_sdk_version == rhs.max_sdk_version &&
113 lhs.manifest == rhs.manifest;
114 }
115};
116
117// TODO: Make device features more than just an arbitrary string?
118using DeviceFeature = std::string;
119
120/** Represents a mapping of texture paths to a GL texture format. */
121struct GlTexture {
122 std::string name;
123 std::vector<std::string> texture_paths;
124
125 inline friend bool operator==(const GlTexture& lhs, const GlTexture& rhs) {
126 return lhs.name == rhs.name && lhs.texture_paths == rhs.texture_paths;
127 }
128};
129
Shane Farmer280be342017-06-21 15:20:15 -0700130/** AAPT2 XML configuration file binary representation. */
131struct PostProcessingConfiguration {
Shane Farmer57669432017-06-19 12:52:04 -0700132 // TODO: Support named artifacts?
133 std::vector<Artifact> artifacts;
Shane Farmer74cdea32017-05-12 16:22:36 -0700134 Maybe<std::string> artifact_format;
135
136 Group<Abi> abi_groups;
137 Group<ConfigDescription> screen_density_groups;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700138 Group<ConfigDescription> locale_groups;
Shane Farmer74cdea32017-05-12 16:22:36 -0700139 Group<AndroidSdk> android_sdk_groups;
140 Group<DeviceFeature> device_feature_groups;
141 Group<GlTexture> gl_texture_groups;
142};
143
144} // namespace configuration
145
146// Forward declaration of classes used in the API.
147struct IDiagnostics;
148namespace xml {
149class Element;
150}
151
152/**
153 * XML configuration file parser for the split and optimize commands.
154 */
155class ConfigurationParser {
156 public:
Shane Farmerb1027272017-06-14 09:10:28 -0700157
158 /** Returns a ConfigurationParser for the file located at the provided path. */
159 static Maybe<ConfigurationParser> ForPath(const std::string& path);
160
Shane Farmer74cdea32017-05-12 16:22:36 -0700161 /** Returns a ConfigurationParser for the configuration in the provided file contents. */
162 static ConfigurationParser ForContents(const std::string& contents) {
163 ConfigurationParser parser{contents};
164 return parser;
165 }
166
Shane Farmer74cdea32017-05-12 16:22:36 -0700167 /** Sets the diagnostics context to use when parsing. */
168 ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) {
169 diag_ = diagnostics;
170 return *this;
171 }
172
173 /**
174 * Parses the configuration file and returns the results. If the configuration could not be parsed
175 * the result is empty and any errors will be displayed with the provided diagnostics context.
176 */
Shane Farmer280be342017-06-21 15:20:15 -0700177 Maybe<configuration::PostProcessingConfiguration> Parse();
Shane Farmer74cdea32017-05-12 16:22:36 -0700178
179 protected:
180 /**
181 * Instantiates a new ConfigurationParser with the provided configuration file and a no-op
182 * diagnostics context. The default diagnostics context can be overridden with a call to
183 * WithDiagnostics(IDiagnostics *).
184 */
185 explicit ConfigurationParser(std::string contents);
186
187 /** Returns the current diagnostics context to any subclasses. */
188 IDiagnostics* diagnostics() {
189 return diag_;
190 }
191
192 /**
193 * An ActionHandler for processing XML elements in the XmlActionExecutor. Returns true if the
194 * element was successfully processed, otherwise returns false.
195 */
Shane Farmer280be342017-06-21 15:20:15 -0700196 using ActionHandler = std::function<bool(configuration::PostProcessingConfiguration* config,
197 xml::Element* element, IDiagnostics* diag)>;
Shane Farmer74cdea32017-05-12 16:22:36 -0700198
199 /** Handler for <artifact> tags. */
200 static ActionHandler artifact_handler_;
201 /** Handler for <artifact-format> tags. */
202 static ActionHandler artifact_format_handler_;
203 /** Handler for <abi-group> tags. */
204 static ActionHandler abi_group_handler_;
205 /** Handler for <screen-density-group> tags. */
206 static ActionHandler screen_density_group_handler_;
207 /** Handler for <locale-group> tags. */
208 static ActionHandler locale_group_handler_;
209 /** Handler for <android-sdk-group> tags. */
210 static ActionHandler android_sdk_group_handler_;
211 /** Handler for <gl-texture-group> tags. */
212 static ActionHandler gl_texture_group_handler_;
213 /** Handler for <device-feature-group> tags. */
214 static ActionHandler device_feature_group_handler_;
215
216 private:
217 /** The contents of the configuration file to parse. */
218 const std::string contents_;
219 /** The diagnostics context to send messages to. */
220 IDiagnostics* diag_;
221};
222
223} // namespace aapt
224
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700225#endif // AAPT2_CONFIGURATION_H