Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2016 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 #pragma once
     18 
     19 #include <stdlib.h>
     20 
     21 #include <array>
     22 #include <initializer_list>
     23 #include <optional>
     24 #include <set>
     25 #include <string>
     26 #include <unordered_map>
     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);
     38 std::optional<Arch> arch_from_string(const std::string& name);
     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:
     81   std::array<T, size_t(Arch::x86_64) + 1> data_ = {};
     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 
    141 static const std::set<int> default_levels = { 14, 15, 16, 17, 18, 19, 21, 23, 24, 25, 26, 27, 28, 29 };
    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 };
    151 
    152 static const std::unordered_map<std::string, int> api_codename_map{
    153   {"G", 9},
    154   {"I", 14},
    155   {"J", 16},
    156   {"J-MR1", 17},
    157   {"J-MR2", 18},
    158   {"K", 19},
    159   {"L", 21},
    160   {"L-MR1", 22},
    161   {"M", 23},
    162   {"N", 24},
    163   {"N-MR1", 25},
    164   {"O", 26},
    165   {"O-MR1", 27},
    166   {"P", 28},
    167   {"Q", 29},
    168 };
    169