Charles Davis | 53ca1f3 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===// |
Mikhail Glushenkov | 0cdadd8 | 2009-02-08 21:10:57 +0000 | [diff] [blame] | 2 | // |
Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Mikhail Glushenkov | 0cdadd8 | 2009-02-08 21:10:57 +0000 | [diff] [blame] | 7 | // |
Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the UNIX Host support. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 16 | //=== is guaranteed to work on *all* UNIX variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Daniel Dunbar | 7096395 | 2009-09-03 01:10:13 +0000 | [diff] [blame] | 19 | #include "llvm/Config/config.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 21 | #include "Unix.h" |
| 22 | #include <sys/utsname.h> |
| 23 | #include <string> |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 27 | static std::string getOSVersion() { |
Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 28 | struct utsname info; |
| 29 | |
| 30 | if (uname(&info)) |
| 31 | return ""; |
| 32 | |
| 33 | return info.release; |
| 34 | } |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 35 | |
| 36 | std::string sys::getHostTriple() { |
Daniel Dunbar | 7096395 | 2009-09-03 01:10:13 +0000 | [diff] [blame] | 37 | // FIXME: Derive directly instead of relying on the autoconf generated |
| 38 | // variable. |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 39 | |
Daniel Dunbar | 7096395 | 2009-09-03 01:10:13 +0000 | [diff] [blame] | 40 | StringRef HostTripleString(LLVM_HOSTTRIPLE); |
| 41 | std::pair<StringRef, StringRef> ArchSplit = HostTripleString.split('-'); |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 42 | |
Daniel Dunbar | 7096395 | 2009-09-03 01:10:13 +0000 | [diff] [blame] | 43 | // Normalize the arch, since the host triple may not actually match the host. |
| 44 | std::string Arch = ArchSplit.first; |
| 45 | |
| 46 | // It would be nice to do this in terms of llvm::Triple, but that is in |
| 47 | // Support which is layered above us. |
| 48 | #if defined(__x86_64__) |
| 49 | Arch = "x86_64"; |
| 50 | #elif defined(__i386__) |
| 51 | Arch = "i386"; |
| 52 | #elif defined(__ppc64__) |
| 53 | Arch = "powerpc64"; |
| 54 | #elif defined(__ppc__) |
| 55 | Arch = "powerpc"; |
| 56 | #elif defined(__arm__) |
| 57 | |
| 58 | // FIXME: We need to pick the right ARM triple (which involves querying the |
| 59 | // chip). However, for now this is most important for LLVM arch selection, so |
| 60 | // we only need to make sure to distinguish ARM and Thumb. |
| 61 | # if defined(__thumb__) |
| 62 | Arch = "thumb"; |
| 63 | # else |
| 64 | Arch = "arm"; |
| 65 | # endif |
| 66 | |
| 67 | #else |
| 68 | |
| 69 | // FIXME: When enough auto-detection is in place, this should just |
| 70 | // #error. Then at least the arch selection is done, and we only need the OS |
| 71 | // etc selection to kill off the use of LLVM_HOSTTRIPLE. |
| 72 | |
| 73 | #endif |
| 74 | |
| 75 | std::string Triple(Arch); |
| 76 | Triple += '-'; |
| 77 | Triple += ArchSplit.second; |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 78 | |
| 79 | // Force i<N>86 to i386. |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 80 | if (Triple[0] == 'i' && isdigit(Triple[1]) && |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 81 | Triple[2] == '8' && Triple[3] == '6') |
| 82 | Triple[1] = '3'; |
| 83 | |
| 84 | // On darwin, we want to update the version to match that of the |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 85 | // host. |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 86 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 87 | if (DarwinDashIdx != std::string::npos) { |
| 88 | Triple.resize(DarwinDashIdx + strlen("-darwin")); |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 89 | |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 90 | // Only add the major part of the os version. |
| 91 | std::string Version = getOSVersion(); |
| 92 | Triple += Version.substr(0, Version.find('.')); |
| 93 | } |
| 94 | |
| 95 | return Triple; |
| 96 | } |