blob: 72bd7ad6f0468c65de9450b5f91dc1d367a46fa3 [file] [log] [blame]
Reid Spencerb89a2232004-08-25 06:20:07 +00001//===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencerb89a2232004-08-25 06:20:07 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencerb89a2232004-08-25 06:20:07 +00008//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system Path concept.
11//
12//===----------------------------------------------------------------------===//
Reid Spencer8e665952004-08-29 05:24:01 +000013
Reid Spencerb89a2232004-08-25 06:20:07 +000014#include "llvm/System/Path.h"
Reid Spencer79fc9242004-12-13 18:41:28 +000015#include "llvm/Config/config.h"
Alkis Evlogimenos98bc8ed2004-11-14 22:37:42 +000016#include <cassert>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000017#include <cstring>
Chris Lattnerc67dc452006-07-07 18:11:32 +000018#include <ostream>
19using namespace llvm;
Reid Spencer8e665952004-08-29 05:24:01 +000020using namespace sys;
Reid Spencerb89a2232004-08-25 06:20:07 +000021
22//===----------------------------------------------------------------------===//
23//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000024//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000025//===----------------------------------------------------------------------===//
26
Bill Wendling40db5d42008-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
Chris Lattnerc67dc452006-07-07 18:11:32 +000039std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
40 strm << aPath.toString();
41 return strm;
42}
43
Reid Spencerc29befb2004-12-15 01:50:13 +000044Path
45Path::GetLLVMConfigDir() {
46 Path result;
Jeff Cohenab68df02004-12-15 04:08:15 +000047#ifdef LLVM_ETCDIR
Reid Spencerdd04df02005-07-07 23:21:43 +000048 if (result.set(LLVM_ETCDIR))
Reid Spencerc29befb2004-12-15 01:50:13 +000049 return result;
Jeff Cohenab68df02004-12-15 04:08:15 +000050#endif
Reid Spencerc29befb2004-12-15 01:50:13 +000051 return GetLLVMDefaultConfigDir();
52}
53
Misha Brukmanf976c852005-04-21 22:55:34 +000054LLVMFileType
Chris Lattner6fa6a322008-07-09 05:14:23 +000055sys::IdentifyFileType(const char *magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000056 assert(magic && "Invalid magic number string");
57 assert(length >=4 && "Invalid magic number length");
Torok Edwin4cdc44c2009-04-25 10:25:12 +000058 switch ((unsigned char)magic[0]) {
Chris Lattner6fa6a322008-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;
Chris Lattnerf283a5e2007-05-06 05:32:21 +000064 case 'B':
65 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
66 return Bitcode_FileType;
67 break;
Reid Spencer8bb5fd12007-04-04 06:30:26 +000068 case '!':
69 if (length >= 8)
70 if (memcmp(magic,"!<arch>\n",8) == 0)
71 return Archive_FileType;
72 break;
73
74 case '\177':
Chris Lattner24eac6c2007-05-03 18:15:56 +000075 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Reid Spencer947aa7d2007-04-11 02:02:09 +000076 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 }
Chris Lattner24eac6c2007-05-03 18:15:56 +000084 }
Reid Spencerf37ce992004-11-14 22:05:32 +000085 break;
86
Chris Lattnerade75922007-04-11 03:15:35 +000087 case 0xCA:
Chris Lattnerade75922007-04-11 03:15:35 +000088 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
89 magic[3] == char(0xBE)) {
Chris Lattner65215492008-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 Wendlingfc1fd542008-06-26 08:32:05 +000099 case 0xCE: {
100 uint16_t type = 0;
Chris Lattner65215492008-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 Wendlingfc1fd542008-06-26 08:32:05 +0000109 }
Chris Lattner65215492008-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.
Chris Lattnerade75922007-04-11 03:15:35 +0000122 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000123 break;
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000124 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +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;
Reid Spencerf37ce992004-11-14 22:05:32 +0000138 break;
139
140 default:
141 break;
142 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000143 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000144}
145
Reid Spencerccb23a12004-12-13 03:00:39 +0000146bool
147Path::isArchive() const {
Reid Spencerc7f08322005-07-07 18:21:42 +0000148 if (canRead())
Reid Spencerccb23a12004-12-13 03:00:39 +0000149 return hasMagicNumber("!<arch>\012");
150 return false;
151}
152
153bool
154Path::isDynamicLibrary() const {
Reid Spencer410aa022007-04-11 00:49:39 +0000155 if (canRead()) {
156 std::string Magic;
157 if (getMagicNumber(Magic, 64))
Evan Cheng34cd4a42008-05-05 18:30:58 +0000158 switch (IdentifyFileType(Magic.c_str(),
159 static_cast<unsigned>(Magic.length()))) {
Reid Spencer410aa022007-04-11 00:49:39 +0000160 default: return false;
Reid Spencer947aa7d2007-04-11 02:02:09 +0000161 case Mach_O_FixedVirtualMemorySharedLib_FileType:
162 case Mach_O_DynamicallyLinkedSharedLib_FileType:
163 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
164 case ELF_SharedObject_FileType:
Reid Spencer410aa022007-04-11 00:49:39 +0000165 case COFF_FileType: return true;
166 }
167 }
Reid Spencerccb23a12004-12-13 03:00:39 +0000168 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]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000177 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000178 if (FullPath.isDynamicLibrary())
179 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000180 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000181 FullPath.appendSuffix("a");
182 if (FullPath.isArchive())
183 return FullPath;
184 }
185 return sys::Path();
186}
187
Chris Lattnerc67dc452006-07-07 18:11:32 +0000188std::string Path::GetDLLSuffix() {
Reid Spencer79fc9242004-12-13 18:41:28 +0000189 return LTDL_SHLIB_EXT;
190}
191
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000192bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000193Path::isBitcodeFile() const {
194 std::string actualMagic;
195 if (!getMagicNumber(actualMagic, 4))
196 return false;
Devang Patel54ce5362008-07-22 18:00:36 +0000197 LLVMFileType FT =
198 IdentifyFileType(actualMagic.c_str(),
199 static_cast<unsigned>(actualMagic.length()));
200 return FT == Bitcode_FileType;
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000201}
202
203bool Path::hasMagicNumber(const std::string &Magic) const {
204 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000205 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000206 return Magic == actualMagic;
207 return false;
208}
209
Daniel Dunbar6ddf2c12009-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 Lattnere1b332a2008-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 Kremenek9b01cc02008-04-07 22:01:32 +0000241static std::string getDirnameCharSep(const std::string& path, char Sep) {
Ted Kremenekcf55c8e2008-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 Cheng34cd4a42008-05-05 18:30:58 +0000249 signed pos = static_cast<signed>(path.size()) - 1;
Ted Kremenekcf55c8e2008-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
Reid Spencerb89a2232004-08-25 06:20:07 +0000280// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000281#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000282#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000283#endif
284#if defined(LLVM_ON_WIN32)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000285#include "Win32/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000286#endif
Reid Spencer23dd3322006-07-26 16:55:39 +0000287