1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
22 //===----------------------------------------------------------------------===//
23 //=== WARNING: Implementation here must contain only TRULY operating system
24 //=== independent code.
25 //===----------------------------------------------------------------------===//
27 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
28 strm << aPath.toString();
33 Path::GetLLVMConfigDir() {
36 if (result.set(LLVM_ETCDIR))
39 return GetLLVMDefaultConfigDir();
43 sys::IdentifyFileType(const char*magic, unsigned length) {
44 assert(magic && "Invalid magic number string");
45 assert(length >=4 && "Invalid magic number length");
48 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
49 return Bitcode_FileType;
53 if (memcmp(magic,"!<arch>\n",8) == 0)
54 return Archive_FileType;
58 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
59 if (length >= 18 && magic[17] == 0)
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;
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;
77 // FIXME: How does this work?
78 if (length >= 14 && magic[13] == 0)
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;
94 case 0xF0: // PowerPC Windows
95 case 0x83: // Alpha 32-bit
96 case 0x84: // Alpha 64-bit
97 case 0x66: // MPS R4000 Windows
99 case 0x4c: // 80386 Windows
100 if (magic[1] == 0x01)
101 return COFF_FileType;
103 case 0x90: // PA-RISC Windows
104 case 0x68: // mc68K Windows
105 if (magic[1] == 0x02)
106 return COFF_FileType;
112 return Unknown_FileType;
116 Path::isArchive() const {
118 return hasMagicNumber("!<arch>\012");
123 Path::isDynamicLibrary() const {
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;
140 Path::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())
148 FullPath.eraseSuffix();
149 FullPath.appendSuffix("a");
150 if (FullPath.isArchive())
156 std::string Path::GetDLLSuffix() {
157 return LTDL_SHLIB_EXT;
161 Path::isBitcodeFile() const {
162 std::string actualMagic;
163 if (!getMagicNumber(actualMagic, 4))
165 return actualMagic == "BC\xC0\xDE";
168 bool Path::hasMagicNumber(const std::string &Magic) const {
169 std::string actualMagic;
170 if (getMagicNumber(actualMagic, Magic.size()))
171 return Magic == actualMagic;
176 Path::getSuffix() const {
177 return path.substr(path.rfind('.') + 1);
180 static void getPathList(const char*path, std::vector<Path>& Paths) {
181 const char* at = path;
182 const char* delim = strchr(at, PathSeparator);
185 std::string tmp(at, size_t(delim-at));
186 if (tmpPath.set(tmp))
187 if (tmpPath.canRead())
188 Paths.push_back(tmpPath);
190 delim = strchr(at, PathSeparator);
194 if (tmpPath.set(std::string(at)))
195 if (tmpPath.canRead())
196 Paths.push_back(tmpPath);
199 // Include the truly platform-specific parts of this class.
200 #if defined(LLVM_ON_UNIX)
201 #include "Unix/Path.inc"
203 #if defined(LLVM_ON_WIN32)
204 #include "Win32/Path.inc"