xref: /freebsd/contrib/llvm-project/llvm/lib/TargetParser/Unix/Host.inc (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1//===- llvm/TargetParser/Unix/Host.inc --------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the UNIX Host support.
10//
11//===----------------------------------------------------------------------===//
12
13//===----------------------------------------------------------------------===//
14//=== WARNING: Implementation here must contain only generic UNIX code that
15//===          is guaranteed to work on *all* UNIX variants.
16//===----------------------------------------------------------------------===//
17
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Config/config.h"
20#include <cctype>
21#include <string>
22#include <sys/utsname.h>
23
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
27
28using namespace llvm;
29
30static std::string getOSVersion() {
31  struct utsname info;
32
33  if (uname(&info))
34    return "";
35
36  return info.release;
37}
38
39static 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;
46  }
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();
54  }
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()) {
60      struct utsname name;
61      if (uname(&name) != -1) {
62        std::string NewOSName = std::string(Triple::getOSTypeName(Triple::AIX));
63        NewOSName += name.version;
64        NewOSName += '.';
65        NewOSName += name.release;
66        NewOSName += ".0.0";
67        TT.setOSName(NewOSName);
68        return TT.str();
69      }
70    }
71  }
72  return TargetTripleString;
73}
74
75std::string sys::getDefaultTargetTriple() {
76  std::string TargetTripleString =
77      updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE);
78
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;
84#endif
85
86  return TargetTripleString;
87}
88