blob: ed74b6759901adbacd222622ef55c1048024c46e [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
47 // It would be nice to do this in terms of llvm::Triple, but that is in
48 // Support which is layered above us.
49#if defined(__x86_64__)
50 Arch = "x86_64";
51#elif defined(__i386__)
52 Arch = "i386";
53#elif defined(__ppc64__)
54 Arch = "powerpc64";
55#elif defined(__ppc__)
56 Arch = "powerpc";
57#elif defined(__arm__)
58
59 // FIXME: We need to pick the right ARM triple (which involves querying the
60 // chip). However, for now this is most important for LLVM arch selection, so
61 // we only need to make sure to distinguish ARM and Thumb.
62# if defined(__thumb__)
63 Arch = "thumb";
64# else
65 Arch = "arm";
66# endif
67
68#else
69
70 // FIXME: When enough auto-detection is in place, this should just
71 // #error. Then at least the arch selection is done, and we only need the OS
72 // etc selection to kill off the use of LLVM_HOSTTRIPLE.
73
74#endif
75
76 std::string Triple(Arch);
77 Triple += '-';
78 Triple += ArchSplit.second;
Daniel Dunbarbe46c792009-03-31 17:30:15 +000079
80 // Force i<N>86 to i386.
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000081 if (Triple[0] == 'i' && isdigit(Triple[1]) &&
Daniel Dunbarbe46c792009-03-31 17:30:15 +000082 Triple[2] == '8' && Triple[3] == '6')
83 Triple[1] = '3';
84
85 // On darwin, we want to update the version to match that of the
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000086 // host.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000087 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
88 if (DarwinDashIdx != std::string::npos) {
89 Triple.resize(DarwinDashIdx + strlen("-darwin"));
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000090
Daniel Dunbarbe46c792009-03-31 17:30:15 +000091 // Only add the major part of the os version.
92 std::string Version = getOSVersion();
93 Triple += Version.substr(0, Version.find('.'));
94 }
95
96 return Triple;
97}