blob: 086c9f0238c42715cbb7e5b719530cdbc4aa5fe9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system Path concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/Path.h"
15#include "llvm/Config/config.h"
16#include <cassert>
Duncan Sandsfca20142008-01-09 19:42:09 +000017#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include <ostream>
19using namespace llvm;
20using namespace sys;
21
22//===----------------------------------------------------------------------===//
23//=== WARNING: Implementation here must contain only TRULY operating system
24//=== independent code.
25//===----------------------------------------------------------------------===//
26
27std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
28 strm << aPath.toString();
29 return strm;
30}
31
32Path
33Path::GetLLVMConfigDir() {
34 Path result;
35#ifdef LLVM_ETCDIR
36 if (result.set(LLVM_ETCDIR))
37 return result;
38#endif
39 return GetLLVMDefaultConfigDir();
40}
41
42LLVMFileType
43sys::IdentifyFileType(const char*magic, unsigned length) {
44 assert(magic && "Invalid magic number string");
45 assert(length >=4 && "Invalid magic number length");
46 switch (magic[0]) {
47 case 'B':
48 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
49 return Bitcode_FileType;
50 break;
51 case '!':
52 if (length >= 8)
53 if (memcmp(magic,"!<arch>\n",8) == 0)
54 return Archive_FileType;
55 break;
56
57 case '\177':
58 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
59 if (length >= 18 && magic[17] == 0)
60 switch (magic[16]) {
61 default: break;
62 case 1: return ELF_Relocatable_FileType;
63 case 2: return ELF_Executable_FileType;
64 case 3: return ELF_SharedObject_FileType;
65 case 4: return ELF_Core_FileType;
66 }
67 }
68 break;
69
70 case 0xCA:
71 // This is complicated by an overlap with Java class files.
72 // See the Mach-O section in /usr/share/file/magic for details.
73 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
74 magic[3] == char(0xBE)) {
75 return Mach_O_DynamicallyLinkedSharedLib_FileType;
76
77 // FIXME: How does this work?
78 if (length >= 14 && magic[13] == 0)
79 switch (magic[12]) {
80 default: break;
81 case 1: return Mach_O_Object_FileType;
82 case 2: return Mach_O_Executable_FileType;
83 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
84 case 4: return Mach_O_Core_FileType;
85 case 5: return Mach_O_PreloadExectuable_FileType;
86 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
87 case 7: return Mach_O_DynamicLinker_FileType;
88 case 8: return Mach_O_Bundle_FileType;
89 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
90 }
91 }
92 break;
93
94 case 0xF0: // PowerPC Windows
95 case 0x83: // Alpha 32-bit
96 case 0x84: // Alpha 64-bit
97 case 0x66: // MPS R4000 Windows
98 case 0x50: // mc68K
99 case 0x4c: // 80386 Windows
100 if (magic[1] == 0x01)
101 return COFF_FileType;
102
103 case 0x90: // PA-RISC Windows
104 case 0x68: // mc68K Windows
105 if (magic[1] == 0x02)
106 return COFF_FileType;
107 break;
108
109 default:
110 break;
111 }
112 return Unknown_FileType;
113}
114
115bool
116Path::isArchive() const {
117 if (canRead())
118 return hasMagicNumber("!<arch>\012");
119 return false;
120}
121
122bool
123Path::isDynamicLibrary() const {
124 if (canRead()) {
125 std::string Magic;
126 if (getMagicNumber(Magic, 64))
Evan Cheng591bfc82008-05-05 18:30:58 +0000127 switch (IdentifyFileType(Magic.c_str(),
128 static_cast<unsigned>(Magic.length()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 default: return false;
130 case Mach_O_FixedVirtualMemorySharedLib_FileType:
131 case Mach_O_DynamicallyLinkedSharedLib_FileType:
132 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
133 case ELF_SharedObject_FileType:
134 case COFF_FileType: return true;
135 }
136 }
137 return false;
138}
139
140Path
141Path::FindLibrary(std::string& name) {
142 std::vector<sys::Path> LibPaths;
143 GetSystemLibraryPaths(LibPaths);
144 for (unsigned i = 0; i < LibPaths.size(); ++i) {
145 sys::Path FullPath(LibPaths[i]);
146 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
147 if (FullPath.isDynamicLibrary())
148 return FullPath;
149 FullPath.eraseSuffix();
150 FullPath.appendSuffix("a");
151 if (FullPath.isArchive())
152 return FullPath;
153 }
154 return sys::Path();
155}
156
157std::string Path::GetDLLSuffix() {
158 return LTDL_SHLIB_EXT;
159}
160
161bool
162Path::isBitcodeFile() const {
163 std::string actualMagic;
164 if (!getMagicNumber(actualMagic, 4))
165 return false;
166 return actualMagic == "BC\xC0\xDE";
167}
168
169bool Path::hasMagicNumber(const std::string &Magic) const {
170 std::string actualMagic;
Evan Cheng591bfc82008-05-05 18:30:58 +0000171 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 return Magic == actualMagic;
173 return false;
174}
175
Anton Korobeynikov382ff1c2008-02-20 19:41:22 +0000176std::string
177Path::getSuffix() const {
178 return path.substr(path.rfind('.') + 1);
179}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180
Chris Lattner4b8f1c62008-02-27 06:17:10 +0000181static void getPathList(const char*path, std::vector<Path>& Paths) {
182 const char* at = path;
183 const char* delim = strchr(at, PathSeparator);
184 Path tmpPath;
185 while (delim != 0) {
186 std::string tmp(at, size_t(delim-at));
187 if (tmpPath.set(tmp))
188 if (tmpPath.canRead())
189 Paths.push_back(tmpPath);
190 at = delim + 1;
191 delim = strchr(at, PathSeparator);
192 }
193
194 if (*at != 0)
195 if (tmpPath.set(std::string(at)))
196 if (tmpPath.canRead())
197 Paths.push_back(tmpPath);
198}
199
Ted Kremenekb169dfa2008-04-07 22:01:32 +0000200static std::string getDirnameCharSep(const std::string& path, char Sep) {
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000201
202 if (path.empty())
203 return ".";
204
205 // If the path is all slashes, return a single slash.
206 // Otherwise, remove all trailing slashes.
207
Evan Cheng591bfc82008-05-05 18:30:58 +0000208 signed pos = static_cast<signed>(path.size()) - 1;
Ted Kremenek4a4f5ed2008-04-07 21:53:57 +0000209
210 while (pos >= 0 && path[pos] == Sep)
211 --pos;
212
213 if (pos < 0)
214 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
215
216 // Any slashes left?
217 signed i = 0;
218
219 while (i < pos && path[i] != Sep)
220 ++i;
221
222 if (i == pos) // No slashes? Return "."
223 return ".";
224
225 // There is at least one slash left. Remove all trailing non-slashes.
226 while (pos >= 0 && path[pos] != Sep)
227 --pos;
228
229 // Remove any trailing slashes.
230 while (pos >= 0 && path[pos] == Sep)
231 --pos;
232
233 if (pos < 0)
234 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
235
236 return path.substr(0, pos+1);
237}
238
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239// Include the truly platform-specific parts of this class.
240#if defined(LLVM_ON_UNIX)
241#include "Unix/Path.inc"
242#endif
243#if defined(LLVM_ON_WIN32)
244#include "Win32/Path.inc"
245#endif
246