Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 1 | //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system Path concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 13 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 14 | #include "llvm/System/Path.h" |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Alkis Evlogimenos | 98bc8ed | 2004-11-14 22:37:42 +0000 | [diff] [blame] | 16 | #include <cassert> |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 17 | |
| 18 | namespace llvm { |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 19 | using namespace sys; |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 23 | //=== independent code. |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 26 | Path |
| 27 | Path::GetLLVMConfigDir() { |
| 28 | Path result; |
Jeff Cohen | ab68df0 | 2004-12-15 04:08:15 +0000 | [diff] [blame] | 29 | #ifdef LLVM_ETCDIR |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 30 | if (result.setDirectory(LLVM_ETCDIR)) |
| 31 | return result; |
Jeff Cohen | ab68df0 | 2004-12-15 04:08:15 +0000 | [diff] [blame] | 32 | #endif |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 33 | return GetLLVMDefaultConfigDir(); |
| 34 | } |
| 35 | |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 36 | LLVMFileType |
Reid Spencer | a2a6221 | 2004-11-14 23:26:18 +0000 | [diff] [blame] | 37 | sys::IdentifyFileType(const char*magic, unsigned length) { |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 38 | assert(magic && "Invalid magic number string"); |
| 39 | assert(length >=4 && "Invalid magic number length"); |
| 40 | switch (magic[0]) { |
| 41 | case 'l': |
| 42 | if (magic[1] == 'l' && magic[2] == 'v') { |
| 43 | if (magic[3] == 'c') |
| 44 | return CompressedBytecodeFileType; |
| 45 | else if (magic[3] == 'm') |
| 46 | return BytecodeFileType; |
| 47 | } |
| 48 | break; |
| 49 | |
| 50 | case '!': |
| 51 | if (length >= 8) { |
| 52 | if (memcmp(magic,"!<arch>\n",8) == 0) |
| 53 | return ArchiveFileType; |
| 54 | } |
| 55 | break; |
| 56 | |
| 57 | default: |
| 58 | break; |
| 59 | } |
| 60 | return UnknownFileType; |
| 61 | } |
| 62 | |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 63 | bool |
| 64 | Path::isArchive() const { |
| 65 | if (readable()) |
| 66 | return hasMagicNumber("!<arch>\012"); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | bool |
| 71 | Path::isDynamicLibrary() const { |
| 72 | if (readable()) |
| 73 | return hasMagicNumber("\177ELF"); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | Path |
| 78 | Path::FindLibrary(std::string& name) { |
| 79 | std::vector<sys::Path> LibPaths; |
| 80 | GetSystemLibraryPaths(LibPaths); |
| 81 | for (unsigned i = 0; i < LibPaths.size(); ++i) { |
| 82 | sys::Path FullPath(LibPaths[i]); |
| 83 | FullPath.appendFile("lib" + name + LTDL_SHLIB_EXT); |
| 84 | if (FullPath.isDynamicLibrary()) |
| 85 | return FullPath; |
| 86 | FullPath.elideSuffix(); |
| 87 | FullPath.appendSuffix("a"); |
| 88 | if (FullPath.isArchive()) |
| 89 | return FullPath; |
| 90 | } |
| 91 | return sys::Path(); |
| 92 | } |
| 93 | |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 94 | std::string |
| 95 | Path::GetDLLSuffix() { |
| 96 | return LTDL_SHLIB_EXT; |
| 97 | } |
| 98 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Include the truly platform-specific parts of this class. |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 102 | |
| 103 | #if defined(LLVM_ON_UNIX) |
| 104 | #include "Unix/Path.cpp" |
| 105 | #endif |
| 106 | #if defined(LLVM_ON_WIN32) |
| 107 | #include "Win32/Path.cpp" |
| 108 | #endif |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 109 | |
Reid Spencer | 36853b9 | 2004-08-29 19:24:53 +0000 | [diff] [blame] | 110 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |