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/Support/Path.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/FileSystem.h"
24 //===----------------------------------------------------------------------===//
25 //=== WARNING: Implementation here must contain only TRULY operating system
26 //=== independent code.
27 //===----------------------------------------------------------------------===//
29 bool Path::operator==(const Path &that) const {
30 return path == that.path;
33 bool Path::operator<(const Path& that) const {
34 return path < that.path;
38 Path::GetLLVMConfigDir() {
41 if (result.set(LLVM_ETCDIR))
44 return GetLLVMDefaultConfigDir();
48 sys::IdentifyFileType(const char *magic, unsigned length) {
49 assert(magic && "Invalid magic number string");
50 assert(length >=4 && "Invalid magic number length");
51 switch ((unsigned char)magic[0]) {
52 case 0xDE: // 0x0B17C0DE = BC wraper
53 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
54 magic[3] == (char)0x0B)
55 return Bitcode_FileType;
58 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
59 return Bitcode_FileType;
63 if (memcmp(magic,"!<arch>\n",8) == 0)
64 return Archive_FileType;
68 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
69 if (length >= 18 && magic[17] == 0)
72 case 1: return ELF_Relocatable_FileType;
73 case 2: return ELF_Executable_FileType;
74 case 3: return ELF_SharedObject_FileType;
75 case 4: return ELF_Core_FileType;
81 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
82 magic[3] == char(0xBE)) {
83 // This is complicated by an overlap with Java class files.
84 // See the Mach-O section in /usr/share/file/magic for details.
85 if (length >= 8 && magic[7] < 43)
86 // FIXME: Universal Binary of any type.
87 return Mach_O_DynamicallyLinkedSharedLib_FileType;
94 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
95 magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
97 if (length >= 16) type = magic[14] << 8 | magic[15];
98 } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) &&
99 magic[2] == char(0xED) && magic[3] == char(0xFE)) {
101 if (length >= 14) type = magic[13] << 8 | magic[12];
105 case 1: return Mach_O_Object_FileType;
106 case 2: return Mach_O_Executable_FileType;
107 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
108 case 4: return Mach_O_Core_FileType;
109 case 5: return Mach_O_PreloadExecutable_FileType;
110 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
111 case 7: return Mach_O_DynamicLinker_FileType;
112 case 8: return Mach_O_Bundle_FileType;
113 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
114 case 10: break; // FIXME: MH_DSYM companion file with only debug.
118 case 0xF0: // PowerPC Windows
119 case 0x83: // Alpha 32-bit
120 case 0x84: // Alpha 64-bit
121 case 0x66: // MPS R4000 Windows
123 case 0x4c: // 80386 Windows
124 if (magic[1] == 0x01)
125 return COFF_FileType;
127 case 0x90: // PA-RISC Windows
128 case 0x68: // mc68K Windows
129 if (magic[1] == 0x02)
130 return COFF_FileType;
132 case 0x64: // x86-64 Windows.
133 if (magic[1] == char(0x86))
134 return COFF_FileType;
140 return Unknown_FileType;
144 Path::isArchive() const {
146 if (fs::identify_magic(str(), type))
148 return type == Archive_FileType;
152 Path::isDynamicLibrary() const {
154 if (fs::identify_magic(str(), type))
157 default: return false;
158 case Mach_O_FixedVirtualMemorySharedLib_FileType:
159 case Mach_O_DynamicallyLinkedSharedLib_FileType:
160 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
161 case ELF_SharedObject_FileType:
162 case COFF_FileType: return true;
167 Path::isObjectFile() const {
169 if (fs::identify_magic(str(), type) || type == Unknown_FileType)
175 Path::FindLibrary(std::string& name) {
176 std::vector<sys::Path> LibPaths;
177 GetSystemLibraryPaths(LibPaths);
178 for (unsigned i = 0; i < LibPaths.size(); ++i) {
179 sys::Path FullPath(LibPaths[i]);
180 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
181 if (FullPath.isDynamicLibrary())
183 FullPath.eraseSuffix();
184 FullPath.appendSuffix("a");
185 if (FullPath.isArchive())
191 StringRef Path::GetDLLSuffix() {
192 return &(LTDL_SHLIB_EXT[1]);
196 Path::appendSuffix(StringRef suffix) {
197 if (!suffix.empty()) {
204 Path::isBitcodeFile() const {
206 if (fs::identify_magic(str(), type))
208 return type == Bitcode_FileType;
211 bool Path::hasMagicNumber(StringRef Magic) const {
212 std::string actualMagic;
213 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
214 return Magic == actualMagic;
218 static void getPathList(const char*path, std::vector<Path>& Paths) {
219 const char* at = path;
220 const char* delim = strchr(at, PathSeparator);
223 std::string tmp(at, size_t(delim-at));
224 if (tmpPath.set(tmp))
225 if (tmpPath.canRead())
226 Paths.push_back(tmpPath);
228 delim = strchr(at, PathSeparator);
232 if (tmpPath.set(std::string(at)))
233 if (tmpPath.canRead())
234 Paths.push_back(tmpPath);
237 static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
238 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
239 "Sep must be a 1-character string literal.");
243 // If the path is all slashes, return a single slash.
244 // Otherwise, remove all trailing slashes.
246 signed pos = static_cast<signed>(path.size()) - 1;
248 while (pos >= 0 && path[pos] == Sep[0])
252 return path[0] == Sep[0] ? Sep : ".";
257 while (i < pos && path[i] != Sep[0])
260 if (i == pos) // No slashes? Return "."
263 // There is at least one slash left. Remove all trailing non-slashes.
264 while (pos >= 0 && path[pos] != Sep[0])
267 // Remove any trailing slashes.
268 while (pos >= 0 && path[pos] == Sep[0])
272 return path[0] == Sep[0] ? Sep : ".";
274 return path.substr(0, pos+1);
277 // Include the truly platform-specific parts of this class.
278 #if defined(LLVM_ON_UNIX)
279 #include "Unix/Path.inc"
281 #if defined(LLVM_ON_WIN32)
282 #include "Windows/Path.inc"