blob: ce21d358aab8bc99d5a3c7061e2f0ca4609f7916 [file] [log] [blame]
NAKAMURA Takumi3d238b42014-09-24 04:44:37 +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
NAKAMURA Takumi12abbda2014-09-24 04:44:50 +000042 // Normalize the arch, since the target triple may not actually match the
43 // target.
Daniel Dunbar18d69592009-09-03 01:10:13 +000044 std::string Arch = ArchSplit.first;
Sebastian Pop99ab2732012-01-05 18:28:46 +000045
Daniel Dunbar18d69592009-09-03 01:10:13 +000046 std::string Triple(Arch);
47 Triple += '-';
48 Triple += ArchSplit.second;
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000049
50 // Force i<N>86 to i386.
Michael J. Spencer447762d2010-11-29 18:16:10 +000051 if (Triple[0] == 'i' && isdigit(Triple[1]) &&
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000052 Triple[2] == '8' && Triple[3] == '6')
53 Triple[1] = '3';
54
55 // On darwin, we want to update the version to match that of the
Sebastian Pop94441fb2011-11-01 21:32:20 +000056 // target.
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000057 std::string::size_type DarwinDashIdx = Triple.find("-darwin");
58 if (DarwinDashIdx != std::string::npos) {
59 Triple.resize(DarwinDashIdx + strlen("-darwin"));
Daniel Dunbar8991d612011-04-20 15:44:33 +000060 Triple += getOSVersion();
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000061 }
62
Saleem Abdulrasoolceec2cb2014-03-30 03:22:37 +000063 return Triple::normalize(Triple);
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000064}