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
;
35 bool Path::operator<(const Path
& that
) const {
36 return path
< that
.path
;
39 std::ostream
& llvm::operator<<(std::ostream
&strm
, const sys::Path
&aPath
) {
40 strm
<< aPath
.toString();
45 Path::GetLLVMConfigDir() {
48 if (result
.set(LLVM_ETCDIR
))
51 return GetLLVMDefaultConfigDir();
55 sys::IdentifyFileType(const char *magic
, unsigned length
) {
56 assert(magic
&& "Invalid magic number string");
57 assert(length
>=4 && "Invalid magic number length");
58 switch ((unsigned char)magic
[0]) {
59 case 0xDE: // 0x0B17C0DE = BC wraper
60 if (magic
[1] == (char)0xC0 && magic
[2] == (char)0x17 &&
61 magic
[3] == (char)0x0B)
62 return Bitcode_FileType
;
65 if (magic
[1] == 'C' && magic
[2] == (char)0xC0 && magic
[3] == (char)0xDE)
66 return Bitcode_FileType
;
70 if (memcmp(magic
,"!<arch>\n",8) == 0)
71 return Archive_FileType
;
75 if (magic
[1] == 'E' && magic
[2] == 'L' && magic
[3] == 'F') {
76 if (length
>= 18 && magic
[17] == 0)
79 case 1: return ELF_Relocatable_FileType
;
80 case 2: return ELF_Executable_FileType
;
81 case 3: return ELF_SharedObject_FileType
;
82 case 4: return ELF_Core_FileType
;
88 if (magic
[1] == char(0xFE) && magic
[2] == char(0xBA) &&
89 magic
[3] == char(0xBE)) {
90 // This is complicated by an overlap with Java class files.
91 // See the Mach-O section in /usr/share/file/magic for details.
92 if (length
>= 8 && magic
[7] < 43)
93 // FIXME: Universal Binary of any type.
94 return Mach_O_DynamicallyLinkedSharedLib_FileType
;
101 if (magic
[0] == char(0xFE) && magic
[1] == char(0xED) &&
102 magic
[2] == char(0xFA) && magic
[3] == char(0xCE)) {
104 if (length
>= 16) type
= magic
[14] << 8 | magic
[15];
105 } else if (magic
[0] == char(0xCE) && magic
[1] == char(0xFA) &&
106 magic
[2] == char(0xED) && magic
[3] == char(0xFE)) {
108 if (length
>= 14) type
= magic
[13] << 8 | magic
[12];
112 case 1: return Mach_O_Object_FileType
;
113 case 2: return Mach_O_Executable_FileType
;
114 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType
;
115 case 4: return Mach_O_Core_FileType
;
116 case 5: return Mach_O_PreloadExectuable_FileType
;
117 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType
;
118 case 7: return Mach_O_DynamicLinker_FileType
;
119 case 8: return Mach_O_Bundle_FileType
;
120 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType
;
121 case 10: break; // FIXME: MH_DSYM companion file with only debug.
125 case 0xF0: // PowerPC Windows
126 case 0x83: // Alpha 32-bit
127 case 0x84: // Alpha 64-bit
128 case 0x66: // MPS R4000 Windows
130 case 0x4c: // 80386 Windows
131 if (magic
[1] == 0x01)
132 return COFF_FileType
;
134 case 0x90: // PA-RISC Windows
135 case 0x68: // mc68K Windows
136 if (magic
[1] == 0x02)
137 return COFF_FileType
;
143 return Unknown_FileType
;
147 Path::isArchive() const {
149 return hasMagicNumber("!<arch>\012");
154 Path::isDynamicLibrary() const {
157 if (getMagicNumber(Magic
, 64))
158 switch (IdentifyFileType(Magic
.c_str(),
159 static_cast<unsigned>(Magic
.length()))) {
160 default: return false;
161 case Mach_O_FixedVirtualMemorySharedLib_FileType
:
162 case Mach_O_DynamicallyLinkedSharedLib_FileType
:
163 case Mach_O_DynamicallyLinkedSharedLibStub_FileType
:
164 case ELF_SharedObject_FileType
:
165 case COFF_FileType
: return true;
172 Path::FindLibrary(std::string
& name
) {
173 std::vector
<sys::Path
> LibPaths
;
174 GetSystemLibraryPaths(LibPaths
);
175 for (unsigned i
= 0; i
< LibPaths
.size(); ++i
) {
176 sys::Path
FullPath(LibPaths
[i
]);
177 FullPath
.appendComponent("lib" + name
+ LTDL_SHLIB_EXT
);
178 if (FullPath
.isDynamicLibrary())
180 FullPath
.eraseSuffix();
181 FullPath
.appendSuffix("a");
182 if (FullPath
.isArchive())
188 std::string
Path::GetDLLSuffix() {
189 return LTDL_SHLIB_EXT
;
193 Path::isBitcodeFile() const {
194 std::string actualMagic
;
195 if (!getMagicNumber(actualMagic
, 4))
198 IdentifyFileType(actualMagic
.c_str(),
199 static_cast<unsigned>(actualMagic
.length()));
200 return FT
== Bitcode_FileType
;
203 bool Path::hasMagicNumber(const std::string
&Magic
) const {
204 std::string actualMagic
;
205 if (getMagicNumber(actualMagic
, static_cast<unsigned>(Magic
.size())))
206 return Magic
== actualMagic
;
210 void Path::makeAbsolute() {
214 Path CWD
= Path::GetCurrentDirectory();
215 assert(CWD
.isAbsolute() && "GetCurrentDirectory returned relative path!");
217 CWD
.appendComponent(path
);
219 path
= CWD
.toString();
222 static void getPathList(const char*path
, std::vector
<Path
>& Paths
) {
223 const char* at
= path
;
224 const char* delim
= strchr(at
, PathSeparator
);
227 std::string
tmp(at
, size_t(delim
-at
));
228 if (tmpPath
.set(tmp
))
229 if (tmpPath
.canRead())
230 Paths
.push_back(tmpPath
);
232 delim
= strchr(at
, PathSeparator
);
236 if (tmpPath
.set(std::string(at
)))
237 if (tmpPath
.canRead())
238 Paths
.push_back(tmpPath
);
241 static std::string
getDirnameCharSep(const std::string
& path
, char Sep
) {
246 // If the path is all slashes, return a single slash.
247 // Otherwise, remove all trailing slashes.
249 signed pos
= static_cast<signed>(path
.size()) - 1;
251 while (pos
>= 0 && path
[pos
] == Sep
)
255 return path
[0] == Sep
? std::string(1, Sep
) : std::string(".");
260 while (i
< pos
&& path
[i
] != Sep
)
263 if (i
== pos
) // No slashes? Return "."
266 // There is at least one slash left. Remove all trailing non-slashes.
267 while (pos
>= 0 && path
[pos
] != Sep
)
270 // Remove any trailing slashes.
271 while (pos
>= 0 && path
[pos
] == Sep
)
275 return path
[0] == Sep
? std::string(1, Sep
) : std::string(".");
277 return path
.substr(0, pos
+1);
280 // Include the truly platform-specific parts of this class.
281 #if defined(LLVM_ON_UNIX)
282 #include "Unix/Path.inc"
284 #if defined(LLVM_ON_WIN32)
285 #include "Win32/Path.inc"