blob: e4bbcc44aa9c2b854d1b3d86b9bf58066c6c82bf [file] [log] [blame]
Josh Gaobfb6bae2016-07-15 17:25:21 -07001/*
Elliott Hughesdfb74c52016-10-24 12:53:17 -07002 * Copyright (C) 2016 The Android Open Source Project
Josh Gaobfb6bae2016-07-15 17:25:21 -07003 *
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 *
Elliott Hughesdfb74c52016-10-24 12:53:17 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Josh Gaobfb6bae2016-07-15 17:25:21 -07009 *
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#pragma once
18
19#include <stdlib.h>
20
21#include <array>
22#include <initializer_list>
Logan Chien9c123232018-10-23 10:47:17 +080023#include <optional>
Josh Gaobfb6bae2016-07-15 17:25:21 -070024#include <set>
25#include <string>
Logan Chien9c123232018-10-23 10:47:17 +080026#include <unordered_map>
Josh Gaobfb6bae2016-07-15 17:25:21 -070027
28enum class Arch : size_t {
29 arm = 0,
30 arm64,
31 mips,
32 mips64,
33 x86,
34 x86_64,
35};
36
37std::string to_string(const Arch& arch);
Logan Chien9c123232018-10-23 10:47:17 +080038std::optional<Arch> arch_from_string(const std::string& name);
Josh Gaobfb6bae2016-07-15 17:25:21 -070039
40template <typename T>
41class ArchMapIterator;
42
43template <typename T>
44class ArchMap {
45 public:
46 ArchMap() {
47 }
48
49 ArchMap(std::initializer_list<std::pair<Arch, T>> initializer) {
50 for (auto& pair : initializer) {
51 this->operator[](pair.first) = pair.second;
52 }
53 }
54
55 T& operator[](Arch arch) {
56 return data_[size_t(arch)];
57 }
58
59 const T& operator[](Arch arch) const {
60 return data_[size_t(arch)];
61 }
62
63 bool operator==(const ArchMap& other) const {
64 for (size_t i = 0; i < data_.size(); ++i) {
65 if (data_[i] != other.data_[i]) {
66 return false;
67 }
68 }
69 return true;
70 }
71
72 ArchMapIterator<T> begin() const {
73 return ArchMapIterator<T>(*this, Arch::arm);
74 }
75
76 ArchMapIterator<T> end() const {
77 return ArchMapIterator<T>(*this, Arch(size_t(Arch::x86_64) + 1));
78 }
79
80 private:
Josh Gao16057882016-08-02 14:54:09 -070081 std::array<T, size_t(Arch::x86_64) + 1> data_ = {};
Josh Gaobfb6bae2016-07-15 17:25:21 -070082};
83
84template <typename T>
85class ArchMapIterator {
86 const ArchMap<T>& map_;
87 Arch arch_ = Arch::arm;
88
89 public:
90 ArchMapIterator() = delete;
91
92 ArchMapIterator(const ArchMap<T>& map, Arch arch) : map_(map), arch_(arch) {
93 }
94
95 bool operator==(const ArchMapIterator<T>& rhs) const {
96 return map_ == rhs.map_ && arch_ == rhs.arch_;
97 }
98
99 bool operator!=(const ArchMapIterator<T>& rhs) const {
100 return !(*this == rhs);
101 }
102
103 ArchMapIterator& operator++() {
104 arch_ = Arch(size_t(arch_) + 1);
105 return *this;
106 }
107
108 ArchMapIterator operator++(int) {
109 ArchMapIterator result = *this;
110 ++*this;
111 return result;
112 }
113
114 std::pair<const Arch&, const T&> operator*() const {
115 return std::tie(arch_, map_[arch_]);
116 }
117
118 std::pair<const Arch&, const T&> operator->() const {
119 return std::tie(arch_, map_[arch_]);
120 }
121};
122
123static const std::set<Arch> supported_archs = {
124 Arch::arm,
125 Arch::arm64,
126 Arch::mips,
127 Arch::mips64,
128 Arch::x86,
129 Arch::x86_64,
130};
131
132static ArchMap<std::string> arch_targets = {
133 { Arch::arm, "arm-linux-androideabi" },
134 { Arch::arm64, "aarch64-linux-android" },
135 { Arch::mips, "mipsel-linux-android" },
136 { Arch::mips64, "mips64el-linux-android" },
137 { Arch::x86, "i686-linux-android" },
138 { Arch::x86_64, "x86_64-linux-android" },
139};
140
Logan Chien8fd8b992019-08-27 10:45:42 -0700141static const std::set<int> default_levels = {
142 14, 15, 16, 17, 18, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30,
143};
Josh Gaobfb6bae2016-07-15 17:25:21 -0700144
145static const ArchMap<int> arch_min_api = {
146 { Arch::arm, 9 },
147 { Arch::arm64, 21 },
148 { Arch::mips, 9 },
149 { Arch::mips64, 21 },
150 { Arch::x86, 9 },
151 { Arch::x86_64, 21 },
152};
Logan Chien9c123232018-10-23 10:47:17 +0800153
Logan Chien9c123232018-10-23 10:47:17 +0800154static const std::unordered_map<std::string, int> api_codename_map{
155 {"G", 9},
156 {"I", 14},
157 {"J", 16},
158 {"J-MR1", 17},
159 {"J-MR2", 18},
160 {"K", 19},
161 {"L", 21},
162 {"L-MR1", 22},
163 {"M", 23},
164 {"N", 24},
165 {"N-MR1", 25},
166 {"O", 26},
167 {"O-MR1", 27},
168 {"P", 28},
Elliott Hughes52700172019-03-14 13:34:21 -0700169 {"Q", 29},
Logan Chien8fd8b992019-08-27 10:45:42 -0700170 {"R", 30},
Logan Chien9c123232018-10-23 10:47:17 +0800171};