1 //===- llvm/TargetParser/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 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Config/config.h"
22 #include <sys/utsname.h>
30 static std::string getOSVersion() {
39 static std::string updateTripleOSVersion(std::string TargetTripleString) {
40 // On darwin, we want to update the version to match that of the target.
41 std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin");
42 if (DarwinDashIdx != std::string::npos) {
43 TargetTripleString.resize(DarwinDashIdx + strlen("-darwin"));
44 TargetTripleString += getOSVersion();
45 return TargetTripleString;
47 std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos");
48 if (MacOSDashIdx != std::string::npos) {
49 TargetTripleString.resize(MacOSDashIdx);
50 // Reset the OS to darwin as the OS version from `uname` doesn't use the
51 // macOS version scheme.
52 TargetTripleString += "-darwin";
53 TargetTripleString += getOSVersion();
55 // On AIX, the AIX version and release should be that of the current host
56 // unless if the version has already been specified.
57 if (Triple(LLVM_HOST_TRIPLE).getOS() == Triple::AIX) {
58 Triple TT(TargetTripleString);
59 if (TT.getOS() == Triple::AIX && !TT.getOSMajorVersion()) {
61 if (uname(&name) != -1) {
62 std::string NewOSName = std::string(Triple::getOSTypeName(Triple::AIX));
63 NewOSName += name.version;
65 NewOSName += name.release;
67 TT.setOSName(NewOSName);
72 return TargetTripleString;
75 std::string sys::getDefaultTargetTriple() {
76 std::string TargetTripleString =
77 updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE);
79 // Override the default target with an environment variable named by
80 // LLVM_TARGET_TRIPLE_ENV.
81 #if defined(LLVM_TARGET_TRIPLE_ENV)
82 if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV))
83 TargetTripleString = EnvTriple;
86 return TargetTripleString;