blob: 726e2fbcf056686f41533547a670a90bafcde7c6 [file] [log] [blame]
Charles Davis54c9eb62010-11-29 19:44:50 +00001 //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===//
Mikhail Glushenkova6435d42009-02-08 21:10:57 +00002//
Daniel Dunbare52e6bf2008-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 Glushenkova6435d42009-02-08 21:10:57 +00007//
Daniel Dunbare52e6bf2008-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 Dunbar18d69592009-09-03 01:10:13 +000019#include "llvm/Config/config.h"
20#include "llvm/ADT/StringRef.h"
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000021#include "Unix.h"
22#include <sys/utsname.h>
Nick Lewycky0de20af2010-12-19 20:43:38 +000023#include <cctype>
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000024#include <string>
Bob Wilsonde9ec452011-08-09 05:13:36 +000025#include <cstdlib> // ::getenv
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000026
27using namespace llvm;
28
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000029static std::string getOSVersion() {
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000030 struct utsname info;
31
32 if (uname(&info))
33 return "";
34
35 return info.release;
36}
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000037
Sebastian Pop99ab2732012-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 Dunbar18d69592009-09-03 01:10:13 +000043 std::string Arch = ArchSplit.first;
Sebastian Pop99ab2732012-01-05 18:28:46 +000044
Daniel Dunbar18d69592009-09-03 01:10:13 +000045 std::string Triple(Arch);
46 Triple += '-';
47 Triple += ArchSplit.second;
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000048
49 // Force i<N>86 to i386.
Michael J. Spencer447762d2010-11-29 18:16:10 +000050 if (Triple[0] == 'i' && isdigit(Triple[1]) &&
Daniel Dunbarfb1a6eb2009-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 Pop94441fb2011-11-01 21:32:20 +000055 // target.
Daniel Dunbarfb1a6eb2009-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 Dunbar8991d612011-04-20 15:44:33 +000059 Triple += getOSVersion();
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000060 }
61
62 return Triple;
63}