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