1 /* 2 * Copyright (C) 2018 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 #include "common/libs/utils/architecture.h" 18 19 #include <sys/utsname.h> 20 21 #include <cstdlib> 22 #include <string> 23 24 #include <android-base/logging.h> 25 #include <android-base/no_destructor.h> 26 #include <android-base/strings.h> 27 28 namespace cuttlefish { 29 30 /** Returns e.g. aarch64, x86_64, etc */ HostArchStr()31const std::string& HostArchStr() { 32 static android::base::NoDestructor<std::string> arch([] { 33 utsname buf; 34 CHECK_EQ(uname(&buf), 0) << strerror(errno); 35 return std::string(buf.machine); 36 }()); 37 return *arch; 38 } 39 HostArch()40Arch HostArch() { 41 std::string arch_str = HostArchStr(); 42 if (arch_str == "aarch64" || arch_str == "arm64") { 43 return Arch::Arm64; 44 } else if (arch_str == "arm") { 45 return Arch::Arm; 46 } else if (arch_str == "riscv64") { 47 return Arch::RiscV64; 48 } else if (arch_str == "x86_64") { 49 return Arch::X86_64; 50 } else if (arch_str.size() == 4 && arch_str[0] == 'i' && arch_str[2] == '8' && 51 arch_str[3] == '6') { 52 return Arch::X86; 53 } else { 54 LOG(FATAL) << "Unknown host architecture: " << arch_str; 55 return Arch::X86; 56 } 57 } 58 IsHostCompatible(Arch arch)59bool IsHostCompatible(Arch arch) { 60 Arch host_arch = HostArch(); 61 return arch == host_arch || (arch == Arch::Arm && host_arch == Arch::Arm64) || 62 (arch == Arch::X86 && host_arch == Arch::X86_64); 63 } 64 65 } // namespace cuttlefish 66