1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file implements the operating system Path concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/System/Path.h"
15 #include "llvm/Config/config.h"
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //=== independent code.
24 //===----------------------------------------------------------------------===//
26 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
27 strm << aPath.toString();
32 Path::GetLLVMConfigDir() {
35 if (result.set(LLVM_ETCDIR))
38 return GetLLVMDefaultConfigDir();
42 sys::IdentifyFileType(const char*magic, unsigned length) {
43 assert(magic && "Invalid magic number string");
44 assert(length >=4 && "Invalid magic number length");
47 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
48 return Bitcode_FileType;
52 if (memcmp(magic,"!<arch>\n",8) == 0)
53 return Archive_FileType;
57 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
58 if (length >= 18 && magic[17] == 0)
61 case 1: return ELF_Relocatable_FileType;
62 case 2: return ELF_Executable_FileType;
63 case 3: return ELF_SharedObject_FileType;
64 case 4: return ELF_Core_FileType;
70 // This is complicated by an overlap with Java class files.
71 // See the Mach-O section in /usr/share/file/magic for details.
72 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
73 magic[3] == char(0xBE)) {
74 return Mach_O_DynamicallyLinkedSharedLib_FileType;
76 // FIXME: How does this work?
77 if (length >= 14 && magic[13] == 0)
80 case 1: return Mach_O_Object_FileType;
81 case 2: return Mach_O_Executable_FileType;
82 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
83 case 4: return Mach_O_Core_FileType;
84 case 5: return Mach_O_PreloadExectuable_FileType;
85 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
86 case 7: return Mach_O_DynamicLinker_FileType;
87 case 8: return Mach_O_Bundle_FileType;
88 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
93 case 0xF0: // PowerPC Windows
94 case 0x83: // Alpha 32-bit
95 case 0x84: // Alpha 64-bit
96 case 0x66: // MPS R4000 Windows
98 case 0x4c: // 80386 Windows
100 return COFF_FileType;
102 case 0x90: // PA-RISC Windows
103 case 0x68: // mc68K Windows
104 if (magic[1] == 0x02)
105 return COFF_FileType;
111 return Unknown_FileType;
115 Path::isArchive() const {
117 return hasMagicNumber("!<arch>\012");
122 Path::isDynamicLibrary() const {
125 if (getMagicNumber(Magic, 64))
126 switch (IdentifyFileType(Magic.c_str(), Magic.length())) {
127 default: return false;
128 case Mach_O_FixedVirtualMemorySharedLib_FileType:
129 case Mach_O_DynamicallyLinkedSharedLib_FileType:
130 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
131 case ELF_SharedObject_FileType:
132 case COFF_FileType: return true;
139 Path::FindLibrary(std::string& name) {
140 std::vector<sys::Path> LibPaths;
141 GetSystemLibraryPaths(LibPaths);
142 for (unsigned i = 0; i < LibPaths.size(); ++i) {
143 sys::Path FullPath(LibPaths[i]);
144 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
145 if (FullPath.isDynamicLibrary())
147 FullPath.eraseSuffix();
148 FullPath.appendSuffix("a");
149 if (FullPath.isArchive())
155 std::string Path::GetDLLSuffix() {
156 return LTDL_SHLIB_EXT;
160 Path::isBitcodeFile() const {
161 std::string actualMagic;
162 if (!getMagicNumber(actualMagic, 4))
164 return actualMagic == "BC\xC0\xDE";
167 bool Path::hasMagicNumber(const std::string &Magic) const {
168 std::string actualMagic;
169 if (getMagicNumber(actualMagic, Magic.size()))
170 return Magic == actualMagic;
176 // Include the truly platform-specific parts of this class.
177 #if defined(LLVM_ON_UNIX)
178 #include "Unix/Path.inc"
180 #if defined(LLVM_ON_WIN32)
181 #include "Win32/Path.inc"
184 DEFINING_FILE_FOR(SystemPath)