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");
60 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
61 return Bitcode_FileType;
65 if (memcmp(magic,"!<arch>\n",8) == 0)
66 return Archive_FileType;
70 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
71 if (length >= 18 && magic[17] == 0)
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;
83 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
84 magic[3] == char(0xBE)) {
85 // This is complicated by an overlap with Java class files.
86 // See the Mach-O section in /usr/share/file/magic for details.
87 if (length >= 8 && magic[7] < 43)
88 // FIXME: Universal Binary of any type.
89 return Mach_O_DynamicallyLinkedSharedLib_FileType;
96 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
97 magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
99 if (length >= 16) type = magic[14] << 8 | magic[15];
100 } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) &&
101 magic[2] == char(0xED) && magic[3] == char(0xFE)) {
103 if (length >= 14) type = magic[13] << 8 | magic[12];
107 case 1: return Mach_O_Object_FileType;
108 case 2: return Mach_O_Executable_FileType;
109 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
110 case 4: return Mach_O_Core_FileType;
111 case 5: return Mach_O_PreloadExectuable_FileType;
112 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
113 case 7: return Mach_O_DynamicLinker_FileType;
114 case 8: return Mach_O_Bundle_FileType;
115 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
116 case 10: break; // FIXME: MH_DSYM companion file with only debug.
120 case 0xF0: // PowerPC Windows
121 case 0x83: // Alpha 32-bit
122 case 0x84: // Alpha 64-bit
123 case 0x66: // MPS R4000 Windows
125 case 0x4c: // 80386 Windows
126 if (magic[1] == 0x01)
127 return COFF_FileType;
129 case 0x90: // PA-RISC Windows
130 case 0x68: // mc68K Windows
131 if (magic[1] == 0x02)
132 return COFF_FileType;
138 return Unknown_FileType;
142 Path::isArchive() const {
144 return hasMagicNumber("!<arch>\012");
149 Path::isDynamicLibrary() const {
152 if (getMagicNumber(Magic, 64))
153 switch (IdentifyFileType(Magic.c_str(),
154 static_cast<unsigned>(Magic.length()))) {
155 default: return false;
156 case Mach_O_FixedVirtualMemorySharedLib_FileType:
157 case Mach_O_DynamicallyLinkedSharedLib_FileType:
158 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
159 case ELF_SharedObject_FileType:
160 case COFF_FileType: return true;
167 Path::FindLibrary(std::string& name) {
168 std::vector<sys::Path> LibPaths;
169 GetSystemLibraryPaths(LibPaths);
170 for (unsigned i = 0; i < LibPaths.size(); ++i) {
171 sys::Path FullPath(LibPaths[i]);
172 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
173 if (FullPath.isDynamicLibrary())
175 FullPath.eraseSuffix();
176 FullPath.appendSuffix("a");
177 if (FullPath.isArchive())
183 std::string Path::GetDLLSuffix() {
184 return LTDL_SHLIB_EXT;
188 Path::isBitcodeFile() const {
189 std::string actualMagic;
190 if (!getMagicNumber(actualMagic, 4))
192 return actualMagic == "BC\xC0\xDE";
195 bool Path::hasMagicNumber(const std::string &Magic) const {
196 std::string actualMagic;
197 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
198 return Magic == actualMagic;
202 static void getPathList(const char*path, std::vector<Path>& Paths) {
203 const char* at = path;
204 const char* delim = strchr(at, PathSeparator);
207 std::string tmp(at, size_t(delim-at));
208 if (tmpPath.set(tmp))
209 if (tmpPath.canRead())
210 Paths.push_back(tmpPath);
212 delim = strchr(at, PathSeparator);
216 if (tmpPath.set(std::string(at)))
217 if (tmpPath.canRead())
218 Paths.push_back(tmpPath);
221 static std::string getDirnameCharSep(const std::string& path, char Sep) {
226 // If the path is all slashes, return a single slash.
227 // Otherwise, remove all trailing slashes.
229 signed pos = static_cast<signed>(path.size()) - 1;
231 while (pos >= 0 && path[pos] == Sep)
235 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
240 while (i < pos && path[i] != Sep)
243 if (i == pos) // No slashes? Return "."
246 // There is at least one slash left. Remove all trailing non-slashes.
247 while (pos >= 0 && path[pos] != Sep)
250 // Remove any trailing slashes.
251 while (pos >= 0 && path[pos] == Sep)
255 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
257 return path.substr(0, pos+1);
260 // Include the truly platform-specific parts of this class.
261 #if defined(LLVM_ON_UNIX)
262 #include "Unix/Path.inc"
264 #if defined(LLVM_ON_WIN32)
265 #include "Win32/Path.inc"