blob: d61a015f68c8b191870db6410b4ec4369895a8a0 [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;
Jeff Cohenab68df02004-12-15 04:08:15 +000029#ifdef LLVM_ETCDIR
Reid Spencerc29befb2004-12-15 01:50:13 +000030 if (result.setDirectory(LLVM_ETCDIR))
31 return result;
Jeff Cohenab68df02004-12-15 04:08:15 +000032#endif
Reid Spencerc29befb2004-12-15 01:50:13 +000033 return GetLLVMDefaultConfigDir();
34}
35
Reid Spencerf37ce992004-11-14 22:05:32 +000036LLVMFileType
Reid Spencera2a62212004-11-14 23:26:18 +000037sys::IdentifyFileType(const char*magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000038 assert(magic && "Invalid magic number string");
39 assert(length >=4 && "Invalid magic number length");
40 switch (magic[0]) {
41 case 'l':
42 if (magic[1] == 'l' && magic[2] == 'v') {
43 if (magic[3] == 'c')
44 return CompressedBytecodeFileType;
45 else if (magic[3] == 'm')
46 return BytecodeFileType;
47 }
48 break;
49
50 case '!':
51 if (length >= 8) {
52 if (memcmp(magic,"!<arch>\n",8) == 0)
53 return ArchiveFileType;
54 }
55 break;
56
57 default:
58 break;
59 }
60 return UnknownFileType;
61}
62
Reid Spencerccb23a12004-12-13 03:00:39 +000063bool
64Path::isArchive() const {
65 if (readable())
66 return hasMagicNumber("!<arch>\012");
67 return false;
68}
69
70bool
71Path::isDynamicLibrary() const {
72 if (readable())
73 return hasMagicNumber("\177ELF");
74 return false;
75}
76
77Path
78Path::FindLibrary(std::string& name) {
79 std::vector<sys::Path> LibPaths;
80 GetSystemLibraryPaths(LibPaths);
81 for (unsigned i = 0; i < LibPaths.size(); ++i) {
82 sys::Path FullPath(LibPaths[i]);
83 FullPath.appendFile("lib" + name + LTDL_SHLIB_EXT);
84 if (FullPath.isDynamicLibrary())
85 return FullPath;
86 FullPath.elideSuffix();
87 FullPath.appendSuffix("a");
88 if (FullPath.isArchive())
89 return FullPath;
90 }
91 return sys::Path();
92}
93
Reid Spencer79fc9242004-12-13 18:41:28 +000094std::string
95Path::GetDLLSuffix() {
96 return LTDL_SHLIB_EXT;
97}
98
Reid Spencerb89a2232004-08-25 06:20:07 +000099}
100
101// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000102
103#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000104#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000105#endif
106#if defined(LLVM_ON_WIN32)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000107#include "Win32/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000108#endif
Reid Spencerb89a2232004-08-25 06:20:07 +0000109
Reid Spencer36853b92004-08-29 19:24:53 +0000110// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab