blob: 0bd48490bb5298b347a4206b865aaf39c530b567 [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]) {
46 case 'l':
Reid Spencer8bb5fd12007-04-04 06:30:26 +000047 if (magic[1] == 'l' && magic[2] == 'v')
Reid Spencerf37ce992004-11-14 22:05:32 +000048 if (magic[3] == 'c')
Reid Spencer8bb5fd12007-04-04 06:30:26 +000049 return CompressedBytecode_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +000050 else if (magic[3] == 'm')
Reid Spencer8bb5fd12007-04-04 06:30:26 +000051 return Bytecode_FileType;
52 break;
53 case '!':
54 if (length >= 8)
55 if (memcmp(magic,"!<arch>\n",8) == 0)
56 return Archive_FileType;
57 break;
58
59 case '\177':
60 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F')
61 return ELF_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +000062 break;
63
Reid Spencer8bb5fd12007-04-04 06:30:26 +000064 case 0xCE:
65 case 0xCF:
66 // This is complicated by an overlap with Java class files.
67 // See the Mach-O section in /usr/share/file/magic for details.
68 if (magic[1] == char(0xFA) && magic[2] == char(0xED) &&
69 magic[3] == char(0xFE))
70 if (length >= 15)
71 if (magic[15] == 1 || magic[15] == 3 || magic[15] == 6 ||
72 magic[15] == 9)
73 return Mach_O_FileType;
74 break;
75
76 case 0xF0: // PowerPC Windows
77 case 0x83: // Alpha 32-bit
78 case 0x84: // Alpha 64-bit
79 case 0x66: // MPS R4000 Windows
80 case 0x50: // mc68K
81 case 0x4c: // 80386 Windows
82 if (magic[1] == 0x01)
83 return COFF_FileType;
84
85 case 0x90: // PA-RISC Windows
86 case 0x68: // mc68K Windows
87 if (magic[1] == 0x02)
88 return COFF_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +000089 break;
90
91 default:
92 break;
93 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +000094 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +000095}
96
Reid Spencerccb23a12004-12-13 03:00:39 +000097bool
98Path::isArchive() const {
Reid Spencerc7f08322005-07-07 18:21:42 +000099 if (canRead())
Reid Spencerccb23a12004-12-13 03:00:39 +0000100 return hasMagicNumber("!<arch>\012");
101 return false;
102}
103
104bool
105Path::isDynamicLibrary() const {
Reid Spencer410aa022007-04-11 00:49:39 +0000106 if (canRead()) {
107 std::string Magic;
108 if (getMagicNumber(Magic, 64))
109 switch (IdentifyFileType(Magic.c_str(), Magic.length())) {
110 default: return false;
111 case ELF_FileType:
112 case Mach_O_FileType:
113 case COFF_FileType: return true;
114 }
115 }
Reid Spencerccb23a12004-12-13 03:00:39 +0000116 return false;
117}
118
119Path
120Path::FindLibrary(std::string& name) {
121 std::vector<sys::Path> LibPaths;
122 GetSystemLibraryPaths(LibPaths);
123 for (unsigned i = 0; i < LibPaths.size(); ++i) {
124 sys::Path FullPath(LibPaths[i]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000125 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000126 if (FullPath.isDynamicLibrary())
127 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000128 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000129 FullPath.appendSuffix("a");
130 if (FullPath.isArchive())
131 return FullPath;
132 }
133 return sys::Path();
134}
135
Chris Lattnerc67dc452006-07-07 18:11:32 +0000136std::string Path::GetDLLSuffix() {
Reid Spencer79fc9242004-12-13 18:41:28 +0000137 return LTDL_SHLIB_EXT;
138}
139
Reid Spencerb89a2232004-08-25 06:20:07 +0000140// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000141#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000142#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000143#endif
144#if defined(LLVM_ON_WIN32)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000145#include "Win32/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000146#endif
Reid Spencer23dd3322006-07-26 16:55:39 +0000147
148DEFINING_FILE_FOR(SystemPath)