blob: d66f4ab000a3c75b47a66bde65db0b889c5356de [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"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000027#include "androidfw/IDiagnostics.h"
Shane Farmer74cdea32017-05-12 16:22:36 -070028
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. */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070046android::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. */
Ryan Mitchell4382e442021-07-14 12:53:01 -070057 std::optional<std::string> lang;
Shane Farmer74cdea32017-05-12 16:22:36 -070058 /** The ISO<?> standard locale region code. */
Ryan Mitchell4382e442021-07-14 12:53:01 -070059 std::optional<std::string> region;
Shane Farmer74cdea32017-05-12 16:22:36 -070060
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 Farmer78c43d72017-12-04 09:08:38 -080074 std::string label;
75 int min_sdk_version; // min_sdk_version is mandatory if splitting by SDK.
Ryan Mitchell4382e442021-07-14 12:53:01 -070076 std::optional<int> target_sdk_version;
77 std::optional<int> max_sdk_version;
78 std::optional<AndroidManifest> manifest;
Shane Farmer74cdea32017-05-12 16:22:36 -070079
Shane Farmer3edd4722017-09-01 14:34:22 -070080 static AndroidSdk ForMinSdk(int min_sdk) {
Shane Farmerefe45392017-08-21 14:39:28 -070081 AndroidSdk sdk;
Shane Farmer3edd4722017-09-01 14:34:22 -070082 sdk.min_sdk_version = min_sdk;
Shane Farmerefe45392017-08-21 14:39:28 -070083 return sdk;
84 }
85
Shane Farmer74cdea32017-05-12 16:22:36 -070086 inline friend bool operator==(const AndroidSdk& lhs, const AndroidSdk& rhs) {
87 return lhs.min_sdk_version == rhs.min_sdk_version &&
88 lhs.target_sdk_version == rhs.target_sdk_version &&
89 lhs.max_sdk_version == rhs.max_sdk_version &&
90 lhs.manifest == rhs.manifest;
91 }
92};
93
94// TODO: Make device features more than just an arbitrary string?
95using DeviceFeature = std::string;
96
97/** Represents a mapping of texture paths to a GL texture format. */
98struct GlTexture {
99 std::string name;
100 std::vector<std::string> texture_paths;
101
102 inline friend bool operator==(const GlTexture& lhs, const GlTexture& rhs) {
103 return lhs.name == rhs.name && lhs.texture_paths == rhs.texture_paths;
104 }
105};
106
Shane Farmercb6c3f92017-11-27 13:19:36 -0800107/** An artifact with all the details pulled from the PostProcessingConfiguration. */
108struct OutputArtifact {
109 std::string name;
110 int version;
111 std::vector<Abi> abis;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200112 std::vector<android::ConfigDescription> screen_densities;
113 std::vector<android::ConfigDescription> locales;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700114 std::optional<AndroidSdk> android_sdk;
Shane Farmercb6c3f92017-11-27 13:19:36 -0800115 std::vector<DeviceFeature> features;
116 std::vector<GlTexture> textures;
Shane Farmer78c43d72017-12-04 09:08:38 -0800117
118 inline int GetMinSdk(int default_value = -1) const {
119 if (!android_sdk) {
120 return default_value;
121 }
122 return android_sdk.value().min_sdk_version;
123 }
Shane Farmer74cdea32017-05-12 16:22:36 -0700124};
125
126} // namespace configuration
127
Shane Farmer74cdea32017-05-12 16:22:36 -0700128/**
129 * XML configuration file parser for the split and optimize commands.
130 */
131class ConfigurationParser {
132 public:
Shane Farmerb1027272017-06-14 09:10:28 -0700133
134 /** Returns a ConfigurationParser for the file located at the provided path. */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700135 static std::optional<ConfigurationParser> ForPath(const std::string& path);
Shane Farmerb1027272017-06-14 09:10:28 -0700136
Shane Farmer74cdea32017-05-12 16:22:36 -0700137 /** Returns a ConfigurationParser for the configuration in the provided file contents. */
Shane Farmer78c43d72017-12-04 09:08:38 -0800138 static ConfigurationParser ForContents(const std::string& contents, const std::string& path) {
139 ConfigurationParser parser{contents, path};
Shane Farmer74cdea32017-05-12 16:22:36 -0700140 return parser;
141 }
142
Shane Farmer74cdea32017-05-12 16:22:36 -0700143 /** Sets the diagnostics context to use when parsing. */
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000144 ConfigurationParser& WithDiagnostics(android::IDiagnostics* diagnostics) {
Shane Farmer74cdea32017-05-12 16:22:36 -0700145 diag_ = diagnostics;
146 return *this;
147 }
148
149 /**
150 * Parses the configuration file and returns the results. If the configuration could not be parsed
151 * the result is empty and any errors will be displayed with the provided diagnostics context.
152 */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700153 std::optional<std::vector<configuration::OutputArtifact>> Parse(android::StringPiece apk_path);
Shane Farmer74cdea32017-05-12 16:22:36 -0700154
155 protected:
156 /**
157 * Instantiates a new ConfigurationParser with the provided configuration file and a no-op
158 * diagnostics context. The default diagnostics context can be overridden with a call to
159 * WithDiagnostics(IDiagnostics *).
160 */
Shane Farmer78c43d72017-12-04 09:08:38 -0800161 ConfigurationParser(std::string contents, const std::string& config_path);
Shane Farmer74cdea32017-05-12 16:22:36 -0700162
163 /** Returns the current diagnostics context to any subclasses. */
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000164 android::IDiagnostics* diagnostics() {
Shane Farmer74cdea32017-05-12 16:22:36 -0700165 return diag_;
166 }
167
Shane Farmer74cdea32017-05-12 16:22:36 -0700168 private:
169 /** The contents of the configuration file to parse. */
170 const std::string contents_;
Shane Farmer78c43d72017-12-04 09:08:38 -0800171 /** Path to the input configuration. */
172 const std::string config_path_;
Shane Farmer74cdea32017-05-12 16:22:36 -0700173 /** The diagnostics context to send messages to. */
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000174 android::IDiagnostics* diag_;
Shane Farmer74cdea32017-05-12 16:22:36 -0700175};
176
177} // namespace aapt
178
Shane Farmer9f0e7f12017-06-22 12:26:44 -0700179#endif // AAPT2_CONFIGURATION_H