/home/arjun/llvm-project/llvm/lib/Support/Unix/Host.inc
Line | Count | Source (jump to first uncovered line) |
1 | | //===- llvm/Support/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 "Unix.h" |
19 | | #include "llvm/ADT/StringRef.h" |
20 | | #include "llvm/Config/config.h" |
21 | | #include <cctype> |
22 | | #include <string> |
23 | | #include <sys/utsname.h> |
24 | | |
25 | | using namespace llvm; |
26 | | |
27 | 0 | static std::string getOSVersion() { |
28 | 0 | struct utsname info; |
29 | 0 |
|
30 | 0 | if (uname(&info)) |
31 | 0 | return ""; |
32 | 0 | |
33 | 0 | return info.release; |
34 | 0 | } |
35 | | |
36 | 2 | static std::string updateTripleOSVersion(std::string TargetTripleString) { |
37 | 2 | // On darwin, we want to update the version to match that of the target. |
38 | 2 | std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin"); |
39 | 2 | if (DarwinDashIdx != std::string::npos) { |
40 | 0 | TargetTripleString.resize(DarwinDashIdx + strlen("-darwin")); |
41 | 0 | TargetTripleString += getOSVersion(); |
42 | 0 | return TargetTripleString; |
43 | 0 | } |
44 | 2 | std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos"); |
45 | 2 | if (MacOSDashIdx != std::string::npos) { |
46 | 0 | TargetTripleString.resize(MacOSDashIdx); |
47 | 0 | // Reset the OS to darwin as the OS version from `uname` doesn't use the |
48 | 0 | // macOS version scheme. |
49 | 0 | TargetTripleString += "-darwin"; |
50 | 0 | TargetTripleString += getOSVersion(); |
51 | 0 | } |
52 | 2 | // On AIX, the AIX version and release should be that of the current host |
53 | 2 | // unless if the version has already been specified. |
54 | 2 | if (Triple(LLVM_HOST_TRIPLE).getOS() == Triple::AIX) { |
55 | 0 | Triple TT(TargetTripleString); |
56 | 0 | if (TT.getOS() == Triple::AIX && !TT.getOSMajorVersion()) { |
57 | 0 | struct utsname name; |
58 | 0 | if (uname(&name) != -1) { |
59 | 0 | std::string NewOSName = std::string(Triple::getOSTypeName(Triple::AIX)); |
60 | 0 | NewOSName += name.version; |
61 | 0 | NewOSName += '.'; |
62 | 0 | NewOSName += name.release; |
63 | 0 | NewOSName += ".0.0"; |
64 | 0 | TT.setOSName(NewOSName); |
65 | 0 | return TT.str(); |
66 | 0 | } |
67 | 2 | } |
68 | 0 | } |
69 | 2 | return TargetTripleString; |
70 | 2 | } |
71 | | |
72 | 0 | std::string sys::getDefaultTargetTriple() { |
73 | 0 | std::string TargetTripleString = |
74 | 0 | updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE); |
75 | 0 |
|
76 | 0 | // Override the default target with an environment variable named by |
77 | 0 | // LLVM_TARGET_TRIPLE_ENV. |
78 | | #if defined(LLVM_TARGET_TRIPLE_ENV) |
79 | | if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV)) |
80 | | TargetTripleString = EnvTriple; |
81 | | #endif |
82 | |
|
83 | 0 | return TargetTripleString; |
84 | 0 | } |