1 1.1 joerg //===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===// 2 1.1 joerg // 3 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 1.1 joerg // See https://llvm.org/LICENSE.txt for license information. 5 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 1.1 joerg // 7 1.1 joerg //===----------------------------------------------------------------------===// 8 1.1 joerg // 9 1.1 joerg // Collect the dependencies of a set of modules. 10 1.1 joerg // 11 1.1 joerg //===----------------------------------------------------------------------===// 12 1.1 joerg 13 1.1 joerg #include "clang/Basic/CharInfo.h" 14 1.1 joerg #include "clang/Frontend/Utils.h" 15 1.1 joerg #include "clang/Lex/Preprocessor.h" 16 1.1 joerg #include "clang/Serialization/ASTReader.h" 17 1.1 joerg #include "llvm/ADT/iterator_range.h" 18 1.1 joerg #include "llvm/Config/llvm-config.h" 19 1.1 joerg #include "llvm/Support/FileSystem.h" 20 1.1 joerg #include "llvm/Support/Path.h" 21 1.1 joerg #include "llvm/Support/raw_ostream.h" 22 1.1 joerg 23 1.1 joerg using namespace clang; 24 1.1 joerg 25 1.1 joerg namespace { 26 1.1 joerg /// Private implementations for ModuleDependencyCollector 27 1.1 joerg class ModuleDependencyListener : public ASTReaderListener { 28 1.1 joerg ModuleDependencyCollector &Collector; 29 1.1 joerg public: 30 1.1 joerg ModuleDependencyListener(ModuleDependencyCollector &Collector) 31 1.1 joerg : Collector(Collector) {} 32 1.1 joerg bool needsInputFileVisitation() override { return true; } 33 1.1 joerg bool needsSystemInputFileVisitation() override { return true; } 34 1.1 joerg bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden, 35 1.1 joerg bool IsExplicitModule) override { 36 1.1 joerg Collector.addFile(Filename); 37 1.1 joerg return true; 38 1.1 joerg } 39 1.1 joerg }; 40 1.1 joerg 41 1.1 joerg struct ModuleDependencyPPCallbacks : public PPCallbacks { 42 1.1 joerg ModuleDependencyCollector &Collector; 43 1.1 joerg SourceManager &SM; 44 1.1 joerg ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector, 45 1.1 joerg SourceManager &SM) 46 1.1 joerg : Collector(Collector), SM(SM) {} 47 1.1 joerg 48 1.1 joerg void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 49 1.1 joerg StringRef FileName, bool IsAngled, 50 1.1 joerg CharSourceRange FilenameRange, const FileEntry *File, 51 1.1 joerg StringRef SearchPath, StringRef RelativePath, 52 1.1 joerg const Module *Imported, 53 1.1 joerg SrcMgr::CharacteristicKind FileType) override { 54 1.1 joerg if (!File) 55 1.1 joerg return; 56 1.1 joerg Collector.addFile(File->getName()); 57 1.1 joerg } 58 1.1 joerg }; 59 1.1 joerg 60 1.1 joerg struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks { 61 1.1 joerg ModuleDependencyCollector &Collector; 62 1.1 joerg ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector) 63 1.1 joerg : Collector(Collector) {} 64 1.1 joerg 65 1.1 joerg void moduleMapAddHeader(StringRef HeaderPath) override { 66 1.1 joerg if (llvm::sys::path::is_absolute(HeaderPath)) 67 1.1 joerg Collector.addFile(HeaderPath); 68 1.1 joerg } 69 1.1 joerg void moduleMapAddUmbrellaHeader(FileManager *FileMgr, 70 1.1 joerg const FileEntry *Header) override { 71 1.1 joerg StringRef HeaderFilename = Header->getName(); 72 1.1 joerg moduleMapAddHeader(HeaderFilename); 73 1.1 joerg // The FileManager can find and cache the symbolic link for a framework 74 1.1 joerg // header before its real path, this means a module can have some of its 75 1.1 joerg // headers to use other paths. Although this is usually not a problem, it's 76 1.1 joerg // inconsistent, and not collecting the original path header leads to 77 1.1 joerg // umbrella clashes while rebuilding modules in the crash reproducer. For 78 1.1 joerg // example: 79 1.1 joerg // ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h 80 1.1 joerg // instead of: 81 1.1 joerg // ImageIO.framework/ImageIO.h 82 1.1 joerg // 83 1.1 joerg // FIXME: this shouldn't be necessary once we have FileName instances 84 1.1 joerg // around instead of FileEntry ones. For now, make sure we collect all 85 1.1 joerg // that we need for the reproducer to work correctly. 86 1.1 joerg StringRef UmbreallDirFromHeader = 87 1.1 joerg llvm::sys::path::parent_path(HeaderFilename); 88 1.1 joerg StringRef UmbrellaDir = Header->getDir()->getName(); 89 1.1 joerg if (!UmbrellaDir.equals(UmbreallDirFromHeader)) { 90 1.1 joerg SmallString<128> AltHeaderFilename; 91 1.1 joerg llvm::sys::path::append(AltHeaderFilename, UmbrellaDir, 92 1.1 joerg llvm::sys::path::filename(HeaderFilename)); 93 1.1 joerg if (FileMgr->getFile(AltHeaderFilename)) 94 1.1 joerg moduleMapAddHeader(AltHeaderFilename); 95 1.1 joerg } 96 1.1 joerg } 97 1.1 joerg }; 98 1.1 joerg 99 1.1 joerg } 100 1.1 joerg 101 1.1 joerg void ModuleDependencyCollector::attachToASTReader(ASTReader &R) { 102 1.1 joerg R.addListener(std::make_unique<ModuleDependencyListener>(*this)); 103 1.1 joerg } 104 1.1 joerg 105 1.1 joerg void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) { 106 1.1 joerg PP.addPPCallbacks(std::make_unique<ModuleDependencyPPCallbacks>( 107 1.1 joerg *this, PP.getSourceManager())); 108 1.1 joerg PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( 109 1.1 joerg std::make_unique<ModuleDependencyMMCallbacks>(*this)); 110 1.1 joerg } 111 1.1 joerg 112 1.1 joerg static bool isCaseSensitivePath(StringRef Path) { 113 1.1 joerg SmallString<256> TmpDest = Path, UpperDest, RealDest; 114 1.1 joerg // Remove component traversals, links, etc. 115 1.1 joerg if (llvm::sys::fs::real_path(Path, TmpDest)) 116 1.1 joerg return true; // Current default value in vfs.yaml 117 1.1 joerg Path = TmpDest; 118 1.1 joerg 119 1.1 joerg // Change path to all upper case and ask for its real path, if the latter 120 1.1 joerg // exists and is equal to Path, it's not case sensitive. Default to case 121 1.1 joerg // sensitive in the absence of realpath, since this is what the VFSWriter 122 1.1 joerg // already expects when sensitivity isn't setup. 123 1.1 joerg for (auto &C : Path) 124 1.1 joerg UpperDest.push_back(toUppercase(C)); 125 1.1 joerg if (!llvm::sys::fs::real_path(UpperDest, RealDest) && Path.equals(RealDest)) 126 1.1 joerg return false; 127 1.1 joerg return true; 128 1.1 joerg } 129 1.1 joerg 130 1.1 joerg void ModuleDependencyCollector::writeFileMap() { 131 1.1 joerg if (Seen.empty()) 132 1.1 joerg return; 133 1.1 joerg 134 1.1 joerg StringRef VFSDir = getDest(); 135 1.1 joerg 136 1.1 joerg // Default to use relative overlay directories in the VFS yaml file. This 137 1.1 joerg // allows crash reproducer scripts to work across machines. 138 1.1 joerg VFSWriter.setOverlayDir(VFSDir); 139 1.1 joerg 140 1.1 joerg // Explicitly set case sensitivity for the YAML writer. For that, find out 141 1.1 joerg // the sensitivity at the path where the headers all collected to. 142 1.1 joerg VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir)); 143 1.1 joerg 144 1.1 joerg // Do not rely on real path names when executing the crash reproducer scripts 145 1.1 joerg // since we only want to actually use the files we have on the VFS cache. 146 1.1 joerg VFSWriter.setUseExternalNames(false); 147 1.1 joerg 148 1.1 joerg std::error_code EC; 149 1.1 joerg SmallString<256> YAMLPath = VFSDir; 150 1.1 joerg llvm::sys::path::append(YAMLPath, "vfs.yaml"); 151 1.1.1.2 joerg llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::OF_TextWithCRLF); 152 1.1 joerg if (EC) { 153 1.1 joerg HasErrors = true; 154 1.1 joerg return; 155 1.1 joerg } 156 1.1 joerg VFSWriter.write(OS); 157 1.1 joerg } 158 1.1 joerg 159 1.1 joerg std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src, 160 1.1 joerg StringRef Dst) { 161 1.1 joerg using namespace llvm::sys; 162 1.1.1.2 joerg llvm::FileCollector::PathCanonicalizer::PathStorage Paths = 163 1.1.1.2 joerg Canonicalizer.canonicalize(Src); 164 1.1 joerg 165 1.1 joerg SmallString<256> CacheDst = getDest(); 166 1.1 joerg 167 1.1 joerg if (Dst.empty()) { 168 1.1 joerg // The common case is to map the virtual path to the same path inside the 169 1.1 joerg // cache. 170 1.1.1.2 joerg path::append(CacheDst, path::relative_path(Paths.CopyFrom)); 171 1.1 joerg } else { 172 1.1 joerg // When collecting entries from input vfsoverlays, copy the external 173 1.1 joerg // contents into the cache but still map from the source. 174 1.1 joerg if (!fs::exists(Dst)) 175 1.1 joerg return std::error_code(); 176 1.1 joerg path::append(CacheDst, Dst); 177 1.1.1.2 joerg Paths.CopyFrom = Dst; 178 1.1 joerg } 179 1.1 joerg 180 1.1 joerg // Copy the file into place. 181 1.1 joerg if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst), 182 1.1 joerg /*IgnoreExisting=*/true)) 183 1.1 joerg return EC; 184 1.1.1.2 joerg if (std::error_code EC = fs::copy_file(Paths.CopyFrom, CacheDst)) 185 1.1 joerg return EC; 186 1.1 joerg 187 1.1 joerg // Always map a canonical src path to its real path into the YAML, by doing 188 1.1 joerg // this we map different virtual src paths to the same entry in the VFS 189 1.1 joerg // overlay, which is a way to emulate symlink inside the VFS; this is also 190 1.1 joerg // needed for correctness, not doing that can lead to module redefinition 191 1.1 joerg // errors. 192 1.1.1.2 joerg addFileMapping(Paths.VirtualPath, CacheDst); 193 1.1 joerg return std::error_code(); 194 1.1 joerg } 195 1.1 joerg 196 1.1 joerg void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) { 197 1.1 joerg if (insertSeen(Filename)) 198 1.1 joerg if (copyToRoot(Filename, FileDst)) 199 1.1 joerg HasErrors = true; 200 1.1 joerg } 201