b873b9ab4a0714f1b724e14b1c2a0f1a13c6d3fd
[oota-llvm.git] / lib / Support / Path.cpp
1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file implements the operating system Path concept.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Path.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/FileSystem.h"
18 #include <cassert>
19 #include <cstring>
20 #include <ostream>
21 using namespace llvm;
22 using namespace sys;
23 namespace {
24 using support::ulittle32_t;
25 }
26
27 //===----------------------------------------------------------------------===//
28 //=== WARNING: Implementation here must contain only TRULY operating system
29 //===          independent code.
30 //===----------------------------------------------------------------------===//
31
32 bool Path::operator==(const Path &that) const {
33   return path == that.path;
34 }
35
36 bool Path::operator<(const Path& that) const {
37   return path < that.path;
38 }
39
40 bool
41 Path::isArchive() const {
42   fs::file_magic type;
43   if (fs::identify_magic(str(), type))
44     return false;
45   return type == fs::file_magic::archive;
46 }
47
48 bool
49 Path::isDynamicLibrary() const {
50   fs::file_magic type;
51   if (fs::identify_magic(str(), type))
52     return false;
53   switch (type) {
54     default: return false;
55     case fs::file_magic::macho_fixed_virtual_memory_shared_lib:
56     case fs::file_magic::macho_dynamically_linked_shared_lib:
57     case fs::file_magic::macho_dynamically_linked_shared_lib_stub:
58     case fs::file_magic::elf_shared_object:
59     case fs::file_magic::pecoff_executable:  return true;
60   }
61 }
62
63 bool
64 Path::isObjectFile() const {
65   fs::file_magic type;
66   if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
67     return false;
68   return true;
69 }
70
71 void
72 Path::appendSuffix(StringRef suffix) {
73   if (!suffix.empty()) {
74     path.append(".");
75     path.append(suffix);
76   }
77 }
78
79 bool
80 Path::isBitcodeFile() const {
81   fs::file_magic type;
82   if (fs::identify_magic(str(), type))
83     return false;
84   return type == fs::file_magic::bitcode;
85 }
86
87 bool Path::hasMagicNumber(StringRef Magic) const {
88   std::string actualMagic;
89   if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
90     return Magic == actualMagic;
91   return false;
92 }
93
94 // Include the truly platform-specific parts of this class.
95 #if defined(LLVM_ON_UNIX)
96 #include "Unix/Path.inc"
97 #endif
98 #if defined(LLVM_ON_WIN32)
99 #include "Windows/Path.inc"
100 #endif