Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 1 | //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system Path concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 13 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 14 | #include "llvm/System/Path.h" |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Alkis Evlogimenos | 98bc8ed | 2004-11-14 22:37:42 +0000 | [diff] [blame] | 16 | #include <cassert> |
Duncan Sands | f52e32a | 2008-01-09 19:42:09 +0000 | [diff] [blame] | 17 | #include <cstring> |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 18 | #include <ostream> |
| 19 | using namespace llvm; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 20 | using namespace sys; |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 24 | //=== independent code. |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Bill Wendling | 40db5d4 | 2008-05-21 21:20:07 +0000 | [diff] [blame] | 27 | bool Path::operator==(const Path &that) const { |
| 28 | return path == that.path; |
| 29 | } |
| 30 | |
| 31 | bool Path::operator!=(const Path &that) const { |
| 32 | return path != that.path; |
| 33 | } |
| 34 | |
| 35 | bool Path::operator<(const Path& that) const { |
| 36 | return path < that.path; |
| 37 | } |
| 38 | |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 39 | std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) { |
| 40 | strm << aPath.toString(); |
| 41 | return strm; |
| 42 | } |
| 43 | |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 44 | Path |
| 45 | Path::GetLLVMConfigDir() { |
| 46 | Path result; |
Jeff Cohen | ab68df0 | 2004-12-15 04:08:15 +0000 | [diff] [blame] | 47 | #ifdef LLVM_ETCDIR |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 48 | if (result.set(LLVM_ETCDIR)) |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 49 | return result; |
Jeff Cohen | ab68df0 | 2004-12-15 04:08:15 +0000 | [diff] [blame] | 50 | #endif |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 51 | return GetLLVMDefaultConfigDir(); |
| 52 | } |
| 53 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 54 | LLVMFileType |
Reid Spencer | a2a6221 | 2004-11-14 23:26:18 +0000 | [diff] [blame] | 55 | sys::IdentifyFileType(const char*magic, unsigned length) { |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 56 | assert(magic && "Invalid magic number string"); |
| 57 | assert(length >=4 && "Invalid magic number length"); |
| 58 | switch (magic[0]) { |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 59 | case 'B': |
| 60 | if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE) |
| 61 | return Bitcode_FileType; |
| 62 | break; |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 63 | case '!': |
| 64 | if (length >= 8) |
| 65 | if (memcmp(magic,"!<arch>\n",8) == 0) |
| 66 | return Archive_FileType; |
| 67 | break; |
| 68 | |
| 69 | case '\177': |
Chris Lattner | 24eac6c | 2007-05-03 18:15:56 +0000 | [diff] [blame] | 70 | if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') { |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 71 | if (length >= 18 && magic[17] == 0) |
| 72 | switch (magic[16]) { |
| 73 | default: break; |
| 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; |
| 78 | } |
Chris Lattner | 24eac6c | 2007-05-03 18:15:56 +0000 | [diff] [blame] | 79 | } |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 80 | break; |
| 81 | |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 82 | case 0xCA: |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 83 | if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && |
| 84 | magic[3] == char(0xBE)) { |
Chris Lattner | 6521549 | 2008-06-26 05:17:18 +0000 | [diff] [blame] | 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; |
| 90 | } |
| 91 | break; |
| 92 | |
| 93 | case 0xFE: |
Bill Wendling | fc1fd54 | 2008-06-26 08:32:05 +0000 | [diff] [blame] | 94 | case 0xCE: { |
| 95 | uint16_t type = 0; |
Chris Lattner | 6521549 | 2008-06-26 05:17:18 +0000 | [diff] [blame] | 96 | if (magic[0] == char(0xFE) && magic[1] == char(0xED) && |
| 97 | magic[2] == char(0xFA) && magic[3] == char(0xCE)) { |
| 98 | /* Native endian */ |
| 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)) { |
| 102 | /* Reverse endian */ |
| 103 | if (length >= 14) type = magic[13] << 8 | magic[12]; |
Bill Wendling | fc1fd54 | 2008-06-26 08:32:05 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | 6521549 | 2008-06-26 05:17:18 +0000 | [diff] [blame] | 105 | switch (type) { |
| 106 | default: break; |
| 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. |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 117 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 118 | break; |
Bill Wendling | fc1fd54 | 2008-06-26 08:32:05 +0000 | [diff] [blame] | 119 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 120 | case 0xF0: // PowerPC Windows |
| 121 | case 0x83: // Alpha 32-bit |
| 122 | case 0x84: // Alpha 64-bit |
| 123 | case 0x66: // MPS R4000 Windows |
| 124 | case 0x50: // mc68K |
| 125 | case 0x4c: // 80386 Windows |
| 126 | if (magic[1] == 0x01) |
| 127 | return COFF_FileType; |
| 128 | |
| 129 | case 0x90: // PA-RISC Windows |
| 130 | case 0x68: // mc68K Windows |
| 131 | if (magic[1] == 0x02) |
| 132 | return COFF_FileType; |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 133 | break; |
| 134 | |
| 135 | default: |
| 136 | break; |
| 137 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 138 | return Unknown_FileType; |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 141 | bool |
| 142 | Path::isArchive() const { |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 143 | if (canRead()) |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 144 | return hasMagicNumber("!<arch>\012"); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | bool |
| 149 | Path::isDynamicLibrary() const { |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 150 | if (canRead()) { |
| 151 | std::string Magic; |
| 152 | if (getMagicNumber(Magic, 64)) |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 153 | switch (IdentifyFileType(Magic.c_str(), |
| 154 | static_cast<unsigned>(Magic.length()))) { |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 155 | default: return false; |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 156 | case Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 157 | case Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 158 | case Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
| 159 | case ELF_SharedObject_FileType: |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 160 | case COFF_FileType: return true; |
| 161 | } |
| 162 | } |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
| 166 | Path |
| 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]); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 172 | FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT); |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 173 | if (FullPath.isDynamicLibrary()) |
| 174 | return FullPath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 175 | FullPath.eraseSuffix(); |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 176 | FullPath.appendSuffix("a"); |
| 177 | if (FullPath.isArchive()) |
| 178 | return FullPath; |
| 179 | } |
| 180 | return sys::Path(); |
| 181 | } |
| 182 | |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 183 | std::string Path::GetDLLSuffix() { |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 184 | return LTDL_SHLIB_EXT; |
| 185 | } |
| 186 | |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 187 | bool |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 188 | Path::isBitcodeFile() const { |
| 189 | std::string actualMagic; |
| 190 | if (!getMagicNumber(actualMagic, 4)) |
| 191 | return false; |
| 192 | return actualMagic == "BC\xC0\xDE"; |
| 193 | } |
| 194 | |
| 195 | bool Path::hasMagicNumber(const std::string &Magic) const { |
| 196 | std::string actualMagic; |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 197 | if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size()))) |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 198 | return Magic == actualMagic; |
| 199 | return false; |
| 200 | } |
| 201 | |
Chris Lattner | e1b332a | 2008-02-27 06:17:10 +0000 | [diff] [blame] | 202 | static void getPathList(const char*path, std::vector<Path>& Paths) { |
| 203 | const char* at = path; |
| 204 | const char* delim = strchr(at, PathSeparator); |
| 205 | Path tmpPath; |
| 206 | while (delim != 0) { |
| 207 | std::string tmp(at, size_t(delim-at)); |
| 208 | if (tmpPath.set(tmp)) |
| 209 | if (tmpPath.canRead()) |
| 210 | Paths.push_back(tmpPath); |
| 211 | at = delim + 1; |
| 212 | delim = strchr(at, PathSeparator); |
| 213 | } |
| 214 | |
| 215 | if (*at != 0) |
| 216 | if (tmpPath.set(std::string(at))) |
| 217 | if (tmpPath.canRead()) |
| 218 | Paths.push_back(tmpPath); |
| 219 | } |
| 220 | |
Ted Kremenek | 9b01cc0 | 2008-04-07 22:01:32 +0000 | [diff] [blame] | 221 | static std::string getDirnameCharSep(const std::string& path, char Sep) { |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 222 | |
| 223 | if (path.empty()) |
| 224 | return "."; |
| 225 | |
| 226 | // If the path is all slashes, return a single slash. |
| 227 | // Otherwise, remove all trailing slashes. |
| 228 | |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 229 | signed pos = static_cast<signed>(path.size()) - 1; |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 230 | |
| 231 | while (pos >= 0 && path[pos] == Sep) |
| 232 | --pos; |
| 233 | |
| 234 | if (pos < 0) |
| 235 | return path[0] == Sep ? std::string(1, Sep) : std::string("."); |
| 236 | |
| 237 | // Any slashes left? |
| 238 | signed i = 0; |
| 239 | |
| 240 | while (i < pos && path[i] != Sep) |
| 241 | ++i; |
| 242 | |
| 243 | if (i == pos) // No slashes? Return "." |
| 244 | return "."; |
| 245 | |
| 246 | // There is at least one slash left. Remove all trailing non-slashes. |
| 247 | while (pos >= 0 && path[pos] != Sep) |
| 248 | --pos; |
| 249 | |
| 250 | // Remove any trailing slashes. |
| 251 | while (pos >= 0 && path[pos] == Sep) |
| 252 | --pos; |
| 253 | |
| 254 | if (pos < 0) |
| 255 | return path[0] == Sep ? std::string(1, Sep) : std::string("."); |
| 256 | |
| 257 | return path.substr(0, pos+1); |
| 258 | } |
| 259 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 260 | // Include the truly platform-specific parts of this class. |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 261 | #if defined(LLVM_ON_UNIX) |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 262 | #include "Unix/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 263 | #endif |
| 264 | #if defined(LLVM_ON_WIN32) |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 265 | #include "Win32/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 266 | #endif |
Reid Spencer | 23dd332 | 2006-07-26 16:55:39 +0000 | [diff] [blame] | 267 | |