108 lines · plain
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the UNIX Host support.10//11//===----------------------------------------------------------------------===//12 13//===----------------------------------------------------------------------===//14//=== WARNING: Implementation here must contain only generic UNIX code that15//=== 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_H25#include <unistd.h>26#endif27 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 the51 // macOS version scheme.52 TargetTripleString += "-darwin";53 TargetTripleString += getOSVersion();54 }55 // On AIX, the AIX version and release should be that of the current host56 // 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 release = name.release;63 64 if (strcmp(name.sysname, "OS400") == 0) {65 /*66 PASE uses different versioning system than AIX.67 The following table shows the currently supported PASE68 releases and the corresponding AIX release:69 --------------------------70 PASE | AIX71 --------------------------72 V7R4 | 7.2 (TL2)73 --------------------------74 V7R5 | 7.2 (TL5)75 --------------------------76 V7R6 | 7.3 (TL1)77 --------------------------78 */79 release = (release == "4" || release == "5") ? "2" : "3";80 }81 82 std::string NewOSName = std::string(Triple::getOSTypeName(Triple::AIX));83 NewOSName += name.version;84 NewOSName += '.';85 NewOSName += release;86 NewOSName += ".0.0";87 TT.setOSName(NewOSName);88 return TT.str();89 }90 }91 }92 return TargetTripleString;93}94 95std::string sys::getDefaultTargetTriple() {96 std::string TargetTripleString =97 updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE);98 99 // Override the default target with an environment variable named by100 // LLVM_TARGET_TRIPLE_ENV.101#if defined(LLVM_TARGET_TRIPLE_ENV)102 if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV))103 TargetTripleString = EnvTriple;104#endif105 106 return TargetTripleString;107}108