1 //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the UNIX Host support.
11 //===----------------------------------------------------------------------===//
13 //===----------------------------------------------------------------------===//
14 //=== WARNING: Implementation here must contain only generic UNIX code that
15 //=== is guaranteed to work on *all* UNIX variants.
16 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Config/config.h"
23 #include <sys/utsname.h>
27 static std::string getOSVersion() {
36 static std::string updateTripleOSVersion(std::string TargetTripleString) {
37 // On darwin, we want to update the version to match that of the target.
38 std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin");
39 if (DarwinDashIdx != std::string::npos) {
40 TargetTripleString.resize(DarwinDashIdx + strlen("-darwin"));
41 TargetTripleString += getOSVersion();
42 return TargetTripleString;
44 std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos");
45 if (MacOSDashIdx != std::string::npos) {
46 TargetTripleString.resize(MacOSDashIdx);
47 // Reset the OS to darwin as the OS version from `uname` doesn't use the
48 // macOS version scheme.
49 TargetTripleString += "-darwin";
50 TargetTripleString += getOSVersion();
52 return TargetTripleString;
55 std::string sys::getDefaultTargetTriple() {
56 std::string TargetTripleString =
57 updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE);
59 // Override the default target with an environment variable named by
60 // LLVM_TARGET_TRIPLE_ENV.
61 #if defined(LLVM_TARGET_TRIPLE_ENV)
62 if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV))
63 TargetTripleString = EnvTriple;
66 return TargetTripleString;