0f3e31a41b92acd3b5f0e5496fbfad07c532b21d
[oota-llvm.git] / lib / Support / Unix / Host.inc
1  //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===//
2 //
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.
7 //
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
19 #include "llvm/Config/config.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "Unix.h"
22 #include <sys/utsname.h>
23 #include <cctype>
24 #include <string>
25 #include <cstdlib> // ::getenv
26
27 using namespace llvm;
28
29 static std::string getOSVersion() {
30   struct utsname info;
31
32   // Recognize UNAME_RELEASE environment variable to match Darwin uname.
33   const char *UnameOverride = ::getenv("UNAME_RELEASE");
34   if (UnameOverride && UnameOverride[0] != '\0')
35     return UnameOverride;
36
37   if (uname(&info))
38     return "";
39
40   return info.release;
41 }
42
43 std::string sys::getHostTriple() {
44   // FIXME: Derive directly instead of relying on the autoconf generated
45   // variable.
46
47   StringRef HostTripleString(LLVM_HOSTTRIPLE);
48   std::pair<StringRef, StringRef> ArchSplit = HostTripleString.split('-');
49
50   // Normalize the arch, since the host triple may not actually match the host.
51   std::string Arch = ArchSplit.first;
52
53   std::string Triple(Arch);
54   Triple += '-';
55   Triple += ArchSplit.second;
56
57   // Force i<N>86 to i386.
58   if (Triple[0] == 'i' && isdigit(Triple[1]) &&
59       Triple[2] == '8' && Triple[3] == '6')
60     Triple[1] = '3';
61
62   // On darwin, we want to update the version to match that of the
63   // host.
64   std::string::size_type DarwinDashIdx = Triple.find("-darwin");
65   if (DarwinDashIdx != std::string::npos) {
66     Triple.resize(DarwinDashIdx + strlen("-darwin"));
67     Triple += getOSVersion();
68   }
69
70   return Triple;
71 }