blob: b873b9ab4a0714f1b724e14b1c2a0f1a13c6d3fd [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"
Alkis Evlogimenos98bc8ed2004-11-14 22:37:42 +000018#include <cassert>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000019#include <cstring>
Chris Lattnerc67dc452006-07-07 18:11:32 +000020#include <ostream>
21using namespace llvm;
Reid Spencer8e665952004-08-29 05:24:01 +000022using namespace sys;
Eric Christopher539d8d82011-04-03 22:53:19 +000023namespace {
24using support::ulittle32_t;
25}
Reid Spencerb89a2232004-08-25 06:20:07 +000026
27//===----------------------------------------------------------------------===//
28//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000029//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000030//===----------------------------------------------------------------------===//
31
Bill Wendling40db5d42008-05-21 21:20:07 +000032bool Path::operator==(const Path &that) const {
33 return path == that.path;
34}
35
Bill Wendling40db5d42008-05-21 21:20:07 +000036bool Path::operator<(const Path& that) const {
37 return path < that.path;
38}
39
Reid Spencerccb23a12004-12-13 03:00:39 +000040bool
41Path::isArchive() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +000042 fs::file_magic type;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000043 if (fs::identify_magic(str(), type))
44 return false;
Michael J. Spencer5b082302011-12-13 23:17:12 +000045 return type == fs::file_magic::archive;
Reid Spencerccb23a12004-12-13 03:00:39 +000046}
47
48bool
49Path::isDynamicLibrary() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +000050 fs::file_magic type;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000051 if (fs::identify_magic(str(), type))
52 return false;
53 switch (type) {
54 default: return false;
Michael J. Spencer5b082302011-12-13 23:17:12 +000055 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;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000060 }
Reid Spencerccb23a12004-12-13 03:00:39 +000061}
62
Michael J. Spencer8a26f812010-09-15 22:45:45 +000063bool
64Path::isObjectFile() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +000065 fs::file_magic type;
66 if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000067 return false;
68 return true;
Michael J. Spencer8a26f812010-09-15 22:45:45 +000069}
70
Dan Gohman552a3c22010-12-01 02:46:41 +000071void
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +000072Path::appendSuffix(StringRef suffix) {
73 if (!suffix.empty()) {
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +000074 path.append(".");
75 path.append(suffix);
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +000076 }
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +000077}
78
79bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +000080Path::isBitcodeFile() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +000081 fs::file_magic type;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000082 if (fs::identify_magic(str(), type))
Chris Lattnerf283a5e2007-05-06 05:32:21 +000083 return false;
Michael J. Spencer5b082302011-12-13 23:17:12 +000084 return type == fs::file_magic::bitcode;
Chris Lattnerf283a5e2007-05-06 05:32:21 +000085}
86
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +000087bool Path::hasMagicNumber(StringRef Magic) const {
Chris Lattnerf283a5e2007-05-06 05:32:21 +000088 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +000089 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +000090 return Magic == actualMagic;
91 return false;
92}
93
Reid Spencerb89a2232004-08-25 06:20:07 +000094// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +000095#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +000096#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +000097#endif
98#if defined(LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000099#include "Windows/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000100#endif