blob: 726e2fbcf056686f41533547a670a90bafcde7c6 [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
32 if (uname(&info))
33 return "";
34
35 return info.release;
36}
Daniel Dunbarbe46c792009-03-31 17:30:15 +000037
Sebastian Pop6e9b5eb2012-01-05 18:28:46 +000038std::string sys::getDefaultTargetTriple() {
39 StringRef TargetTripleString(LLVM_DEFAULT_TARGET_TRIPLE);
40 std::pair<StringRef, StringRef> ArchSplit = TargetTripleString.split('-');
41
42 // Normalize the arch, since the target triple may not actually match the target.
Daniel Dunbar70963952009-09-03 01:10:13 +000043 std::string Arch = ArchSplit.first;
Sebastian Pop6e9b5eb2012-01-05 18:28:46 +000044
Daniel Dunbar70963952009-09-03 01:10:13 +000045 std::string Triple(Arch);
46 Triple += '-';
47 Triple += ArchSplit.second;
Daniel Dunbarbe46c792009-03-31 17:30:15 +000048
49 // Force i<N>86 to i386.
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000050 if (Triple[0] == 'i' && isdigit(Triple[1]) &&
Daniel Dunbarbe46c792009-03-31 17:30:15 +000051 Triple[2] == '8' && Triple[3] == '6')
52 Triple[1] = '3';
53
54 // On darwin, we want to update the version to match that of the
Sebastian Pop01738642011-11-01 21:32:20 +000055 // target.
Daniel Dunbarbe46c792009-03-31 17:30:15 +000056 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
57 if (DarwinDashIdx != std::string::npos) {
58 Triple.resize(DarwinDashIdx + strlen("-darwin"));
Daniel Dunbar88c924c2011-04-20 15:44:33 +000059 Triple += getOSVersion();
Daniel Dunbarbe46c792009-03-31 17:30:15 +000060 }
61
62 return Triple;
63}