blob: fcdc5ff22cf3cec57474a8c97d6cf41ea064345b [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//
Misha Brukmanf976c852005-04-21 22:55:34 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencerb89a2232004-08-25 06:20:07 +00006// University of Illinois Open Source 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>
Chris Lattnerc67dc452006-07-07 18:11:32 +000017#include <ostream>
18using namespace llvm;
Reid Spencer8e665952004-08-29 05:24:01 +000019using namespace sys;
Reid Spencerb89a2232004-08-25 06:20:07 +000020
21//===----------------------------------------------------------------------===//
22//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000023//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000024//===----------------------------------------------------------------------===//
25
Chris Lattnerc67dc452006-07-07 18:11:32 +000026std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
27 strm << aPath.toString();
28 return strm;
29}
30
Reid Spencerc29befb2004-12-15 01:50:13 +000031Path
32Path::GetLLVMConfigDir() {
33 Path result;
Jeff Cohenab68df02004-12-15 04:08:15 +000034#ifdef LLVM_ETCDIR
Reid Spencerdd04df02005-07-07 23:21:43 +000035 if (result.set(LLVM_ETCDIR))
Reid Spencerc29befb2004-12-15 01:50:13 +000036 return result;
Jeff Cohenab68df02004-12-15 04:08:15 +000037#endif
Reid Spencerc29befb2004-12-15 01:50:13 +000038 return GetLLVMDefaultConfigDir();
39}
40
Misha Brukmanf976c852005-04-21 22:55:34 +000041LLVMFileType
Reid Spencera2a62212004-11-14 23:26:18 +000042sys::IdentifyFileType(const char*magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000043 assert(magic && "Invalid magic number string");
44 assert(length >=4 && "Invalid magic number length");
45 switch (magic[0]) {
Chris Lattnerf283a5e2007-05-06 05:32:21 +000046 case 'B':
47 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
48 return Bitcode_FileType;
49 break;
Reid Spencerf37ce992004-11-14 22:05:32 +000050 case 'l':
Chris Lattner24eac6c2007-05-03 18:15:56 +000051 if (magic[1] == 'l' && magic[2] == 'v') {
Reid Spencerf37ce992004-11-14 22:05:32 +000052 if (magic[3] == 'c')
Reid Spencer8bb5fd12007-04-04 06:30:26 +000053 return CompressedBytecode_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +000054 else if (magic[3] == 'm')
Reid Spencer8bb5fd12007-04-04 06:30:26 +000055 return Bytecode_FileType;
Chris Lattner24eac6c2007-05-03 18:15:56 +000056 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +000057 break;
58 case '!':
59 if (length >= 8)
60 if (memcmp(magic,"!<arch>\n",8) == 0)
61 return Archive_FileType;
62 break;
63
64 case '\177':
Chris Lattner24eac6c2007-05-03 18:15:56 +000065 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Reid Spencer947aa7d2007-04-11 02:02:09 +000066 if (length >= 18 && magic[17] == 0)
67 switch (magic[16]) {
68 default: break;
69 case 1: return ELF_Relocatable_FileType;
70 case 2: return ELF_Executable_FileType;
71 case 3: return ELF_SharedObject_FileType;
72 case 4: return ELF_Core_FileType;
73 }
Chris Lattner24eac6c2007-05-03 18:15:56 +000074 }
Reid Spencerf37ce992004-11-14 22:05:32 +000075 break;
76
Chris Lattnerade75922007-04-11 03:15:35 +000077 case 0xCA:
Reid Spencer8bb5fd12007-04-04 06:30:26 +000078 // This is complicated by an overlap with Java class files.
79 // See the Mach-O section in /usr/share/file/magic for details.
Chris Lattnerade75922007-04-11 03:15:35 +000080 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
81 magic[3] == char(0xBE)) {
82 return Mach_O_DynamicallyLinkedSharedLib_FileType;
83
84 // FIXME: How does this work?
Reid Spencer947aa7d2007-04-11 02:02:09 +000085 if (length >= 14 && magic[13] == 0)
86 switch (magic[12]) {
87 default: break;
88 case 1: return Mach_O_Object_FileType;
89 case 2: return Mach_O_Executable_FileType;
90 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
91 case 4: return Mach_O_Core_FileType;
92 case 5: return Mach_O_PreloadExectuable_FileType;
93 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
94 case 7: return Mach_O_DynamicLinker_FileType;
95 case 8: return Mach_O_Bundle_FileType;
96 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
97 }
Chris Lattnerade75922007-04-11 03:15:35 +000098 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +000099 break;
100
101 case 0xF0: // PowerPC Windows
102 case 0x83: // Alpha 32-bit
103 case 0x84: // Alpha 64-bit
104 case 0x66: // MPS R4000 Windows
105 case 0x50: // mc68K
106 case 0x4c: // 80386 Windows
107 if (magic[1] == 0x01)
108 return COFF_FileType;
109
110 case 0x90: // PA-RISC Windows
111 case 0x68: // mc68K Windows
112 if (magic[1] == 0x02)
113 return COFF_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000114 break;
115
116 default:
117 break;
118 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000119 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000120}
121
Reid Spencerccb23a12004-12-13 03:00:39 +0000122bool
123Path::isArchive() const {
Reid Spencerc7f08322005-07-07 18:21:42 +0000124 if (canRead())
Reid Spencerccb23a12004-12-13 03:00:39 +0000125 return hasMagicNumber("!<arch>\012");
126 return false;
127}
128
129bool
130Path::isDynamicLibrary() const {
Reid Spencer410aa022007-04-11 00:49:39 +0000131 if (canRead()) {
132 std::string Magic;
133 if (getMagicNumber(Magic, 64))
134 switch (IdentifyFileType(Magic.c_str(), Magic.length())) {
135 default: return false;
Reid Spencer947aa7d2007-04-11 02:02:09 +0000136 case Mach_O_FixedVirtualMemorySharedLib_FileType:
137 case Mach_O_DynamicallyLinkedSharedLib_FileType:
138 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
139 case ELF_SharedObject_FileType:
Reid Spencer410aa022007-04-11 00:49:39 +0000140 case COFF_FileType: return true;
141 }
142 }
Reid Spencerccb23a12004-12-13 03:00:39 +0000143 return false;
144}
145
146Path
147Path::FindLibrary(std::string& name) {
148 std::vector<sys::Path> LibPaths;
149 GetSystemLibraryPaths(LibPaths);
150 for (unsigned i = 0; i < LibPaths.size(); ++i) {
151 sys::Path FullPath(LibPaths[i]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000152 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000153 if (FullPath.isDynamicLibrary())
154 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000155 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000156 FullPath.appendSuffix("a");
157 if (FullPath.isArchive())
158 return FullPath;
159 }
160 return sys::Path();
161}
162
Chris Lattnerc67dc452006-07-07 18:11:32 +0000163std::string Path::GetDLLSuffix() {
Reid Spencer79fc9242004-12-13 18:41:28 +0000164 return LTDL_SHLIB_EXT;
165}
166
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000167bool
168Path::isBytecodeFile() const {
169 std::string actualMagic;
170 if (!getMagicNumber(actualMagic, 4))
171 return false;
172 return actualMagic == "llvc" || actualMagic == "llvm";
173}
174
175bool
176Path::isBitcodeFile() const {
177 std::string actualMagic;
178 if (!getMagicNumber(actualMagic, 4))
179 return false;
180 return actualMagic == "BC\xC0\xDE";
181}
182
183bool Path::hasMagicNumber(const std::string &Magic) const {
184 std::string actualMagic;
185 if (getMagicNumber(actualMagic, Magic.size()))
186 return Magic == actualMagic;
187 return false;
188}
189
190
191
Reid Spencerb89a2232004-08-25 06:20:07 +0000192// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000193#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000194#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000195#endif
196#if defined(LLVM_ON_WIN32)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000197#include "Win32/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000198#endif
Reid Spencer23dd3322006-07-26 16:55:39 +0000199
200DEFINING_FILE_FOR(SystemPath)