blob: 5580e63893c6da75e888cde30523bafb2198fd09 [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
Alex Lorenz3803df32017-07-07 09:53:47 +000037static std::string updateTripleOSVersion(std::string TargetTripleString) {
38 // On darwin, we want to update the version to match that of the target.
Tim Northoverbe0fda32015-02-12 15:12:13 +000039 std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin");
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000040 if (DarwinDashIdx != std::string::npos) {
Tim Northoverbe0fda32015-02-12 15:12:13 +000041 TargetTripleString.resize(DarwinDashIdx + strlen("-darwin"));
42 TargetTripleString += getOSVersion();
Alex Lorenz3803df32017-07-07 09:53:47 +000043 return TargetTripleString;
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000044 }
Alex Lorenz3803df32017-07-07 09:53:47 +000045 std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos");
46 if (MacOSDashIdx != std::string::npos) {
47 TargetTripleString.resize(MacOSDashIdx);
48 // Reset the OS to darwin as the OS version from `uname` doesn't use the
49 // macOS version scheme.
50 TargetTripleString += "-darwin";
51 TargetTripleString += getOSVersion();
52 }
53 return TargetTripleString;
54}
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000055
Alex Lorenz3803df32017-07-07 09:53:47 +000056std::string sys::getDefaultTargetTriple() {
57 std::string TargetTripleString =
58 updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE);
59
60 // Override the default target with an environment variable named by
61 // LLVM_TARGET_TRIPLE_ENV.
NAKAMURA Takumifc7f3b72017-06-17 03:19:08 +000062#if defined(LLVM_TARGET_TRIPLE_ENV)
63 if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV))
64 TargetTripleString = EnvTriple;
65#endif
66
Tim Northoverbe0fda32015-02-12 15:12:13 +000067 return Triple::normalize(TargetTripleString);
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000068}