blob: efadfa9adf2208f75bcc866036c7f64eba91e4b9 [file] [log] [blame]
Reid Spencer814ba572004-08-25 06:20:07 +00001//===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer814ba572004-08-25 06:20:07 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman10468d82005-04-21 22:55:34 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer814ba572004-08-25 06:20:07 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer814ba572004-08-25 06:20:07 +00008//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system Path concept.
11//
12//===----------------------------------------------------------------------===//
Reid Spencer6df221d2004-08-29 05:24:01 +000013
Reid Spencer814ba572004-08-25 06:20:07 +000014#include "llvm/System/Path.h"
Reid Spencere05ab4a12004-12-13 18:41:28 +000015#include "llvm/Config/config.h"
Alkis Evlogimenos55117fa2004-11-14 22:37:42 +000016#include <cassert>
Chris Lattner9d9cd7932006-07-07 18:11:32 +000017#include <ostream>
18using namespace llvm;
Reid Spencer6df221d2004-08-29 05:24:01 +000019using namespace sys;
Reid Spencer814ba572004-08-25 06:20:07 +000020
21//===----------------------------------------------------------------------===//
22//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukman10468d82005-04-21 22:55:34 +000023//=== independent code.
Reid Spencer814ba572004-08-25 06:20:07 +000024//===----------------------------------------------------------------------===//
25
Chris Lattner9d9cd7932006-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 Spencerf66d9322004-12-15 01:50:13 +000031Path
32Path::GetLLVMConfigDir() {
33 Path result;
Jeff Cohene4083412004-12-15 04:08:15 +000034#ifdef LLVM_ETCDIR
Reid Spencerc9c04732005-07-07 23:21:43 +000035 if (result.set(LLVM_ETCDIR))
Reid Spencerf66d9322004-12-15 01:50:13 +000036 return result;
Jeff Cohene4083412004-12-15 04:08:15 +000037#endif
Reid Spencerf66d9322004-12-15 01:50:13 +000038 return GetLLVMDefaultConfigDir();
39}
40
Misha Brukman10468d82005-04-21 22:55:34 +000041LLVMFileType
Reid Spencer68816232004-11-14 23:26:18 +000042sys::IdentifyFileType(const char*magic, unsigned length) {
Reid Spencer0d0d07e2004-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 Spencer66b81822004-12-13 03:00:39 +000068bool
69Path::isArchive() const {
Reid Spencer5b891e92005-07-07 18:21:42 +000070 if (canRead())
Reid Spencer66b81822004-12-13 03:00:39 +000071 return hasMagicNumber("!<arch>\012");
72 return false;
73}
74
75bool
76Path::isDynamicLibrary() const {
Reid Spencer5b891e92005-07-07 18:21:42 +000077 if (canRead())
Reid Spencer66b81822004-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 Spencerc9c04732005-07-07 23:21:43 +000088 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencer66b81822004-12-13 03:00:39 +000089 if (FullPath.isDynamicLibrary())
90 return FullPath;
Reid Spencerc9c04732005-07-07 23:21:43 +000091 FullPath.eraseSuffix();
Reid Spencer66b81822004-12-13 03:00:39 +000092 FullPath.appendSuffix("a");
93 if (FullPath.isArchive())
94 return FullPath;
95 }
96 return sys::Path();
97}
98
Chris Lattner9d9cd7932006-07-07 18:11:32 +000099std::string Path::GetDLLSuffix() {
Reid Spencere05ab4a12004-12-13 18:41:28 +0000100 return LTDL_SHLIB_EXT;
101}
102
Reid Spencer814ba572004-08-25 06:20:07 +0000103// Include the truly platform-specific parts of this class.
Reid Spencer328ee532004-12-24 06:29:17 +0000104#if defined(LLVM_ON_UNIX)
Reid Spencerc892a0d2005-01-09 23:29:00 +0000105#include "Unix/Path.inc"
Reid Spencer328ee532004-12-24 06:29:17 +0000106#endif
107#if defined(LLVM_ON_WIN32)
Reid Spencerc892a0d2005-01-09 23:29:00 +0000108#include "Win32/Path.inc"
Reid Spencer328ee532004-12-24 06:29:17 +0000109#endif