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 |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +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"); |
Torok Edwin | 4cdc44c | 2009-04-25 10:25:12 +0000 | [diff] [blame] | 58 | switch ((unsigned char)magic[0]) { |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 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; |
| 63 | break; |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 64 | case 'B': |
| 65 | if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE) |
| 66 | return Bitcode_FileType; |
| 67 | break; |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 68 | case '!': |
| 69 | if (length >= 8) |
| 70 | if (memcmp(magic,"!<arch>\n",8) == 0) |
| 71 | return Archive_FileType; |
| 72 | break; |
| 73 | |
| 74 | case '\177': |
Chris Lattner | 24eac6c | 2007-05-03 18:15:56 +0000 | [diff] [blame] | 75 | if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') { |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 76 | if (length >= 18 && magic[17] == 0) |
| 77 | switch (magic[16]) { |
| 78 | default: break; |
| 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; |
| 83 | } |
Chris Lattner | 24eac6c | 2007-05-03 18:15:56 +0000 | [diff] [blame] | 84 | } |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 85 | break; |
| 86 | |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 87 | case 0xCA: |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 88 | if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && |
| 89 | magic[3] == char(0xBE)) { |
Chris Lattner | 6521549 | 2008-06-26 05:17:18 +0000 | [diff] [blame] | 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; |
| 95 | } |
| 96 | break; |
| 97 | |
| 98 | case 0xFE: |
Bill Wendling | fc1fd54 | 2008-06-26 08:32:05 +0000 | [diff] [blame] | 99 | case 0xCE: { |
| 100 | uint16_t type = 0; |
Chris Lattner | 6521549 | 2008-06-26 05:17:18 +0000 | [diff] [blame] | 101 | if (magic[0] == char(0xFE) && magic[1] == char(0xED) && |
| 102 | magic[2] == char(0xFA) && magic[3] == char(0xCE)) { |
| 103 | /* Native endian */ |
| 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)) { |
| 107 | /* Reverse endian */ |
| 108 | if (length >= 14) type = magic[13] << 8 | magic[12]; |
Bill Wendling | fc1fd54 | 2008-06-26 08:32:05 +0000 | [diff] [blame] | 109 | } |
Chris Lattner | 6521549 | 2008-06-26 05:17:18 +0000 | [diff] [blame] | 110 | switch (type) { |
| 111 | default: break; |
| 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. |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 122 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 123 | break; |
Bill Wendling | fc1fd54 | 2008-06-26 08:32:05 +0000 | [diff] [blame] | 124 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 125 | case 0xF0: // PowerPC Windows |
| 126 | case 0x83: // Alpha 32-bit |
| 127 | case 0x84: // Alpha 64-bit |
| 128 | case 0x66: // MPS R4000 Windows |
| 129 | case 0x50: // mc68K |
| 130 | case 0x4c: // 80386 Windows |
| 131 | if (magic[1] == 0x01) |
| 132 | return COFF_FileType; |
| 133 | |
| 134 | case 0x90: // PA-RISC Windows |
| 135 | case 0x68: // mc68K Windows |
| 136 | if (magic[1] == 0x02) |
| 137 | return COFF_FileType; |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 138 | break; |
| 139 | |
| 140 | default: |
| 141 | break; |
| 142 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 143 | return Unknown_FileType; |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 146 | bool |
| 147 | Path::isArchive() const { |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 148 | if (canRead()) |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 149 | return hasMagicNumber("!<arch>\012"); |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | bool |
| 154 | Path::isDynamicLibrary() const { |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 155 | if (canRead()) { |
| 156 | std::string Magic; |
| 157 | if (getMagicNumber(Magic, 64)) |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 158 | switch (IdentifyFileType(Magic.c_str(), |
| 159 | static_cast<unsigned>(Magic.length()))) { |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 160 | default: return false; |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 161 | case Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 162 | case Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 163 | case Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
| 164 | case ELF_SharedObject_FileType: |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 165 | case COFF_FileType: return true; |
| 166 | } |
| 167 | } |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | |
| 171 | Path |
| 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]); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 177 | FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT); |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 178 | if (FullPath.isDynamicLibrary()) |
| 179 | return FullPath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 180 | FullPath.eraseSuffix(); |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 181 | FullPath.appendSuffix("a"); |
| 182 | if (FullPath.isArchive()) |
| 183 | return FullPath; |
| 184 | } |
| 185 | return sys::Path(); |
| 186 | } |
| 187 | |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 188 | std::string Path::GetDLLSuffix() { |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 189 | return LTDL_SHLIB_EXT; |
| 190 | } |
| 191 | |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 192 | bool |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 193 | Path::isBitcodeFile() const { |
| 194 | std::string actualMagic; |
| 195 | if (!getMagicNumber(actualMagic, 4)) |
| 196 | return false; |
Devang Patel | 54ce536 | 2008-07-22 18:00:36 +0000 | [diff] [blame] | 197 | LLVMFileType FT = |
| 198 | IdentifyFileType(actualMagic.c_str(), |
| 199 | static_cast<unsigned>(actualMagic.length())); |
| 200 | return FT == Bitcode_FileType; |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | bool Path::hasMagicNumber(const std::string &Magic) const { |
| 204 | std::string actualMagic; |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 205 | if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size()))) |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 206 | return Magic == actualMagic; |
| 207 | return false; |
| 208 | } |
| 209 | |
Daniel Dunbar | 6ddf2c1 | 2009-04-09 00:33:08 +0000 | [diff] [blame] | 210 | void Path::makeAbsolute() { |
| 211 | if (isAbsolute()) |
| 212 | return; |
| 213 | |
| 214 | Path CWD = Path::GetCurrentDirectory(); |
| 215 | assert(CWD.isAbsolute() && "GetCurrentDirectory returned relative path!"); |
| 216 | |
| 217 | CWD.appendComponent(path); |
| 218 | |
| 219 | path = CWD.toString(); |
| 220 | } |
| 221 | |
Chris Lattner | e1b332a | 2008-02-27 06:17:10 +0000 | [diff] [blame] | 222 | static void getPathList(const char*path, std::vector<Path>& Paths) { |
| 223 | const char* at = path; |
| 224 | const char* delim = strchr(at, PathSeparator); |
| 225 | Path tmpPath; |
| 226 | while (delim != 0) { |
| 227 | std::string tmp(at, size_t(delim-at)); |
| 228 | if (tmpPath.set(tmp)) |
| 229 | if (tmpPath.canRead()) |
| 230 | Paths.push_back(tmpPath); |
| 231 | at = delim + 1; |
| 232 | delim = strchr(at, PathSeparator); |
| 233 | } |
| 234 | |
| 235 | if (*at != 0) |
| 236 | if (tmpPath.set(std::string(at))) |
| 237 | if (tmpPath.canRead()) |
| 238 | Paths.push_back(tmpPath); |
| 239 | } |
| 240 | |
Ted Kremenek | 9b01cc0 | 2008-04-07 22:01:32 +0000 | [diff] [blame] | 241 | static std::string getDirnameCharSep(const std::string& path, char Sep) { |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 242 | |
| 243 | if (path.empty()) |
| 244 | return "."; |
| 245 | |
| 246 | // If the path is all slashes, return a single slash. |
| 247 | // Otherwise, remove all trailing slashes. |
| 248 | |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 249 | signed pos = static_cast<signed>(path.size()) - 1; |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 250 | |
| 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 | // Any slashes left? |
| 258 | signed i = 0; |
| 259 | |
| 260 | while (i < pos && path[i] != Sep) |
| 261 | ++i; |
| 262 | |
| 263 | if (i == pos) // No slashes? Return "." |
| 264 | return "."; |
| 265 | |
| 266 | // There is at least one slash left. Remove all trailing non-slashes. |
| 267 | while (pos >= 0 && path[pos] != Sep) |
| 268 | --pos; |
| 269 | |
| 270 | // Remove any trailing slashes. |
| 271 | while (pos >= 0 && path[pos] == Sep) |
| 272 | --pos; |
| 273 | |
| 274 | if (pos < 0) |
| 275 | return path[0] == Sep ? std::string(1, Sep) : std::string("."); |
| 276 | |
| 277 | return path.substr(0, pos+1); |
| 278 | } |
| 279 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 280 | // Include the truly platform-specific parts of this class. |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 281 | #if defined(LLVM_ON_UNIX) |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 282 | #include "Unix/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 283 | #endif |
| 284 | #if defined(LLVM_ON_WIN32) |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 285 | #include "Win32/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 286 | #endif |
Reid Spencer | 23dd332 | 2006-07-26 16:55:39 +0000 | [diff] [blame] | 287 | |