blob: b9e3be9393c926db7889769ca4f381e1aa5900ff [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
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020025#include "androidfw/ConfigDescription.h"
26
Shane Farmer9f0e7f12017-06-22 12:26:44 -070027#include "Diagnostics.h"
Shane Farmer74cdea32017-05-12 16:22:36 -070028#include "util/Maybe.h"
29
30namespace aapt {
31
32namespace configuration {
33
Shane Farmer74cdea32017-05-12 16:22:36 -070034/** Enumeration of currently supported ABIs. */
35enum class Abi {
36 kArmeV6,
37 kArmV7a,
38 kArm64V8a,
39 kX86,
40 kX86_64,
41 kMips,
42 kMips64,
43 kUniversal
44};
45
Shane Farmer57669432017-06-19 12:52:04 -070046/** Helper method to convert an ABI to a string representing the path within the APK. */
Shane Farmercb6c3f92017-11-27 13:19:36 -080047const android::StringPiece& AbiToString(Abi abi);
Shane Farmer57669432017-06-19 12:52:04 -070048
Shane Farmer74cdea32017-05-12 16:22:36 -070049/**
50 * Represents an individual locale. When a locale is included, it must be
51 * declared from least specific to most specific, as a region does not make
52 * sense without a language. If neither the language or region are specified it
53 * acts as a special case for catch all. This can allow all locales to be kept,
54 * or compressed.
55 */
56struct Locale {
57 /** The ISO<?> standard locale language code. */
58 Maybe<std::string> lang;
59 /** The ISO<?> standard locale region code. */
60 Maybe<std::string> region;
61
62 inline friend bool operator==(const Locale& lhs, const Locale& rhs) {
63 return lhs.lang == rhs.lang && lhs.region == rhs.region;
64 }
65};
66
67// TODO: Encapsulate manifest modifications from the configuration file.
68struct AndroidManifest {
69 inline friend bool operator==(const AndroidManifest& lhs, const AndroidManifest& rhs) {
70 return true; // nothing to compare yet.
71 }
72};
73
74struct AndroidSdk {
Shane Farmer78c43d72017-12-04 09:08:38 -080075 std::string label;
76 int min_sdk_version; // min_sdk_version is mandatory if splitting by SDK.
Shane Farmer3edd4722017-09-01 14:34:22 -070077 Maybe<int> target_sdk_version;
78 Maybe<int> max_sdk_version;
Shane Farmer74cdea32017-05-12 16:22:36 -070079 Maybe<AndroidManifest> manifest;
80
Shane Farmer3edd4722017-09-01 14:34:22 -070081 static AndroidSdk ForMinSdk(int min_sdk) {
Shane Farmerefe45392017-08-21 14:39:28 -070082 AndroidSdk sdk;
Shane Farmer3edd4722017-09-01 14:34:22 -070083 sdk.min_sdk_version = min_sdk;
Shane Farmerefe45392017-08-21 14:39:28 -070084 return sdk;
85 }
86
Shane Farmer74cdea32017-05-12 16:22:36 -070087 inline friend bool operator==(const AndroidSdk& lhs, const AndroidSdk& rhs) {
88 return lhs.min_sdk_version == rhs.min_sdk_version &&
89 lhs.target_sdk_version == rhs.target_sdk_version &&
90 lhs.max_sdk_version == rhs.max_sdk_version &&
91 lhs.manifest == rhs.manifest;
92 }
93};
94
95// TODO: Make device features more than just an arbitrary string?
96using DeviceFeature = std::string;
97
98/** Represents a mapping of texture paths to a GL texture format. */
99struct GlTexture {
100 std::string name;
101 std::vector<std::string> texture_paths;
102
103 inline friend bool operator==(const GlTexture& lhs, const GlTexture& rhs) {
104 return lhs.name == rhs.name && lhs.texture_paths == rhs.texture_paths;
105 }
106};
107
Shane Farmercb6c3f92017-11-27 13:19:36 -0800108/** An artifact with all the details pulled from the PostProcessingConfiguration. */
109struct OutputArtifact {
110 std::string name;
111 int version;
112 std::vector<Abi> abis;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200113 std::vector<android::ConfigDescription> screen_densities;
114 std::vector<android::ConfigDescription> locales;
Shane Farmercb6c3f92017-11-27 13:19:36 -0800115 Maybe<AndroidSdk> android_sdk;
116 std::vector<DeviceFeature> features;
117 std::vector<GlTexture> textures;
Shane Farmer78c43d72017-12-04 09:08:38 -0800118
119 inline int GetMinSdk(int default_value = -1) const {
120 if (!android_sdk) {
121 return default_value;
122 }
123 return android_sdk.value().min_sdk_version;
124 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700125};
126
127} // namespace configuration
128
129// Forward declaration of classes used in the API.
130struct IDiagnostics;
Shane Farmer74cdea32017-05-12 16:22:36 -0700131
132/**
133 * XML configuration file parser for the split and optimize commands.
134 */
135class ConfigurationParser {
136 public:
Shane Farmerb1027272017-06-14 09:10:28 -0700137
138 /** Returns a ConfigurationParser for the file located at the provided path. */
139 static Maybe<ConfigurationParser> ForPath(const std::string& path);
140
Shane Farmer74cdea32017-05-12 16:22:36 -0700141 /** Returns a ConfigurationParser for the configuration in the provided file contents. */
Shane Farmer78c43d72017-12-04 09:08:38 -0800142 static ConfigurationParser ForContents(const std::string& contents, const std::string& path) {
143 ConfigurationParser parser{contents, path};
Shane Farmer74cdea32017-05-12 16:22:36 -0700144 return parser;
145 }
146
Shane Farmer74cdea32017-05-12 16:22:36 -0700147 /** Sets the diagnostics context to use when parsing. */
148 ConfigurationParser& WithDiagnostics(IDiagnostics* diagnostics) {
149 diag_ = diagnostics;
150 return *this;
151 }
152
153 /**
154 * Parses the configuration file and returns the results. If the configuration could not be parsed
155 * the result is empty and any errors will be displayed with the provided diagnostics context.
156 */
Shane Farmercb6c3f92017-11-27 13:19:36 -0800157 Maybe<std::vector<configuration::OutputArtifact>> Parse(const android::StringPiece& apk_path);
Shane Farmer74cdea32017-05-12 16:22:36 -0700158
159 protected:
160 /**
161 * Instantiates a new ConfigurationParser with the provided configuration file and a no-op
162 * diagnostics context. The default diagnostics context can be overridden with a call to
163 * WithDiagnostics(IDiagnostics *).
164 */
Shane Farmer78c43d72017-12-04 09:08:38 -0800165 ConfigurationParser(std::string contents, const std::string& config_path);
Shane Farmer74cdea32017-05-12 16:22:36 -0700166
167 /** Returns the current diagnostics context to any subclasses. */
168 IDiagnostics* diagnostics() {
169 return diag_;
170 }
171
Shane Farmer74cdea32017-05-12 16:22:36 -0700172 private:
173 /** The contents of the configuration file to parse. */
174 const std::string contents_;
Shane Farmer78c43d72017-12-04 09:08:38 -0800175 /** Path to the input configuration. */
176 const std::string config_path_;
Shane Farmer74cdea32017-05-12 16:22:36 -0700177 /** The diagnostics context to send messages to. */
178 IDiagnostics* diag_;
179};
180
181} // namespace aapt
182
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700183#endif // AAPT2_CONFIGURATION_H