blob: 1235257b27e20a3b3d6de7c57178b2e5e114ef12 [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
Bill Wendling0df675d2008-05-21 21:20:07 +000031bool Path::operator<(const Path& that) const {
32 return path < that.path;
33}
34
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035Path
36Path::GetLLVMConfigDir() {
37 Path result;
38#ifdef LLVM_ETCDIR
39 if (result.set(LLVM_ETCDIR))
40 return result;
41#endif
42 return GetLLVMDefaultConfigDir();
43}
44
45LLVMFileType
Chris Lattner65b13ff2008-07-09 05:14:23 +000046sys::IdentifyFileType(const char *magic, unsigned length) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 assert(magic && "Invalid magic number string");
48 assert(length >=4 && "Invalid magic number length");
Edwin Törökb2d71d72009-04-25 10:25:12 +000049 switch ((unsigned char)magic[0]) {
Chris Lattner65b13ff2008-07-09 05:14:23 +000050 case 0xDE: // 0x0B17C0DE = BC wraper
51 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
52 magic[3] == (char)0x0B)
53 return Bitcode_FileType;
54 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 case 'B':
56 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
57 return Bitcode_FileType;
58 break;
59 case '!':
60 if (length >= 8)
61 if (memcmp(magic,"!<arch>\n",8) == 0)
62 return Archive_FileType;
63 break;
64
65 case '\177':
66 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
67 if (length >= 18 && magic[17] == 0)
68 switch (magic[16]) {
69 default: break;
70 case 1: return ELF_Relocatable_FileType;
71 case 2: return ELF_Executable_FileType;
72 case 3: return ELF_SharedObject_FileType;
73 case 4: return ELF_Core_FileType;
74 }
75 }
76 break;
77
78 case 0xCA:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
80 magic[3] == char(0xBE)) {
Chris Lattner59ca9092008-06-26 05:17:18 +000081 // This is complicated by an overlap with Java class files.
82 // See the Mach-O section in /usr/share/file/magic for details.
83 if (length >= 8 && magic[7] < 43)
84 // FIXME: Universal Binary of any type.
85 return Mach_O_DynamicallyLinkedSharedLib_FileType;
86 }
87 break;
88
89 case 0xFE:
Bill Wendlinge5a33b22008-06-26 08:32:05 +000090 case 0xCE: {
91 uint16_t type = 0;
Chris Lattner59ca9092008-06-26 05:17:18 +000092 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
93 magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
94 /* Native endian */
95 if (length >= 16) type = magic[14] << 8 | magic[15];
96 } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) &&
97 magic[2] == char(0xED) && magic[3] == char(0xFE)) {
98 /* Reverse endian */
99 if (length >= 14) type = magic[13] << 8 | magic[12];
Bill Wendlinge5a33b22008-06-26 08:32:05 +0000100 }
Chris Lattner59ca9092008-06-26 05:17:18 +0000101 switch (type) {
102 default: break;
103 case 1: return Mach_O_Object_FileType;
104 case 2: return Mach_O_Executable_FileType;
105 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
106 case 4: return Mach_O_Core_FileType;
107 case 5: return Mach_O_PreloadExectuable_FileType;
108 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
109 case 7: return Mach_O_DynamicLinker_FileType;
110 case 8: return Mach_O_Bundle_FileType;
111 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
112 case 10: break; // FIXME: MH_DSYM companion file with only debug.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 }
114 break;
Bill Wendlinge5a33b22008-06-26 08:32:05 +0000115 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 case 0xF0: // PowerPC Windows
117 case 0x83: // Alpha 32-bit
118 case 0x84: // Alpha 64-bit
119 case 0x66: // MPS R4000 Windows
120 case 0x50: // mc68K
121 case 0x4c: // 80386 Windows
122 if (magic[1] == 0x01)
123 return COFF_FileType;
124
125 case 0x90: // PA-RISC Windows
126 case 0x68: // mc68K Windows
127 if (magic[1] == 0x02)
128 return COFF_FileType;
129 break;
130
131 default:
132 break;
133 }
134 return Unknown_FileType;
135}
136
137bool
138Path::isArchive() const {
Dan Gohman664c1d92010-05-27 17:12:23 +0000139 return hasMagicNumber("!<arch>\012");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140}
141
142bool
143Path::isDynamicLibrary() const {
Dan Gohman664c1d92010-05-27 17:12:23 +0000144 std::string Magic;
145 if (getMagicNumber(Magic, 64))
146 switch (IdentifyFileType(Magic.c_str(),
147 static_cast<unsigned>(Magic.length()))) {
148 default: return false;
149 case Mach_O_FixedVirtualMemorySharedLib_FileType:
150 case Mach_O_DynamicallyLinkedSharedLib_FileType:
151 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
152 case ELF_SharedObject_FileType:
153 case COFF_FileType: return true;
154 }
155
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 return false;
157}
158
159Path
160Path::FindLibrary(std::string& name) {
161 std::vector<sys::Path> LibPaths;
162 GetSystemLibraryPaths(LibPaths);
163 for (unsigned i = 0; i < LibPaths.size(); ++i) {
164 sys::Path FullPath(LibPaths[i]);
165 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
166 if (FullPath.isDynamicLibrary())
167 return FullPath;
168 FullPath.eraseSuffix();
169 FullPath.appendSuffix("a");
170 if (FullPath.isArchive())
171 return FullPath;
172 }
173 return sys::Path();
174}
175
Jeffrey Yasskinb36523a2009-12-17 21:02:39 +0000176StringRef Path::GetDLLSuffix() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 return LTDL_SHLIB_EXT;
178}
179
180bool
181Path::isBitcodeFile() const {
182 std::string actualMagic;
183 if (!getMagicNumber(actualMagic, 4))
184 return false;
Devang Patel86a79b92008-07-22 18:00:36 +0000185 LLVMFileType FT =
186 IdentifyFileType(actualMagic.c_str(),
187 static_cast<unsigned>(actualMagic.length()));
188 return FT == Bitcode_FileType;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189}
190
Jeffrey Yasskinb36523a2009-12-17 21:02:39 +0000191bool Path::hasMagicNumber(StringRef Magic) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 std::string actualMagic;
Evan Cheng591bfc82008-05-05 18:30:58 +0000193 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 return Magic == actualMagic;
195 return false;
196}
197
Chris Lattner4b8f1c62008-02-27 06:17:10 +0000198static void getPathList(const char*path, std::vector<Path>& Paths) {
199 const char* at = path;
200 const char* delim = strchr(at, PathSeparator);
201 Path tmpPath;
202 while (delim != 0) {
203 std::string tmp(at, size_t(delim-at));
204 if (tmpPath.set(tmp))
205 if (tmpPath.canRead())
206 Paths.push_back(tmpPath);
207 at = delim + 1;
208 delim = strchr(at, PathSeparator);
209 }
210
211 if (*at != 0)
212 if (tmpPath.set(std::string(at)))
213 if (tmpPath.canRead())
214 Paths.push_back(tmpPath);
215}
216
Jeffrey Yasskinb36523a2009-12-17 21:02:39 +0000217static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
218 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
219 "Sep must be a 1-character string literal.");
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000220 if (path.empty())
221 return ".";
222
223 // If the path is all slashes, return a single slash.
224 // Otherwise, remove all trailing slashes.
225
Evan Cheng591bfc82008-05-05 18:30:58 +0000226 signed pos = static_cast<signed>(path.size()) - 1;
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000227
Jeffrey Yasskinb36523a2009-12-17 21:02:39 +0000228 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000229 --pos;
230
231 if (pos < 0)
Jeffrey Yasskinb36523a2009-12-17 21:02:39 +0000232 return path[0] == Sep[0] ? Sep : ".";
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000233
234 // Any slashes left?
235 signed i = 0;
236
Jeffrey Yasskinb36523a2009-12-17 21:02:39 +0000237 while (i < pos && path[i] != Sep[0])
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000238 ++i;
239
240 if (i == pos) // No slashes? Return "."
241 return ".";
242
243 // There is at least one slash left. Remove all trailing non-slashes.
Jeffrey Yasskinb36523a2009-12-17 21:02:39 +0000244 while (pos >= 0 && path[pos] != Sep[0])
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000245 --pos;
246
247 // Remove any trailing slashes.
Jeffrey Yasskinb36523a2009-12-17 21:02:39 +0000248 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000249 --pos;
250
251 if (pos < 0)
Jeffrey Yasskinb36523a2009-12-17 21:02:39 +0000252 return path[0] == Sep[0] ? Sep : ".";
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000253
254 return path.substr(0, pos+1);
255}
256
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257// Include the truly platform-specific parts of this class.
258#if defined(LLVM_ON_UNIX)
259#include "Unix/Path.inc"
260#endif
261#if defined(LLVM_ON_WIN32)
262#include "Win32/Path.inc"
263#endif
264