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