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 | |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 27 | std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) { |
| 28 | strm << aPath.toString(); |
| 29 | return strm; |
| 30 | } |
| 31 | |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 32 | Path |
| 33 | Path::GetLLVMConfigDir() { |
| 34 | Path result; |
Jeff Cohen | ab68df0 | 2004-12-15 04:08:15 +0000 | [diff] [blame] | 35 | #ifdef LLVM_ETCDIR |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 36 | if (result.set(LLVM_ETCDIR)) |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 37 | return result; |
Jeff Cohen | ab68df0 | 2004-12-15 04:08:15 +0000 | [diff] [blame] | 38 | #endif |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 39 | return GetLLVMDefaultConfigDir(); |
| 40 | } |
| 41 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 42 | LLVMFileType |
Reid Spencer | a2a6221 | 2004-11-14 23:26:18 +0000 | [diff] [blame] | 43 | sys::IdentifyFileType(const char*magic, unsigned length) { |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 44 | assert(magic && "Invalid magic number string"); |
| 45 | assert(length >=4 && "Invalid magic number length"); |
| 46 | switch (magic[0]) { |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 47 | case 'B': |
| 48 | if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE) |
| 49 | return Bitcode_FileType; |
| 50 | break; |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 51 | case '!': |
| 52 | if (length >= 8) |
| 53 | if (memcmp(magic,"!<arch>\n",8) == 0) |
| 54 | return Archive_FileType; |
| 55 | break; |
| 56 | |
| 57 | case '\177': |
Chris Lattner | 24eac6c | 2007-05-03 18:15:56 +0000 | [diff] [blame] | 58 | if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') { |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 59 | if (length >= 18 && magic[17] == 0) |
| 60 | switch (magic[16]) { |
| 61 | default: break; |
| 62 | case 1: return ELF_Relocatable_FileType; |
| 63 | case 2: return ELF_Executable_FileType; |
| 64 | case 3: return ELF_SharedObject_FileType; |
| 65 | case 4: return ELF_Core_FileType; |
| 66 | } |
Chris Lattner | 24eac6c | 2007-05-03 18:15:56 +0000 | [diff] [blame] | 67 | } |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 68 | break; |
| 69 | |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 70 | case 0xCA: |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 71 | // This is complicated by an overlap with Java class files. |
| 72 | // See the Mach-O section in /usr/share/file/magic for details. |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 73 | if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && |
| 74 | magic[3] == char(0xBE)) { |
| 75 | return Mach_O_DynamicallyLinkedSharedLib_FileType; |
| 76 | |
| 77 | // FIXME: How does this work? |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 78 | if (length >= 14 && magic[13] == 0) |
| 79 | switch (magic[12]) { |
| 80 | default: break; |
| 81 | case 1: return Mach_O_Object_FileType; |
| 82 | case 2: return Mach_O_Executable_FileType; |
| 83 | case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType; |
| 84 | case 4: return Mach_O_Core_FileType; |
| 85 | case 5: return Mach_O_PreloadExectuable_FileType; |
| 86 | case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType; |
| 87 | case 7: return Mach_O_DynamicLinker_FileType; |
| 88 | case 8: return Mach_O_Bundle_FileType; |
| 89 | case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType; |
| 90 | } |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 91 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 92 | break; |
| 93 | |
| 94 | case 0xF0: // PowerPC Windows |
| 95 | case 0x83: // Alpha 32-bit |
| 96 | case 0x84: // Alpha 64-bit |
| 97 | case 0x66: // MPS R4000 Windows |
| 98 | case 0x50: // mc68K |
| 99 | case 0x4c: // 80386 Windows |
| 100 | if (magic[1] == 0x01) |
| 101 | return COFF_FileType; |
| 102 | |
| 103 | case 0x90: // PA-RISC Windows |
| 104 | case 0x68: // mc68K Windows |
| 105 | if (magic[1] == 0x02) |
| 106 | return COFF_FileType; |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 107 | break; |
| 108 | |
| 109 | default: |
| 110 | break; |
| 111 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 112 | return Unknown_FileType; |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 115 | bool |
| 116 | Path::isArchive() const { |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 117 | if (canRead()) |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 118 | return hasMagicNumber("!<arch>\012"); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | bool |
| 123 | Path::isDynamicLibrary() const { |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 124 | if (canRead()) { |
| 125 | std::string Magic; |
| 126 | if (getMagicNumber(Magic, 64)) |
| 127 | switch (IdentifyFileType(Magic.c_str(), Magic.length())) { |
| 128 | default: return false; |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 129 | case Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 130 | case Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 131 | case Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
| 132 | case ELF_SharedObject_FileType: |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 133 | case COFF_FileType: return true; |
| 134 | } |
| 135 | } |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
| 139 | Path |
| 140 | Path::FindLibrary(std::string& name) { |
| 141 | std::vector<sys::Path> LibPaths; |
| 142 | GetSystemLibraryPaths(LibPaths); |
| 143 | for (unsigned i = 0; i < LibPaths.size(); ++i) { |
| 144 | sys::Path FullPath(LibPaths[i]); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 145 | FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT); |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 146 | if (FullPath.isDynamicLibrary()) |
| 147 | return FullPath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 148 | FullPath.eraseSuffix(); |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 149 | FullPath.appendSuffix("a"); |
| 150 | if (FullPath.isArchive()) |
| 151 | return FullPath; |
| 152 | } |
| 153 | return sys::Path(); |
| 154 | } |
| 155 | |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 156 | std::string Path::GetDLLSuffix() { |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 157 | return LTDL_SHLIB_EXT; |
| 158 | } |
| 159 | |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 160 | bool |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 161 | Path::isBitcodeFile() const { |
| 162 | std::string actualMagic; |
| 163 | if (!getMagicNumber(actualMagic, 4)) |
| 164 | return false; |
| 165 | return actualMagic == "BC\xC0\xDE"; |
| 166 | } |
| 167 | |
| 168 | bool Path::hasMagicNumber(const std::string &Magic) const { |
| 169 | std::string actualMagic; |
| 170 | if (getMagicNumber(actualMagic, Magic.size())) |
| 171 | return Magic == actualMagic; |
| 172 | return false; |
| 173 | } |
| 174 | |
Anton Korobeynikov | e18bc08 | 2008-02-20 19:41:22 +0000 | [diff] [blame] | 175 | std::string |
| 176 | Path::getSuffix() const { |
| 177 | return path.substr(path.rfind('.') + 1); |
| 178 | } |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 179 | |
Chris Lattner | e1b332a | 2008-02-27 06:17:10 +0000 | [diff] [blame] | 180 | static void getPathList(const char*path, std::vector<Path>& Paths) { |
| 181 | const char* at = path; |
| 182 | const char* delim = strchr(at, PathSeparator); |
| 183 | Path tmpPath; |
| 184 | while (delim != 0) { |
| 185 | std::string tmp(at, size_t(delim-at)); |
| 186 | if (tmpPath.set(tmp)) |
| 187 | if (tmpPath.canRead()) |
| 188 | Paths.push_back(tmpPath); |
| 189 | at = delim + 1; |
| 190 | delim = strchr(at, PathSeparator); |
| 191 | } |
| 192 | |
| 193 | if (*at != 0) |
| 194 | if (tmpPath.set(std::string(at))) |
| 195 | if (tmpPath.canRead()) |
| 196 | Paths.push_back(tmpPath); |
| 197 | } |
| 198 | |
Ted Kremenek | cf55c8e | 2008-04-07 21:53:57 +0000 | [diff] [blame^] | 199 | std::string Path::getDirnameCharSep(char Sep) const { |
| 200 | |
| 201 | if (path.empty()) |
| 202 | return "."; |
| 203 | |
| 204 | // If the path is all slashes, return a single slash. |
| 205 | // Otherwise, remove all trailing slashes. |
| 206 | |
| 207 | signed pos = path.size() - 1; |
| 208 | |
| 209 | while (pos >= 0 && path[pos] == Sep) |
| 210 | --pos; |
| 211 | |
| 212 | if (pos < 0) |
| 213 | return path[0] == Sep ? std::string(1, Sep) : std::string("."); |
| 214 | |
| 215 | // Any slashes left? |
| 216 | signed i = 0; |
| 217 | |
| 218 | while (i < pos && path[i] != Sep) |
| 219 | ++i; |
| 220 | |
| 221 | if (i == pos) // No slashes? Return "." |
| 222 | return "."; |
| 223 | |
| 224 | // There is at least one slash left. Remove all trailing non-slashes. |
| 225 | while (pos >= 0 && path[pos] != Sep) |
| 226 | --pos; |
| 227 | |
| 228 | // Remove any trailing slashes. |
| 229 | while (pos >= 0 && path[pos] == Sep) |
| 230 | --pos; |
| 231 | |
| 232 | if (pos < 0) |
| 233 | return path[0] == Sep ? std::string(1, Sep) : std::string("."); |
| 234 | |
| 235 | return path.substr(0, pos+1); |
| 236 | } |
| 237 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 238 | // Include the truly platform-specific parts of this class. |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 239 | #if defined(LLVM_ON_UNIX) |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 240 | #include "Unix/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 241 | #endif |
| 242 | #if defined(LLVM_ON_WIN32) |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 243 | #include "Win32/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 244 | #endif |
Reid Spencer | 23dd332 | 2006-07-26 16:55:39 +0000 | [diff] [blame] | 245 | |