blob: 0f3e31a41b92acd3b5f0e5496fbfad07c532b21d [file] [log] [blame]
Charles Davis53ca1f32010-11-29 19:44:50 +00001 //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===//
Mikhail Glushenkov0cdadd82009-02-08 21:10:57 +00002//
Daniel Dunbarbb146722008-10-02 01:17:28 +00003// 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 Glushenkov0cdadd82009-02-08 21:10:57 +00007//
Daniel Dunbarbb146722008-10-02 01:17:28 +00008//===----------------------------------------------------------------------===//
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 Dunbar70963952009-09-03 01:10:13 +000019#include "llvm/Config/config.h"
20#include "llvm/ADT/StringRef.h"
Daniel Dunbarbb146722008-10-02 01:17:28 +000021#include "Unix.h"
22#include <sys/utsname.h>
Nick Lewycky476b2422010-12-19 20:43:38 +000023#include <cctype>
Daniel Dunbarbb146722008-10-02 01:17:28 +000024#include <string>
Bob Wilsonf5b75762011-08-09 05:13:36 +000025#include <cstdlib> // ::getenv
Daniel Dunbarbb146722008-10-02 01:17:28 +000026
27using namespace llvm;
28
Daniel Dunbarbe46c792009-03-31 17:30:15 +000029static std::string getOSVersion() {
Daniel Dunbarbb146722008-10-02 01:17:28 +000030 struct utsname info;
31
Bob Wilsonf5b75762011-08-09 05:13:36 +000032 // 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
Daniel Dunbarbb146722008-10-02 01:17:28 +000037 if (uname(&info))
38 return "";
39
40 return info.release;
41}
Daniel Dunbarbe46c792009-03-31 17:30:15 +000042
43std::string sys::getHostTriple() {
Daniel Dunbar70963952009-09-03 01:10:13 +000044 // FIXME: Derive directly instead of relying on the autoconf generated
45 // variable.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000046
Daniel Dunbar70963952009-09-03 01:10:13 +000047 StringRef HostTripleString(LLVM_HOSTTRIPLE);
48 std::pair<StringRef, StringRef> ArchSplit = HostTripleString.split('-');
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000049
Daniel Dunbar70963952009-09-03 01:10:13 +000050 // Normalize the arch, since the host triple may not actually match the host.
51 std::string Arch = ArchSplit.first;
52
Daniel Dunbar70963952009-09-03 01:10:13 +000053 std::string Triple(Arch);
54 Triple += '-';
55 Triple += ArchSplit.second;
Daniel Dunbarbe46c792009-03-31 17:30:15 +000056
57 // Force i<N>86 to i386.
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000058 if (Triple[0] == 'i' && isdigit(Triple[1]) &&
Daniel Dunbarbe46c792009-03-31 17:30:15 +000059 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
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000063 // host.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000064 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
65 if (DarwinDashIdx != std::string::npos) {
66 Triple.resize(DarwinDashIdx + strlen("-darwin"));
Daniel Dunbar88c924c2011-04-20 15:44:33 +000067 Triple += getOSVersion();
Daniel Dunbarbe46c792009-03-31 17:30:15 +000068 }
69
70 return Triple;
71}