blob: 4689208ded31d28dcdb6287cc2e3536b61601bc0 [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
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000014#include "llvm/Support/Path.h"
Reid Spencer79fc9242004-12-13 18:41:28 +000015#include "llvm/Config/config.h"
Michael J. Spencer54453f22011-01-10 02:34:23 +000016#include "llvm/Support/FileSystem.h"
Alkis Evlogimenos98bc8ed2004-11-14 22:37:42 +000017#include <cassert>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000018#include <cstring>
Chris Lattnerc67dc452006-07-07 18:11:32 +000019#include <ostream>
20using namespace llvm;
Reid Spencer8e665952004-08-29 05:24:01 +000021using namespace sys;
Reid Spencerb89a2232004-08-25 06:20:07 +000022
23//===----------------------------------------------------------------------===//
24//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000025//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000026//===----------------------------------------------------------------------===//
27
Bill Wendling40db5d42008-05-21 21:20:07 +000028bool Path::operator==(const Path &that) const {
29 return path == that.path;
30}
31
Bill Wendling40db5d42008-05-21 21:20:07 +000032bool Path::operator<(const Path& that) const {
33 return path < that.path;
34}
35
Reid Spencerc29befb2004-12-15 01:50:13 +000036Path
37Path::GetLLVMConfigDir() {
38 Path result;
Jeff Cohenab68df02004-12-15 04:08:15 +000039#ifdef LLVM_ETCDIR
Reid Spencerdd04df02005-07-07 23:21:43 +000040 if (result.set(LLVM_ETCDIR))
Reid Spencerc29befb2004-12-15 01:50:13 +000041 return result;
Jeff Cohenab68df02004-12-15 04:08:15 +000042#endif
Reid Spencerc29befb2004-12-15 01:50:13 +000043 return GetLLVMDefaultConfigDir();
44}
45
Misha Brukmanf976c852005-04-21 22:55:34 +000046LLVMFileType
Chris Lattner6fa6a322008-07-09 05:14:23 +000047sys::IdentifyFileType(const char *magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000048 assert(magic && "Invalid magic number string");
49 assert(length >=4 && "Invalid magic number length");
Torok Edwin4cdc44c2009-04-25 10:25:12 +000050 switch ((unsigned char)magic[0]) {
Chris Lattner6fa6a322008-07-09 05:14:23 +000051 case 0xDE: // 0x0B17C0DE = BC wraper
52 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
53 magic[3] == (char)0x0B)
54 return Bitcode_FileType;
55 break;
Chris Lattnerf283a5e2007-05-06 05:32:21 +000056 case 'B':
57 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
58 return Bitcode_FileType;
59 break;
Reid Spencer8bb5fd12007-04-04 06:30:26 +000060 case '!':
61 if (length >= 8)
62 if (memcmp(magic,"!<arch>\n",8) == 0)
63 return Archive_FileType;
64 break;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000065
Reid Spencer8bb5fd12007-04-04 06:30:26 +000066 case '\177':
Chris Lattner24eac6c2007-05-03 18:15:56 +000067 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Reid Spencer947aa7d2007-04-11 02:02:09 +000068 if (length >= 18 && magic[17] == 0)
69 switch (magic[16]) {
70 default: break;
71 case 1: return ELF_Relocatable_FileType;
72 case 2: return ELF_Executable_FileType;
73 case 3: return ELF_SharedObject_FileType;
74 case 4: return ELF_Core_FileType;
75 }
Chris Lattner24eac6c2007-05-03 18:15:56 +000076 }
Reid Spencerf37ce992004-11-14 22:05:32 +000077 break;
78
Chris Lattnerade75922007-04-11 03:15:35 +000079 case 0xCA:
Michael J. Spencer4a295d32010-08-31 06:36:46 +000080 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
Chris Lattnerade75922007-04-11 03:15:35 +000081 magic[3] == char(0xBE)) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +000082 // This is complicated by an overlap with Java class files.
Chris Lattner65215492008-06-26 05:17:18 +000083 // See the Mach-O section in /usr/share/file/magic for details.
Michael J. Spencer4a295d32010-08-31 06:36:46 +000084 if (length >= 8 && magic[7] < 43)
Chris Lattner65215492008-06-26 05:17:18 +000085 // FIXME: Universal Binary of any type.
86 return Mach_O_DynamicallyLinkedSharedLib_FileType;
87 }
88 break;
89
90 case 0xFE:
Bill Wendlingfc1fd542008-06-26 08:32:05 +000091 case 0xCE: {
92 uint16_t type = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000093 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
Chris Lattner65215492008-06-26 05:17:18 +000094 magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
95 /* Native endian */
96 if (length >= 16) type = magic[14] << 8 | magic[15];
Michael J. Spencer4a295d32010-08-31 06:36:46 +000097 } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) &&
Chris Lattner65215492008-06-26 05:17:18 +000098 magic[2] == char(0xED) && magic[3] == char(0xFE)) {
99 /* Reverse endian */
100 if (length >= 14) type = magic[13] << 8 | magic[12];
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000101 }
Chris Lattner65215492008-06-26 05:17:18 +0000102 switch (type) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000103 default: break;
104 case 1: return Mach_O_Object_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000105 case 2: return Mach_O_Executable_FileType;
106 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
107 case 4: return Mach_O_Core_FileType;
Peter Collingbourneb1a33c42010-11-17 00:43:43 +0000108 case 5: return Mach_O_PreloadExecutable_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000109 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
110 case 7: return Mach_O_DynamicLinker_FileType;
111 case 8: return Mach_O_Bundle_FileType;
112 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
113 case 10: break; // FIXME: MH_DSYM companion file with only debug.
Chris Lattnerade75922007-04-11 03:15:35 +0000114 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000115 break;
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000116 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000117 case 0xF0: // PowerPC Windows
118 case 0x83: // Alpha 32-bit
119 case 0x84: // Alpha 64-bit
120 case 0x66: // MPS R4000 Windows
121 case 0x50: // mc68K
122 case 0x4c: // 80386 Windows
123 if (magic[1] == 0x01)
124 return COFF_FileType;
125
126 case 0x90: // PA-RISC Windows
127 case 0x68: // mc68K Windows
128 if (magic[1] == 0x02)
129 return COFF_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000130 break;
Michael J. Spencer7e7d01d2010-09-15 23:04:14 +0000131 case 0x64: // x86-64 Windows.
132 if (magic[1] == char(0x86))
133 return COFF_FileType;
134 break;
Reid Spencerf37ce992004-11-14 22:05:32 +0000135
136 default:
137 break;
138 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000139 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000140}
141
Reid Spencerccb23a12004-12-13 03:00:39 +0000142bool
143Path::isArchive() const {
Michael J. Spencer42136182010-12-28 01:49:21 +0000144 std::string Magic;
145 if (getMagicNumber(Magic, 8))
146 if (IdentifyFileType(Magic.c_str(), Magic.length()) == Archive_FileType)
147 return true;
148 return false;
Reid Spencerccb23a12004-12-13 03:00:39 +0000149}
150
151bool
152Path::isDynamicLibrary() const {
Dan Gohman130de9c2010-05-27 17:12:23 +0000153 std::string Magic;
154 if (getMagicNumber(Magic, 64))
155 switch (IdentifyFileType(Magic.c_str(),
156 static_cast<unsigned>(Magic.length()))) {
157 default: return false;
158 case Mach_O_FixedVirtualMemorySharedLib_FileType:
159 case Mach_O_DynamicallyLinkedSharedLib_FileType:
160 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
161 case ELF_SharedObject_FileType:
162 case COFF_FileType: return true;
163 }
164
Reid Spencerccb23a12004-12-13 03:00:39 +0000165 return false;
166}
167
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000168bool
169Path::isObjectFile() const {
170 std::string Magic;
171 if (getMagicNumber(Magic, 64))
172 if (IdentifyFileType(Magic.c_str(),
173 static_cast<unsigned>(Magic.length()))
174 != Unknown_FileType) {
175 // Everything in LLVMFileType is currently an object file.
176 return true;
177 }
178
179 return false;
180}
181
Reid Spencerccb23a12004-12-13 03:00:39 +0000182Path
183Path::FindLibrary(std::string& name) {
184 std::vector<sys::Path> LibPaths;
185 GetSystemLibraryPaths(LibPaths);
186 for (unsigned i = 0; i < LibPaths.size(); ++i) {
187 sys::Path FullPath(LibPaths[i]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000188 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000189 if (FullPath.isDynamicLibrary())
190 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000191 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000192 FullPath.appendSuffix("a");
193 if (FullPath.isArchive())
194 return FullPath;
195 }
196 return sys::Path();
197}
198
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000199StringRef Path::GetDLLSuffix() {
Mikhail Glushenkovc8aef4b2010-11-02 20:32:59 +0000200 return &(LTDL_SHLIB_EXT[1]);
Reid Spencer79fc9242004-12-13 18:41:28 +0000201}
202
Dan Gohman552a3c22010-12-01 02:46:41 +0000203void
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000204Path::appendSuffix(StringRef suffix) {
205 if (!suffix.empty()) {
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000206 path.append(".");
207 path.append(suffix);
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000208 }
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000209}
210
211bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000212Path::isBitcodeFile() const {
213 std::string actualMagic;
214 if (!getMagicNumber(actualMagic, 4))
215 return false;
Devang Patel54ce5362008-07-22 18:00:36 +0000216 LLVMFileType FT =
217 IdentifyFileType(actualMagic.c_str(),
218 static_cast<unsigned>(actualMagic.length()));
219 return FT == Bitcode_FileType;
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000220}
221
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000222bool Path::hasMagicNumber(StringRef Magic) const {
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000223 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000224 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000225 return Magic == actualMagic;
226 return false;
227}
228
Chris Lattnere1b332a2008-02-27 06:17:10 +0000229static void getPathList(const char*path, std::vector<Path>& Paths) {
230 const char* at = path;
231 const char* delim = strchr(at, PathSeparator);
232 Path tmpPath;
233 while (delim != 0) {
234 std::string tmp(at, size_t(delim-at));
235 if (tmpPath.set(tmp))
236 if (tmpPath.canRead())
237 Paths.push_back(tmpPath);
238 at = delim + 1;
239 delim = strchr(at, PathSeparator);
240 }
241
242 if (*at != 0)
243 if (tmpPath.set(std::string(at)))
244 if (tmpPath.canRead())
245 Paths.push_back(tmpPath);
246}
247
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000248static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
249 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
250 "Sep must be a 1-character string literal.");
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000251 if (path.empty())
252 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000253
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000254 // If the path is all slashes, return a single slash.
255 // Otherwise, remove all trailing slashes.
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000256
Evan Cheng34cd4a42008-05-05 18:30:58 +0000257 signed pos = static_cast<signed>(path.size()) - 1;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000258
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000259 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000260 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000261
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000262 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000263 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000264
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000265 // Any slashes left?
266 signed i = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000267
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000268 while (i < pos && path[i] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000269 ++i;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000270
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000271 if (i == pos) // No slashes? Return "."
272 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000273
274 // There is at least one slash left. Remove all trailing non-slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000275 while (pos >= 0 && path[pos] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000276 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000277
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000278 // Remove any trailing slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000279 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000280 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000281
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000282 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000283 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000284
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000285 return path.substr(0, pos+1);
286}
287
Reid Spencerb89a2232004-08-25 06:20:07 +0000288// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000289#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000290#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000291#endif
292#if defined(LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000293#include "Windows/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000294#endif