Hack to get sys::Path to recognize macho dylibs.
[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 #include <ostream>
18 using namespace llvm;
19 using namespace sys;
20
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //===          independent code.
24 //===----------------------------------------------------------------------===//
25
26 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
27   strm << aPath.toString();
28   return strm;
29 }
30
31 Path
32 Path::GetLLVMConfigDir() {
33   Path result;
34 #ifdef LLVM_ETCDIR
35   if (result.set(LLVM_ETCDIR))
36     return result;
37 #endif
38   return GetLLVMDefaultConfigDir();
39 }
40
41 LLVMFileType
42 sys::IdentifyFileType(const char*magic, unsigned length) {
43   assert(magic && "Invalid magic number string");
44   assert(length >=4 && "Invalid magic number length");
45   switch (magic[0]) {
46     case 'l':
47       if (magic[1] == 'l' && magic[2] == 'v')
48         if (magic[3] == 'c')
49           return CompressedBytecode_FileType;
50         else if (magic[3] == 'm')
51           return Bytecode_FileType;
52       break;
53     case '!':
54       if (length >= 8)
55         if (memcmp(magic,"!<arch>\n",8) == 0)
56           return Archive_FileType;
57       break;
58       
59     case '\177':
60       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F')
61         if (length >= 18 && magic[17] == 0)
62           switch (magic[16]) {
63             default: break;
64             case 1: return ELF_Relocatable_FileType;
65             case 2: return ELF_Executable_FileType;
66             case 3: return ELF_SharedObject_FileType;
67             case 4: return ELF_Core_FileType;
68           }
69       break;
70
71     case 0xCA:
72       // This is complicated by an overlap with Java class files. 
73       // See the Mach-O section in /usr/share/file/magic for details.
74       if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && 
75           magic[3] == char(0xBE)) {
76         return Mach_O_DynamicallyLinkedSharedLib_FileType;
77         
78         // FIXME: How does this work?
79         if (length >= 14 && magic[13] == 0)
80           switch (magic[12]) {
81             default: break;
82             case 1: return Mach_O_Object_FileType;
83             case 2: return Mach_O_Executable_FileType;
84             case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
85             case 4: return Mach_O_Core_FileType;
86             case 5: return Mach_O_PreloadExectuable_FileType;
87             case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
88             case 7: return Mach_O_DynamicLinker_FileType;
89             case 8: return Mach_O_Bundle_FileType;
90             case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
91           }
92       }
93       break;
94
95     case 0xF0: // PowerPC Windows
96     case 0x83: // Alpha 32-bit
97     case 0x84: // Alpha 64-bit
98     case 0x66: // MPS R4000 Windows
99     case 0x50: // mc68K
100     case 0x4c: // 80386 Windows
101       if (magic[1] == 0x01)
102         return COFF_FileType;
103
104     case 0x90: // PA-RISC Windows
105     case 0x68: // mc68K Windows
106       if (magic[1] == 0x02)
107         return COFF_FileType;
108       break;
109
110     default:
111       break;
112   }
113   return Unknown_FileType;
114 }
115
116 bool
117 Path::isArchive() const {
118   if (canRead())
119     return hasMagicNumber("!<arch>\012");
120   return false;
121 }
122
123 bool
124 Path::isDynamicLibrary() const {
125   if (canRead()) {
126     std::string Magic;
127     if (getMagicNumber(Magic, 64))
128       switch (IdentifyFileType(Magic.c_str(), Magic.length())) {
129         default: return false;
130         case Mach_O_FixedVirtualMemorySharedLib_FileType:
131         case Mach_O_DynamicallyLinkedSharedLib_FileType:
132         case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
133         case ELF_SharedObject_FileType:
134         case COFF_FileType:  return true;
135       }
136   }
137   return false;
138 }
139
140 Path
141 Path::FindLibrary(std::string& name) {
142   std::vector<sys::Path> LibPaths;
143   GetSystemLibraryPaths(LibPaths);
144   for (unsigned i = 0; i < LibPaths.size(); ++i) {
145     sys::Path FullPath(LibPaths[i]);
146     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
147     if (FullPath.isDynamicLibrary())
148       return FullPath;
149     FullPath.eraseSuffix();
150     FullPath.appendSuffix("a");
151     if (FullPath.isArchive())
152       return FullPath;
153   }
154   return sys::Path();
155 }
156
157 std::string Path::GetDLLSuffix() {
158   return LTDL_SHLIB_EXT;
159 }
160
161 // Include the truly platform-specific parts of this class.
162 #if defined(LLVM_ON_UNIX)
163 #include "Unix/Path.inc"
164 #endif
165 #if defined(LLVM_ON_WIN32)
166 #include "Win32/Path.inc"
167 #endif
168
169 DEFINING_FILE_FOR(SystemPath)