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 //===----------------------------------------------------------------------===//
9 #include "mcld/MC/MCLDDirectory.h"
10 #include "mcld/Support/FileSystem.h"
11 
12 namespace mcld {
13 
14 //===----------------------------------------------------------------------===//
15 // MCLDDirectory
16 //===----------------------------------------------------------------------===//
MCLDDirectory()17 MCLDDirectory::MCLDDirectory() : Directory(), m_Name(), m_bInSysroot(false) {
18 }
19 
MCLDDirectory(const char * pName)20 MCLDDirectory::MCLDDirectory(const char* pName) : Directory(), m_Name(pName) {
21   Directory::m_Path.assign(pName);
22 
23   if (!Directory::m_Path.empty())
24     m_bInSysroot = (Directory::m_Path.native()[0] == '=');
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
30     sys::fs::detail::open_dir(*this);
31 }
32 
MCLDDirectory(const std::string & pName)33 MCLDDirectory::MCLDDirectory(const std::string& pName)
34     : Directory(), m_Name(pName) {
35   Directory::m_Path.assign(pName);
36 
37   if (!Directory::m_Path.empty())
38     m_bInSysroot = (Directory::m_Path.native()[0] == '=');
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
44     sys::fs::detail::open_dir(*this);
45 }
46 
MCLDDirectory(llvm::StringRef pName)47 MCLDDirectory::MCLDDirectory(llvm::StringRef pName)
48     : Directory(), m_Name(pName.data(), pName.size()) {
49   Directory::m_Path.assign(pName.str());
50 
51   if (!Directory::m_Path.empty())
52     m_bInSysroot = (Directory::m_Path.native()[0] == '=');
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
58     sys::fs::detail::open_dir(*this);
59 }
60 
assign(llvm::StringRef pName)61 MCLDDirectory& MCLDDirectory::assign(llvm::StringRef pName) {
62   m_Name.assign(pName.data(), pName.size());
63   Directory::m_Path.assign(pName.str());
64 
65   if (!Directory::m_Path.empty())
66     m_bInSysroot = (Directory::m_Path.native()[0] == '=');
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
72     sys::fs::detail::open_dir(*this);
73   Directory::m_FileStatus = sys::fs::FileStatus();
74   Directory::m_SymLinkStatus = sys::fs::FileStatus();
75   Directory::m_Cache.clear();
76   Directory::m_Handler = 0;
77   return (*this);
78 }
79 
~MCLDDirectory()80 MCLDDirectory::~MCLDDirectory() {
81 }
82 
isInSysroot() const83 bool MCLDDirectory::isInSysroot() const {
84   return m_bInSysroot;
85 }
86 
setSysroot(const sys::fs::Path & pSysroot)87 void MCLDDirectory::setSysroot(const sys::fs::Path& pSysroot) {
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;
93     sys::fs::detail::canonicalize(Directory::m_Path.native());
94     sys::fs::detail::open_dir(*this);
95   }
96 }
97 
98 }  // namespace mcld
99