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