1 //===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Collect the dependencies of a set of modules. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Frontend/Utils.h" 15 #include "clang/Lex/Preprocessor.h" 16 #include "clang/Serialization/ASTReader.h" 17 #include "llvm/ADT/StringMap.h" 18 #include "llvm/ADT/iterator_range.h" 19 #include "llvm/Support/FileSystem.h" 20 #include "llvm/Support/Path.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 using namespace clang; 24 25 namespace { 26 /// Private implementations for ModuleDependencyCollector 27 class ModuleDependencyListener : public ASTReaderListener { 28 ModuleDependencyCollector &Collector; 29 public: 30 ModuleDependencyListener(ModuleDependencyCollector &Collector) 31 : Collector(Collector) {} 32 bool needsInputFileVisitation() override { return true; } 33 bool needsSystemInputFileVisitation() override { return true; } 34 bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden, 35 bool IsExplicitModule) override { 36 Collector.addFile(Filename); 37 return true; 38 } 39 }; 40 41 struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks { 42 ModuleDependencyCollector &Collector; 43 ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector) 44 : Collector(Collector) {} 45 46 void moduleMapAddHeader(const FileEntry &File) override { 47 StringRef HeaderPath = File.getName(); 48 if (llvm::sys::path::is_absolute(HeaderPath)) 49 Collector.addFile(HeaderPath); 50 } 51 }; 52 53 } 54 55 void ModuleDependencyCollector::attachToASTReader(ASTReader &R) { 56 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this)); 57 } 58 59 void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) { 60 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( 61 llvm::make_unique<ModuleDependencyMMCallbacks>(*this)); 62 } 63 64 void ModuleDependencyCollector::writeFileMap() { 65 if (Seen.empty()) 66 return; 67 68 SmallString<256> Dest = getDest(); 69 llvm::sys::path::append(Dest, "vfs.yaml"); 70 71 // Default to use relative overlay directories in the VFS yaml file. This 72 // allows crash reproducer scripts to work across machines. 73 VFSWriter.setOverlayDir(getDest()); 74 75 std::error_code EC; 76 llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text); 77 if (EC) { 78 HasErrors = true; 79 return; 80 } 81 VFSWriter.write(OS); 82 } 83 84 // TODO: move this to Support/Path.h and check for HAVE_REALPATH? 85 static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) { 86 #ifdef LLVM_ON_UNIX 87 char CanonicalPath[PATH_MAX]; 88 89 // TODO: emit a warning in case this fails...? 90 if (!realpath(SrcPath.str().c_str(), CanonicalPath)) 91 return false; 92 93 SmallString<256> RPath(CanonicalPath); 94 RealPath.swap(RPath); 95 return true; 96 #else 97 // FIXME: Add support for systems without realpath. 98 return false; 99 #endif 100 } 101 102 bool ModuleDependencyCollector::getRealPath(StringRef SrcPath, 103 SmallVectorImpl<char> &Result) { 104 using namespace llvm::sys; 105 SmallString<256> RealPath; 106 StringRef FileName = path::filename(SrcPath); 107 std::string Dir = path::parent_path(SrcPath).str(); 108 auto DirWithSymLink = SymLinkMap.find(Dir); 109 110 // Use real_path to fix any symbolic link component present in a path. 111 // Computing the real path is expensive, cache the search through the 112 // parent path directory. 113 if (DirWithSymLink == SymLinkMap.end()) { 114 if (!real_path(Dir, RealPath)) 115 return false; 116 SymLinkMap[Dir] = RealPath.str(); 117 } else { 118 RealPath = DirWithSymLink->second; 119 } 120 121 path::append(RealPath, FileName); 122 Result.swap(RealPath); 123 return true; 124 } 125 126 std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) { 127 using namespace llvm::sys; 128 129 // We need an absolute path to append to the root. 130 SmallString<256> AbsoluteSrc = Src; 131 fs::make_absolute(AbsoluteSrc); 132 // Canonicalize to a native path to avoid mixed separator styles. 133 path::native(AbsoluteSrc); 134 // Remove redundant leading "./" pieces and consecutive separators. 135 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc); 136 137 // Canonicalize path by removing "..", "." components. 138 SmallString<256> CanonicalPath = AbsoluteSrc; 139 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true); 140 141 // If a ".." component is present after a symlink component, remove_dots may 142 // lead to the wrong real destination path. Let the source be canonicalized 143 // like that but make sure the destination uses the real path. 144 bool HasDotDotInPath = 145 std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0; 146 SmallString<256> RealPath; 147 bool HasRemovedSymlinkComponent = HasDotDotInPath && 148 getRealPath(AbsoluteSrc, RealPath) && 149 !StringRef(CanonicalPath).equals(RealPath); 150 151 // Build the destination path. 152 SmallString<256> Dest = getDest(); 153 path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath 154 : CanonicalPath)); 155 156 // Copy the file into place. 157 if (std::error_code EC = fs::create_directories(path::parent_path(Dest), 158 /*IgnoreExisting=*/true)) 159 return EC; 160 if (std::error_code EC = fs::copy_file( 161 HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest)) 162 return EC; 163 164 // Use the canonical path under the root for the file mapping. Also create 165 // an additional entry for the real path. 166 addFileMapping(CanonicalPath, Dest); 167 if (HasRemovedSymlinkComponent) 168 addFileMapping(RealPath, Dest); 169 170 return std::error_code(); 171 } 172 173 void ModuleDependencyCollector::addFile(StringRef Filename) { 174 if (insertSeen(Filename)) 175 if (copyToRoot(Filename)) 176 HasErrors = true; 177 } 178