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; |
| 29 | if (result.setDirectory(LLVM_ETCDIR)) |
| 30 | return result; |
| 31 | return GetLLVMDefaultConfigDir(); |
| 32 | } |
| 33 | |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 34 | LLVMFileType |
Reid Spencer | a2a6221 | 2004-11-14 23:26:18 +0000 | [diff] [blame] | 35 | sys::IdentifyFileType(const char*magic, unsigned length) { |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 36 | assert(magic && "Invalid magic number string"); |
| 37 | assert(length >=4 && "Invalid magic number length"); |
| 38 | switch (magic[0]) { |
| 39 | case 'l': |
| 40 | if (magic[1] == 'l' && magic[2] == 'v') { |
| 41 | if (magic[3] == 'c') |
| 42 | return CompressedBytecodeFileType; |
| 43 | else if (magic[3] == 'm') |
| 44 | return BytecodeFileType; |
| 45 | } |
| 46 | break; |
| 47 | |
| 48 | case '!': |
| 49 | if (length >= 8) { |
| 50 | if (memcmp(magic,"!<arch>\n",8) == 0) |
| 51 | return ArchiveFileType; |
| 52 | } |
| 53 | break; |
| 54 | |
| 55 | default: |
| 56 | break; |
| 57 | } |
| 58 | return UnknownFileType; |
| 59 | } |
| 60 | |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 61 | bool |
| 62 | Path::isArchive() const { |
| 63 | if (readable()) |
| 64 | return hasMagicNumber("!<arch>\012"); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | bool |
| 69 | Path::isDynamicLibrary() const { |
| 70 | if (readable()) |
| 71 | return hasMagicNumber("\177ELF"); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | Path |
| 76 | Path::FindLibrary(std::string& name) { |
| 77 | std::vector<sys::Path> LibPaths; |
| 78 | GetSystemLibraryPaths(LibPaths); |
| 79 | for (unsigned i = 0; i < LibPaths.size(); ++i) { |
| 80 | sys::Path FullPath(LibPaths[i]); |
| 81 | FullPath.appendFile("lib" + name + LTDL_SHLIB_EXT); |
| 82 | if (FullPath.isDynamicLibrary()) |
| 83 | return FullPath; |
| 84 | FullPath.elideSuffix(); |
| 85 | FullPath.appendSuffix("a"); |
| 86 | if (FullPath.isArchive()) |
| 87 | return FullPath; |
| 88 | } |
| 89 | return sys::Path(); |
| 90 | } |
| 91 | |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 92 | std::string |
| 93 | Path::GetDLLSuffix() { |
| 94 | return LTDL_SHLIB_EXT; |
| 95 | } |
| 96 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Include the truly platform-specific parts of this class. |
| 100 | #include "platform/Path.cpp" |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 101 | |
Reid Spencer | 36853b9 | 2004-08-29 19:24:53 +0000 | [diff] [blame] | 102 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |