LLVM API Documentation

Path.cpp

Go to the documentation of this file.
00001 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by Reid Spencer and is distributed under the
00006 // University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This header file implements the operating system Path concept.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/System/Path.h"
00015 #include "llvm/Config/config.h"
00016 #include <cassert>
00017 
00018 namespace llvm {
00019 using namespace sys;
00020 
00021 //===----------------------------------------------------------------------===//
00022 //=== WARNING: Implementation here must contain only TRULY operating system
00023 //===          independent code.
00024 //===----------------------------------------------------------------------===//
00025 
00026 Path
00027 Path::GetLLVMConfigDir() {
00028   Path result;
00029 #ifdef LLVM_ETCDIR
00030   if (result.set(LLVM_ETCDIR))
00031     return result;
00032 #endif
00033   return GetLLVMDefaultConfigDir();
00034 }
00035 
00036 LLVMFileType
00037 sys::IdentifyFileType(const char*magic, unsigned length) {
00038   assert(magic && "Invalid magic number string");
00039   assert(length >=4 && "Invalid magic number length");
00040   switch (magic[0]) {
00041     case 'l':
00042       if (magic[1] == 'l' && magic[2] == 'v') {
00043         if (magic[3] == 'c')
00044           return CompressedBytecodeFileType;
00045         else if (magic[3] == 'm')
00046           return BytecodeFileType;
00047       }
00048       break;
00049 
00050     case '!':
00051       if (length >= 8) {
00052         if (memcmp(magic,"!<arch>\n",8) == 0)
00053           return ArchiveFileType;
00054       }
00055       break;
00056 
00057     default:
00058       break;
00059   }
00060   return UnknownFileType;
00061 }
00062 
00063 bool
00064 Path::isArchive() const {
00065   if (canRead())
00066     return hasMagicNumber("!<arch>\012");
00067   return false;
00068 }
00069 
00070 bool
00071 Path::isDynamicLibrary() const {
00072   if (canRead())
00073     return hasMagicNumber("\177ELF");
00074   return false;
00075 }
00076 
00077 Path
00078 Path::FindLibrary(std::string& name) {
00079   std::vector<sys::Path> LibPaths;
00080   GetSystemLibraryPaths(LibPaths);
00081   for (unsigned i = 0; i < LibPaths.size(); ++i) {
00082     sys::Path FullPath(LibPaths[i]);
00083     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
00084     if (FullPath.isDynamicLibrary())
00085       return FullPath;
00086     FullPath.eraseSuffix();
00087     FullPath.appendSuffix("a");
00088     if (FullPath.isArchive())
00089       return FullPath;
00090   }
00091   return sys::Path();
00092 }
00093 
00094 std::string
00095 Path::GetDLLSuffix() {
00096   return LTDL_SHLIB_EXT;
00097 }
00098 
00099 }
00100 
00101 // Include the truly platform-specific parts of this class.
00102 
00103 #if defined(LLVM_ON_UNIX)
00104 #include "Unix/Path.inc"
00105 #endif
00106 #if defined(LLVM_ON_WIN32)
00107 #include "Win32/Path.inc"
00108 #endif
00109