1*bdd1243dSDimitry Andric//===- llvm/TargetParser/Unix/Host.inc --------------------------*- C++ -*-===// 2*bdd1243dSDimitry Andric// 3*bdd1243dSDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*bdd1243dSDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 5*bdd1243dSDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*bdd1243dSDimitry Andric// 7*bdd1243dSDimitry Andric//===----------------------------------------------------------------------===// 8*bdd1243dSDimitry Andric// 9*bdd1243dSDimitry Andric// This file implements the UNIX Host support. 10*bdd1243dSDimitry Andric// 11*bdd1243dSDimitry Andric//===----------------------------------------------------------------------===// 12*bdd1243dSDimitry Andric 13*bdd1243dSDimitry Andric//===----------------------------------------------------------------------===// 14*bdd1243dSDimitry Andric//=== WARNING: Implementation here must contain only generic UNIX code that 15*bdd1243dSDimitry Andric//=== is guaranteed to work on *all* UNIX variants. 16*bdd1243dSDimitry Andric//===----------------------------------------------------------------------===// 17*bdd1243dSDimitry Andric 18*bdd1243dSDimitry Andric#include "llvm/ADT/StringRef.h" 19*bdd1243dSDimitry Andric#include "llvm/Config/config.h" 20*bdd1243dSDimitry Andric#include <cctype> 21*bdd1243dSDimitry Andric#include <string> 22*bdd1243dSDimitry Andric#include <sys/utsname.h> 23*bdd1243dSDimitry Andric 24*bdd1243dSDimitry Andric#ifdef HAVE_UNISTD_H 25*bdd1243dSDimitry Andric#include <unistd.h> 26*bdd1243dSDimitry Andric#endif 27*bdd1243dSDimitry Andric 28*bdd1243dSDimitry Andricusing namespace llvm; 29*bdd1243dSDimitry Andric 30*bdd1243dSDimitry Andricstatic std::string getOSVersion() { 31*bdd1243dSDimitry Andric struct utsname info; 32*bdd1243dSDimitry Andric 33*bdd1243dSDimitry Andric if (uname(&info)) 34*bdd1243dSDimitry Andric return ""; 35*bdd1243dSDimitry Andric 36*bdd1243dSDimitry Andric return info.release; 37*bdd1243dSDimitry Andric} 38*bdd1243dSDimitry Andric 39*bdd1243dSDimitry Andricstatic std::string updateTripleOSVersion(std::string TargetTripleString) { 40*bdd1243dSDimitry Andric // On darwin, we want to update the version to match that of the target. 41*bdd1243dSDimitry Andric std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin"); 42*bdd1243dSDimitry Andric if (DarwinDashIdx != std::string::npos) { 43*bdd1243dSDimitry Andric TargetTripleString.resize(DarwinDashIdx + strlen("-darwin")); 44*bdd1243dSDimitry Andric TargetTripleString += getOSVersion(); 45*bdd1243dSDimitry Andric return TargetTripleString; 46*bdd1243dSDimitry Andric } 47*bdd1243dSDimitry Andric std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos"); 48*bdd1243dSDimitry Andric if (MacOSDashIdx != std::string::npos) { 49*bdd1243dSDimitry Andric TargetTripleString.resize(MacOSDashIdx); 50*bdd1243dSDimitry Andric // Reset the OS to darwin as the OS version from `uname` doesn't use the 51*bdd1243dSDimitry Andric // macOS version scheme. 52*bdd1243dSDimitry Andric TargetTripleString += "-darwin"; 53*bdd1243dSDimitry Andric TargetTripleString += getOSVersion(); 54*bdd1243dSDimitry Andric } 55*bdd1243dSDimitry Andric // On AIX, the AIX version and release should be that of the current host 56*bdd1243dSDimitry Andric // unless if the version has already been specified. 57*bdd1243dSDimitry Andric if (Triple(LLVM_HOST_TRIPLE).getOS() == Triple::AIX) { 58*bdd1243dSDimitry Andric Triple TT(TargetTripleString); 59*bdd1243dSDimitry Andric if (TT.getOS() == Triple::AIX && !TT.getOSMajorVersion()) { 60*bdd1243dSDimitry Andric struct utsname name; 61*bdd1243dSDimitry Andric if (uname(&name) != -1) { 62*bdd1243dSDimitry Andric std::string NewOSName = std::string(Triple::getOSTypeName(Triple::AIX)); 63*bdd1243dSDimitry Andric NewOSName += name.version; 64*bdd1243dSDimitry Andric NewOSName += '.'; 65*bdd1243dSDimitry Andric NewOSName += name.release; 66*bdd1243dSDimitry Andric NewOSName += ".0.0"; 67*bdd1243dSDimitry Andric TT.setOSName(NewOSName); 68*bdd1243dSDimitry Andric return TT.str(); 69*bdd1243dSDimitry Andric } 70*bdd1243dSDimitry Andric } 71*bdd1243dSDimitry Andric } 72*bdd1243dSDimitry Andric return TargetTripleString; 73*bdd1243dSDimitry Andric} 74*bdd1243dSDimitry Andric 75*bdd1243dSDimitry Andricstd::string sys::getDefaultTargetTriple() { 76*bdd1243dSDimitry Andric std::string TargetTripleString = 77*bdd1243dSDimitry Andric updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE); 78*bdd1243dSDimitry Andric 79*bdd1243dSDimitry Andric // Override the default target with an environment variable named by 80*bdd1243dSDimitry Andric // LLVM_TARGET_TRIPLE_ENV. 81*bdd1243dSDimitry Andric#if defined(LLVM_TARGET_TRIPLE_ENV) 82*bdd1243dSDimitry Andric if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV)) 83*bdd1243dSDimitry Andric TargetTripleString = EnvTriple; 84*bdd1243dSDimitry Andric#endif 85*bdd1243dSDimitry Andric 86*bdd1243dSDimitry Andric return TargetTripleString; 87*bdd1243dSDimitry Andric} 88