blob: 1a6d030f89679af4a313c72f4211b6a186257ca0 [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"
Alkis Evlogimenos98bc8ed2004-11-14 22:37:42 +000015#include <cassert>
Reid Spencerb89a2232004-08-25 06:20:07 +000016
17namespace llvm {
Reid Spencer8e665952004-08-29 05:24:01 +000018using namespace sys;
Reid Spencerb89a2232004-08-25 06:20:07 +000019
20//===----------------------------------------------------------------------===//
21//=== WARNING: Implementation here must contain only TRULY operating system
22//=== independent code.
23//===----------------------------------------------------------------------===//
24
Reid Spencerf37ce992004-11-14 22:05:32 +000025LLVMFileType
Reid Spencera2a62212004-11-14 23:26:18 +000026sys::IdentifyFileType(const char*magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000027 assert(magic && "Invalid magic number string");
28 assert(length >=4 && "Invalid magic number length");
29 switch (magic[0]) {
30 case 'l':
31 if (magic[1] == 'l' && magic[2] == 'v') {
32 if (magic[3] == 'c')
33 return CompressedBytecodeFileType;
34 else if (magic[3] == 'm')
35 return BytecodeFileType;
36 }
37 break;
38
39 case '!':
40 if (length >= 8) {
41 if (memcmp(magic,"!<arch>\n",8) == 0)
42 return ArchiveFileType;
43 }
44 break;
45
46 default:
47 break;
48 }
49 return UnknownFileType;
50}
51
Reid Spencerccb23a12004-12-13 03:00:39 +000052bool
53Path::isArchive() const {
54 if (readable())
55 return hasMagicNumber("!<arch>\012");
56 return false;
57}
58
59bool
60Path::isDynamicLibrary() const {
61 if (readable())
62 return hasMagicNumber("\177ELF");
63 return false;
64}
65
66Path
67Path::FindLibrary(std::string& name) {
68 std::vector<sys::Path> LibPaths;
69 GetSystemLibraryPaths(LibPaths);
70 for (unsigned i = 0; i < LibPaths.size(); ++i) {
71 sys::Path FullPath(LibPaths[i]);
72 FullPath.appendFile("lib" + name + LTDL_SHLIB_EXT);
73 if (FullPath.isDynamicLibrary())
74 return FullPath;
75 FullPath.elideSuffix();
76 FullPath.appendSuffix("a");
77 if (FullPath.isArchive())
78 return FullPath;
79 }
80 return sys::Path();
81}
82
Reid Spencerb89a2232004-08-25 06:20:07 +000083}
84
85// Include the truly platform-specific parts of this class.
86#include "platform/Path.cpp"
Reid Spencerb89a2232004-08-25 06:20:07 +000087
Reid Spencer36853b92004-08-29 19:24:53 +000088// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab