blob: ce4762e56d84ce1d4ad59aa93f7217b525fc5be3 [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"
Daniel Dunbarbf8e8712009-07-12 20:23:56 +000016#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include <cassert>
Duncan Sandsfca20142008-01-09 19:42:09 +000018#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include <ostream>
20using namespace llvm;
21using namespace sys;
22
23//===----------------------------------------------------------------------===//
24//=== WARNING: Implementation here must contain only TRULY operating system
25//=== independent code.
26//===----------------------------------------------------------------------===//
27
Bill Wendling0df675d2008-05-21 21:20:07 +000028bool Path::operator==(const Path &that) const {
29 return path == that.path;
30}
31
32bool Path::operator!=(const Path &that) const {
33 return path != that.path;
34}
35
36bool Path::operator<(const Path& that) const {
37 return path < that.path;
38}
39
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
41 strm << aPath.toString();
42 return strm;
43}
44
45Path
46Path::GetLLVMConfigDir() {
47 Path result;
48#ifdef LLVM_ETCDIR
49 if (result.set(LLVM_ETCDIR))
50 return result;
51#endif
52 return GetLLVMDefaultConfigDir();
53}
54
55LLVMFileType
Chris Lattner65b13ff2008-07-09 05:14:23 +000056sys::IdentifyFileType(const char *magic, unsigned length) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 assert(magic && "Invalid magic number string");
58 assert(length >=4 && "Invalid magic number length");
Edwin Törökb2d71d72009-04-25 10:25:12 +000059 switch ((unsigned char)magic[0]) {
Chris Lattner65b13ff2008-07-09 05:14:23 +000060 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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 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:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
90 magic[3] == char(0xBE)) {
Chris Lattner59ca9092008-06-26 05:17:18 +000091 // 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:
Bill Wendlinge5a33b22008-06-26 08:32:05 +0000100 case 0xCE: {
101 uint16_t type = 0;
Chris Lattner59ca9092008-06-26 05:17:18 +0000102 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];
Bill Wendlinge5a33b22008-06-26 08:32:05 +0000110 }
Chris Lattner59ca9092008-06-26 05:17:18 +0000111 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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 }
124 break;
Bill Wendlinge5a33b22008-06-26 08:32:05 +0000125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 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
147bool
148Path::isArchive() const {
149 if (canRead())
150 return hasMagicNumber("!<arch>\012");
151 return false;
152}
153
154bool
155Path::isDynamicLibrary() const {
156 if (canRead()) {
157 std::string Magic;
158 if (getMagicNumber(Magic, 64))
Evan Cheng591bfc82008-05-05 18:30:58 +0000159 switch (IdentifyFileType(Magic.c_str(),
160 static_cast<unsigned>(Magic.length()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 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
172Path
173Path::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
189std::string Path::GetDLLSuffix() {
190 return LTDL_SHLIB_EXT;
191}
192
193bool
194Path::isBitcodeFile() const {
195 std::string actualMagic;
196 if (!getMagicNumber(actualMagic, 4))
197 return false;
Devang Patel86a79b92008-07-22 18:00:36 +0000198 LLVMFileType FT =
199 IdentifyFileType(actualMagic.c_str(),
200 static_cast<unsigned>(actualMagic.length()));
201 return FT == Bitcode_FileType;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202}
203
204bool Path::hasMagicNumber(const std::string &Magic) const {
205 std::string actualMagic;
Evan Cheng591bfc82008-05-05 18:30:58 +0000206 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 return Magic == actualMagic;
208 return false;
209}
210
Chris Lattner4b8f1c62008-02-27 06:17:10 +0000211static 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
Ted Kremenekb169dfa2008-04-07 22:01:32 +0000230static std::string getDirnameCharSep(const std::string& path, char Sep) {
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000231
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
Evan Cheng591bfc82008-05-05 18:30:58 +0000238 signed pos = static_cast<signed>(path.size()) - 1;
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000239
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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269// 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