blob: 519f2a1fe68779e9193a6023a979a66abbd2cf6a [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 Dunbare52e6bf2008-10-02 01:17:28 +000019#include "Unix.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/ADT/StringRef.h"
21#include "llvm/Config/config.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000022#include <cctype>
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000023#include <string>
Chandler Carruthd9903882015-01-14 11:23:27 +000024#include <sys/utsname.h>
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000025
26using namespace llvm;
27
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000028static std::string getOSVersion() {
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000029 struct utsname info;
30
31 if (uname(&info))
32 return "";
33
34 return info.release;
35}
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000036
Sebastian Pop99ab2732012-01-05 18:28:46 +000037std::string sys::getDefaultTargetTriple() {
38 StringRef TargetTripleString(LLVM_DEFAULT_TARGET_TRIPLE);
39 std::pair<StringRef, StringRef> ArchSplit = TargetTripleString.split('-');
40
NAKAMURA Takumi12abbda2014-09-24 04:44:50 +000041 // Normalize the arch, since the target triple may not actually match the
42 // 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
Saleem Abdulrasoolceec2cb2014-03-30 03:22:37 +000062 return Triple::normalize(Triple);
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000063}