blob: 4c965db7223383e085954dcd14df76c27e04cedc [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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>
Duncan Sandsfca20142008-01-09 19:42:09 +000017#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include <ostream>
19using namespace llvm;
20using namespace sys;
21
22//===----------------------------------------------------------------------===//
23//=== WARNING: Implementation here must contain only TRULY operating system
24//=== independent code.
25//===----------------------------------------------------------------------===//
26
Bill Wendling0df675d2008-05-21 21:20:07 +000027bool Path::operator==(const Path &that) const {
28 return path == that.path;
29}
30
31bool Path::operator!=(const Path &that) const {
32 return path != that.path;
33}
34
35bool Path::operator<(const Path& that) const {
36 return path < that.path;
37}
38
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
40 strm << aPath.toString();
41 return strm;
42}
43
44Path
45Path::GetLLVMConfigDir() {
46 Path result;
47#ifdef LLVM_ETCDIR
48 if (result.set(LLVM_ETCDIR))
49 return result;
50#endif
51 return GetLLVMDefaultConfigDir();
52}
53
54LLVMFileType
Chris Lattner65b13ff2008-07-09 05:14:23 +000055sys::IdentifyFileType(const char *magic, unsigned length) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 assert(magic && "Invalid magic number string");
57 assert(length >=4 && "Invalid magic number length");
58 switch (magic[0]) {
Chris Lattner65b13ff2008-07-09 05:14:23 +000059 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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 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;
73
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;
83 }
84 }
85 break;
86
87 case 0xCA:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
89 magic[3] == char(0xBE)) {
Chris Lattner59ca9092008-06-26 05:17:18 +000090 // 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;
95 }
96 break;
97
98 case 0xFE:
Bill Wendlinge5a33b22008-06-26 08:32:05 +000099 case 0xCE: {
100 uint16_t type = 0;
Chris Lattner59ca9092008-06-26 05:17:18 +0000101 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];
Bill Wendlinge5a33b22008-06-26 08:32:05 +0000109 }
Chris Lattner59ca9092008-06-26 05:17:18 +0000110 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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 }
123 break;
Bill Wendlinge5a33b22008-06-26 08:32:05 +0000124 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 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;
133
134 case 0x90: // PA-RISC Windows
135 case 0x68: // mc68K Windows
136 if (magic[1] == 0x02)
137 return COFF_FileType;
138 break;
139
140 default:
141 break;
142 }
143 return Unknown_FileType;
144}
145
146bool
147Path::isArchive() const {
148 if (canRead())
149 return hasMagicNumber("!<arch>\012");
150 return false;
151}
152
153bool
154Path::isDynamicLibrary() const {
155 if (canRead()) {
156 std::string Magic;
157 if (getMagicNumber(Magic, 64))
Evan Cheng591bfc82008-05-05 18:30:58 +0000158 switch (IdentifyFileType(Magic.c_str(),
159 static_cast<unsigned>(Magic.length()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 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;
166 }
167 }
168 return false;
169}
170
171Path
172Path::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;
184 }
185 return sys::Path();
186}
187
188std::string Path::GetDLLSuffix() {
189 return LTDL_SHLIB_EXT;
190}
191
192bool
193Path::isBitcodeFile() const {
194 std::string actualMagic;
195 if (!getMagicNumber(actualMagic, 4))
196 return false;
Devang Patel86a79b92008-07-22 18:00:36 +0000197 LLVMFileType FT =
198 IdentifyFileType(actualMagic.c_str(),
199 static_cast<unsigned>(actualMagic.length()));
200 return FT == Bitcode_FileType;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201}
202
203bool Path::hasMagicNumber(const std::string &Magic) const {
204 std::string actualMagic;
Evan Cheng591bfc82008-05-05 18:30:58 +0000205 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 return Magic == actualMagic;
207 return false;
208}
209
Daniel Dunbareef0caf2009-04-09 00:33:08 +0000210void Path::makeAbsolute() {
211 if (isAbsolute())
212 return;
213
214 Path CWD = Path::GetCurrentDirectory();
215 assert(CWD.isAbsolute() && "GetCurrentDirectory returned relative path!");
216
217 CWD.appendComponent(path);
218
219 path = CWD.toString();
220}
221
Chris Lattner4b8f1c62008-02-27 06:17:10 +0000222static 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);
233 }
234
235 if (*at != 0)
236 if (tmpPath.set(std::string(at)))
237 if (tmpPath.canRead())
238 Paths.push_back(tmpPath);
239}
240
Ted Kremenekb169dfa2008-04-07 22:01:32 +0000241static std::string getDirnameCharSep(const std::string& path, char Sep) {
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000242
243 if (path.empty())
244 return ".";
245
246 // If the path is all slashes, return a single slash.
247 // Otherwise, remove all trailing slashes.
248
Evan Cheng591bfc82008-05-05 18:30:58 +0000249 signed pos = static_cast<signed>(path.size()) - 1;
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000250
251 while (pos >= 0 && path[pos] == Sep)
252 --pos;
253
254 if (pos < 0)
255 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
256
257 // Any slashes left?
258 signed i = 0;
259
260 while (i < pos && path[i] != Sep)
261 ++i;
262
263 if (i == pos) // No slashes? Return "."
264 return ".";
265
266 // There is at least one slash left. Remove all trailing non-slashes.
267 while (pos >= 0 && path[pos] != Sep)
268 --pos;
269
270 // Remove any trailing slashes.
271 while (pos >= 0 && path[pos] == Sep)
272 --pos;
273
274 if (pos < 0)
275 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
276
277 return path.substr(0, pos+1);
278}
279
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280// 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
287