1 //===--- DependencyFile.cpp - Generate dependency file --------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This code generates dependency files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/FileManager.h" 14 #include "clang/Basic/SourceManager.h" 15 #include "clang/Frontend/DependencyOutputOptions.h" 16 #include "clang/Frontend/FrontendDiagnostic.h" 17 #include "clang/Frontend/Utils.h" 18 #include "clang/Lex/DirectoryLookup.h" 19 #include "clang/Lex/ModuleMap.h" 20 #include "clang/Lex/PPCallbacks.h" 21 #include "clang/Lex/Preprocessor.h" 22 #include "clang/Serialization/ASTReader.h" 23 #include "llvm/ADT/StringSet.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/Path.h" 26 #include "llvm/Support/raw_ostream.h" 27 #include <optional> 28 29 using namespace clang; 30 31 namespace { 32 struct DepCollectorPPCallbacks : public PPCallbacks { 33 DependencyCollector &DepCollector; 34 Preprocessor &PP; 35 DepCollectorPPCallbacks(DependencyCollector &L, Preprocessor &PP) 36 : DepCollector(L), PP(PP) {} 37 38 void LexedFileChanged(FileID FID, LexedFileChangeReason Reason, 39 SrcMgr::CharacteristicKind FileType, FileID PrevFID, 40 SourceLocation Loc) override { 41 if (Reason != PPCallbacks::LexedFileChangeReason::EnterFile) 42 return; 43 44 // Dependency generation really does want to go all the way to the 45 // file entry for a source location to find out what is depended on. 46 // We do not want #line markers to affect dependency generation! 47 if (Optional<StringRef> Filename = 48 PP.getSourceManager().getNonBuiltinFilenameForID(FID)) 49 DepCollector.maybeAddDependency( 50 llvm::sys::path::remove_leading_dotslash(*Filename), 51 /*FromModule*/ false, isSystem(FileType), /*IsModuleFile*/ false, 52 /*IsMissing*/ false); 53 } 54 55 void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok, 56 SrcMgr::CharacteristicKind FileType) override { 57 StringRef Filename = 58 llvm::sys::path::remove_leading_dotslash(SkippedFile.getName()); 59 DepCollector.maybeAddDependency(Filename, /*FromModule=*/false, 60 /*IsSystem=*/isSystem(FileType), 61 /*IsModuleFile=*/false, 62 /*IsMissing=*/false); 63 } 64 65 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 66 StringRef FileName, bool IsAngled, 67 CharSourceRange FilenameRange, 68 std::optional<FileEntryRef> File, 69 StringRef SearchPath, StringRef RelativePath, 70 const Module *Imported, 71 SrcMgr::CharacteristicKind FileType) override { 72 if (!File) 73 DepCollector.maybeAddDependency(FileName, /*FromModule*/false, 74 /*IsSystem*/false, /*IsModuleFile*/false, 75 /*IsMissing*/true); 76 // Files that actually exist are handled by FileChanged. 77 } 78 79 void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled, 80 std::optional<FileEntryRef> File, 81 SrcMgr::CharacteristicKind FileType) override { 82 if (!File) 83 return; 84 StringRef Filename = 85 llvm::sys::path::remove_leading_dotslash(File->getName()); 86 DepCollector.maybeAddDependency(Filename, /*FromModule=*/false, 87 /*IsSystem=*/isSystem(FileType), 88 /*IsModuleFile=*/false, 89 /*IsMissing=*/false); 90 } 91 92 void EndOfMainFile() override { 93 DepCollector.finishedMainFile(PP.getDiagnostics()); 94 } 95 }; 96 97 struct DepCollectorMMCallbacks : public ModuleMapCallbacks { 98 DependencyCollector &DepCollector; 99 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {} 100 101 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry, 102 bool IsSystem) override { 103 StringRef Filename = Entry.getName(); 104 DepCollector.maybeAddDependency(Filename, /*FromModule*/false, 105 /*IsSystem*/IsSystem, 106 /*IsModuleFile*/false, 107 /*IsMissing*/false); 108 } 109 }; 110 111 struct DepCollectorASTListener : public ASTReaderListener { 112 DependencyCollector &DepCollector; 113 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { } 114 bool needsInputFileVisitation() override { return true; } 115 bool needsSystemInputFileVisitation() override { 116 return DepCollector.needSystemDependencies(); 117 } 118 void visitModuleFile(StringRef Filename, 119 serialization::ModuleKind Kind) override { 120 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, 121 /*IsSystem*/false, /*IsModuleFile*/true, 122 /*IsMissing*/false); 123 } 124 bool visitInputFile(StringRef Filename, bool IsSystem, 125 bool IsOverridden, bool IsExplicitModule) override { 126 if (IsOverridden || IsExplicitModule) 127 return true; 128 129 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem, 130 /*IsModuleFile*/false, /*IsMissing*/false); 131 return true; 132 } 133 }; 134 } // end anonymous namespace 135 136 void DependencyCollector::maybeAddDependency(StringRef Filename, 137 bool FromModule, bool IsSystem, 138 bool IsModuleFile, 139 bool IsMissing) { 140 if (sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing)) 141 addDependency(Filename); 142 } 143 144 bool DependencyCollector::addDependency(StringRef Filename) { 145 StringRef SearchPath; 146 #ifdef _WIN32 147 // Make the search insensitive to case and separators. 148 llvm::SmallString<256> TmpPath = Filename; 149 llvm::sys::path::native(TmpPath); 150 std::transform(TmpPath.begin(), TmpPath.end(), TmpPath.begin(), ::tolower); 151 SearchPath = TmpPath.str(); 152 #else 153 SearchPath = Filename; 154 #endif 155 156 if (Seen.insert(SearchPath).second) { 157 Dependencies.push_back(std::string(Filename)); 158 return true; 159 } 160 return false; 161 } 162 163 static bool isSpecialFilename(StringRef Filename) { 164 return Filename == "<built-in>"; 165 } 166 167 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, 168 bool IsSystem, bool IsModuleFile, 169 bool IsMissing) { 170 return !isSpecialFilename(Filename) && 171 (needSystemDependencies() || !IsSystem); 172 } 173 174 DependencyCollector::~DependencyCollector() { } 175 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { 176 PP.addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>(*this, PP)); 177 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( 178 std::make_unique<DepCollectorMMCallbacks>(*this)); 179 } 180 void DependencyCollector::attachToASTReader(ASTReader &R) { 181 R.addListener(std::make_unique<DepCollectorASTListener>(*this)); 182 } 183 184 DependencyFileGenerator::DependencyFileGenerator( 185 const DependencyOutputOptions &Opts) 186 : OutputFile(Opts.OutputFile), Targets(Opts.Targets), 187 IncludeSystemHeaders(Opts.IncludeSystemHeaders), 188 PhonyTarget(Opts.UsePhonyTargets), 189 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(false), 190 IncludeModuleFiles(Opts.IncludeModuleFiles), 191 OutputFormat(Opts.OutputFormat), InputFileIndex(0) { 192 for (const auto &ExtraDep : Opts.ExtraDeps) { 193 if (addDependency(ExtraDep.first)) 194 ++InputFileIndex; 195 } 196 } 197 198 void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) { 199 // Disable the "file not found" diagnostic if the -MG option was given. 200 if (AddMissingHeaderDeps) 201 PP.SetSuppressIncludeNotFoundError(true); 202 203 DependencyCollector::attachToPreprocessor(PP); 204 } 205 206 bool DependencyFileGenerator::sawDependency(StringRef Filename, bool FromModule, 207 bool IsSystem, bool IsModuleFile, 208 bool IsMissing) { 209 if (IsMissing) { 210 // Handle the case of missing file from an inclusion directive. 211 if (AddMissingHeaderDeps) 212 return true; 213 SeenMissingHeader = true; 214 return false; 215 } 216 if (IsModuleFile && !IncludeModuleFiles) 217 return false; 218 219 if (isSpecialFilename(Filename)) 220 return false; 221 222 if (IncludeSystemHeaders) 223 return true; 224 225 return !IsSystem; 226 } 227 228 void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) { 229 outputDependencyFile(Diags); 230 } 231 232 /// Print the filename, with escaping or quoting that accommodates the three 233 /// most likely tools that use dependency files: GNU Make, BSD Make, and 234 /// NMake/Jom. 235 /// 236 /// BSD Make is the simplest case: It does no escaping at all. This means 237 /// characters that are normally delimiters, i.e. space and # (the comment 238 /// character) simply aren't supported in filenames. 239 /// 240 /// GNU Make does allow space and # in filenames, but to avoid being treated 241 /// as a delimiter or comment, these must be escaped with a backslash. Because 242 /// backslash is itself the escape character, if a backslash appears in a 243 /// filename, it should be escaped as well. (As a special case, $ is escaped 244 /// as $$, which is the normal Make way to handle the $ character.) 245 /// For compatibility with BSD Make and historical practice, if GNU Make 246 /// un-escapes characters in a filename but doesn't find a match, it will 247 /// retry with the unmodified original string. 248 /// 249 /// GCC tries to accommodate both Make formats by escaping any space or # 250 /// characters in the original filename, but not escaping backslashes. The 251 /// apparent intent is so that filenames with backslashes will be handled 252 /// correctly by BSD Make, and by GNU Make in its fallback mode of using the 253 /// unmodified original string; filenames with # or space characters aren't 254 /// supported by BSD Make at all, but will be handled correctly by GNU Make 255 /// due to the escaping. 256 /// 257 /// A corner case that GCC gets only partly right is when the original filename 258 /// has a backslash immediately followed by space or #. GNU Make would expect 259 /// this backslash to be escaped; however GCC escapes the original backslash 260 /// only when followed by space, not #. It will therefore take a dependency 261 /// from a directive such as 262 /// #include "a\ b\#c.h" 263 /// and emit it as 264 /// a\\\ b\\#c.h 265 /// which GNU Make will interpret as 266 /// a\ b\ 267 /// followed by a comment. Failing to find this file, it will fall back to the 268 /// original string, which probably doesn't exist either; in any case it won't 269 /// find 270 /// a\ b\#c.h 271 /// which is the actual filename specified by the include directive. 272 /// 273 /// Clang does what GCC does, rather than what GNU Make expects. 274 /// 275 /// NMake/Jom has a different set of scary characters, but wraps filespecs in 276 /// double-quotes to avoid misinterpreting them; see 277 /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info, 278 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx 279 /// for Windows file-naming info. 280 static void PrintFilename(raw_ostream &OS, StringRef Filename, 281 DependencyOutputFormat OutputFormat) { 282 // Convert filename to platform native path 283 llvm::SmallString<256> NativePath; 284 llvm::sys::path::native(Filename.str(), NativePath); 285 286 if (OutputFormat == DependencyOutputFormat::NMake) { 287 // Add quotes if needed. These are the characters listed as "special" to 288 // NMake, that are legal in a Windows filespec, and that could cause 289 // misinterpretation of the dependency string. 290 if (NativePath.find_first_of(" #${}^!") != StringRef::npos) 291 OS << '\"' << NativePath << '\"'; 292 else 293 OS << NativePath; 294 return; 295 } 296 assert(OutputFormat == DependencyOutputFormat::Make); 297 for (unsigned i = 0, e = NativePath.size(); i != e; ++i) { 298 if (NativePath[i] == '#') // Handle '#' the broken gcc way. 299 OS << '\\'; 300 else if (NativePath[i] == ' ') { // Handle space correctly. 301 OS << '\\'; 302 unsigned j = i; 303 while (j > 0 && NativePath[--j] == '\\') 304 OS << '\\'; 305 } else if (NativePath[i] == '$') // $ is escaped by $$. 306 OS << '$'; 307 OS << NativePath[i]; 308 } 309 } 310 311 void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) { 312 if (SeenMissingHeader) { 313 llvm::sys::fs::remove(OutputFile); 314 return; 315 } 316 317 std::error_code EC; 318 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF); 319 if (EC) { 320 Diags.Report(diag::err_fe_error_opening) << OutputFile << EC.message(); 321 return; 322 } 323 324 outputDependencyFile(OS); 325 } 326 327 void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) { 328 // Write out the dependency targets, trying to avoid overly long 329 // lines when possible. We try our best to emit exactly the same 330 // dependency file as GCC>=10, assuming the included files are the 331 // same. 332 const unsigned MaxColumns = 75; 333 unsigned Columns = 0; 334 335 for (StringRef Target : Targets) { 336 unsigned N = Target.size(); 337 if (Columns == 0) { 338 Columns += N; 339 } else if (Columns + N + 2 > MaxColumns) { 340 Columns = N + 2; 341 OS << " \\\n "; 342 } else { 343 Columns += N + 1; 344 OS << ' '; 345 } 346 // Targets already quoted as needed. 347 OS << Target; 348 } 349 350 OS << ':'; 351 Columns += 1; 352 353 // Now add each dependency in the order it was seen, but avoiding 354 // duplicates. 355 ArrayRef<std::string> Files = getDependencies(); 356 for (StringRef File : Files) { 357 if (File == "<stdin>") 358 continue; 359 // Start a new line if this would exceed the column limit. Make 360 // sure to leave space for a trailing " \" in case we need to 361 // break the line on the next iteration. 362 unsigned N = File.size(); 363 if (Columns + (N + 1) + 2 > MaxColumns) { 364 OS << " \\\n "; 365 Columns = 2; 366 } 367 OS << ' '; 368 PrintFilename(OS, File, OutputFormat); 369 Columns += N + 1; 370 } 371 OS << '\n'; 372 373 // Create phony targets if requested. 374 if (PhonyTarget && !Files.empty()) { 375 unsigned Index = 0; 376 for (auto I = Files.begin(), E = Files.end(); I != E; ++I) { 377 if (Index++ == InputFileIndex) 378 continue; 379 PrintFilename(OS, *I, OutputFormat); 380 OS << ":\n"; 381 } 382 } 383 } 384