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 | // |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Mikhail Glushenkov | a6435d4 | 2009-02-08 21:10:57 +0000 | [diff] [blame] | 7 | // |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the UNIX Host support. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 16 | //=== is guaranteed to work on *all* UNIX variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 19 | #include "Unix.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/Config/config.h" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 22 | #include <cctype> |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 23 | #include <string> |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 24 | #include <sys/utsname.h> |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 28 | static std::string getOSVersion() { |
Daniel Dunbar | e52e6bf | 2008-10-02 01:17:28 +0000 | [diff] [blame] | 29 | struct utsname info; |
| 30 | |
| 31 | if (uname(&info)) |
| 32 | return ""; |
| 33 | |
| 34 | return info.release; |
| 35 | } |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 36 | |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 37 | static std::string updateTripleOSVersion(std::string TargetTripleString) { |
| 38 | // 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] | 39 | std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin"); |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 40 | if (DarwinDashIdx != std::string::npos) { |
Tim Northover | be0fda3 | 2015-02-12 15:12:13 +0000 | [diff] [blame] | 41 | TargetTripleString.resize(DarwinDashIdx + strlen("-darwin")); |
| 42 | TargetTripleString += getOSVersion(); |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 43 | return TargetTripleString; |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 44 | } |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 45 | std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos"); |
| 46 | if (MacOSDashIdx != std::string::npos) { |
| 47 | TargetTripleString.resize(MacOSDashIdx); |
| 48 | // Reset the OS to darwin as the OS version from `uname` doesn't use the |
| 49 | // macOS version scheme. |
| 50 | TargetTripleString += "-darwin"; |
| 51 | TargetTripleString += getOSVersion(); |
| 52 | } |
| 53 | return TargetTripleString; |
| 54 | } |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 55 | |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 56 | std::string sys::getDefaultTargetTriple() { |
| 57 | std::string TargetTripleString = |
| 58 | updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE); |
| 59 | |
| 60 | // Override the default target with an environment variable named by |
| 61 | // LLVM_TARGET_TRIPLE_ENV. |
NAKAMURA Takumi | fc7f3b7 | 2017-06-17 03:19:08 +0000 | [diff] [blame] | 62 | #if defined(LLVM_TARGET_TRIPLE_ENV) |
| 63 | if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV)) |
| 64 | TargetTripleString = EnvTriple; |
| 65 | #endif |
| 66 | |
Tim Northover | be0fda3 | 2015-02-12 15:12:13 +0000 | [diff] [blame] | 67 | return Triple::normalize(TargetTripleString); |
Daniel Dunbar | fb1a6eb | 2009-03-31 17:30:15 +0000 | [diff] [blame] | 68 | } |