blob: 3fae5dd0a99041c4f4a9c60819d92629a2279d77 [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
Shane Farmer280be342017-06-21 15:20:15 -0700120/** AAPT2 XML configuration file binary representation. */
121struct PostProcessingConfiguration {
Shane Farmer57669432017-06-19 12:52:04 -0700122 // TODO: Support named artifacts?
123 std::vector<Artifact> artifacts;
Shane Farmer74cdea32017-05-12 16:22:36 -0700124 Maybe<std::string> artifact_format;
125
126 Group<Abi> abi_groups;
127 Group<ConfigDescription> screen_density_groups;
128 Group<Locale> locale_groups;
129 Group<AndroidSdk> android_sdk_groups;
130 Group<DeviceFeature> device_feature_groups;
131 Group<GlTexture> gl_texture_groups;
132};
133
134} // namespace configuration
135
136// Forward declaration of classes used in the API.
137struct IDiagnostics;
138namespace xml {
139class Element;
140}
141
142/**
143 * XML configuration file parser for the split and optimize commands.
144 */
145class ConfigurationParser {
146 public:
Shane Farmerb1027272017-06-14 09:10:28 -0700147
148 /** Returns a ConfigurationParser for the file located at the provided path. */
149 static Maybe<ConfigurationParser> ForPath(const std::string& path);
150
Shane Farmer74cdea32017-05-12 16:22:36 -0700151 /** Returns a ConfigurationParser for the configuration in the provided file contents. */
152 static ConfigurationParser ForContents(const std::string& contents) {
153 ConfigurationParser parser{contents};
154 return parser;
155 }
156
Shane Farmer74cdea32017-05-12 16:22:36 -0700157 /** Sets the diagnostics context to use when parsing. */
158 ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) {
159 diag_ = diagnostics;
160 return *this;
161 }
162
163 /**
164 * Parses the configuration file and returns the results. If the configuration could not be parsed
165 * the result is empty and any errors will be displayed with the provided diagnostics context.
166 */
Shane Farmer280be342017-06-21 15:20:15 -0700167 Maybe<configuration::PostProcessingConfiguration> Parse();
Shane Farmer74cdea32017-05-12 16:22:36 -0700168
169 protected:
170 /**
171 * Instantiates a new ConfigurationParser with the provided configuration file and a no-op
172 * diagnostics context. The default diagnostics context can be overridden with a call to
173 * WithDiagnostics(IDiagnostics *).
174 */
175 explicit ConfigurationParser(std::string contents);
176
177 /** Returns the current diagnostics context to any subclasses. */
178 IDiagnostics* diagnostics() {
179 return diag_;
180 }
181
182 /**
183 * An ActionHandler for processing XML elements in the XmlActionExecutor. Returns true if the
184 * element was successfully processed, otherwise returns false.
185 */
Shane Farmer280be342017-06-21 15:20:15 -0700186 using ActionHandler = std::function<bool(configuration::PostProcessingConfiguration* config,
187 xml::Element* element, IDiagnostics* diag)>;
Shane Farmer74cdea32017-05-12 16:22:36 -0700188
189 /** Handler for <artifact> tags. */
190 static ActionHandler artifact_handler_;
191 /** Handler for <artifact-format> tags. */
192 static ActionHandler artifact_format_handler_;
193 /** Handler for <abi-group> tags. */
194 static ActionHandler abi_group_handler_;
195 /** Handler for <screen-density-group> tags. */
196 static ActionHandler screen_density_group_handler_;
197 /** Handler for <locale-group> tags. */
198 static ActionHandler locale_group_handler_;
199 /** Handler for <android-sdk-group> tags. */
200 static ActionHandler android_sdk_group_handler_;
201 /** Handler for <gl-texture-group> tags. */
202 static ActionHandler gl_texture_group_handler_;
203 /** Handler for <device-feature-group> tags. */
204 static ActionHandler device_feature_group_handler_;
205
206 private:
207 /** The contents of the configuration file to parse. */
208 const std::string contents_;
209 /** The diagnostics context to send messages to. */
210 IDiagnostics* diag_;
211};
212
213} // namespace aapt
214
215#endif //AAPT2_CONFIGURATION_H