blob: 195b4baac3199365fb12b14343a4227a93c24982 [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
Ryan Mitchell4382e442021-07-14 12:53:01 -070020#include <optional>
Shane Farmer810fd182017-09-21 14:37:44 -070021#include <set>
Shane Farmer74cdea32017-05-12 16:22:36 -070022#include <string>
23#include <unordered_map>
24#include <vector>
Shane Farmer74cdea32017-05-12 16:22:36 -070025
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020026#include "androidfw/ConfigDescription.h"
27
Shane Farmer9f0e7f12017-06-22 12:26:44 -070028#include "Diagnostics.h"
Shane Farmer74cdea32017-05-12 16:22:36 -070029
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. */
Ryan Mitchell4382e442021-07-14 12:53:01 -070058 std::optional<std::string> lang;
Shane Farmer74cdea32017-05-12 16:22:36 -070059 /** The ISO<?> standard locale region code. */
Ryan Mitchell4382e442021-07-14 12:53:01 -070060 std::optional<std::string> region;
Shane Farmer74cdea32017-05-12 16:22:36 -070061
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.
Ryan Mitchell4382e442021-07-14 12:53:01 -070077 std::optional<int> target_sdk_version;
78 std::optional<int> max_sdk_version;
79 std::optional<AndroidManifest> manifest;
Shane Farmer74cdea32017-05-12 16:22:36 -070080
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;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700115 std::optional<AndroidSdk> android_sdk;
Shane Farmercb6c3f92017-11-27 13:19:36 -0800116 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. */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700139 static std::optional<ConfigurationParser> ForPath(const std::string& path);
Shane Farmerb1027272017-06-14 09:10:28 -0700140
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 */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700157 std::optional<std::vector<configuration::OutputArtifact>> Parse(
158 const android::StringPiece& apk_path);
Shane Farmer74cdea32017-05-12 16:22:36 -0700159
160 protected:
161 /**
162 * Instantiates a new ConfigurationParser with the provided configuration file and a no-op
163 * diagnostics context. The default diagnostics context can be overridden with a call to
164 * WithDiagnostics(IDiagnostics *).
165 */
Shane Farmer78c43d72017-12-04 09:08:38 -0800166 ConfigurationParser(std::string contents, const std::string& config_path);
Shane Farmer74cdea32017-05-12 16:22:36 -0700167
168 /** Returns the current diagnostics context to any subclasses. */
169 IDiagnostics* diagnostics() {
170 return diag_;
171 }
172
Shane Farmer74cdea32017-05-12 16:22:36 -0700173 private:
174 /** The contents of the configuration file to parse. */
175 const std::string contents_;
Shane Farmer78c43d72017-12-04 09:08:38 -0800176 /** Path to the input configuration. */
177 const std::string config_path_;
Shane Farmer74cdea32017-05-12 16:22:36 -0700178 /** The diagnostics context to send messages to. */
179 IDiagnostics* diag_;
180};
181
182} // namespace aapt
183
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700184#endif // AAPT2_CONFIGURATION_H