NAKAMURA Takumi | 3d238b4 | 2014-09-24 04:44:37 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===// |
Mikhail Glushenkov | a6435d4 | 2009-02-08 21:10:57 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Mikhail Glushenkov | a6435d4 | 2009-02-08 21:10:57 +0000 | [diff] [blame] | 6 | // |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 18 | #include "Unix.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/Config/config.h" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 21 | #include <cctype> |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 22 | #include <string> |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 23 | #include <sys/utsname.h> |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 27 | static std::string getOSVersion() { |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 28 | struct utsname info; |
| 29 | |
| 30 | if (uname(&info)) |
| 31 | return ""; |
| 32 | |
| 33 | return info.release; |
| 34 | } |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 35 | |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 36 | static std::string updateTripleOSVersion(std::string TargetTripleString) { |
| 37 | // On darwin, we want to update the version to match that of the target. |
Tim Northover | be0fda3 | 2015-02-12 15:12:13 +0000 | [diff] [blame] | 38 | std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin"); |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 39 | if (DarwinDashIdx != std::string::npos) { |
Tim Northover | be0fda3 | 2015-02-12 15:12:13 +0000 | [diff] [blame] | 40 | TargetTripleString.resize(DarwinDashIdx + strlen("-darwin")); |
| 41 | TargetTripleString += getOSVersion(); |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 42 | return TargetTripleString; |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 43 | } |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 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(); |
| 51 | } |
Hubert Tong | 72db2ab | 2019-03-13 00:12:43 +0000 | [diff] [blame] | 52 | // On AIX, the AIX version and release should be that of the current host |
| 53 | // unless if the version has already been specified. |
| 54 | if (Triple(LLVM_HOST_TRIPLE).getOS() == Triple::AIX) { |
| 55 | Triple TT(TargetTripleString); |
| 56 | if (TT.getOS() == Triple::AIX && !TT.getOSMajorVersion()) { |
| 57 | struct utsname name; |
| 58 | if (uname(&name) != -1) { |
| 59 | std::string NewOSName = Triple::getOSTypeName(Triple::AIX); |
| 60 | NewOSName += name.version; |
| 61 | NewOSName += '.'; |
| 62 | NewOSName += name.release; |
| 63 | NewOSName += ".0.0"; |
| 64 | TT.setOSName(NewOSName); |
| 65 | return TT.str(); |
| 66 | } |
| 67 | } |
| 68 | } |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 69 | return TargetTripleString; |
| 70 | } |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 71 | |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 72 | std::string sys::getDefaultTargetTriple() { |
| 73 | std::string TargetTripleString = |
| 74 | updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE); |
| 75 | |
| 76 | // Override the default target with an environment variable named by |
| 77 | // LLVM_TARGET_TRIPLE_ENV. |
NAKAMURA Takumi | fc7f3b7 | 2017-06-17 03:19:08 +0000 | [diff] [blame] | 78 | #if defined(LLVM_TARGET_TRIPLE_ENV) |
| 79 | if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV)) |
| 80 | TargetTripleString = EnvTriple; |
| 81 | #endif |
| 82 | |
Petr Hosek | f92ca01 | 2018-05-25 20:39:37 +0000 | [diff] [blame] | 83 | return TargetTripleString; |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 84 | } |