blob: af400e95e3cd5127cd8c9492235a3bf23b4bb3eb [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
Reid Spencera2a62212004-11-14 23:26:18 +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");
58 switch (magic[0]) {
Chris Lattnerf283a5e2007-05-06 05:32:21 +000059 case 'B':
60 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
61 return Bitcode_FileType;
62 break;
Reid Spencer8bb5fd12007-04-04 06:30:26 +000063 case '!':
64 if (length >= 8)
65 if (memcmp(magic,"!<arch>\n",8) == 0)
66 return Archive_FileType;
67 break;
68
69 case '\177':
Chris Lattner24eac6c2007-05-03 18:15:56 +000070 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Reid Spencer947aa7d2007-04-11 02:02:09 +000071 if (length >= 18 && magic[17] == 0)
72 switch (magic[16]) {
73 default: break;
74 case 1: return ELF_Relocatable_FileType;
75 case 2: return ELF_Executable_FileType;
76 case 3: return ELF_SharedObject_FileType;
77 case 4: return ELF_Core_FileType;
78 }
Chris Lattner24eac6c2007-05-03 18:15:56 +000079 }
Reid Spencerf37ce992004-11-14 22:05:32 +000080 break;
81
Chris Lattnerade75922007-04-11 03:15:35 +000082 case 0xCA:
Reid Spencer8bb5fd12007-04-04 06:30:26 +000083 // This is complicated by an overlap with Java class files.
84 // See the Mach-O section in /usr/share/file/magic for details.
Chris Lattnerade75922007-04-11 03:15:35 +000085 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
86 magic[3] == char(0xBE)) {
87 return Mach_O_DynamicallyLinkedSharedLib_FileType;
88
89 // FIXME: How does this work?
Reid Spencer947aa7d2007-04-11 02:02:09 +000090 if (length >= 14 && magic[13] == 0)
91 switch (magic[12]) {
92 default: break;
93 case 1: return Mach_O_Object_FileType;
94 case 2: return Mach_O_Executable_FileType;
95 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
96 case 4: return Mach_O_Core_FileType;
97 case 5: return Mach_O_PreloadExectuable_FileType;
98 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
99 case 7: return Mach_O_DynamicLinker_FileType;
100 case 8: return Mach_O_Bundle_FileType;
101 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
102 }
Chris Lattnerade75922007-04-11 03:15:35 +0000103 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000104 break;
105
106 case 0xF0: // PowerPC Windows
107 case 0x83: // Alpha 32-bit
108 case 0x84: // Alpha 64-bit
109 case 0x66: // MPS R4000 Windows
110 case 0x50: // mc68K
111 case 0x4c: // 80386 Windows
112 if (magic[1] == 0x01)
113 return COFF_FileType;
114
115 case 0x90: // PA-RISC Windows
116 case 0x68: // mc68K Windows
117 if (magic[1] == 0x02)
118 return COFF_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000119 break;
120
121 default:
122 break;
123 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000124 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000125}
126
Reid Spencerccb23a12004-12-13 03:00:39 +0000127bool
128Path::isArchive() const {
Reid Spencerc7f08322005-07-07 18:21:42 +0000129 if (canRead())
Reid Spencerccb23a12004-12-13 03:00:39 +0000130 return hasMagicNumber("!<arch>\012");
131 return false;
132}
133
134bool
135Path::isDynamicLibrary() const {
Reid Spencer410aa022007-04-11 00:49:39 +0000136 if (canRead()) {
137 std::string Magic;
138 if (getMagicNumber(Magic, 64))
Evan Cheng34cd4a42008-05-05 18:30:58 +0000139 switch (IdentifyFileType(Magic.c_str(),
140 static_cast<unsigned>(Magic.length()))) {
Reid Spencer410aa022007-04-11 00:49:39 +0000141 default: return false;
Reid Spencer947aa7d2007-04-11 02:02:09 +0000142 case Mach_O_FixedVirtualMemorySharedLib_FileType:
143 case Mach_O_DynamicallyLinkedSharedLib_FileType:
144 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
145 case ELF_SharedObject_FileType:
Reid Spencer410aa022007-04-11 00:49:39 +0000146 case COFF_FileType: return true;
147 }
148 }
Reid Spencerccb23a12004-12-13 03:00:39 +0000149 return false;
150}
151
152Path
153Path::FindLibrary(std::string& name) {
154 std::vector<sys::Path> LibPaths;
155 GetSystemLibraryPaths(LibPaths);
156 for (unsigned i = 0; i < LibPaths.size(); ++i) {
157 sys::Path FullPath(LibPaths[i]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000158 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000159 if (FullPath.isDynamicLibrary())
160 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000161 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000162 FullPath.appendSuffix("a");
163 if (FullPath.isArchive())
164 return FullPath;
165 }
166 return sys::Path();
167}
168
Chris Lattnerc67dc452006-07-07 18:11:32 +0000169std::string Path::GetDLLSuffix() {
Reid Spencer79fc9242004-12-13 18:41:28 +0000170 return LTDL_SHLIB_EXT;
171}
172
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000173bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000174Path::isBitcodeFile() const {
175 std::string actualMagic;
176 if (!getMagicNumber(actualMagic, 4))
177 return false;
178 return actualMagic == "BC\xC0\xDE";
179}
180
181bool Path::hasMagicNumber(const std::string &Magic) const {
182 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000183 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000184 return Magic == actualMagic;
185 return false;
186}
187
Chris Lattnere1b332a2008-02-27 06:17:10 +0000188static void getPathList(const char*path, std::vector<Path>& Paths) {
189 const char* at = path;
190 const char* delim = strchr(at, PathSeparator);
191 Path tmpPath;
192 while (delim != 0) {
193 std::string tmp(at, size_t(delim-at));
194 if (tmpPath.set(tmp))
195 if (tmpPath.canRead())
196 Paths.push_back(tmpPath);
197 at = delim + 1;
198 delim = strchr(at, PathSeparator);
199 }
200
201 if (*at != 0)
202 if (tmpPath.set(std::string(at)))
203 if (tmpPath.canRead())
204 Paths.push_back(tmpPath);
205}
206
Ted Kremenek9b01cc02008-04-07 22:01:32 +0000207static std::string getDirnameCharSep(const std::string& path, char Sep) {
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000208
209 if (path.empty())
210 return ".";
211
212 // If the path is all slashes, return a single slash.
213 // Otherwise, remove all trailing slashes.
214
Evan Cheng34cd4a42008-05-05 18:30:58 +0000215 signed pos = static_cast<signed>(path.size()) - 1;
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000216
217 while (pos >= 0 && path[pos] == Sep)
218 --pos;
219
220 if (pos < 0)
221 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
222
223 // Any slashes left?
224 signed i = 0;
225
226 while (i < pos && path[i] != Sep)
227 ++i;
228
229 if (i == pos) // No slashes? Return "."
230 return ".";
231
232 // There is at least one slash left. Remove all trailing non-slashes.
233 while (pos >= 0 && path[pos] != Sep)
234 --pos;
235
236 // Remove any trailing slashes.
237 while (pos >= 0 && path[pos] == Sep)
238 --pos;
239
240 if (pos < 0)
241 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
242
243 return path.substr(0, pos+1);
244}
245
Reid Spencerb89a2232004-08-25 06:20:07 +0000246// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000247#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000248#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000249#endif
250#if defined(LLVM_ON_WIN32)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000251#include "Win32/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000252#endif
Reid Spencer23dd3322006-07-26 16:55:39 +0000253