blob: 5fd0e5e0790b2745fec51c35ab060c0f83bb673f [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>
25
26using namespace llvm;
27
Daniel Dunbarbe46c792009-03-31 17:30:15 +000028static std::string getOSVersion() {
Daniel Dunbarbb146722008-10-02 01:17:28 +000029 struct utsname info;
30
31 if (uname(&info))
32 return "";
33
34 return info.release;
35}
Daniel Dunbarbe46c792009-03-31 17:30:15 +000036
37std::string sys::getHostTriple() {
Daniel Dunbar70963952009-09-03 01:10:13 +000038 // FIXME: Derive directly instead of relying on the autoconf generated
39 // variable.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000040
Daniel Dunbar70963952009-09-03 01:10:13 +000041 StringRef HostTripleString(LLVM_HOSTTRIPLE);
42 std::pair<StringRef, StringRef> ArchSplit = HostTripleString.split('-');
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000043
Daniel Dunbar70963952009-09-03 01:10:13 +000044 // Normalize the arch, since the host triple may not actually match the host.
45 std::string Arch = ArchSplit.first;
46
Daniel Dunbar70963952009-09-03 01:10:13 +000047 std::string Triple(Arch);
48 Triple += '-';
49 Triple += ArchSplit.second;
Daniel Dunbarbe46c792009-03-31 17:30:15 +000050
51 // Force i<N>86 to i386.
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000052 if (Triple[0] == 'i' && isdigit(Triple[1]) &&
Daniel Dunbarbe46c792009-03-31 17:30:15 +000053 Triple[2] == '8' && Triple[3] == '6')
54 Triple[1] = '3';
55
56 // On darwin, we want to update the version to match that of the
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000057 // host.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000058 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
59 if (DarwinDashIdx != std::string::npos) {
60 Triple.resize(DarwinDashIdx + strlen("-darwin"));
Daniel Dunbar88c924c2011-04-20 15:44:33 +000061 Triple += getOSVersion();
Daniel Dunbarbe46c792009-03-31 17:30:15 +000062 }
63
64 return Triple;
65}