blob: 744fb9e699699a53b67aebb4a0b0c467ce1b97e7 [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 Wilson0dc8b422011-08-09 19:54:32 +000032#ifdef __APPLE__
Bob Wilsonf5b75762011-08-09 05:13:36 +000033 // 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 Wilson0dc8b422011-08-09 19:54:32 +000037#endif // __APPLE__
Bob Wilsonf5b75762011-08-09 05:13:36 +000038
Daniel Dunbarbb146722008-10-02 01:17:28 +000039 if (uname(&info))
40 return "";
41
42 return info.release;
43}
Daniel Dunbarbe46c792009-03-31 17:30:15 +000044
45std::string sys::getHostTriple() {
Daniel Dunbar70963952009-09-03 01:10:13 +000046 // FIXME: Derive directly instead of relying on the autoconf generated
47 // variable.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000048
Daniel Dunbar70963952009-09-03 01:10:13 +000049 StringRef HostTripleString(LLVM_HOSTTRIPLE);
50 std::pair<StringRef, StringRef> ArchSplit = HostTripleString.split('-');
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000051
Daniel Dunbar70963952009-09-03 01:10:13 +000052 // Normalize the arch, since the host triple may not actually match the host.
53 std::string Arch = ArchSplit.first;
54
Daniel Dunbar70963952009-09-03 01:10:13 +000055 std::string Triple(Arch);
56 Triple += '-';
57 Triple += ArchSplit.second;
Daniel Dunbarbe46c792009-03-31 17:30:15 +000058
59 // Force i<N>86 to i386.
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000060 if (Triple[0] == 'i' && isdigit(Triple[1]) &&
Daniel Dunbarbe46c792009-03-31 17:30:15 +000061 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. Spencer1f6efa32010-11-29 18:16:10 +000065 // host.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000066 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
67 if (DarwinDashIdx != std::string::npos) {
68 Triple.resize(DarwinDashIdx + strlen("-darwin"));
Daniel Dunbar88c924c2011-04-20 15:44:33 +000069 Triple += getOSVersion();
Daniel Dunbarbe46c792009-03-31 17:30:15 +000070 }
71
72 return Triple;
73}