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 // Do not rely on real path names when executing the crash reproducer scripts 116 // since we only want to actually use the files we have on the VFS cache. 117 VFSWriter.setUseExternalNames(false); 118 119 std::error_code EC; 120 SmallString<256> YAMLPath = VFSDir; 121 llvm::sys::path::append(YAMLPath, "vfs.yaml"); 122 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text); 123 if (EC) { 124 HasErrors = true; 125 return; 126 } 127 VFSWriter.write(OS); 128 } 129 130 bool ModuleDependencyCollector::getRealPath(StringRef SrcPath, 131 SmallVectorImpl<char> &Result) { 132 using namespace llvm::sys; 133 SmallString<256> RealPath; 134 StringRef FileName = path::filename(SrcPath); 135 std::string Dir = path::parent_path(SrcPath).str(); 136 auto DirWithSymLink = SymLinkMap.find(Dir); 137 138 // Use real_path to fix any symbolic link component present in a path. 139 // Computing the real path is expensive, cache the search through the 140 // parent path directory. 141 if (DirWithSymLink == SymLinkMap.end()) { 142 if (!real_path(Dir, RealPath)) 143 return false; 144 SymLinkMap[Dir] = RealPath.str(); 145 } else { 146 RealPath = DirWithSymLink->second; 147 } 148 149 path::append(RealPath, FileName); 150 Result.swap(RealPath); 151 return true; 152 } 153 154 std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) { 155 using namespace llvm::sys; 156 157 // We need an absolute path to append to the root. 158 SmallString<256> AbsoluteSrc = Src; 159 fs::make_absolute(AbsoluteSrc); 160 // Canonicalize to a native path to avoid mixed separator styles. 161 path::native(AbsoluteSrc); 162 // Remove redundant leading "./" pieces and consecutive separators. 163 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc); 164 165 // Canonicalize path by removing "..", "." components. 166 SmallString<256> CanonicalPath = AbsoluteSrc; 167 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true); 168 169 // If a ".." component is present after a symlink component, remove_dots may 170 // lead to the wrong real destination path. Let the source be canonicalized 171 // like that but make sure the destination uses the real path. 172 bool HasDotDotInPath = 173 std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0; 174 SmallString<256> RealPath; 175 bool HasRemovedSymlinkComponent = HasDotDotInPath && 176 getRealPath(AbsoluteSrc, RealPath) && 177 !StringRef(CanonicalPath).equals(RealPath); 178 179 // Build the destination path. 180 SmallString<256> Dest = getDest(); 181 path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath 182 : CanonicalPath)); 183 184 // Copy the file into place. 185 if (std::error_code EC = fs::create_directories(path::parent_path(Dest), 186 /*IgnoreExisting=*/true)) 187 return EC; 188 if (std::error_code EC = fs::copy_file( 189 HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest)) 190 return EC; 191 192 // Use the canonical path under the root for the file mapping. Also create 193 // an additional entry for the real path. 194 addFileMapping(CanonicalPath, Dest); 195 if (HasRemovedSymlinkComponent) 196 addFileMapping(RealPath, Dest); 197 198 return std::error_code(); 199 } 200 201 void ModuleDependencyCollector::addFile(StringRef Filename) { 202 if (insertSeen(Filename)) 203 if (copyToRoot(Filename)) 204 HasErrors = true; 205 } 206