Be careful to erase values from all of the appropriate sets when they're not needed...
[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 'B':
47       if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
48         return Bitcode_FileType;
49       break;
50     case 'l':
51       if (magic[1] == 'l' && magic[2] == 'v') {
52         if (magic[3] == 'c')
53           return CompressedBytecode_FileType;
54         else if (magic[3] == 'm')
55           return Bytecode_FileType;
56       }
57       break;
58     case '!':
59       if (length >= 8)
60         if (memcmp(magic,"!<arch>\n",8) == 0)
61           return Archive_FileType;
62       break;
63       
64     case '\177':
65       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
66         if (length >= 18 && magic[17] == 0)
67           switch (magic[16]) {
68             default: break;
69             case 1: return ELF_Relocatable_FileType;
70             case 2: return ELF_Executable_FileType;
71             case 3: return ELF_SharedObject_FileType;
72             case 4: return ELF_Core_FileType;
73           }
74       }
75       break;
76
77     case 0xCA:
78       // This is complicated by an overlap with Java class files. 
79       // See the Mach-O section in /usr/share/file/magic for details.
80       if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && 
81           magic[3] == char(0xBE)) {
82         return Mach_O_DynamicallyLinkedSharedLib_FileType;
83         
84         // FIXME: How does this work?
85         if (length >= 14 && magic[13] == 0)
86           switch (magic[12]) {
87             default: break;
88             case 1: return Mach_O_Object_FileType;
89             case 2: return Mach_O_Executable_FileType;
90             case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
91             case 4: return Mach_O_Core_FileType;
92             case 5: return Mach_O_PreloadExectuable_FileType;
93             case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
94             case 7: return Mach_O_DynamicLinker_FileType;
95             case 8: return Mach_O_Bundle_FileType;
96             case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
97           }
98       }
99       break;
100
101     case 0xF0: // PowerPC Windows
102     case 0x83: // Alpha 32-bit
103     case 0x84: // Alpha 64-bit
104     case 0x66: // MPS R4000 Windows
105     case 0x50: // mc68K
106     case 0x4c: // 80386 Windows
107       if (magic[1] == 0x01)
108         return COFF_FileType;
109
110     case 0x90: // PA-RISC Windows
111     case 0x68: // mc68K Windows
112       if (magic[1] == 0x02)
113         return COFF_FileType;
114       break;
115
116     default:
117       break;
118   }
119   return Unknown_FileType;
120 }
121
122 bool
123 Path::isArchive() const {
124   if (canRead())
125     return hasMagicNumber("!<arch>\012");
126   return false;
127 }
128
129 bool
130 Path::isDynamicLibrary() const {
131   if (canRead()) {
132     std::string Magic;
133     if (getMagicNumber(Magic, 64))
134       switch (IdentifyFileType(Magic.c_str(), Magic.length())) {
135         default: return false;
136         case Mach_O_FixedVirtualMemorySharedLib_FileType:
137         case Mach_O_DynamicallyLinkedSharedLib_FileType:
138         case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
139         case ELF_SharedObject_FileType:
140         case COFF_FileType:  return true;
141       }
142   }
143   return false;
144 }
145
146 Path
147 Path::FindLibrary(std::string& name) {
148   std::vector<sys::Path> LibPaths;
149   GetSystemLibraryPaths(LibPaths);
150   for (unsigned i = 0; i < LibPaths.size(); ++i) {
151     sys::Path FullPath(LibPaths[i]);
152     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
153     if (FullPath.isDynamicLibrary())
154       return FullPath;
155     FullPath.eraseSuffix();
156     FullPath.appendSuffix("a");
157     if (FullPath.isArchive())
158       return FullPath;
159   }
160   return sys::Path();
161 }
162
163 std::string Path::GetDLLSuffix() {
164   return LTDL_SHLIB_EXT;
165 }
166
167 bool
168 Path::isBytecodeFile() const {
169   std::string actualMagic;
170   if (!getMagicNumber(actualMagic, 4))
171     return false;
172   return actualMagic == "llvc" || actualMagic == "llvm";
173 }
174
175 bool
176 Path::isBitcodeFile() const {
177   std::string actualMagic;
178   if (!getMagicNumber(actualMagic, 4))
179     return false;
180   return actualMagic == "BC\xC0\xDE";
181 }
182
183 bool Path::hasMagicNumber(const std::string &Magic) const {
184   std::string actualMagic;
185   if (getMagicNumber(actualMagic, Magic.size()))
186     return Magic == actualMagic;
187   return false;
188 }
189
190
191
192 // Include the truly platform-specific parts of this class.
193 #if defined(LLVM_ON_UNIX)
194 #include "Unix/Path.inc"
195 #endif
196 #if defined(LLVM_ON_WIN32)
197 #include "Win32/Path.inc"
198 #endif
199
200 DEFINING_FILE_FOR(SystemPath)