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(const FileEntry &File) override { 48 StringRef HeaderPath = File.getName(); 49 if (llvm::sys::path::is_absolute(HeaderPath)) 50 Collector.addFile(HeaderPath); 51 } 52 }; 53 54 } 55 56 // TODO: move this to Support/Path.h and check for HAVE_REALPATH? 57 static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) { 58 #ifdef LLVM_ON_UNIX 59 char CanonicalPath[PATH_MAX]; 60 61 // TODO: emit a warning in case this fails...? 62 if (!realpath(SrcPath.str().c_str(), CanonicalPath)) 63 return false; 64 65 SmallString<256> RPath(CanonicalPath); 66 RealPath.swap(RPath); 67 return true; 68 #else 69 // FIXME: Add support for systems without realpath. 70 return false; 71 #endif 72 } 73 74 void ModuleDependencyCollector::attachToASTReader(ASTReader &R) { 75 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this)); 76 } 77 78 void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) { 79 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( 80 llvm::make_unique<ModuleDependencyMMCallbacks>(*this)); 81 } 82 83 static bool isCaseSensitivePath(StringRef Path) { 84 SmallString<256> TmpDest = Path, UpperDest, RealDest; 85 // Remove component traversals, links, etc. 86 if (!real_path(Path, TmpDest)) 87 return true; // Current default value in vfs.yaml 88 Path = TmpDest; 89 90 // Change path to all upper case and ask for its real path, if the latter 91 // exists and is equal to Path, it's not case sensitive. Default to case 92 // sensitive in the absense of realpath, since this is what the VFSWriter 93 // already expects when sensitivity isn't setup. 94 for (auto &C : Path) 95 UpperDest.push_back(toUppercase(C)); 96 if (real_path(UpperDest, RealDest) && Path.equals(RealDest)) 97 return false; 98 return true; 99 } 100 101 void ModuleDependencyCollector::writeFileMap() { 102 if (Seen.empty()) 103 return; 104 105 StringRef VFSDir = getDest(); 106 107 // Default to use relative overlay directories in the VFS yaml file. This 108 // allows crash reproducer scripts to work across machines. 109 VFSWriter.setOverlayDir(VFSDir); 110 111 // Explicitly set case sensitivity for the YAML writer. For that, find out 112 // the sensitivity at the path where the headers all collected to. 113 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir)); 114 115 std::error_code EC; 116 SmallString<256> YAMLPath = VFSDir; 117 llvm::sys::path::append(YAMLPath, "vfs.yaml"); 118 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text); 119 if (EC) { 120 HasErrors = true; 121 return; 122 } 123 VFSWriter.write(OS); 124 } 125 126 bool ModuleDependencyCollector::getRealPath(StringRef SrcPath, 127 SmallVectorImpl<char> &Result) { 128 using namespace llvm::sys; 129 SmallString<256> RealPath; 130 StringRef FileName = path::filename(SrcPath); 131 std::string Dir = path::parent_path(SrcPath).str(); 132 auto DirWithSymLink = SymLinkMap.find(Dir); 133 134 // Use real_path to fix any symbolic link component present in a path. 135 // Computing the real path is expensive, cache the search through the 136 // parent path directory. 137 if (DirWithSymLink == SymLinkMap.end()) { 138 if (!real_path(Dir, RealPath)) 139 return false; 140 SymLinkMap[Dir] = RealPath.str(); 141 } else { 142 RealPath = DirWithSymLink->second; 143 } 144 145 path::append(RealPath, FileName); 146 Result.swap(RealPath); 147 return true; 148 } 149 150 std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) { 151 using namespace llvm::sys; 152 153 // We need an absolute path to append to the root. 154 SmallString<256> AbsoluteSrc = Src; 155 fs::make_absolute(AbsoluteSrc); 156 // Canonicalize to a native path to avoid mixed separator styles. 157 path::native(AbsoluteSrc); 158 // Remove redundant leading "./" pieces and consecutive separators. 159 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc); 160 161 // Canonicalize path by removing "..", "." components. 162 SmallString<256> CanonicalPath = AbsoluteSrc; 163 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true); 164 165 // If a ".." component is present after a symlink component, remove_dots may 166 // lead to the wrong real destination path. Let the source be canonicalized 167 // like that but make sure the destination uses the real path. 168 bool HasDotDotInPath = 169 std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0; 170 SmallString<256> RealPath; 171 bool HasRemovedSymlinkComponent = HasDotDotInPath && 172 getRealPath(AbsoluteSrc, RealPath) && 173 !StringRef(CanonicalPath).equals(RealPath); 174 175 // Build the destination path. 176 SmallString<256> Dest = getDest(); 177 path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath 178 : CanonicalPath)); 179 180 // Copy the file into place. 181 if (std::error_code EC = fs::create_directories(path::parent_path(Dest), 182 /*IgnoreExisting=*/true)) 183 return EC; 184 if (std::error_code EC = fs::copy_file( 185 HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest)) 186 return EC; 187 188 // Use the canonical path under the root for the file mapping. Also create 189 // an additional entry for the real path. 190 addFileMapping(CanonicalPath, Dest); 191 if (HasRemovedSymlinkComponent) 192 addFileMapping(RealPath, Dest); 193 194 return std::error_code(); 195 } 196 197 void ModuleDependencyCollector::addFile(StringRef Filename) { 198 if (insertSeen(Filename)) 199 if (copyToRoot(Filename)) 200 HasErrors = true; 201 } 202