blob: 8fc4153acb1ad60a2600e4b2f2a67c3124c971ae [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
Bill Wendling40db5d42008-05-21 21:20:07 +000031bool Path::operator<(const Path& that) const {
32 return path < that.path;
33}
34
Reid Spencerc29befb2004-12-15 01:50:13 +000035Path
36Path::GetLLVMConfigDir() {
37 Path result;
Jeff Cohenab68df02004-12-15 04:08:15 +000038#ifdef LLVM_ETCDIR
Reid Spencerdd04df02005-07-07 23:21:43 +000039 if (result.set(LLVM_ETCDIR))
Reid Spencerc29befb2004-12-15 01:50:13 +000040 return result;
Jeff Cohenab68df02004-12-15 04:08:15 +000041#endif
Reid Spencerc29befb2004-12-15 01:50:13 +000042 return GetLLVMDefaultConfigDir();
43}
44
Misha Brukmanf976c852005-04-21 22:55:34 +000045LLVMFileType
Chris Lattner6fa6a322008-07-09 05:14:23 +000046sys::IdentifyFileType(const char *magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000047 assert(magic && "Invalid magic number string");
48 assert(length >=4 && "Invalid magic number length");
Torok Edwin4cdc44c2009-04-25 10:25:12 +000049 switch ((unsigned char)magic[0]) {
Chris Lattner6fa6a322008-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;
Chris Lattnerf283a5e2007-05-06 05:32:21 +000055 case 'B':
56 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
57 return Bitcode_FileType;
58 break;
Reid Spencer8bb5fd12007-04-04 06:30:26 +000059 case '!':
60 if (length >= 8)
61 if (memcmp(magic,"!<arch>\n",8) == 0)
62 return Archive_FileType;
63 break;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000064
Reid Spencer8bb5fd12007-04-04 06:30:26 +000065 case '\177':
Chris Lattner24eac6c2007-05-03 18:15:56 +000066 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Reid Spencer947aa7d2007-04-11 02:02:09 +000067 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 }
Chris Lattner24eac6c2007-05-03 18:15:56 +000075 }
Reid Spencerf37ce992004-11-14 22:05:32 +000076 break;
77
Chris Lattnerade75922007-04-11 03:15:35 +000078 case 0xCA:
Michael J. Spencer4a295d32010-08-31 06:36:46 +000079 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
Chris Lattnerade75922007-04-11 03:15:35 +000080 magic[3] == char(0xBE)) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +000081 // This is complicated by an overlap with Java class files.
Chris Lattner65215492008-06-26 05:17:18 +000082 // See the Mach-O section in /usr/share/file/magic for details.
Michael J. Spencer4a295d32010-08-31 06:36:46 +000083 if (length >= 8 && magic[7] < 43)
Chris Lattner65215492008-06-26 05:17:18 +000084 // FIXME: Universal Binary of any type.
85 return Mach_O_DynamicallyLinkedSharedLib_FileType;
86 }
87 break;
88
89 case 0xFE:
Bill Wendlingfc1fd542008-06-26 08:32:05 +000090 case 0xCE: {
91 uint16_t type = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000092 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
Chris Lattner65215492008-06-26 05:17:18 +000093 magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
94 /* Native endian */
95 if (length >= 16) type = magic[14] << 8 | magic[15];
Michael J. Spencer4a295d32010-08-31 06:36:46 +000096 } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) &&
Chris Lattner65215492008-06-26 05:17:18 +000097 magic[2] == char(0xED) && magic[3] == char(0xFE)) {
98 /* Reverse endian */
99 if (length >= 14) type = magic[13] << 8 | magic[12];
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000100 }
Chris Lattner65215492008-06-26 05:17:18 +0000101 switch (type) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000102 default: break;
103 case 1: return Mach_O_Object_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000104 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.
Chris Lattnerade75922007-04-11 03:15:35 +0000113 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000114 break;
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000115 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +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;
Reid Spencerf37ce992004-11-14 22:05:32 +0000129 break;
Michael J. Spencer7e7d01d2010-09-15 23:04:14 +0000130 case 0x64: // x86-64 Windows.
131 if (magic[1] == char(0x86))
132 return COFF_FileType;
133 break;
Reid Spencerf37ce992004-11-14 22:05:32 +0000134
135 default:
136 break;
137 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000138 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000139}
140
Reid Spencerccb23a12004-12-13 03:00:39 +0000141bool
142Path::isArchive() const {
Dan Gohman130de9c2010-05-27 17:12:23 +0000143 return hasMagicNumber("!<arch>\012");
Reid Spencerccb23a12004-12-13 03:00:39 +0000144}
145
146bool
147Path::isDynamicLibrary() const {
Dan Gohman130de9c2010-05-27 17:12:23 +0000148 std::string Magic;
149 if (getMagicNumber(Magic, 64))
150 switch (IdentifyFileType(Magic.c_str(),
151 static_cast<unsigned>(Magic.length()))) {
152 default: return false;
153 case Mach_O_FixedVirtualMemorySharedLib_FileType:
154 case Mach_O_DynamicallyLinkedSharedLib_FileType:
155 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
156 case ELF_SharedObject_FileType:
157 case COFF_FileType: return true;
158 }
159
Reid Spencerccb23a12004-12-13 03:00:39 +0000160 return false;
161}
162
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000163bool
164Path::isObjectFile() const {
165 std::string Magic;
166 if (getMagicNumber(Magic, 64))
167 if (IdentifyFileType(Magic.c_str(),
168 static_cast<unsigned>(Magic.length()))
169 != Unknown_FileType) {
170 // Everything in LLVMFileType is currently an object file.
171 return true;
172 }
173
174 return false;
175}
176
Reid Spencerccb23a12004-12-13 03:00:39 +0000177Path
178Path::FindLibrary(std::string& name) {
179 std::vector<sys::Path> LibPaths;
180 GetSystemLibraryPaths(LibPaths);
181 for (unsigned i = 0; i < LibPaths.size(); ++i) {
182 sys::Path FullPath(LibPaths[i]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000183 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000184 if (FullPath.isDynamicLibrary())
185 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000186 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000187 FullPath.appendSuffix("a");
188 if (FullPath.isArchive())
189 return FullPath;
190 }
191 return sys::Path();
192}
193
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000194StringRef Path::GetDLLSuffix() {
Mikhail Glushenkovc8aef4b2010-11-02 20:32:59 +0000195 return &(LTDL_SHLIB_EXT[1]);
Reid Spencer79fc9242004-12-13 18:41:28 +0000196}
197
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000198bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000199Path::isBitcodeFile() const {
200 std::string actualMagic;
201 if (!getMagicNumber(actualMagic, 4))
202 return false;
Devang Patel54ce5362008-07-22 18:00:36 +0000203 LLVMFileType FT =
204 IdentifyFileType(actualMagic.c_str(),
205 static_cast<unsigned>(actualMagic.length()));
206 return FT == Bitcode_FileType;
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000207}
208
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000209bool Path::hasMagicNumber(StringRef Magic) const {
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000210 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000211 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000212 return Magic == actualMagic;
213 return false;
214}
215
Chris Lattnere1b332a2008-02-27 06:17:10 +0000216static void getPathList(const char*path, std::vector<Path>& Paths) {
217 const char* at = path;
218 const char* delim = strchr(at, PathSeparator);
219 Path tmpPath;
220 while (delim != 0) {
221 std::string tmp(at, size_t(delim-at));
222 if (tmpPath.set(tmp))
223 if (tmpPath.canRead())
224 Paths.push_back(tmpPath);
225 at = delim + 1;
226 delim = strchr(at, PathSeparator);
227 }
228
229 if (*at != 0)
230 if (tmpPath.set(std::string(at)))
231 if (tmpPath.canRead())
232 Paths.push_back(tmpPath);
233}
234
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000235static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
236 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
237 "Sep must be a 1-character string literal.");
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000238 if (path.empty())
239 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000240
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000241 // If the path is all slashes, return a single slash.
242 // Otherwise, remove all trailing slashes.
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000243
Evan Cheng34cd4a42008-05-05 18:30:58 +0000244 signed pos = static_cast<signed>(path.size()) - 1;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000245
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000246 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000247 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000248
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000249 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000250 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000251
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000252 // Any slashes left?
253 signed i = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000254
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000255 while (i < pos && path[i] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000256 ++i;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000257
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000258 if (i == pos) // No slashes? Return "."
259 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000260
261 // There is at least one slash left. Remove all trailing non-slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000262 while (pos >= 0 && path[pos] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000263 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000264
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000265 // Remove any trailing slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000266 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000267 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000268
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000269 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000270 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000271
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000272 return path.substr(0, pos+1);
273}
274
Reid Spencerb89a2232004-08-25 06:20:07 +0000275// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000276#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000277#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000278#endif
279#if defined(LLVM_ON_WIN32)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000280#include "Win32/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000281#endif
Reid Spencer23dd3322006-07-26 16:55:39 +0000282