blob: e5e875bc54d7d9af774a127d45ad048a11a000b5 [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"
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000015#include "llvm/Support/FileSystem.h"
Reid Spencer79fc9242004-12-13 18:41:28 +000016#include "llvm/Config/config.h"
Michael J. Spencer54453f22011-01-10 02:34:23 +000017#include "llvm/Support/FileSystem.h"
Alkis Evlogimenos98bc8ed2004-11-14 22:37:42 +000018#include <cassert>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000019#include <cstring>
Chris Lattnerc67dc452006-07-07 18:11:32 +000020#include <ostream>
21using namespace llvm;
Reid Spencer8e665952004-08-29 05:24:01 +000022using namespace sys;
Reid Spencerb89a2232004-08-25 06:20:07 +000023
24//===----------------------------------------------------------------------===//
25//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000026//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000027//===----------------------------------------------------------------------===//
28
Bill Wendling40db5d42008-05-21 21:20:07 +000029bool Path::operator==(const Path &that) const {
30 return path == that.path;
31}
32
Bill Wendling40db5d42008-05-21 21:20:07 +000033bool Path::operator<(const Path& that) const {
34 return path < that.path;
35}
36
Reid Spencerc29befb2004-12-15 01:50:13 +000037Path
38Path::GetLLVMConfigDir() {
39 Path result;
Jeff Cohenab68df02004-12-15 04:08:15 +000040#ifdef LLVM_ETCDIR
Reid Spencerdd04df02005-07-07 23:21:43 +000041 if (result.set(LLVM_ETCDIR))
Reid Spencerc29befb2004-12-15 01:50:13 +000042 return result;
Jeff Cohenab68df02004-12-15 04:08:15 +000043#endif
Reid Spencerc29befb2004-12-15 01:50:13 +000044 return GetLLVMDefaultConfigDir();
45}
46
Misha Brukmanf976c852005-04-21 22:55:34 +000047LLVMFileType
Chris Lattner6fa6a322008-07-09 05:14:23 +000048sys::IdentifyFileType(const char *magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000049 assert(magic && "Invalid magic number string");
50 assert(length >=4 && "Invalid magic number length");
Torok Edwin4cdc44c2009-04-25 10:25:12 +000051 switch ((unsigned char)magic[0]) {
Chris Lattner6fa6a322008-07-09 05:14:23 +000052 case 0xDE: // 0x0B17C0DE = BC wraper
53 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
54 magic[3] == (char)0x0B)
55 return Bitcode_FileType;
56 break;
Chris Lattnerf283a5e2007-05-06 05:32:21 +000057 case 'B':
58 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
59 return Bitcode_FileType;
60 break;
Reid Spencer8bb5fd12007-04-04 06:30:26 +000061 case '!':
62 if (length >= 8)
63 if (memcmp(magic,"!<arch>\n",8) == 0)
64 return Archive_FileType;
65 break;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000066
Reid Spencer8bb5fd12007-04-04 06:30:26 +000067 case '\177':
Chris Lattner24eac6c2007-05-03 18:15:56 +000068 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Reid Spencer947aa7d2007-04-11 02:02:09 +000069 if (length >= 18 && magic[17] == 0)
70 switch (magic[16]) {
71 default: break;
72 case 1: return ELF_Relocatable_FileType;
73 case 2: return ELF_Executable_FileType;
74 case 3: return ELF_SharedObject_FileType;
75 case 4: return ELF_Core_FileType;
76 }
Chris Lattner24eac6c2007-05-03 18:15:56 +000077 }
Reid Spencerf37ce992004-11-14 22:05:32 +000078 break;
79
Chris Lattnerade75922007-04-11 03:15:35 +000080 case 0xCA:
Michael J. Spencer4a295d32010-08-31 06:36:46 +000081 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
Chris Lattnerade75922007-04-11 03:15:35 +000082 magic[3] == char(0xBE)) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +000083 // This is complicated by an overlap with Java class files.
Chris Lattner65215492008-06-26 05:17:18 +000084 // See the Mach-O section in /usr/share/file/magic for details.
Michael J. Spencer4a295d32010-08-31 06:36:46 +000085 if (length >= 8 && magic[7] < 43)
Chris Lattner65215492008-06-26 05:17:18 +000086 // FIXME: Universal Binary of any type.
87 return Mach_O_DynamicallyLinkedSharedLib_FileType;
88 }
89 break;
90
91 case 0xFE:
Bill Wendlingfc1fd542008-06-26 08:32:05 +000092 case 0xCE: {
93 uint16_t type = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000094 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
Chris Lattner65215492008-06-26 05:17:18 +000095 magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
96 /* Native endian */
97 if (length >= 16) type = magic[14] << 8 | magic[15];
Michael J. Spencer4a295d32010-08-31 06:36:46 +000098 } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) &&
Chris Lattner65215492008-06-26 05:17:18 +000099 magic[2] == char(0xED) && magic[3] == char(0xFE)) {
100 /* Reverse endian */
101 if (length >= 14) type = magic[13] << 8 | magic[12];
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000102 }
Chris Lattner65215492008-06-26 05:17:18 +0000103 switch (type) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000104 default: break;
105 case 1: return Mach_O_Object_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000106 case 2: return Mach_O_Executable_FileType;
107 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
108 case 4: return Mach_O_Core_FileType;
Peter Collingbourneb1a33c42010-11-17 00:43:43 +0000109 case 5: return Mach_O_PreloadExecutable_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000110 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
111 case 7: return Mach_O_DynamicLinker_FileType;
112 case 8: return Mach_O_Bundle_FileType;
113 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
114 case 10: break; // FIXME: MH_DSYM companion file with only debug.
Chris Lattnerade75922007-04-11 03:15:35 +0000115 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000116 break;
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000117 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000118 case 0xF0: // PowerPC Windows
119 case 0x83: // Alpha 32-bit
120 case 0x84: // Alpha 64-bit
121 case 0x66: // MPS R4000 Windows
122 case 0x50: // mc68K
123 case 0x4c: // 80386 Windows
124 if (magic[1] == 0x01)
125 return COFF_FileType;
126
127 case 0x90: // PA-RISC Windows
128 case 0x68: // mc68K Windows
129 if (magic[1] == 0x02)
130 return COFF_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000131 break;
Michael J. Spencer7e7d01d2010-09-15 23:04:14 +0000132 case 0x64: // x86-64 Windows.
133 if (magic[1] == char(0x86))
134 return COFF_FileType;
135 break;
Reid Spencerf37ce992004-11-14 22:05:32 +0000136
137 default:
138 break;
139 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000140 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000141}
142
Reid Spencerccb23a12004-12-13 03:00:39 +0000143bool
144Path::isArchive() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000145 LLVMFileType type;
146 if (fs::identify_magic(str(), type))
147 return false;
148 return type == Archive_FileType;
Reid Spencerccb23a12004-12-13 03:00:39 +0000149}
150
151bool
152Path::isDynamicLibrary() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000153 LLVMFileType type;
154 if (fs::identify_magic(str(), type))
155 return false;
156 switch (type) {
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 }
Reid Spencerccb23a12004-12-13 03:00:39 +0000164}
165
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000166bool
167Path::isObjectFile() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000168 LLVMFileType type;
169 if (fs::identify_magic(str(), type) || type == Unknown_FileType)
170 return false;
171 return true;
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000172}
173
Reid Spencerccb23a12004-12-13 03:00:39 +0000174Path
175Path::FindLibrary(std::string& name) {
176 std::vector<sys::Path> LibPaths;
177 GetSystemLibraryPaths(LibPaths);
178 for (unsigned i = 0; i < LibPaths.size(); ++i) {
179 sys::Path FullPath(LibPaths[i]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000180 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000181 if (FullPath.isDynamicLibrary())
182 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000183 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000184 FullPath.appendSuffix("a");
185 if (FullPath.isArchive())
186 return FullPath;
187 }
188 return sys::Path();
189}
190
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000191StringRef Path::GetDLLSuffix() {
Mikhail Glushenkovc8aef4b2010-11-02 20:32:59 +0000192 return &(LTDL_SHLIB_EXT[1]);
Reid Spencer79fc9242004-12-13 18:41:28 +0000193}
194
Dan Gohman552a3c22010-12-01 02:46:41 +0000195void
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000196Path::appendSuffix(StringRef suffix) {
197 if (!suffix.empty()) {
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000198 path.append(".");
199 path.append(suffix);
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000200 }
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000201}
202
203bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000204Path::isBitcodeFile() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000205 LLVMFileType type;
206 if (fs::identify_magic(str(), type))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000207 return false;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000208 return type == Bitcode_FileType;
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000209}
210
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000211bool Path::hasMagicNumber(StringRef Magic) const {
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000212 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000213 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000214 return Magic == actualMagic;
215 return false;
216}
217
Chris Lattnere1b332a2008-02-27 06:17:10 +0000218static void getPathList(const char*path, std::vector<Path>& Paths) {
219 const char* at = path;
220 const char* delim = strchr(at, PathSeparator);
221 Path tmpPath;
222 while (delim != 0) {
223 std::string tmp(at, size_t(delim-at));
224 if (tmpPath.set(tmp))
225 if (tmpPath.canRead())
226 Paths.push_back(tmpPath);
227 at = delim + 1;
228 delim = strchr(at, PathSeparator);
229 }
230
231 if (*at != 0)
232 if (tmpPath.set(std::string(at)))
233 if (tmpPath.canRead())
234 Paths.push_back(tmpPath);
235}
236
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000237static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
238 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
239 "Sep must be a 1-character string literal.");
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000240 if (path.empty())
241 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000242
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000243 // If the path is all slashes, return a single slash.
244 // Otherwise, remove all trailing slashes.
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000245
Evan Cheng34cd4a42008-05-05 18:30:58 +0000246 signed pos = static_cast<signed>(path.size()) - 1;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000247
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000248 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000249 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000250
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000251 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000252 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000253
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000254 // Any slashes left?
255 signed i = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000256
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000257 while (i < pos && path[i] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000258 ++i;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000259
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000260 if (i == pos) // No slashes? Return "."
261 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000262
263 // There is at least one slash left. Remove all trailing non-slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000264 while (pos >= 0 && path[pos] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000265 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000266
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000267 // Remove any trailing slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000268 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000269 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000270
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000271 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000272 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000273
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000274 return path.substr(0, pos+1);
275}
276
Reid Spencerb89a2232004-08-25 06:20:07 +0000277// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000278#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000279#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000280#endif
281#if defined(LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000282#include "Windows/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000283#endif