LLVM API Documentation

Unix/Host.inc

Go to the documentation of this file.
00001  //===- llvm/System/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 <string>
00024 
00025 using namespace llvm;
00026 
00027 static std::string getOSVersion() {
00028   struct utsname info;
00029 
00030   if (uname(&info))
00031     return "";
00032 
00033   return info.release;
00034 }
00035 
00036 std::string sys::getHostTriple() {
00037   // FIXME: Derive directly instead of relying on the autoconf generated
00038   // variable.
00039 
00040   StringRef HostTripleString(LLVM_HOSTTRIPLE);
00041   std::pair<StringRef, StringRef> ArchSplit = HostTripleString.split('-');
00042   
00043   // Normalize the arch, since the host triple may not actually match the host.
00044   std::string Arch = ArchSplit.first;
00045 
00046   // It would be nice to do this in terms of llvm::Triple, but that is in
00047   // Support which is layered above us.
00048 #if defined(__x86_64__)
00049   Arch = "x86_64";
00050 #elif defined(__i386__)
00051   Arch = "i386";
00052 #elif defined(__ppc64__)
00053   Arch = "powerpc64";
00054 #elif defined(__ppc__)
00055   Arch = "powerpc";
00056 #elif defined(__arm__)
00057 
00058   // FIXME: We need to pick the right ARM triple (which involves querying the
00059   // chip). However, for now this is most important for LLVM arch selection, so
00060   // we only need to make sure to distinguish ARM and Thumb.
00061 #  if defined(__thumb__)
00062   Arch = "thumb";
00063 #  else
00064   Arch = "arm";
00065 #  endif
00066 
00067 #else
00068 
00069   // FIXME: When enough auto-detection is in place, this should just
00070   // #error. Then at least the arch selection is done, and we only need the OS
00071   // etc selection to kill off the use of LLVM_HOSTTRIPLE.
00072 
00073 #endif
00074 
00075   std::string Triple(Arch);
00076   Triple += '-';
00077   Triple += ArchSplit.second;
00078 
00079   // Force i<N>86 to i386.
00080   if (Triple[0] == 'i' && isdigit(Triple[1]) && 
00081       Triple[2] == '8' && Triple[3] == '6')
00082     Triple[1] = '3';
00083 
00084   // On darwin, we want to update the version to match that of the
00085   // host.    
00086   std::string::size_type DarwinDashIdx = Triple.find("-darwin");
00087   if (DarwinDashIdx != std::string::npos) {
00088     Triple.resize(DarwinDashIdx + strlen("-darwin"));
00089     
00090     // Only add the major part of the os version.
00091     std::string Version = getOSVersion();
00092     Triple += Version.substr(0, Version.find('.'));
00093   }
00094 
00095   return Triple;
00096 }