LLVM API Documentation

Unix/Host.inc
Go to the documentation of this file.
00001  //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the UNIX Host support.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 //===----------------------------------------------------------------------===//
00015 //=== WARNING: Implementation here must contain only generic UNIX code that
00016 //===          is guaranteed to work on *all* UNIX variants.
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "llvm/Config/config.h"
00020 #include "llvm/ADT/StringRef.h"
00021 #include "Unix.h"
00022 #include <sys/utsname.h>
00023 #include <cctype>
00024 #include <string>
00025 #include <cstdlib> // ::getenv
00026 
00027 using namespace llvm;
00028 
00029 static std::string getOSVersion() {
00030   struct utsname info;
00031 
00032   if (uname(&info))
00033     return "";
00034 
00035   return info.release;
00036 }
00037 
00038 std::string sys::getDefaultTargetTriple() {
00039   StringRef TargetTripleString(LLVM_DEFAULT_TARGET_TRIPLE);
00040   std::pair<StringRef, StringRef> ArchSplit = TargetTripleString.split('-');
00041 
00042   // Normalize the arch, since the target triple may not actually match the target.
00043   std::string Arch = ArchSplit.first;
00044 
00045   std::string Triple(Arch);
00046   Triple += '-';
00047   Triple += ArchSplit.second;
00048 
00049   // Force i<N>86 to i386.
00050   if (Triple[0] == 'i' && isdigit(Triple[1]) &&
00051       Triple[2] == '8' && Triple[3] == '6')
00052     Triple[1] = '3';
00053 
00054   // On darwin, we want to update the version to match that of the
00055   // target.
00056   std::string::size_type DarwinDashIdx = Triple.find("-darwin");
00057   if (DarwinDashIdx != std::string::npos) {
00058     Triple.resize(DarwinDashIdx + strlen("-darwin"));
00059     Triple += getOSVersion();
00060   }
00061 
00062   return Triple::normalize(Triple);
00063 }