blob: 8a1de75e478c90979d2a6abd6b3fd6d3c42053f5 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system Path concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/Path.h"
15#include "llvm/Config/config.h"
16#include <cassert>
Duncan Sandsfca20142008-01-09 19:42:09 +000017#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include <ostream>
19using namespace llvm;
20using namespace sys;
21
22//===----------------------------------------------------------------------===//
23//=== WARNING: Implementation here must contain only TRULY operating system
24//=== independent code.
25//===----------------------------------------------------------------------===//
26
27std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
28 strm << aPath.toString();
29 return strm;
30}
31
32Path
33Path::GetLLVMConfigDir() {
34 Path result;
35#ifdef LLVM_ETCDIR
36 if (result.set(LLVM_ETCDIR))
37 return result;
38#endif
39 return GetLLVMDefaultConfigDir();
40}
41
42LLVMFileType
43sys::IdentifyFileType(const char*magic, unsigned length) {
44 assert(magic && "Invalid magic number string");
45 assert(length >=4 && "Invalid magic number length");
46 switch (magic[0]) {
47 case 'B':
48 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
49 return Bitcode_FileType;
50 break;
51 case '!':
52 if (length >= 8)
53 if (memcmp(magic,"!<arch>\n",8) == 0)
54 return Archive_FileType;
55 break;
56
57 case '\177':
58 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
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 }
67 }
68 break;
69
70 case 0xCA:
71 // This is complicated by an overlap with Java class files.
72 // See the Mach-O section in /usr/share/file/magic for details.
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?
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 }
91 }
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;
107 break;
108
109 default:
110 break;
111 }
112 return Unknown_FileType;
113}
114
115bool
116Path::isArchive() const {
117 if (canRead())
118 return hasMagicNumber("!<arch>\012");
119 return false;
120}
121
122bool
123Path::isDynamicLibrary() const {
124 if (canRead()) {
125 std::string Magic;
126 if (getMagicNumber(Magic, 64))
127 switch (IdentifyFileType(Magic.c_str(), Magic.length())) {
128 default: return false;
129 case Mach_O_FixedVirtualMemorySharedLib_FileType:
130 case Mach_O_DynamicallyLinkedSharedLib_FileType:
131 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
132 case ELF_SharedObject_FileType:
133 case COFF_FileType: return true;
134 }
135 }
136 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]);
145 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
146 if (FullPath.isDynamicLibrary())
147 return FullPath;
148 FullPath.eraseSuffix();
149 FullPath.appendSuffix("a");
150 if (FullPath.isArchive())
151 return FullPath;
152 }
153 return sys::Path();
154}
155
156std::string Path::GetDLLSuffix() {
157 return LTDL_SHLIB_EXT;
158}
159
160bool
161Path::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 Korobeynikov382ff1c2008-02-20 19:41:22 +0000175std::string
176Path::getSuffix() const {
177 return path.substr(path.rfind('.') + 1);
178}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179
Chris Lattner4b8f1c62008-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199// Include the truly platform-specific parts of this class.
200#if defined(LLVM_ON_UNIX)
201#include "Unix/Path.inc"
202#endif
203#if defined(LLVM_ON_WIN32)
204#include "Win32/Path.inc"
205#endif
206