| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef AAPT2_CONFIGURATIONPARSER_INTERNAL_H |
| 18 |
#define AAPT2_CONFIGURATIONPARSER_INTERNAL_H |
| 19 |
|
| 20 |
#include "androidfw/ConfigDescription.h" |
| 21 |
|
| 22 |
#include "configuration/ConfigurationParser.h" |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <limits> |
| 26 |
|
| 27 |
namespace aapt { |
| 28 |
|
| 29 |
|
| 30 |
namespace xml { |
| 31 |
class Element; |
| 32 |
} |
| 33 |
|
| 34 |
namespace configuration { |
| 35 |
|
| 36 |
template <typename T> |
| 37 |
struct OrderedEntry { |
| 38 |
int32_t order; |
| 39 |
std::vector<T> entry; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
template <class T> |
| 44 |
using Entry = std::unordered_map<std::string, T>; |
| 45 |
|
| 46 |
|
| 47 |
template <class T> |
| 48 |
using Group = Entry<OrderedEntry<T>>; |
| 49 |
|
| 50 |
template<typename T> |
| 51 |
bool IsGroupValid(const Group<T>& group, const std::string& name, IDiagnostics* diag) { |
| 52 |
std::set<int32_t> orders; |
| 53 |
for (const auto& p : group) { |
| 54 |
orders.insert(p.second.order); |
| 55 |
} |
| 56 |
bool valid = orders.size() == group.size(); |
| 57 |
if (!valid) { |
| 58 |
diag->Error(DiagMessage() << name << " have overlapping version-code-order attributes"); |
| 59 |
} |
| 60 |
return valid; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
template <typename T> |
| 65 |
std::vector<T>& GetOrCreateGroup(std::string label, Group<T>* group) { |
| 66 |
OrderedEntry<T>& entry = (*group)[label]; |
| 67 |
|
| 68 |
if (entry.order == 0) { |
| 69 |
entry.order = group->size(); |
| 70 |
} |
| 71 |
return entry.entry; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
class ComparisonChain { |
| 81 |
public: |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
template <typename T> |
| 87 |
ComparisonChain& Add(const Group<T>& groups, const std::optional<std::string>& lhs, |
| 88 |
const std::optional<std::string>& rhs) { |
| 89 |
return Add(GetGroupOrder(groups, lhs), GetGroupOrder(groups, rhs)); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
ComparisonChain& Add(int lhs, int rhs) { |
| 97 |
if (!has_result_) { |
| 98 |
has_result_ = (lhs != rhs); |
| 99 |
result_ = (lhs < rhs); |
| 100 |
} |
| 101 |
return *this; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
bool Compare() { |
| 106 |
return result_; |
| 107 |
} |
| 108 |
|
| 109 |
private: |
| 110 |
template <typename T> |
| 111 |
inline size_t GetGroupOrder(const Entry<T>& groups, const std::optional<std::string>& label) { |
| 112 |
if (!label) { |
| 113 |
return std::numeric_limits<size_t>::max(); |
| 114 |
} |
| 115 |
return groups.at(label.value()).order; |
| 116 |
} |
| 117 |
|
| 118 |
bool has_result_ = false; |
| 119 |
bool result_ = false; |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
struct ConfiguredArtifact { |
| 124 |
|
| 125 |
std::optional<std::string> name; |
| 126 |
|
| 127 |
std::optional<std::string> abi_group; |
| 128 |
|
| 129 |
std::optional<std::string> screen_density_group; |
| 130 |
|
| 131 |
std::optional<std::string> locale_group; |
| 132 |
|
| 133 |
std::optional<std::string> android_sdk; |
| 134 |
|
| 135 |
std::optional<std::string> device_feature_group; |
| 136 |
|
| 137 |
std::optional<std::string> gl_texture_group; |
| 138 |
|
| 139 |
|
| 140 |
std::optional<std::string> ToArtifactName(const android::StringPiece& format, |
| 141 |
const android::StringPiece& apk_name, |
| 142 |
IDiagnostics* diag) const; |
| 143 |
|
| 144 |
|
| 145 |
std::optional<std::string> Name(const android::StringPiece& apk_name, IDiagnostics* diag) const; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
struct PostProcessingConfiguration { |
| 150 |
std::vector<ConfiguredArtifact> artifacts; |
| 151 |
std::optional<std::string> artifact_format; |
| 152 |
|
| 153 |
Group<Abi> abi_groups; |
| 154 |
Group<android::ConfigDescription> screen_density_groups; |
| 155 |
Group<android::ConfigDescription> locale_groups; |
| 156 |
Group<DeviceFeature> device_feature_groups; |
| 157 |
Group<GlTexture> gl_texture_groups; |
| 158 |
Entry<AndroidSdk> android_sdks; |
| 159 |
|
| 160 |
bool ValidateVersionCodeOrdering(IDiagnostics* diag) { |
| 161 |
bool valid = IsGroupValid(abi_groups, "abi-groups", diag); |
| 162 |
valid &= IsGroupValid(screen_density_groups, "screen-density-groups", diag); |
| 163 |
valid &= IsGroupValid(locale_groups, "locale-groups", diag); |
| 164 |
valid &= IsGroupValid(device_feature_groups, "device-feature-groups", diag); |
| 165 |
valid &= IsGroupValid(gl_texture_groups, "gl-texture-groups", diag); |
| 166 |
return valid; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
void SortArtifacts() { |
| 175 |
std::sort(artifacts.begin(), artifacts.end(), *this); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
bool operator()(const ConfiguredArtifact& lhs, const ConfiguredArtifact& rhs) { |
| 180 |
|
| 181 |
|
| 182 |
return ComparisonChain() |
| 183 |
|
| 184 |
|
| 185 |
.Add(GetMinSdk(lhs), GetMinSdk(rhs)) |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
.Add(abi_groups, lhs.abi_group, rhs.abi_group) |
| 190 |
|
| 191 |
.Add(screen_density_groups, lhs.screen_density_group, rhs.screen_density_group) |
| 192 |
.Add(locale_groups, lhs.locale_group, rhs.locale_group) |
| 193 |
.Add(gl_texture_groups, lhs.gl_texture_group, rhs.gl_texture_group) |
| 194 |
.Add(device_feature_groups, lhs.device_feature_group, rhs.device_feature_group) |
| 195 |
.Compare(); |
| 196 |
} |
| 197 |
|
| 198 |
private: |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
inline int GetMinSdk(const ConfiguredArtifact& artifact) { |
| 204 |
if (!artifact.android_sdk) { |
| 205 |
return 0; |
| 206 |
} |
| 207 |
const auto& entry = android_sdks.find(artifact.android_sdk.value()); |
| 208 |
if (entry == android_sdks.end()) { |
| 209 |
return 0; |
| 210 |
} |
| 211 |
return entry->second.min_sdk_version; |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
std::optional<PostProcessingConfiguration> ExtractConfiguration(const std::string& contents, |
| 217 |
const std::string& config_path, |
| 218 |
IDiagnostics* diag); |
| 219 |
|
| 220 |
namespace handler { |
| 221 |
|
| 222 |
|
| 223 |
bool ArtifactTagHandler(configuration::PostProcessingConfiguration* config, xml::Element* element, |
| 224 |
IDiagnostics* diag); |
| 225 |
|
| 226 |
|
| 227 |
bool ArtifactFormatTagHandler(configuration::PostProcessingConfiguration* config, |
| 228 |
xml::Element* element, IDiagnostics* diag); |
| 229 |
|
| 230 |
|
| 231 |
bool AbiGroupTagHandler(configuration::PostProcessingConfiguration* config, xml::Element* element, |
| 232 |
IDiagnostics* diag); |
| 233 |
|
| 234 |
|
| 235 |
bool ScreenDensityGroupTagHandler(configuration::PostProcessingConfiguration* config, |
| 236 |
xml::Element* element, IDiagnostics* diag); |
| 237 |
|
| 238 |
|
| 239 |
bool LocaleGroupTagHandler(configuration::PostProcessingConfiguration* config, |
| 240 |
xml::Element* element, IDiagnostics* diag); |
| 241 |
|
| 242 |
|
| 243 |
bool AndroidSdkTagHandler(configuration::PostProcessingConfiguration* config, xml::Element* element, |
| 244 |
IDiagnostics* diag); |
| 245 |
|
| 246 |
|
| 247 |
bool GlTextureGroupTagHandler(configuration::PostProcessingConfiguration* config, |
| 248 |
xml::Element* element, IDiagnostics* diag); |
| 249 |
|
| 250 |
|
| 251 |
bool DeviceFeatureGroupTagHandler(configuration::PostProcessingConfiguration* config, |
| 252 |
xml::Element* element, IDiagnostics* diag); |
| 253 |
|
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
#endif |