Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 1 | //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system Path concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 13 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Path.h" |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Endian.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | f3e397e | 2013-06-11 20:00:56 +0000 | [diff] [blame^] | 18 | #include "llvm/Support/PathV1.h" |
Alkis Evlogimenos | 98bc8ed | 2004-11-14 22:37:42 +0000 | [diff] [blame] | 19 | #include <cassert> |
Duncan Sands | f52e32a | 2008-01-09 19:42:09 +0000 | [diff] [blame] | 20 | #include <cstring> |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 21 | #include <ostream> |
| 22 | using namespace llvm; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 23 | using namespace sys; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | using support::ulittle32_t; |
| 26 | } |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 30 | //=== independent code. |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Bill Wendling | 40db5d4 | 2008-05-21 21:20:07 +0000 | [diff] [blame] | 33 | bool Path::operator==(const Path &that) const { |
| 34 | return path == that.path; |
| 35 | } |
| 36 | |
Bill Wendling | 40db5d4 | 2008-05-21 21:20:07 +0000 | [diff] [blame] | 37 | bool Path::operator<(const Path& that) const { |
| 38 | return path < that.path; |
| 39 | } |
| 40 | |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 41 | bool |
| 42 | Path::isArchive() const { |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 43 | fs::file_magic type; |
Michael J. Spencer | 28f0ed5 | 2011-01-15 20:39:36 +0000 | [diff] [blame] | 44 | if (fs::identify_magic(str(), type)) |
| 45 | return false; |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 46 | return type == fs::file_magic::archive; |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool |
| 50 | Path::isDynamicLibrary() const { |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 51 | fs::file_magic type; |
Michael J. Spencer | 28f0ed5 | 2011-01-15 20:39:36 +0000 | [diff] [blame] | 52 | if (fs::identify_magic(str(), type)) |
| 53 | return false; |
| 54 | switch (type) { |
| 55 | default: return false; |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 56 | 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. Spencer | 28f0ed5 | 2011-01-15 20:39:36 +0000 | [diff] [blame] | 61 | } |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Michael J. Spencer | 8a26f81 | 2010-09-15 22:45:45 +0000 | [diff] [blame] | 64 | bool |
| 65 | Path::isObjectFile() const { |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 66 | fs::file_magic type; |
| 67 | if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown) |
Michael J. Spencer | 28f0ed5 | 2011-01-15 20:39:36 +0000 | [diff] [blame] | 68 | return false; |
| 69 | return true; |
Michael J. Spencer | 8a26f81 | 2010-09-15 22:45:45 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Dan Gohman | 552a3c2 | 2010-12-01 02:46:41 +0000 | [diff] [blame] | 72 | void |
Mikhail Glushenkov | bd6e032 | 2010-11-02 22:18:37 +0000 | [diff] [blame] | 73 | Path::appendSuffix(StringRef suffix) { |
| 74 | if (!suffix.empty()) { |
Mikhail Glushenkov | bd6e032 | 2010-11-02 22:18:37 +0000 | [diff] [blame] | 75 | path.append("."); |
| 76 | path.append(suffix); |
Mikhail Glushenkov | bd6e032 | 2010-11-02 22:18:37 +0000 | [diff] [blame] | 77 | } |
Mikhail Glushenkov | bd6e032 | 2010-11-02 22:18:37 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | bool |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 81 | Path::isBitcodeFile() const { |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 82 | fs::file_magic type; |
Michael J. Spencer | 28f0ed5 | 2011-01-15 20:39:36 +0000 | [diff] [blame] | 83 | if (fs::identify_magic(str(), type)) |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 84 | return false; |
Michael J. Spencer | 5b08230 | 2011-12-13 23:17:12 +0000 | [diff] [blame] | 85 | return type == fs::file_magic::bitcode; |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Jeffrey Yasskin | 88cd358 | 2009-12-17 21:02:39 +0000 | [diff] [blame] | 88 | bool Path::hasMagicNumber(StringRef Magic) const { |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 89 | std::string actualMagic; |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 90 | if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size()))) |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 91 | return Magic == actualMagic; |
| 92 | return false; |
| 93 | } |
| 94 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 95 | // Include the truly platform-specific parts of this class. |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 96 | #if defined(LLVM_ON_UNIX) |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 97 | #include "Unix/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 98 | #endif |
| 99 | #if defined(LLVM_ON_WIN32) |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 100 | #include "Windows/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 101 | #endif |