elimiante some syscalls
[oota-llvm.git] / lib / System / Path.cpp
1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
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>
17 #include <ostream>
18 using namespace llvm;
19 using namespace sys;
20
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //===          independent code.
24 //===----------------------------------------------------------------------===//
25
26 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
27   strm << aPath.toString();
28   return strm;
29 }
30
31 Path
32 Path::GetLLVMConfigDir() {
33   Path result;
34 #ifdef LLVM_ETCDIR
35   if (result.set(LLVM_ETCDIR))
36     return result;
37 #endif
38   return GetLLVMDefaultConfigDir();
39 }
40
41 LLVMFileType
42 sys::IdentifyFileType(const char*magic, unsigned length) {
43   assert(magic && "Invalid magic number string");
44   assert(length >=4 && "Invalid magic number length");
45   switch (magic[0]) {
46     case 'l':
47       if (magic[1] == 'l' && magic[2] == 'v') {
48         if (magic[3] == 'c')
49           return CompressedBytecodeFileType;
50         else if (magic[3] == 'm')
51           return BytecodeFileType;
52       }
53       break;
54
55     case '!':
56       if (length >= 8) {
57         if (memcmp(magic,"!<arch>\n",8) == 0)
58           return ArchiveFileType;
59       }
60       break;
61
62     default:
63       break;
64   }
65   return UnknownFileType;
66 }
67
68 bool
69 Path::isArchive() const {
70   if (canRead())
71     return hasMagicNumber("!<arch>\012");
72   return false;
73 }
74
75 bool
76 Path::isDynamicLibrary() const {
77   if (canRead())
78     return hasMagicNumber("\177ELF");
79   return false;
80 }
81
82 Path
83 Path::FindLibrary(std::string& name) {
84   std::vector<sys::Path> LibPaths;
85   GetSystemLibraryPaths(LibPaths);
86   for (unsigned i = 0; i < LibPaths.size(); ++i) {
87     sys::Path FullPath(LibPaths[i]);
88     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
89     if (FullPath.isDynamicLibrary())
90       return FullPath;
91     FullPath.eraseSuffix();
92     FullPath.appendSuffix("a");
93     if (FullPath.isArchive())
94       return FullPath;
95   }
96   return sys::Path();
97 }
98
99 std::string Path::GetDLLSuffix() {
100   return LTDL_SHLIB_EXT;
101 }
102
103 // Include the truly platform-specific parts of this class.
104 #if defined(LLVM_ON_UNIX)
105 #include "Unix/Path.inc"
106 #endif
107 #if defined(LLVM_ON_WIN32)
108 #include "Win32/Path.inc"
109 #endif
110
111 DEFINING_FILE_FOR(SystemPath)