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"
16 #include "llvm/Support/ErrorHandling.h"
23 //===----------------------------------------------------------------------===//
24 //=== WARNING: Implementation here must contain only TRULY operating system
25 //=== independent code.
26 //===----------------------------------------------------------------------===//
28 bool Path::operator==(const Path &that) const {
29 return path == that.path;
32 bool Path::operator!=(const Path &that) const {
33 return path != that.path;
36 bool Path::operator<(const Path& that) const {
37 return path < that.path;
40 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
41 strm << aPath.toString();
46 Path::GetLLVMConfigDir() {
49 if (result.set(LLVM_ETCDIR))
52 return GetLLVMDefaultConfigDir();
56 sys::IdentifyFileType(const char *magic, unsigned length) {
57 assert(magic && "Invalid magic number string");
58 assert(length >=4 && "Invalid magic number length");
59 switch ((unsigned char)magic[0]) {
60 case 0xDE: // 0x0B17C0DE = BC wraper
61 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
62 magic[3] == (char)0x0B)
63 return Bitcode_FileType;
66 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
67 return Bitcode_FileType;
71 if (memcmp(magic,"!<arch>\n",8) == 0)
72 return Archive_FileType;
76 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
77 if (length >= 18 && magic[17] == 0)
80 case 1: return ELF_Relocatable_FileType;
81 case 2: return ELF_Executable_FileType;
82 case 3: return ELF_SharedObject_FileType;
83 case 4: return ELF_Core_FileType;
89 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
90 magic[3] == char(0xBE)) {
91 // This is complicated by an overlap with Java class files.
92 // See the Mach-O section in /usr/share/file/magic for details.
93 if (length >= 8 && magic[7] < 43)
94 // FIXME: Universal Binary of any type.
95 return Mach_O_DynamicallyLinkedSharedLib_FileType;
102 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
103 magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
105 if (length >= 16) type = magic[14] << 8 | magic[15];
106 } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) &&
107 magic[2] == char(0xED) && magic[3] == char(0xFE)) {
109 if (length >= 14) type = magic[13] << 8 | magic[12];
113 case 1: return Mach_O_Object_FileType;
114 case 2: return Mach_O_Executable_FileType;
115 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
116 case 4: return Mach_O_Core_FileType;
117 case 5: return Mach_O_PreloadExectuable_FileType;
118 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
119 case 7: return Mach_O_DynamicLinker_FileType;
120 case 8: return Mach_O_Bundle_FileType;
121 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
122 case 10: break; // FIXME: MH_DSYM companion file with only debug.
126 case 0xF0: // PowerPC Windows
127 case 0x83: // Alpha 32-bit
128 case 0x84: // Alpha 64-bit
129 case 0x66: // MPS R4000 Windows
131 case 0x4c: // 80386 Windows
132 if (magic[1] == 0x01)
133 return COFF_FileType;
135 case 0x90: // PA-RISC Windows
136 case 0x68: // mc68K Windows
137 if (magic[1] == 0x02)
138 return COFF_FileType;
144 return Unknown_FileType;
148 Path::isArchive() const {
150 return hasMagicNumber("!<arch>\012");
155 Path::isDynamicLibrary() const {
158 if (getMagicNumber(Magic, 64))
159 switch (IdentifyFileType(Magic.c_str(),
160 static_cast<unsigned>(Magic.length()))) {
161 default: return false;
162 case Mach_O_FixedVirtualMemorySharedLib_FileType:
163 case Mach_O_DynamicallyLinkedSharedLib_FileType:
164 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
165 case ELF_SharedObject_FileType:
166 case COFF_FileType: return true;
173 Path::FindLibrary(std::string& name) {
174 std::vector<sys::Path> LibPaths;
175 GetSystemLibraryPaths(LibPaths);
176 for (unsigned i = 0; i < LibPaths.size(); ++i) {
177 sys::Path FullPath(LibPaths[i]);
178 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
179 if (FullPath.isDynamicLibrary())
181 FullPath.eraseSuffix();
182 FullPath.appendSuffix("a");
183 if (FullPath.isArchive())
189 std::string Path::GetDLLSuffix() {
190 return LTDL_SHLIB_EXT;
194 Path::isBitcodeFile() const {
195 std::string actualMagic;
196 if (!getMagicNumber(actualMagic, 4))
199 IdentifyFileType(actualMagic.c_str(),
200 static_cast<unsigned>(actualMagic.length()));
201 return FT == Bitcode_FileType;
204 bool Path::hasMagicNumber(const std::string &Magic) const {
205 std::string actualMagic;
206 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
207 return Magic == actualMagic;
211 static void getPathList(const char*path, std::vector<Path>& Paths) {
212 const char* at = path;
213 const char* delim = strchr(at, PathSeparator);
216 std::string tmp(at, size_t(delim-at));
217 if (tmpPath.set(tmp))
218 if (tmpPath.canRead())
219 Paths.push_back(tmpPath);
221 delim = strchr(at, PathSeparator);
225 if (tmpPath.set(std::string(at)))
226 if (tmpPath.canRead())
227 Paths.push_back(tmpPath);
230 static std::string getDirnameCharSep(const std::string& path, char Sep) {
235 // If the path is all slashes, return a single slash.
236 // Otherwise, remove all trailing slashes.
238 signed pos = static_cast<signed>(path.size()) - 1;
240 while (pos >= 0 && path[pos] == Sep)
244 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
249 while (i < pos && path[i] != Sep)
252 if (i == pos) // No slashes? Return "."
255 // There is at least one slash left. Remove all trailing non-slashes.
256 while (pos >= 0 && path[pos] != Sep)
259 // Remove any trailing slashes.
260 while (pos >= 0 && path[pos] == Sep)
264 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
266 return path.substr(0, pos+1);
269 // Include the truly platform-specific parts of this class.
270 #if defined(LLVM_ON_UNIX)
271 #include "Unix/Path.inc"
273 #if defined(LLVM_ON_WIN32)
274 #include "Win32/Path.inc"