]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Support/Unix/Host.inc
Import CK as of commit b19ed4c6a56ec93215ab567ba18ba61bf1cfbac8
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Support / Unix / Host.inc
1 //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
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
19 #include "Unix.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Config/config.h"
22 #include <cctype>
23 #include <string>
24 #include <sys/utsname.h>
25
26 using namespace llvm;
27
28 static std::string getOSVersion() {
29   struct utsname info;
30
31   if (uname(&info))
32     return "";
33
34   return info.release;
35 }
36
37 static std::string updateTripleOSVersion(std::string TargetTripleString) {
38   // On darwin, we want to update the version to match that of the target.
39   std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin");
40   if (DarwinDashIdx != std::string::npos) {
41     TargetTripleString.resize(DarwinDashIdx + strlen("-darwin"));
42     TargetTripleString += getOSVersion();
43     return TargetTripleString;
44   }
45   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 }
55
56 std::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.
62 #if defined(LLVM_TARGET_TRIPLE_ENV)
63   if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV))
64     TargetTripleString = EnvTriple;
65 #endif
66
67   return Triple::normalize(TargetTripleString);
68 }