Remove duplication in Program::Execute{And,No}Wait.
[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 is distributed under the University of Illinois Open Source
6 // 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 "llvm/Support/ErrorHandling.h"
17 #include <cassert>
18 #include <cstring>
19 #include <ostream>
20 using namespace llvm;
21 using namespace sys;
22
23 //===----------------------------------------------------------------------===//
24 //=== WARNING: Implementation here must contain only TRULY operating system
25 //===          independent code.
26 //===----------------------------------------------------------------------===//
27
28 bool Path::operator==(const Path &that) const {
29   return path == that.path;
30 }
31
32 bool Path::operator!=(const Path &that) const {
33   return path != that.path;
34 }
35
36 bool Path::operator<(const Path& that) const {
37   return path < that.path;
38 }
39
40 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
41   strm << aPath.toString();
42   return strm;
43 }
44
45 Path
46 Path::GetLLVMConfigDir() {
47   Path result;
48 #ifdef LLVM_ETCDIR
49   if (result.set(LLVM_ETCDIR))
50     return result;
51 #endif
52   return GetLLVMDefaultConfigDir();
53 }
54
55 LLVMFileType
56 sys::IdentifyFileType(const char *magic, unsigned length) {
57   assert(magic && "Invalid magic number string");
58   assert(length >=4 && "Invalid magic number length");
59   switch ((unsigned char)magic[0]) {
60     case 0xDE:  // 0x0B17C0DE = BC wraper
61       if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
62           magic[3] == (char)0x0B)
63         return Bitcode_FileType;
64       break;
65     case 'B':
66       if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
67         return Bitcode_FileType;
68       break;
69     case '!':
70       if (length >= 8)
71         if (memcmp(magic,"!<arch>\n",8) == 0)
72           return Archive_FileType;
73       break;
74       
75     case '\177':
76       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
77         if (length >= 18 && magic[17] == 0)
78           switch (magic[16]) {
79             default: break;
80             case 1: return ELF_Relocatable_FileType;
81             case 2: return ELF_Executable_FileType;
82             case 3: return ELF_SharedObject_FileType;
83             case 4: return ELF_Core_FileType;
84           }
85       }
86       break;
87
88     case 0xCA:
89       if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && 
90           magic[3] == char(0xBE)) {
91         // This is complicated by an overlap with Java class files. 
92         // See the Mach-O section in /usr/share/file/magic for details.
93         if (length >= 8 && magic[7] < 43) 
94           // FIXME: Universal Binary of any type.
95           return Mach_O_DynamicallyLinkedSharedLib_FileType;
96       }
97       break;
98
99     case 0xFE:
100     case 0xCE: {
101       uint16_t type = 0;
102       if (magic[0] == char(0xFE) && magic[1] == char(0xED) && 
103           magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
104         /* Native endian */
105         if (length >= 16) type = magic[14] << 8 | magic[15];
106       } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) && 
107                  magic[2] == char(0xED) && magic[3] == char(0xFE)) {
108         /* Reverse endian */
109         if (length >= 14) type = magic[13] << 8 | magic[12];
110       }
111       switch (type) {
112         default: break;      
113         case 1: return Mach_O_Object_FileType; 
114         case 2: return Mach_O_Executable_FileType;
115         case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
116         case 4: return Mach_O_Core_FileType;
117         case 5: return Mach_O_PreloadExectuable_FileType;
118         case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
119         case 7: return Mach_O_DynamicLinker_FileType;
120         case 8: return Mach_O_Bundle_FileType;
121         case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
122         case 10: break; // FIXME: MH_DSYM companion file with only debug.
123       }
124       break;
125     }
126     case 0xF0: // PowerPC Windows
127     case 0x83: // Alpha 32-bit
128     case 0x84: // Alpha 64-bit
129     case 0x66: // MPS R4000 Windows
130     case 0x50: // mc68K
131     case 0x4c: // 80386 Windows
132       if (magic[1] == 0x01)
133         return COFF_FileType;
134
135     case 0x90: // PA-RISC Windows
136     case 0x68: // mc68K Windows
137       if (magic[1] == 0x02)
138         return COFF_FileType;
139       break;
140
141     default:
142       break;
143   }
144   return Unknown_FileType;
145 }
146
147 bool
148 Path::isArchive() const {
149   if (canRead())
150     return hasMagicNumber("!<arch>\012");
151   return false;
152 }
153
154 bool
155 Path::isDynamicLibrary() const {
156   if (canRead()) {
157     std::string Magic;
158     if (getMagicNumber(Magic, 64))
159       switch (IdentifyFileType(Magic.c_str(),
160                                static_cast<unsigned>(Magic.length()))) {
161         default: return false;
162         case Mach_O_FixedVirtualMemorySharedLib_FileType:
163         case Mach_O_DynamicallyLinkedSharedLib_FileType:
164         case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
165         case ELF_SharedObject_FileType:
166         case COFF_FileType:  return true;
167       }
168   }
169   return false;
170 }
171
172 Path
173 Path::FindLibrary(std::string& name) {
174   std::vector<sys::Path> LibPaths;
175   GetSystemLibraryPaths(LibPaths);
176   for (unsigned i = 0; i < LibPaths.size(); ++i) {
177     sys::Path FullPath(LibPaths[i]);
178     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
179     if (FullPath.isDynamicLibrary())
180       return FullPath;
181     FullPath.eraseSuffix();
182     FullPath.appendSuffix("a");
183     if (FullPath.isArchive())
184       return FullPath;
185   }
186   return sys::Path();
187 }
188
189 std::string Path::GetDLLSuffix() {
190   return LTDL_SHLIB_EXT;
191 }
192
193 bool
194 Path::isBitcodeFile() const {
195   std::string actualMagic;
196   if (!getMagicNumber(actualMagic, 4))
197     return false;
198   LLVMFileType FT =
199     IdentifyFileType(actualMagic.c_str(),
200                      static_cast<unsigned>(actualMagic.length()));
201   return FT == Bitcode_FileType;
202 }
203
204 bool Path::hasMagicNumber(const std::string &Magic) const {
205   std::string actualMagic;
206   if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
207     return Magic == actualMagic;
208   return false;
209 }
210
211 static void getPathList(const char*path, std::vector<Path>& Paths) {
212   const char* at = path;
213   const char* delim = strchr(at, PathSeparator);
214   Path tmpPath;
215   while (delim != 0) {
216     std::string tmp(at, size_t(delim-at));
217     if (tmpPath.set(tmp))
218       if (tmpPath.canRead())
219         Paths.push_back(tmpPath);
220     at = delim + 1;
221     delim = strchr(at, PathSeparator);
222   }
223
224   if (*at != 0)
225     if (tmpPath.set(std::string(at)))
226       if (tmpPath.canRead())
227         Paths.push_back(tmpPath);
228 }
229
230 static std::string getDirnameCharSep(const std::string& path, char Sep) {
231   
232   if (path.empty())
233     return ".";
234   
235   // If the path is all slashes, return a single slash.
236   // Otherwise, remove all trailing slashes.
237   
238   signed pos = static_cast<signed>(path.size()) - 1;
239   
240   while (pos >= 0 && path[pos] == Sep)
241     --pos;
242   
243   if (pos < 0)
244     return path[0] == Sep ? std::string(1, Sep) : std::string(".");
245   
246   // Any slashes left?
247   signed i = 0;
248   
249   while (i < pos && path[i] != Sep)
250     ++i;
251   
252   if (i == pos) // No slashes?  Return "."
253     return ".";
254   
255   // There is at least one slash left.  Remove all trailing non-slashes.  
256   while (pos >= 0 && path[pos] != Sep)
257     --pos;
258   
259   // Remove any trailing slashes.
260   while (pos >= 0 && path[pos] == Sep)
261     --pos;
262   
263   if (pos < 0)
264     return path[0] == Sep ? std::string(1, Sep) : std::string(".");
265   
266   return path.substr(0, pos+1);
267 }
268
269 // Include the truly platform-specific parts of this class.
270 #if defined(LLVM_ON_UNIX)
271 #include "Unix/Path.inc"
272 #endif
273 #if defined(LLVM_ON_WIN32)
274 #include "Win32/Path.inc"
275 #endif
276