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> |
Nick Lewycky | 476b242 | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 23 | #include <cctype> |
Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 24 | #include <string> |
Bob Wilson | f5b7576 | 2011-08-09 05:13:36 +0000 | [diff] [blame] | 25 | #include <cstdlib> // ::getenv |
Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 29 | static std::string getOSVersion() { |
Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 30 | struct utsname info; |
| 31 | |
Bob Wilson | 0dc8b42 | 2011-08-09 19:54:32 +0000 | [diff] [blame] | 32 | #ifdef __APPLE__ |
Bob Wilson | 21ab6c0 | 2011-08-10 05:02:22 +0000 | [diff] [blame] | 33 | // Recognize UNAME_RELEASE environment variable to match Darwin's uname, |
| 34 | // where the value of this variable sets the OS release version |
| 35 | // reported by "uname -r". |
Bob Wilson | f5b7576 | 2011-08-09 05:13:36 +0000 | [diff] [blame] | 36 | const char *UnameOverride = ::getenv("UNAME_RELEASE"); |
| 37 | if (UnameOverride && UnameOverride[0] != '\0') |
| 38 | return UnameOverride; |
Bob Wilson | 0dc8b42 | 2011-08-09 19:54:32 +0000 | [diff] [blame] | 39 | #endif // __APPLE__ |
Bob Wilson | f5b7576 | 2011-08-09 05:13:36 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | bb14672 | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 41 | if (uname(&info)) |
| 42 | return ""; |
| 43 | |
| 44 | return info.release; |
| 45 | } |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 46 | |
| 47 | std::string sys::getHostTriple() { |
Daniel Dunbar | 7096395 | 2009-09-03 01:10:13 +0000 | [diff] [blame] | 48 | // FIXME: Derive directly instead of relying on the autoconf generated |
| 49 | // variable. |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | 7096395 | 2009-09-03 01:10:13 +0000 | [diff] [blame] | 51 | StringRef HostTripleString(LLVM_HOSTTRIPLE); |
| 52 | std::pair<StringRef, StringRef> ArchSplit = HostTripleString.split('-'); |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 53 | |
Daniel Dunbar | 7096395 | 2009-09-03 01:10:13 +0000 | [diff] [blame] | 54 | // Normalize the arch, since the host triple may not actually match the host. |
| 55 | std::string Arch = ArchSplit.first; |
| 56 | |
Daniel Dunbar | 7096395 | 2009-09-03 01:10:13 +0000 | [diff] [blame] | 57 | std::string Triple(Arch); |
| 58 | Triple += '-'; |
| 59 | Triple += ArchSplit.second; |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 60 | |
| 61 | // Force i<N>86 to i386. |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 62 | if (Triple[0] == 'i' && isdigit(Triple[1]) && |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 63 | Triple[2] == '8' && Triple[3] == '6') |
| 64 | Triple[1] = '3'; |
| 65 | |
| 66 | // 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] | 67 | // host. |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 68 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 69 | if (DarwinDashIdx != std::string::npos) { |
| 70 | Triple.resize(DarwinDashIdx + strlen("-darwin")); |
Daniel Dunbar | 88c924c | 2011-04-20 15:44:33 +0000 | [diff] [blame] | 71 | Triple += getOSVersion(); |
Daniel Dunbar | be46c79 | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return Triple; |
| 75 | } |