1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file implements the operating system Path concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/System/Path.h"
15 #include "llvm/Config/config.h"
22 //===----------------------------------------------------------------------===//
23 //=== WARNING: Implementation here must contain only TRULY operating system
24 //=== independent code.
25 //===----------------------------------------------------------------------===//
27 bool Path::operator==(const Path
&that
) const {
28 return path
== that
.path
;
31 bool Path::operator<(const Path
& that
) const {
32 return path
< that
.path
;
36 Path::GetLLVMConfigDir() {
39 if (result
.set(LLVM_ETCDIR
))
42 return GetLLVMDefaultConfigDir();
46 sys::IdentifyFileType(const char *magic
, unsigned length
) {
47 assert(magic
&& "Invalid magic number string");
48 assert(length
>=4 && "Invalid magic number length");
49 switch ((unsigned char)magic
[0]) {
50 case 0xDE: // 0x0B17C0DE = BC wraper
51 if (magic
[1] == (char)0xC0 && magic
[2] == (char)0x17 &&
52 magic
[3] == (char)0x0B)
53 return Bitcode_FileType
;
56 if (magic
[1] == 'C' && magic
[2] == (char)0xC0 && magic
[3] == (char)0xDE)
57 return Bitcode_FileType
;
61 if (memcmp(magic
,"!<arch>\n",8) == 0)
62 return Archive_FileType
;
66 if (magic
[1] == 'E' && magic
[2] == 'L' && magic
[3] == 'F') {
67 if (length
>= 18 && magic
[17] == 0)
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
;
79 if (magic
[1] == char(0xFE) && magic
[2] == char(0xBA) &&
80 magic
[3] == char(0xBE)) {
81 // This is complicated by an overlap with Java class files.
82 // See the Mach-O section in /usr/share/file/magic for details.
83 if (length
>= 8 && magic
[7] < 43)
84 // FIXME: Universal Binary of any type.
85 return Mach_O_DynamicallyLinkedSharedLib_FileType
;
92 if (magic
[0] == char(0xFE) && magic
[1] == char(0xED) &&
93 magic
[2] == char(0xFA) && magic
[3] == char(0xCE)) {
95 if (length
>= 16) type
= magic
[14] << 8 | magic
[15];
96 } else if (magic
[0] == char(0xCE) && magic
[1] == char(0xFA) &&
97 magic
[2] == char(0xED) && magic
[3] == char(0xFE)) {
99 if (length
>= 14) type
= magic
[13] << 8 | magic
[12];
103 case 1: return Mach_O_Object_FileType
;
104 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.
116 case 0xF0: // PowerPC Windows
117 case 0x83: // Alpha 32-bit
118 case 0x84: // Alpha 64-bit
119 case 0x66: // MPS R4000 Windows
121 case 0x4c: // 80386 Windows
122 if (magic
[1] == 0x01)
123 return COFF_FileType
;
125 case 0x90: // PA-RISC Windows
126 case 0x68: // mc68K Windows
127 if (magic
[1] == 0x02)
128 return COFF_FileType
;
130 case 0x64: // x86-64 Windows.
131 if (magic
[1] == char(0x86))
132 return COFF_FileType
;
138 return Unknown_FileType
;
142 Path::isArchive() const {
143 return hasMagicNumber("!<arch>\012");
147 Path::isDynamicLibrary() const {
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;
164 Path::isObjectFile() const {
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.
178 Path::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
]);
183 FullPath
.appendComponent("lib" + name
+ LTDL_SHLIB_EXT
);
184 if (FullPath
.isDynamicLibrary())
186 FullPath
.eraseSuffix();
187 FullPath
.appendSuffix("a");
188 if (FullPath
.isArchive())
194 StringRef
Path::GetDLLSuffix() {
195 return &(LTDL_SHLIB_EXT
[1]);
199 Path::appendSuffix(StringRef suffix
) {
200 if (!suffix
.empty()) {
201 std::string
save(path
);
214 Path::isBitcodeFile() const {
215 std::string actualMagic
;
216 if (!getMagicNumber(actualMagic
, 4))
219 IdentifyFileType(actualMagic
.c_str(),
220 static_cast<unsigned>(actualMagic
.length()));
221 return FT
== Bitcode_FileType
;
224 bool Path::hasMagicNumber(StringRef Magic
) const {
225 std::string actualMagic
;
226 if (getMagicNumber(actualMagic
, static_cast<unsigned>(Magic
.size())))
227 return Magic
== actualMagic
;
231 static void getPathList(const char*path
, std::vector
<Path
>& Paths
) {
232 const char* at
= path
;
233 const char* delim
= strchr(at
, PathSeparator
);
236 std::string
tmp(at
, size_t(delim
-at
));
237 if (tmpPath
.set(tmp
))
238 if (tmpPath
.canRead())
239 Paths
.push_back(tmpPath
);
241 delim
= strchr(at
, PathSeparator
);
245 if (tmpPath
.set(std::string(at
)))
246 if (tmpPath
.canRead())
247 Paths
.push_back(tmpPath
);
250 static StringRef
getDirnameCharSep(StringRef path
, const char *Sep
) {
251 assert(Sep
[0] != '\0' && Sep
[1] == '\0' &&
252 "Sep must be a 1-character string literal.");
256 // If the path is all slashes, return a single slash.
257 // Otherwise, remove all trailing slashes.
259 signed pos
= static_cast<signed>(path
.size()) - 1;
261 while (pos
>= 0 && path
[pos
] == Sep
[0])
265 return path
[0] == Sep
[0] ? Sep
: ".";
270 while (i
< pos
&& path
[i
] != Sep
[0])
273 if (i
== pos
) // No slashes? Return "."
276 // There is at least one slash left. Remove all trailing non-slashes.
277 while (pos
>= 0 && path
[pos
] != Sep
[0])
280 // Remove any trailing slashes.
281 while (pos
>= 0 && path
[pos
] == Sep
[0])
285 return path
[0] == Sep
[0] ? Sep
: ".";
287 return path
.substr(0, pos
+1);
290 // Include the truly platform-specific parts of this class.
291 #if defined(LLVM_ON_UNIX)
292 #include "Unix/Path.inc"
294 #if defined(LLVM_ON_WIN32)
295 #include "Win32/Path.inc"