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/Basic/CharInfo.h" 15 #include "clang/Frontend/Utils.h" 16 #include "clang/Lex/Preprocessor.h" 17 #include "clang/Serialization/ASTReader.h" 18 #include "llvm/ADT/StringMap.h" 19 #include "llvm/ADT/iterator_range.h" 20 #include "llvm/Support/FileSystem.h" 21 #include "llvm/Support/Path.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 using namespace clang; 25 26 namespace { 27 /// Private implementations for ModuleDependencyCollector 28 class ModuleDependencyListener : public ASTReaderListener { 29 ModuleDependencyCollector &Collector; 30 public: 31 ModuleDependencyListener(ModuleDependencyCollector &Collector) 32 : Collector(Collector) {} 33 bool needsInputFileVisitation() override { return true; } 34 bool needsSystemInputFileVisitation() override { return true; } 35 bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden, 36 bool IsExplicitModule) override { 37 Collector.addFile(Filename); 38 return true; 39 } 40 }; 41 42 struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks { 43 ModuleDependencyCollector &Collector; 44 ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector) 45 : Collector(Collector) {} 46 47 void moduleMapAddHeader(StringRef HeaderPath) override { 48 if (llvm::sys::path::is_absolute(HeaderPath)) 49 Collector.addFile(HeaderPath); 50 } 51 }; 52 53 } 54 55 // TODO: move this to Support/Path.h and check for HAVE_REALPATH? 56 static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) { 57 #ifdef LLVM_ON_UNIX 58 char CanonicalPath[PATH_MAX]; 59 60 // TODO: emit a warning in case this fails...? 61 if (!realpath(SrcPath.str().c_str(), CanonicalPath)) 62 return false; 63 64 SmallString<256> RPath(CanonicalPath); 65 RealPath.swap(RPath); 66 return true; 67 #else 68 // FIXME: Add support for systems without realpath. 69 return false; 70 #endif 71 } 72 73 void ModuleDependencyCollector::attachToASTReader(ASTReader &R) { 74 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this)); 75 } 76 77 void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) { 78 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( 79 llvm::make_unique<ModuleDependencyMMCallbacks>(*this)); 80 } 81 82 static bool isCaseSensitivePath(StringRef Path) { 83 SmallString<256> TmpDest = Path, UpperDest, RealDest; 84 // Remove component traversals, links, etc. 85 if (!real_path(Path, TmpDest)) 86 return true; // Current default value in vfs.yaml 87 Path = TmpDest; 88 89 // Change path to all upper case and ask for its real path, if the latter 90 // exists and is equal to Path, it's not case sensitive. Default to case 91 // sensitive in the absense of realpath, since this is what the VFSWriter 92 // already expects when sensitivity isn't setup. 93 for (auto &C : Path) 94 UpperDest.push_back(toUppercase(C)); 95 if (real_path(UpperDest, RealDest) && Path.equals(RealDest)) 96 return false; 97 return true; 98 } 99 100 void ModuleDependencyCollector::writeFileMap() { 101 if (Seen.empty()) 102 return; 103 104 StringRef VFSDir = getDest(); 105 106 // Default to use relative overlay directories in the VFS yaml file. This 107 // allows crash reproducer scripts to work across machines. 108 VFSWriter.setOverlayDir(VFSDir); 109 110 // Explicitly set case sensitivity for the YAML writer. For that, find out 111 // the sensitivity at the path where the headers all collected to. 112 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir)); 113 114 // Do not rely on real path names when executing the crash reproducer scripts 115 // since we only want to actually use the files we have on the VFS cache. 116 VFSWriter.setUseExternalNames(false); 117 118 std::error_code EC; 119 SmallString<256> YAMLPath = VFSDir; 120 llvm::sys::path::append(YAMLPath, "vfs.yaml"); 121 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text); 122 if (EC) { 123 HasErrors = true; 124 return; 125 } 126 VFSWriter.write(OS); 127 } 128 129 bool ModuleDependencyCollector::getRealPath(StringRef SrcPath, 130 SmallVectorImpl<char> &Result) { 131 using namespace llvm::sys; 132 SmallString<256> RealPath; 133 StringRef FileName = path::filename(SrcPath); 134 std::string Dir = path::parent_path(SrcPath).str(); 135 auto DirWithSymLink = SymLinkMap.find(Dir); 136 137 // Use real_path to fix any symbolic link component present in a path. 138 // Computing the real path is expensive, cache the search through the 139 // parent path directory. 140 if (DirWithSymLink == SymLinkMap.end()) { 141 if (!real_path(Dir, RealPath)) 142 return false; 143 SymLinkMap[Dir] = RealPath.str(); 144 } else { 145 RealPath = DirWithSymLink->second; 146 } 147 148 path::append(RealPath, FileName); 149 Result.swap(RealPath); 150 return true; 151 } 152 153 std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) { 154 using namespace llvm::sys; 155 156 // We need an absolute src path to append to the root. 157 SmallString<256> AbsoluteSrc = Src; 158 fs::make_absolute(AbsoluteSrc); 159 // Canonicalize src to a native path to avoid mixed separator styles. 160 path::native(AbsoluteSrc); 161 // Remove redundant leading "./" pieces and consecutive separators. 162 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc); 163 164 // Canonicalize the source path by removing "..", "." components. 165 SmallString<256> CanonicalPath = AbsoluteSrc; 166 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true); 167 168 // If a ".." component is present after a symlink component, remove_dots may 169 // lead to the wrong real destination path. Let the source be canonicalized 170 // like that but make sure we always use the real path for the destination. 171 SmallString<256> RealPath; 172 if (!getRealPath(AbsoluteSrc, RealPath)) 173 RealPath = CanonicalPath; 174 SmallString<256> Dest = getDest(); 175 path::append(Dest, path::relative_path(RealPath)); 176 177 // Copy the file into place. 178 if (std::error_code EC = fs::create_directories(path::parent_path(Dest), 179 /*IgnoreExisting=*/true)) 180 return EC; 181 if (std::error_code EC = fs::copy_file(RealPath, Dest)) 182 return EC; 183 184 // Always map a canonical src path to its real path into the YAML, by doing 185 // this we map different virtual src paths to the same entry in the VFS 186 // overlay, which is a way to emulate symlink inside the VFS; this is also 187 // needed for correctness, not doing that can lead to module redifinition 188 // errors. 189 addFileMapping(CanonicalPath, Dest); 190 return std::error_code(); 191 } 192 193 void ModuleDependencyCollector::addFile(StringRef Filename) { 194 if (insertSeen(Filename)) 195 if (copyToRoot(Filename)) 196 HasErrors = true; 197 } 198