blob: 1bae78137b3f3cc106dabf8368167087839dc7c8 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000014#include "llvm/Support/Path.h"
Reid Spencer79fc9242004-12-13 18:41:28 +000015#include "llvm/Config/config.h"
Eric Christopher539d8d82011-04-03 22:53:19 +000016#include "llvm/Support/Endian.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/Support/FileSystem.h"
Rafael Espindolaf3e397e2013-06-11 20:00:56 +000018#include "llvm/Support/PathV1.h"
Alkis Evlogimenos98bc8ed2004-11-14 22:37:42 +000019#include <cassert>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000020#include <cstring>
Chris Lattnerc67dc452006-07-07 18:11:32 +000021#include <ostream>
22using namespace llvm;
Reid Spencer8e665952004-08-29 05:24:01 +000023using namespace sys;
Eric Christopher539d8d82011-04-03 22:53:19 +000024namespace {
25using support::ulittle32_t;
26}
Reid Spencerb89a2232004-08-25 06:20:07 +000027
28//===----------------------------------------------------------------------===//
29//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000030//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000031//===----------------------------------------------------------------------===//
32
Bill Wendling40db5d42008-05-21 21:20:07 +000033bool Path::operator==(const Path &that) const {
34 return path == that.path;
35}
36
Bill Wendling40db5d42008-05-21 21:20:07 +000037bool Path::operator<(const Path& that) const {
38 return path < that.path;
39}
40
Reid Spencerccb23a12004-12-13 03:00:39 +000041bool
42Path::isArchive() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +000043 fs::file_magic type;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000044 if (fs::identify_magic(str(), type))
45 return false;
Michael J. Spencer5b082302011-12-13 23:17:12 +000046 return type == fs::file_magic::archive;
Reid Spencerccb23a12004-12-13 03:00:39 +000047}
48
49bool
50Path::isDynamicLibrary() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +000051 fs::file_magic type;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000052 if (fs::identify_magic(str(), type))
53 return false;
54 switch (type) {
55 default: return false;
Michael J. Spencer5b082302011-12-13 23:17:12 +000056 case fs::file_magic::macho_fixed_virtual_memory_shared_lib:
57 case fs::file_magic::macho_dynamically_linked_shared_lib:
58 case fs::file_magic::macho_dynamically_linked_shared_lib_stub:
59 case fs::file_magic::elf_shared_object:
60 case fs::file_magic::pecoff_executable: return true;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000061 }
Reid Spencerccb23a12004-12-13 03:00:39 +000062}
63
Michael J. Spencer8a26f812010-09-15 22:45:45 +000064bool
65Path::isObjectFile() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +000066 fs::file_magic type;
67 if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000068 return false;
69 return true;
Michael J. Spencer8a26f812010-09-15 22:45:45 +000070}
71
Dan Gohman552a3c22010-12-01 02:46:41 +000072void
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +000073Path::appendSuffix(StringRef suffix) {
74 if (!suffix.empty()) {
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +000075 path.append(".");
76 path.append(suffix);
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +000077 }
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +000078}
79
80bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +000081Path::isBitcodeFile() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +000082 fs::file_magic type;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000083 if (fs::identify_magic(str(), type))
Chris Lattnerf283a5e2007-05-06 05:32:21 +000084 return false;
Michael J. Spencer5b082302011-12-13 23:17:12 +000085 return type == fs::file_magic::bitcode;
Chris Lattnerf283a5e2007-05-06 05:32:21 +000086}
87
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +000088bool Path::hasMagicNumber(StringRef Magic) const {
Chris Lattnerf283a5e2007-05-06 05:32:21 +000089 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +000090 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +000091 return Magic == actualMagic;
92 return false;
93}
94
Reid Spencerb89a2232004-08-25 06:20:07 +000095// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +000096#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +000097#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +000098#endif
99#if defined(LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000100#include "Windows/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000101#endif