Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1 | //===- MCLDDirectory.cpp --------------------------------------------------===// |
| 2 | // |
| 3 | // The MCLinker Project |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 9 | #include "mcld/MC/MCLDDirectory.h" |
| 10 | #include "mcld/Support/FileSystem.h" |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 11 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 12 | namespace mcld { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 13 | |
Stephen Hines | f7ac0f1 | 2013-05-03 19:09:24 -0700 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 15 | // MCLDDirectory |
Stephen Hines | f7ac0f1 | 2013-05-03 19:09:24 -0700 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 17 | MCLDDirectory::MCLDDirectory() : Directory(), m_Name(), m_bInSysroot(false) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 18 | } |
| 19 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 20 | MCLDDirectory::MCLDDirectory(const char* pName) : Directory(), m_Name(pName) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 21 | Directory::m_Path.assign(pName); |
| 22 | |
| 23 | if (!Directory::m_Path.empty()) |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 24 | m_bInSysroot = (Directory::m_Path.native()[0] == '='); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 25 | |
| 26 | Directory::m_Path.m_append_separator_if_needed(); |
| 27 | if (m_bInSysroot) |
| 28 | Directory::m_Path.native().erase(Directory::m_Path.native().begin()); |
| 29 | else |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 30 | sys::fs::detail::open_dir(*this); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 33 | MCLDDirectory::MCLDDirectory(const std::string& pName) |
| 34 | : Directory(), m_Name(pName) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 35 | Directory::m_Path.assign(pName); |
| 36 | |
| 37 | if (!Directory::m_Path.empty()) |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 38 | m_bInSysroot = (Directory::m_Path.native()[0] == '='); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 39 | |
| 40 | Directory::m_Path.m_append_separator_if_needed(); |
| 41 | if (m_bInSysroot) |
| 42 | Directory::m_Path.native().erase(Directory::m_Path.native().begin()); |
| 43 | else |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 44 | sys::fs::detail::open_dir(*this); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | MCLDDirectory::MCLDDirectory(llvm::StringRef pName) |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 48 | : Directory(), m_Name(pName.data(), pName.size()) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 49 | Directory::m_Path.assign(pName.str()); |
| 50 | |
| 51 | if (!Directory::m_Path.empty()) |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 52 | m_bInSysroot = (Directory::m_Path.native()[0] == '='); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 53 | |
| 54 | Directory::m_Path.m_append_separator_if_needed(); |
| 55 | if (m_bInSysroot) |
| 56 | Directory::m_Path.native().erase(Directory::m_Path.native().begin()); |
| 57 | else |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 58 | sys::fs::detail::open_dir(*this); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 61 | MCLDDirectory& MCLDDirectory::assign(llvm::StringRef pName) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 62 | m_Name.assign(pName.data(), pName.size()); |
| 63 | Directory::m_Path.assign(pName.str()); |
| 64 | |
| 65 | if (!Directory::m_Path.empty()) |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 66 | m_bInSysroot = (Directory::m_Path.native()[0] == '='); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 67 | |
| 68 | Directory::m_Path.m_append_separator_if_needed(); |
| 69 | if (m_bInSysroot) |
| 70 | Directory::m_Path.native().erase(Directory::m_Path.native().begin()); |
| 71 | else |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 72 | sys::fs::detail::open_dir(*this); |
| 73 | Directory::m_FileStatus = sys::fs::FileStatus(); |
| 74 | Directory::m_SymLinkStatus = sys::fs::FileStatus(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 75 | Directory::m_Cache.clear(); |
| 76 | Directory::m_Handler = 0; |
| 77 | return (*this); |
| 78 | } |
| 79 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 80 | MCLDDirectory::~MCLDDirectory() { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 83 | bool MCLDDirectory::isInSysroot() const { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 84 | return m_bInSysroot; |
| 85 | } |
| 86 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 87 | void MCLDDirectory::setSysroot(const sys::fs::Path& pSysroot) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 88 | if (m_bInSysroot) { |
| 89 | std::string old_path = Directory::m_Path.native(); |
| 90 | Directory::m_Path.native() = pSysroot.native(); |
| 91 | Directory::m_Path.m_append_separator_if_needed(); |
| 92 | Directory::m_Path.native() += old_path; |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 93 | sys::fs::detail::canonicalize(Directory::m_Path.native()); |
| 94 | sys::fs::detail::open_dir(*this); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 98 | } // namespace mcld |