1 //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the UNIX Host support.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Config/config.h"
24 #include <sys/utsname.h>
28 static std::string getOSVersion() {
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;
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();
53 return TargetTripleString;
56 std::string sys::getDefaultTargetTriple() {
57 std::string TargetTripleString =
58 updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE);
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;
67 return TargetTripleString;