blob: 1246038f84fe6fa9a0ccfe87cdb1305b884ae66a [file] [log] [blame]
Reid Spencerb89a2232004-08-25 06:20:07 +00001//===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencerb89a2232004-08-25 06:20:07 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukmanf976c852005-04-21 22:55:34 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencerb89a2232004-08-25 06:20:07 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencerb89a2232004-08-25 06:20:07 +00008//===----------------------------------------------------------------------===//
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>
Chris Lattnerc67dc452006-07-07 18:11:32 +000017#include <ostream>
18using namespace 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
Misha Brukmanf976c852005-04-21 22:55:34 +000023//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000024//===----------------------------------------------------------------------===//
25
Chris Lattnerc67dc452006-07-07 18:11:32 +000026std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
27 strm << aPath.toString();
28 return strm;
29}
30
Reid Spencerc29befb2004-12-15 01:50:13 +000031Path
32Path::GetLLVMConfigDir() {
33 Path result;
Jeff Cohenab68df02004-12-15 04:08:15 +000034#ifdef LLVM_ETCDIR
Reid Spencerdd04df02005-07-07 23:21:43 +000035 if (result.set(LLVM_ETCDIR))
Reid Spencerc29befb2004-12-15 01:50:13 +000036 return result;
Jeff Cohenab68df02004-12-15 04:08:15 +000037#endif
Reid Spencerc29befb2004-12-15 01:50:13 +000038 return GetLLVMDefaultConfigDir();
39}
40
Misha Brukmanf976c852005-04-21 22:55:34 +000041LLVMFileType
Reid Spencera2a62212004-11-14 23:26:18 +000042sys::IdentifyFileType(const char*magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000043 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
Reid Spencerccb23a12004-12-13 03:00:39 +000068bool
69Path::isArchive() const {
Reid Spencerc7f08322005-07-07 18:21:42 +000070 if (canRead())
Reid Spencerccb23a12004-12-13 03:00:39 +000071 return hasMagicNumber("!<arch>\012");
72 return false;
73}
74
75bool
76Path::isDynamicLibrary() const {
Reid Spencerc7f08322005-07-07 18:21:42 +000077 if (canRead())
Reid Spencerccb23a12004-12-13 03:00:39 +000078 return hasMagicNumber("\177ELF");
79 return false;
80}
81
82Path
83Path::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]);
Reid Spencerdd04df02005-07-07 23:21:43 +000088 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +000089 if (FullPath.isDynamicLibrary())
90 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +000091 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +000092 FullPath.appendSuffix("a");
93 if (FullPath.isArchive())
94 return FullPath;
95 }
96 return sys::Path();
97}
98
Chris Lattnerc67dc452006-07-07 18:11:32 +000099std::string Path::GetDLLSuffix() {
Reid Spencer79fc9242004-12-13 18:41:28 +0000100 return LTDL_SHLIB_EXT;
101}
102
Reid Spencerb89a2232004-08-25 06:20:07 +0000103// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000104#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000105#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000106#endif
107#if defined(LLVM_ON_WIN32)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000108#include "Win32/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000109#endif
Reid Spencer23dd3322006-07-26 16:55:39 +0000110
111DEFINING_FILE_FOR(SystemPath)