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 #include <ostream>
00018 using namespace llvm;
00019 using namespace sys;
00020 
00021 //===----------------------------------------------------------------------===//
00022 //=== WARNING: Implementation here must contain only TRULY operating system
00023 //===          independent code.
00024 //===----------------------------------------------------------------------===//
00025 
00026 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
00027   strm << aPath.toString();
00028   return strm;
00029 }
00030 
00031 Path
00032 Path::GetLLVMConfigDir() {
00033   Path result;
00034 #ifdef LLVM_ETCDIR
00035   if (result.set(LLVM_ETCDIR))
00036     return result;
00037 #endif
00038   return GetLLVMDefaultConfigDir();
00039 }
00040 
00041 LLVMFileType
00042 sys::IdentifyFileType(const char*magic, unsigned length) {
00043   assert(magic && "Invalid magic number string");
00044   assert(length >=4 && "Invalid magic number length");
00045   switch (magic[0]) {
00046     case 'l':
00047       if (magic[1] == 'l' && magic[2] == 'v') {
00048         if (magic[3] == 'c')
00049           return CompressedBytecodeFileType;
00050         else if (magic[3] == 'm')
00051           return BytecodeFileType;
00052       }
00053       break;
00054 
00055     case '!':
00056       if (length >= 8) {
00057         if (memcmp(magic,"!<arch>\n",8) == 0)
00058           return ArchiveFileType;
00059       }
00060       break;
00061 
00062     default:
00063       break;
00064   }
00065   return UnknownFileType;
00066 }
00067 
00068 bool
00069 Path::isArchive() const {
00070   if (canRead())
00071     return hasMagicNumber("!<arch>\012");
00072   return false;
00073 }
00074 
00075 bool
00076 Path::isDynamicLibrary() const {
00077   if (canRead())
00078     return hasMagicNumber("\177ELF");
00079   return false;
00080 }
00081 
00082 Path
00083 Path::FindLibrary(std::string& name) {
00084   std::vector<sys::Path> LibPaths;
00085   GetSystemLibraryPaths(LibPaths);
00086   for (unsigned i = 0; i < LibPaths.size(); ++i) {
00087     sys::Path FullPath(LibPaths[i]);
00088     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
00089     if (FullPath.isDynamicLibrary())
00090       return FullPath;
00091     FullPath.eraseSuffix();
00092     FullPath.appendSuffix("a");
00093     if (FullPath.isArchive())
00094       return FullPath;
00095   }
00096   return sys::Path();
00097 }
00098 
00099 std::string Path::GetDLLSuffix() {
00100   return LTDL_SHLIB_EXT;
00101 }
00102 
00103 // Include the truly platform-specific parts of this class.
00104 #if defined(LLVM_ON_UNIX)
00105 #include "Unix/Path.inc"
00106 #endif
00107 #if defined(LLVM_ON_WIN32)
00108 #include "Win32/Path.inc"
00109 #endif
00110 
00111 DEFINING_FILE_FOR(SystemPath)