blob: d0ee1a3ae89acfd957b79b870ec68ac1a2f14156 [file] [log] [blame]
Reid Spencerb89a2232004-08-25 06:20:07 +00001//===-- 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//===----------------------------------------------------------------------===//
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>
Reid Spencerb89a2232004-08-25 06:20:07 +000017
18namespace 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
23//=== independent code.
24//===----------------------------------------------------------------------===//
25
Reid Spencerf37ce992004-11-14 22:05:32 +000026LLVMFileType
Reid Spencera2a62212004-11-14 23:26:18 +000027sys::IdentifyFileType(const char*magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000028 assert(magic && "Invalid magic number string");
29 assert(length >=4 && "Invalid magic number length");
30 switch (magic[0]) {
31 case 'l':
32 if (magic[1] == 'l' && magic[2] == 'v') {
33 if (magic[3] == 'c')
34 return CompressedBytecodeFileType;
35 else if (magic[3] == 'm')
36 return BytecodeFileType;
37 }
38 break;
39
40 case '!':
41 if (length >= 8) {
42 if (memcmp(magic,"!<arch>\n",8) == 0)
43 return ArchiveFileType;
44 }
45 break;
46
47 default:
48 break;
49 }
50 return UnknownFileType;
51}
52
Reid Spencerccb23a12004-12-13 03:00:39 +000053bool
54Path::isArchive() const {
55 if (readable())
56 return hasMagicNumber("!<arch>\012");
57 return false;
58}
59
60bool
61Path::isDynamicLibrary() const {
62 if (readable())
63 return hasMagicNumber("\177ELF");
64 return false;
65}
66
67Path
68Path::FindLibrary(std::string& name) {
69 std::vector<sys::Path> LibPaths;
70 GetSystemLibraryPaths(LibPaths);
71 for (unsigned i = 0; i < LibPaths.size(); ++i) {
72 sys::Path FullPath(LibPaths[i]);
73 FullPath.appendFile("lib" + name + LTDL_SHLIB_EXT);
74 if (FullPath.isDynamicLibrary())
75 return FullPath;
76 FullPath.elideSuffix();
77 FullPath.appendSuffix("a");
78 if (FullPath.isArchive())
79 return FullPath;
80 }
81 return sys::Path();
82}
83
Reid Spencer79fc9242004-12-13 18:41:28 +000084std::string
85Path::GetDLLSuffix() {
86 return LTDL_SHLIB_EXT;
87}
88
Reid Spencerb89a2232004-08-25 06:20:07 +000089}
90
91// Include the truly platform-specific parts of this class.
92#include "platform/Path.cpp"
Reid Spencerb89a2232004-08-25 06:20:07 +000093
Reid Spencer36853b92004-08-29 19:24:53 +000094// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab