| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 1 | /* | 
| Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 2 |  * Copyright (C) 2016 The Android Open Source Project | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 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 |  * | 
| Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 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 | #pragma once | 
 | 18 |  | 
 | 19 | #include <stdlib.h> | 
 | 20 |  | 
 | 21 | #include <array> | 
 | 22 | #include <initializer_list> | 
| Logan Chien | 9c12323 | 2018-10-23 10:47:17 +0800 | [diff] [blame] | 23 | #include <optional> | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 24 | #include <set> | 
 | 25 | #include <string> | 
| Logan Chien | 9c12323 | 2018-10-23 10:47:17 +0800 | [diff] [blame] | 26 | #include <unordered_map> | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 27 |  | 
 | 28 | enum class Arch : size_t { | 
 | 29 |   arm = 0, | 
 | 30 |   arm64, | 
 | 31 |   mips, | 
 | 32 |   mips64, | 
 | 33 |   x86, | 
 | 34 |   x86_64, | 
 | 35 | }; | 
 | 36 |  | 
 | 37 | std::string to_string(const Arch& arch); | 
| Logan Chien | 9c12323 | 2018-10-23 10:47:17 +0800 | [diff] [blame] | 38 | std::optional<Arch> arch_from_string(const std::string& name); | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 39 |  | 
 | 40 | template <typename T> | 
 | 41 | class ArchMapIterator; | 
 | 42 |  | 
 | 43 | template <typename T> | 
 | 44 | class 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 Gao | 1605788 | 2016-08-02 14:54:09 -0700 | [diff] [blame] | 81 |   std::array<T, size_t(Arch::x86_64) + 1> data_ = {}; | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 82 | }; | 
 | 83 |  | 
 | 84 | template <typename T> | 
 | 85 | class 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 |  | 
 | 123 | static 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 |  | 
 | 132 | static 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 Chien | 3fc86c1 | 2018-10-23 15:04:00 +0800 | [diff] [blame] | 141 | static const std::set<int> default_levels = { 14, 15, 16, 17, 18, 19, 21, 23, 24, 25, 26, 27, 28 }; | 
| Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 142 |  | 
 | 143 | static const ArchMap<int> arch_min_api = { | 
 | 144 |   { Arch::arm, 9 }, | 
 | 145 |   { Arch::arm64, 21 }, | 
 | 146 |   { Arch::mips, 9 }, | 
 | 147 |   { Arch::mips64, 21 }, | 
 | 148 |   { Arch::x86, 9 }, | 
 | 149 |   { Arch::x86_64, 21 }, | 
 | 150 | }; | 
| Logan Chien | 9c12323 | 2018-10-23 10:47:17 +0800 | [diff] [blame] | 151 |  | 
 | 152 | static constexpr int future_api = 10000; | 
 | 153 |  | 
 | 154 | static 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}, | 
 | 169 |   {"Q", 9001}, | 
 | 170 | }; |