1c2b00e64566bee521264faa7c5a7a6ddf83030a
[oota-llvm.git] / lib / System / Path.cpp
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 //===----------------------------------------------------------------------===//
13
14 #include "llvm/System/Path.h"
15 #include "llvm/Config/config.h"
16 #include <cassert>
17
18 namespace llvm {
19 using namespace sys;
20
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //===          independent code. 
24 //===----------------------------------------------------------------------===//
25
26 Path
27 Path::GetLLVMConfigDir() {
28   Path result;
29   if (result.setDirectory(LLVM_ETCDIR))
30     return result;
31   return GetLLVMDefaultConfigDir();
32 }
33
34 LLVMFileType 
35 sys::IdentifyFileType(const char*magic, unsigned length) {
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
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
92 std::string
93 Path::GetDLLSuffix() {
94   return LTDL_SHLIB_EXT;
95 }
96
97 }
98
99 // Include the truly platform-specific parts of this class.
100 #include "platform/Path.cpp"
101
102 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab