blob: 17d78dc18be758f490fe0b82f7e95d5daa5ca5b2 [file] [log] [blame]
NAKAMURA Takumi3d238b42014-09-24 04:44:37 +00001//===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===//
Mikhail Glushenkova6435d42009-02-08 21:10:57 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Glushenkova6435d42009-02-08 21:10:57 +00006//
Daniel Dunbare52e6bf2008-10-02 01:17:28 +00007//===----------------------------------------------------------------------===//
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 Dunbare52e6bf2008-10-02 01:17:28 +000018#include "Unix.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000019#include "llvm/ADT/StringRef.h"
20#include "llvm/Config/config.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000021#include <cctype>
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000022#include <string>
Chandler Carruthd9903882015-01-14 11:23:27 +000023#include <sys/utsname.h>
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000024
25using namespace llvm;
26
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000027static std::string getOSVersion() {
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000028 struct utsname info;
29
30 if (uname(&info))
31 return "";
32
33 return info.release;
34}
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000035
Alex Lorenz3803df32017-07-07 09:53:47 +000036static std::string updateTripleOSVersion(std::string TargetTripleString) {
37 // On darwin, we want to update the version to match that of the target.
Tim Northoverbe0fda32015-02-12 15:12:13 +000038 std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin");
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000039 if (DarwinDashIdx != std::string::npos) {
Tim Northoverbe0fda32015-02-12 15:12:13 +000040 TargetTripleString.resize(DarwinDashIdx + strlen("-darwin"));
41 TargetTripleString += getOSVersion();
Alex Lorenz3803df32017-07-07 09:53:47 +000042 return TargetTripleString;
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000043 }
Alex Lorenz3803df32017-07-07 09:53:47 +000044 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 Tong72db2ab2019-03-13 00:12:43 +000052 // 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 Lorenz3803df32017-07-07 09:53:47 +000069 return TargetTripleString;
70}
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000071
Alex Lorenz3803df32017-07-07 09:53:47 +000072std::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 Takumifc7f3b72017-06-17 03:19:08 +000078#if defined(LLVM_TARGET_TRIPLE_ENV)
79 if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV))
80 TargetTripleString = EnvTriple;
81#endif
82
Petr Hosekf92ca012018-05-25 20:39:37 +000083 return TargetTripleString;
Daniel Dunbarfb1a6eb2009-03-31 17:30:15 +000084}