blob: c5de97f73c9fe16bb8057aa95f14fc9b1e43b32a [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 Wilson21ab6c02011-08-10 05:02:22 +000033 // 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 Wilsonf5b75762011-08-09 05:13:36 +000036 const char *UnameOverride = ::getenv("UNAME_RELEASE");
37 if (UnameOverride && UnameOverride[0] != '\0')
38 return UnameOverride;
Bob Wilson0dc8b422011-08-09 19:54:32 +000039#endif // __APPLE__
Bob Wilsonf5b75762011-08-09 05:13:36 +000040
Daniel Dunbarbb146722008-10-02 01:17:28 +000041 if (uname(&info))
42 return "";
43
44 return info.release;
45}
Daniel Dunbarbe46c792009-03-31 17:30:15 +000046
47std::string sys::getHostTriple() {
Daniel Dunbar70963952009-09-03 01:10:13 +000048 // FIXME: Derive directly instead of relying on the autoconf generated
49 // variable.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000050
Daniel Dunbar70963952009-09-03 01:10:13 +000051 StringRef HostTripleString(LLVM_HOSTTRIPLE);
52 std::pair<StringRef, StringRef> ArchSplit = HostTripleString.split('-');
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000053
Daniel Dunbar70963952009-09-03 01:10:13 +000054 // Normalize the arch, since the host triple may not actually match the host.
55 std::string Arch = ArchSplit.first;
56
Daniel Dunbar70963952009-09-03 01:10:13 +000057 std::string Triple(Arch);
58 Triple += '-';
59 Triple += ArchSplit.second;
Daniel Dunbarbe46c792009-03-31 17:30:15 +000060
61 // Force i<N>86 to i386.
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000062 if (Triple[0] == 'i' && isdigit(Triple[1]) &&
Daniel Dunbarbe46c792009-03-31 17:30:15 +000063 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. Spencer1f6efa32010-11-29 18:16:10 +000067 // host.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000068 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
69 if (DarwinDashIdx != std::string::npos) {
70 Triple.resize(DarwinDashIdx + strlen("-darwin"));
Daniel Dunbar88c924c2011-04-20 15:44:33 +000071 Triple += getOSVersion();
Daniel Dunbarbe46c792009-03-31 17:30:15 +000072 }
73
74 return Triple;
75}