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 | #include "Arch.h" |
| 18 | |
| 19 | #include <err.h> |
| 20 | |
| 21 | #include <string> |
| 22 | |
| 23 | std::string to_string(const Arch& arch) { |
| 24 | switch (arch) { |
| 25 | case Arch::arm: |
| 26 | return "arm"; |
| 27 | |
| 28 | case Arch::arm64: |
| 29 | return "arm64"; |
| 30 | |
Elliott Hughes | d7f0844 | 2022-10-04 00:36:29 +0000 | [diff] [blame] | 31 | case Arch::riscv64: |
| 32 | return "riscv64"; |
| 33 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 34 | case Arch::x86: |
| 35 | return "x86"; |
| 36 | |
| 37 | case Arch::x86_64: |
| 38 | return "x86_64"; |
| 39 | } |
| 40 | |
| 41 | errx(1, "unknown arch '%zu'", size_t(arch)); |
| 42 | } |
| 43 | |
Logan Chien | 9c12323 | 2018-10-23 10:47:17 +0800 | [diff] [blame] | 44 | static const std::unordered_map<std::string, Arch> arch_name_map{ |
| 45 | {"arm", Arch::arm}, |
| 46 | {"arm64", Arch::arm64}, |
Elliott Hughes | d7f0844 | 2022-10-04 00:36:29 +0000 | [diff] [blame] | 47 | {"riscv64", Arch::riscv64}, |
Logan Chien | 9c12323 | 2018-10-23 10:47:17 +0800 | [diff] [blame] | 48 | {"x86", Arch::x86}, |
| 49 | {"x86_64", Arch::x86_64}, |
| 50 | }; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 51 | |
Logan Chien | 9c12323 | 2018-10-23 10:47:17 +0800 | [diff] [blame] | 52 | std::optional<Arch> arch_from_string(const std::string& name) { |
| 53 | auto it = arch_name_map.find(name); |
| 54 | if (it == arch_name_map.end()) { |
| 55 | return std::nullopt; |
| 56 | } |
| 57 | return std::make_optional(it->second); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 58 | } |