blob: 43c36d5e09b9c97f1100c65027afd2d77eed6b54 [file] [log] [blame]
Reid Spencerb89a2232004-08-25 06:20:07 +00001//===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencerb89a2232004-08-25 06:20:07 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencerb89a2232004-08-25 06:20:07 +00008//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system Path concept.
11//
12//===----------------------------------------------------------------------===//
Reid Spencer8e665952004-08-29 05:24:01 +000013
Reid Spencerb89a2232004-08-25 06:20:07 +000014#include "llvm/System/Path.h"
Reid Spencer79fc9242004-12-13 18:41:28 +000015#include "llvm/Config/config.h"
Alkis Evlogimenos98bc8ed2004-11-14 22:37:42 +000016#include <cassert>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000017#include <cstring>
Chris Lattnerc67dc452006-07-07 18:11:32 +000018#include <ostream>
19using namespace llvm;
Reid Spencer8e665952004-08-29 05:24:01 +000020using namespace sys;
Reid Spencerb89a2232004-08-25 06:20:07 +000021
22//===----------------------------------------------------------------------===//
23//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000024//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000025//===----------------------------------------------------------------------===//
26
Chris Lattnerc67dc452006-07-07 18:11:32 +000027std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
28 strm << aPath.toString();
29 return strm;
30}
31
Reid Spencerc29befb2004-12-15 01:50:13 +000032Path
33Path::GetLLVMConfigDir() {
34 Path result;
Jeff Cohenab68df02004-12-15 04:08:15 +000035#ifdef LLVM_ETCDIR
Reid Spencerdd04df02005-07-07 23:21:43 +000036 if (result.set(LLVM_ETCDIR))
Reid Spencerc29befb2004-12-15 01:50:13 +000037 return result;
Jeff Cohenab68df02004-12-15 04:08:15 +000038#endif
Reid Spencerc29befb2004-12-15 01:50:13 +000039 return GetLLVMDefaultConfigDir();
40}
41
Misha Brukmanf976c852005-04-21 22:55:34 +000042LLVMFileType
Reid Spencera2a62212004-11-14 23:26:18 +000043sys::IdentifyFileType(const char*magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000044 assert(magic && "Invalid magic number string");
45 assert(length >=4 && "Invalid magic number length");
46 switch (magic[0]) {
Chris Lattnerf283a5e2007-05-06 05:32:21 +000047 case 'B':
48 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
49 return Bitcode_FileType;
50 break;
Reid Spencer8bb5fd12007-04-04 06:30:26 +000051 case '!':
52 if (length >= 8)
53 if (memcmp(magic,"!<arch>\n",8) == 0)
54 return Archive_FileType;
55 break;
56
57 case '\177':
Chris Lattner24eac6c2007-05-03 18:15:56 +000058 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Reid Spencer947aa7d2007-04-11 02:02:09 +000059 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 Lattner24eac6c2007-05-03 18:15:56 +000067 }
Reid Spencerf37ce992004-11-14 22:05:32 +000068 break;
69
Chris Lattnerade75922007-04-11 03:15:35 +000070 case 0xCA:
Reid Spencer8bb5fd12007-04-04 06:30:26 +000071 // This is complicated by an overlap with Java class files.
72 // See the Mach-O section in /usr/share/file/magic for details.
Chris Lattnerade75922007-04-11 03:15:35 +000073 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 Spencer947aa7d2007-04-11 02:02:09 +000078 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 Lattnerade75922007-04-11 03:15:35 +000091 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +000092 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 Spencerf37ce992004-11-14 22:05:32 +0000107 break;
108
109 default:
110 break;
111 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000112 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000113}
114
Reid Spencerccb23a12004-12-13 03:00:39 +0000115bool
116Path::isArchive() const {
Reid Spencerc7f08322005-07-07 18:21:42 +0000117 if (canRead())
Reid Spencerccb23a12004-12-13 03:00:39 +0000118 return hasMagicNumber("!<arch>\012");
119 return false;
120}
121
122bool
123Path::isDynamicLibrary() const {
Reid Spencer410aa022007-04-11 00:49:39 +0000124 if (canRead()) {
125 std::string Magic;
126 if (getMagicNumber(Magic, 64))
127 switch (IdentifyFileType(Magic.c_str(), Magic.length())) {
128 default: return false;
Reid Spencer947aa7d2007-04-11 02:02:09 +0000129 case Mach_O_FixedVirtualMemorySharedLib_FileType:
130 case Mach_O_DynamicallyLinkedSharedLib_FileType:
131 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
132 case ELF_SharedObject_FileType:
Reid Spencer410aa022007-04-11 00:49:39 +0000133 case COFF_FileType: return true;
134 }
135 }
Reid Spencerccb23a12004-12-13 03:00:39 +0000136 return false;
137}
138
139Path
140Path::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 Spencerdd04df02005-07-07 23:21:43 +0000145 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000146 if (FullPath.isDynamicLibrary())
147 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000148 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000149 FullPath.appendSuffix("a");
150 if (FullPath.isArchive())
151 return FullPath;
152 }
153 return sys::Path();
154}
155
Chris Lattnerc67dc452006-07-07 18:11:32 +0000156std::string Path::GetDLLSuffix() {
Reid Spencer79fc9242004-12-13 18:41:28 +0000157 return LTDL_SHLIB_EXT;
158}
159
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000160bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000161Path::isBitcodeFile() const {
162 std::string actualMagic;
163 if (!getMagicNumber(actualMagic, 4))
164 return false;
165 return actualMagic == "BC\xC0\xDE";
166}
167
168bool 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 Korobeynikove18bc082008-02-20 19:41:22 +0000175std::string
176Path::getSuffix() const {
177 return path.substr(path.rfind('.') + 1);
178}
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000179
Chris Lattnere1b332a2008-02-27 06:17:10 +0000180static 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 Kremenekcf55c8e2008-04-07 21:53:57 +0000199std::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 Spencerb89a2232004-08-25 06:20:07 +0000238// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000239#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000240#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000241#endif
242#if defined(LLVM_ON_WIN32)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000243#include "Win32/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000244#endif
Reid Spencer23dd3322006-07-26 16:55:39 +0000245