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