Use the new script to sort the includes of every file under lib.
[oota-llvm.git] / lib / Support / 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/Support/Path.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/FileSystem.h"
19 #include <cassert>
20 #include <cstring>
21 #include <ostream>
22 using namespace llvm;
23 using namespace sys;
24 namespace {
25 using support::ulittle32_t;
26 }
27
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only TRULY operating system
30 //===          independent code.
31 //===----------------------------------------------------------------------===//
32
33 bool Path::operator==(const Path &that) const {
34   return path == that.path;
35 }
36
37 bool Path::operator<(const Path& that) const {
38   return path < that.path;
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 ((unsigned char)magic[0]) {
46     case 0xDE:  // 0x0B17C0DE = BC wraper
47       if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
48           magic[3] == (char)0x0B)
49         return Bitcode_FileType;
50       break;
51     case 'B':
52       if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
53         return Bitcode_FileType;
54       break;
55     case '!':
56       if (length >= 8)
57         if (memcmp(magic,"!<arch>\n",8) == 0)
58           return Archive_FileType;
59       break;
60
61     case '\177':
62       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
63         bool Data2MSB = magic[5] == 2;
64         unsigned high = Data2MSB ? 16 : 17;
65         unsigned low  = Data2MSB ? 17 : 16;
66         if (length >= 18 && magic[high] == 0)
67           switch (magic[low]) {
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       if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
79           magic[3] == char(0xBE)) {
80         // This is complicated by an overlap with Java class files.
81         // See the Mach-O section in /usr/share/file/magic for details.
82         if (length >= 8 && magic[7] < 43)
83           // FIXME: Universal Binary of any type.
84           return Mach_O_DynamicallyLinkedSharedLib_FileType;
85       }
86       break;
87
88       // The two magic numbers for mach-o are:
89       // 0xfeedface - 32-bit mach-o
90       // 0xfeedfacf - 64-bit mach-o
91     case 0xFE:
92     case 0xCE:
93     case 0xCF: {
94       uint16_t type = 0;
95       if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
96           magic[2] == char(0xFA) &&
97           (magic[3] == char(0xCE) || magic[3] == char(0xCF))) {
98         /* Native endian */
99         if (length >= 16) type = magic[14] << 8 | magic[15];
100       } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) &&
101                  magic[1] == char(0xFA) && magic[2] == char(0xED) &&
102                  magic[3] == char(0xFE)) {
103         /* Reverse endian */
104         if (length >= 14) type = magic[13] << 8 | magic[12];
105       }
106       switch (type) {
107         default: break;
108         case 1: return Mach_O_Object_FileType;
109         case 2: return Mach_O_Executable_FileType;
110         case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
111         case 4: return Mach_O_Core_FileType;
112         case 5: return Mach_O_PreloadExecutable_FileType;
113         case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
114         case 7: return Mach_O_DynamicLinker_FileType;
115         case 8: return Mach_O_Bundle_FileType;
116         case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
117         case 10: return Mach_O_DSYMCompanion_FileType;
118       }
119       break;
120     }
121     case 0xF0: // PowerPC Windows
122     case 0x83: // Alpha 32-bit
123     case 0x84: // Alpha 64-bit
124     case 0x66: // MPS R4000 Windows
125     case 0x50: // mc68K
126     case 0x4c: // 80386 Windows
127       if (magic[1] == 0x01)
128         return COFF_FileType;
129
130     case 0x90: // PA-RISC Windows
131     case 0x68: // mc68K Windows
132       if (magic[1] == 0x02)
133         return COFF_FileType;
134       break;
135
136     case 0x4d: // Possible MS-DOS stub on Windows PE file
137       if (magic[1] == 0x5a) {
138         uint32_t off = *reinterpret_cast<const ulittle32_t *>(magic + 0x3c);
139         // PE/COFF file, either EXE or DLL.
140         if (off < length && memcmp(magic + off, "PE\0\0",4) == 0)
141           return COFF_FileType;
142       }
143       break;
144
145     case 0x64: // x86-64 Windows.
146       if (magic[1] == char(0x86))
147         return COFF_FileType;
148       break;
149
150     default:
151       break;
152   }
153   return Unknown_FileType;
154 }
155
156 bool
157 Path::isArchive() const {
158   fs::file_magic type;
159   if (fs::identify_magic(str(), type))
160     return false;
161   return type == fs::file_magic::archive;
162 }
163
164 bool
165 Path::isDynamicLibrary() const {
166   fs::file_magic type;
167   if (fs::identify_magic(str(), type))
168     return false;
169   switch (type) {
170     default: return false;
171     case fs::file_magic::macho_fixed_virtual_memory_shared_lib:
172     case fs::file_magic::macho_dynamically_linked_shared_lib:
173     case fs::file_magic::macho_dynamically_linked_shared_lib_stub:
174     case fs::file_magic::elf_shared_object:
175     case fs::file_magic::pecoff_executable:  return true;
176   }
177 }
178
179 bool
180 Path::isObjectFile() const {
181   fs::file_magic type;
182   if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
183     return false;
184   return true;
185 }
186
187 Path
188 Path::FindLibrary(std::string& name) {
189   std::vector<sys::Path> LibPaths;
190   GetSystemLibraryPaths(LibPaths);
191   for (unsigned i = 0; i < LibPaths.size(); ++i) {
192     sys::Path FullPath(LibPaths[i]);
193     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
194     if (FullPath.isDynamicLibrary())
195       return FullPath;
196     FullPath.eraseSuffix();
197     FullPath.appendSuffix("a");
198     if (FullPath.isArchive())
199       return FullPath;
200   }
201   return sys::Path();
202 }
203
204 StringRef Path::GetDLLSuffix() {
205   return &(LTDL_SHLIB_EXT[1]);
206 }
207
208 void
209 Path::appendSuffix(StringRef suffix) {
210   if (!suffix.empty()) {
211     path.append(".");
212     path.append(suffix);
213   }
214 }
215
216 bool
217 Path::isBitcodeFile() const {
218   fs::file_magic type;
219   if (fs::identify_magic(str(), type))
220     return false;
221   return type == fs::file_magic::bitcode;
222 }
223
224 bool Path::hasMagicNumber(StringRef Magic) const {
225   std::string actualMagic;
226   if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
227     return Magic == actualMagic;
228   return false;
229 }
230
231 static void getPathList(const char*path, std::vector<Path>& Paths) {
232   const char* at = path;
233   const char* delim = strchr(at, PathSeparator);
234   Path tmpPath;
235   while (delim != 0) {
236     std::string tmp(at, size_t(delim-at));
237     if (tmpPath.set(tmp))
238       if (tmpPath.canRead())
239         Paths.push_back(tmpPath);
240     at = delim + 1;
241     delim = strchr(at, PathSeparator);
242   }
243
244   if (*at != 0)
245     if (tmpPath.set(std::string(at)))
246       if (tmpPath.canRead())
247         Paths.push_back(tmpPath);
248 }
249
250 static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
251   assert(Sep[0] != '\0' && Sep[1] == '\0' &&
252          "Sep must be a 1-character string literal.");
253   if (path.empty())
254     return ".";
255
256   // If the path is all slashes, return a single slash.
257   // Otherwise, remove all trailing slashes.
258
259   signed pos = static_cast<signed>(path.size()) - 1;
260
261   while (pos >= 0 && path[pos] == Sep[0])
262     --pos;
263
264   if (pos < 0)
265     return path[0] == Sep[0] ? Sep : ".";
266
267   // Any slashes left?
268   signed i = 0;
269
270   while (i < pos && path[i] != Sep[0])
271     ++i;
272
273   if (i == pos) // No slashes?  Return "."
274     return ".";
275
276   // There is at least one slash left.  Remove all trailing non-slashes.
277   while (pos >= 0 && path[pos] != Sep[0])
278     --pos;
279
280   // Remove any trailing slashes.
281   while (pos >= 0 && path[pos] == Sep[0])
282     --pos;
283
284   if (pos < 0)
285     return path[0] == Sep[0] ? Sep : ".";
286
287   return path.substr(0, pos+1);
288 }
289
290 // Include the truly platform-specific parts of this class.
291 #if defined(LLVM_ON_UNIX)
292 #include "Unix/Path.inc"
293 #endif
294 #if defined(LLVM_ON_WIN32)
295 #include "Windows/Path.inc"
296 #endif