blob: ca5891025c92c5ff7dd356e9fd4da29b1f672634 [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
Shane Farmer810fd182017-09-21 14:37:44 -070020#include <set>
Shane Farmer74cdea32017-05-12 16:22:36 -070021#include <string>
22#include <unordered_map>
23#include <vector>
Shane Farmer74cdea32017-05-12 16:22:36 -070024
Shane Farmer9f0e7f12017-06-22 12:26:44 -070025#include "ConfigDescription.h"
26#include "Diagnostics.h"
Shane Farmer74cdea32017-05-12 16:22:36 -070027#include "util/Maybe.h"
28
29namespace aapt {
30
31namespace configuration {
32
Shane Farmer74cdea32017-05-12 16:22:36 -070033/** Enumeration of currently supported ABIs. */
34enum class Abi {
35 kArmeV6,
36 kArmV7a,
37 kArm64V8a,
38 kX86,
39 kX86_64,
40 kMips,
41 kMips64,
42 kUniversal
43};
44
Shane Farmer57669432017-06-19 12:52:04 -070045/** Helper method to convert an ABI to a string representing the path within the APK. */
Shane Farmercb6c3f92017-11-27 13:19:36 -080046const android::StringPiece& AbiToString(Abi abi);
Shane Farmer57669432017-06-19 12:52:04 -070047
Shane Farmer74cdea32017-05-12 16:22:36 -070048/**
49 * Represents an individual locale. When a locale is included, it must be
50 * declared from least specific to most specific, as a region does not make
51 * sense without a language. If neither the language or region are specified it
52 * acts as a special case for catch all. This can allow all locales to be kept,
53 * or compressed.
54 */
55struct Locale {
56 /** The ISO<?> standard locale language code. */
57 Maybe<std::string> lang;
58 /** The ISO<?> standard locale region code. */
59 Maybe<std::string> region;
60
61 inline friend bool operator==(const Locale& lhs, const Locale& rhs) {
62 return lhs.lang == rhs.lang && lhs.region == rhs.region;
63 }
64};
65
66// TODO: Encapsulate manifest modifications from the configuration file.
67struct AndroidManifest {
68 inline friend bool operator==(const AndroidManifest& lhs, const AndroidManifest& rhs) {
69 return true; // nothing to compare yet.
70 }
71};
72
73struct AndroidSdk {
Shane Farmer3edd4722017-09-01 14:34:22 -070074 Maybe<int> min_sdk_version;
75 Maybe<int> target_sdk_version;
76 Maybe<int> max_sdk_version;
Shane Farmer74cdea32017-05-12 16:22:36 -070077 Maybe<AndroidManifest> manifest;
78
Shane Farmer3edd4722017-09-01 14:34:22 -070079 static AndroidSdk ForMinSdk(int min_sdk) {
Shane Farmerefe45392017-08-21 14:39:28 -070080 AndroidSdk sdk;
Shane Farmer3edd4722017-09-01 14:34:22 -070081 sdk.min_sdk_version = min_sdk;
Shane Farmerefe45392017-08-21 14:39:28 -070082 return sdk;
83 }
84
Shane Farmer74cdea32017-05-12 16:22:36 -070085 inline friend bool operator==(const AndroidSdk& lhs, const AndroidSdk& rhs) {
86 return lhs.min_sdk_version == rhs.min_sdk_version &&
87 lhs.target_sdk_version == rhs.target_sdk_version &&
88 lhs.max_sdk_version == rhs.max_sdk_version &&
89 lhs.manifest == rhs.manifest;
90 }
91};
92
93// TODO: Make device features more than just an arbitrary string?
94using DeviceFeature = std::string;
95
96/** Represents a mapping of texture paths to a GL texture format. */
97struct GlTexture {
98 std::string name;
99 std::vector<std::string> texture_paths;
100
101 inline friend bool operator==(const GlTexture& lhs, const GlTexture& rhs) {
102 return lhs.name == rhs.name && lhs.texture_paths == rhs.texture_paths;
103 }
104};
105
Shane Farmercb6c3f92017-11-27 13:19:36 -0800106/** An artifact with all the details pulled from the PostProcessingConfiguration. */
107struct OutputArtifact {
108 std::string name;
109 int version;
110 std::vector<Abi> abis;
111 std::vector<ConfigDescription> screen_densities;
112 std::vector<ConfigDescription> locales;
113 Maybe<AndroidSdk> android_sdk;
114 std::vector<DeviceFeature> features;
115 std::vector<GlTexture> textures;
Shane Farmer74cdea32017-05-12 16:22:36 -0700116};
117
118} // namespace configuration
119
120// Forward declaration of classes used in the API.
121struct IDiagnostics;
122namespace xml {
123class Element;
124}
125
126/**
127 * XML configuration file parser for the split and optimize commands.
128 */
129class ConfigurationParser {
130 public:
Shane Farmerb1027272017-06-14 09:10:28 -0700131
132 /** Returns a ConfigurationParser for the file located at the provided path. */
133 static Maybe<ConfigurationParser> ForPath(const std::string& path);
134
Shane Farmer74cdea32017-05-12 16:22:36 -0700135 /** Returns a ConfigurationParser for the configuration in the provided file contents. */
136 static ConfigurationParser ForContents(const std::string& contents) {
137 ConfigurationParser parser{contents};
138 return parser;
139 }
140
Shane Farmer74cdea32017-05-12 16:22:36 -0700141 /** Sets the diagnostics context to use when parsing. */
142 ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) {
143 diag_ = diagnostics;
144 return *this;
145 }
146
147 /**
148 * Parses the configuration file and returns the results. If the configuration could not be parsed
149 * the result is empty and any errors will be displayed with the provided diagnostics context.
150 */
Shane Farmercb6c3f92017-11-27 13:19:36 -0800151 Maybe<std::vector<configuration::OutputArtifact>> Parse(const android::StringPiece& apk_path);
Shane Farmer74cdea32017-05-12 16:22:36 -0700152
153 protected:
154 /**
155 * Instantiates a new ConfigurationParser with the provided configuration file and a no-op
156 * diagnostics context. The default diagnostics context can be overridden with a call to
157 * WithDiagnostics(IDiagnostics *).
158 */
159 explicit ConfigurationParser(std::string contents);
160
161 /** Returns the current diagnostics context to any subclasses. */
162 IDiagnostics* diagnostics() {
163 return diag_;
164 }
165
Shane Farmer74cdea32017-05-12 16:22:36 -0700166 private:
167 /** The contents of the configuration file to parse. */
168 const std::string contents_;
169 /** The diagnostics context to send messages to. */
170 IDiagnostics* diag_;
171};
172
173} // namespace aapt
174
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700175#endif // AAPT2_CONFIGURATION_H