blob: b472eaaae0542f86fa55b9c4431350355fd0e758 [file] [log] [blame]
Daniel Dunbarfa0caca2008-10-24 22:12:41 +00001//===--- DependencyFile.cpp - Generate dependency file --------------------===//
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// This code generates dependency files.
11//
12//===----------------------------------------------------------------------===//
13
Eli Friedman16b7b6f2009-05-19 04:14:29 +000014#include "clang/Frontend/Utils.h"
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000015#include "clang/Basic/FileManager.h"
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000016#include "clang/Basic/SourceManager.h"
17#include "clang/Frontend/DependencyOutputOptions.h"
18#include "clang/Frontend/FrontendDiagnostic.h"
19#include "clang/Lex/DirectoryLookup.h"
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +000020#include "clang/Lex/LexDiagnostic.h"
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000021#include "clang/Lex/PPCallbacks.h"
22#include "clang/Lex/Preprocessor.h"
Ben Langmuircb69b572014-03-07 06:40:32 +000023#include "clang/Serialization/ASTReader.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000024#include "llvm/ADT/StringSet.h"
Benjamin Kramer33d43302013-06-13 14:26:04 +000025#include "llvm/Support/FileSystem.h"
Eli Friedmanf7ca26a2011-07-08 20:17:28 +000026#include "llvm/Support/Path.h"
Daniel Dunbar966c2e12008-10-27 20:01:06 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000028
29using namespace clang;
30
31namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +000032/// Private implementation for DependencyFileGenerator
33class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +000034 std::vector<std::string> Files;
35 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000036 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +000037 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +000038 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +000039 bool IncludeSystemHeaders;
40 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +000041 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +000042 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +000043 bool IncludeModuleFiles;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000044private:
45 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +000046 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000047 void OutputDependencyFile();
48
49public:
Ben Langmuircb69b572014-03-07 06:40:32 +000050 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +000051 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000052 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +000053 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +000054 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +000055 SeenMissingHeader(false),
56 IncludeModuleFiles(Opts.IncludeModuleFiles) {}
Daniel Dunbar52e96cc2009-03-30 00:34:04 +000057
Craig Topperafa7cb32014-03-13 06:07:04 +000058 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
59 SrcMgr::CharacteristicKind FileType,
60 FileID PrevFID) override;
61 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
62 StringRef FileName, bool IsAngled,
63 CharSourceRange FilenameRange, const FileEntry *File,
64 StringRef SearchPath, StringRef RelativePath,
65 const Module *Imported) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +000066
Craig Topperafa7cb32014-03-13 06:07:04 +000067 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +000068 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +000069 }
Ben Langmuircb69b572014-03-07 06:40:32 +000070
71 void AddFilename(StringRef Filename);
72 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +000073 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +000074};
75
76class DFGASTReaderListener : public ASTReaderListener {
77 DFGImpl &Parent;
78public:
79 DFGASTReaderListener(DFGImpl &Parent)
80 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +000081 bool needsInputFileVisitation() override { return true; }
82 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +000083 return Parent.includeSystemHeaders();
84 }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +000085 void visitModuleFile(StringRef Filename) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +000086 bool visitInputFile(StringRef Filename, bool isSystem,
87 bool isOverridden) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000088};
89}
90
Ben Langmuircb69b572014-03-07 06:40:32 +000091DependencyFileGenerator::DependencyFileGenerator(void *Impl)
92: Impl(Impl) { }
93
94DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
95 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
96
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000097 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +000098 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Ben Langmuircb69b572014-03-07 06:40:32 +000099 return NULL;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000100 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000101
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000102 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000103 if (Opts.AddMissingHeaderDeps)
104 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000105
Ben Langmuircb69b572014-03-07 06:40:32 +0000106 DFGImpl *Callback = new DFGImpl(&PP, Opts);
107 PP.addPPCallbacks(Callback); // PP owns the Callback
108 return new DependencyFileGenerator(Callback);
109}
110
111void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
112 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
113 assert(I && "missing implementation");
114 R.addListener(new DFGASTReaderListener(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000115}
116
117/// FileMatchesDepCriteria - Determine whether the given Filename should be
118/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000119bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
120 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000121 if (strcmp("<built-in>", Filename) == 0)
122 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000123
Eli Friedman33cd03c2009-05-19 03:35:57 +0000124 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000125 return true;
126
127 return FileType == SrcMgr::C_User;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000128}
129
Ben Langmuircb69b572014-03-07 06:40:32 +0000130void DFGImpl::FileChanged(SourceLocation Loc,
131 FileChangeReason Reason,
132 SrcMgr::CharacteristicKind FileType,
133 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000134 if (Reason != PPCallbacks::EnterFile)
135 return;
Mike Stump11289f42009-09-09 15:08:12 +0000136
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000137 // Dependency generation really does want to go all the way to the
138 // file entry for a source location to find out what is depended on.
139 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000140 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000141
Chris Lattner64d8fc22009-01-28 05:42:38 +0000142 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000143 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Chris Lattner64d8fc22009-01-28 05:42:38 +0000144 if (FE == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +0000145
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000146 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000147 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000148 return;
149
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000150 // Remove leading "./" (or ".//" or "././" etc.)
151 while (Filename.size() > 2 && Filename[0] == '.' &&
152 llvm::sys::path::is_separator(Filename[1])) {
153 Filename = Filename.substr(1);
154 while (llvm::sys::path::is_separator(Filename[0]))
155 Filename = Filename.substr(1);
156 }
157
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000158 AddFilename(Filename);
159}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000160
Ben Langmuircb69b572014-03-07 06:40:32 +0000161void DFGImpl::InclusionDirective(SourceLocation HashLoc,
162 const Token &IncludeTok,
163 StringRef FileName,
164 bool IsAngled,
165 CharSourceRange FilenameRange,
166 const FileEntry *File,
167 StringRef SearchPath,
168 StringRef RelativePath,
169 const Module *Imported) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000170 if (!File) {
171 if (AddMissingHeaderDeps)
172 AddFilename(FileName);
173 else
174 SeenMissingHeader = true;
175 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000176}
177
Ben Langmuircb69b572014-03-07 06:40:32 +0000178void DFGImpl::AddFilename(StringRef Filename) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000179 if (FilesSet.insert(Filename))
180 Files.push_back(Filename);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000181}
182
Benjamin Kramerae611512013-04-02 13:38:48 +0000183/// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
184/// other scary characters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000185static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000186 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Benjamin Kramerae611512013-04-02 13:38:48 +0000187 if (Filename[i] == ' ' || Filename[i] == '#')
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000188 OS << '\\';
Benjamin Kramerae611512013-04-02 13:38:48 +0000189 else if (Filename[i] == '$') // $ is escaped by $$.
190 OS << '$';
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000191 OS << Filename[i];
192 }
193}
194
Ben Langmuircb69b572014-03-07 06:40:32 +0000195void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000196 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000197 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000198 return;
199 }
200
201 std::string Err;
Rafael Espindola4fbd3732014-02-24 18:20:21 +0000202 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err, llvm::sys::fs::F_Text);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000203 if (!Err.empty()) {
204 PP->getDiagnostics().Report(diag::err_fe_error_opening)
205 << OutputFile << Err;
206 return;
207 }
208
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000209 // Write out the dependency targets, trying to avoid overly long
210 // lines when possible. We try our best to emit exactly the same
211 // dependency file as GCC (4.2), assuming the included files are the
212 // same.
213 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000214 unsigned Columns = 0;
215
216 for (std::vector<std::string>::iterator
217 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
218 unsigned N = I->length();
219 if (Columns == 0) {
220 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000221 } else if (Columns + N + 2 > MaxColumns) {
222 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000223 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000224 } else {
225 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000226 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000227 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000228 // Targets already quoted as needed.
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000229 OS << *I;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000230 }
231
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000232 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000233 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000234
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000235 // Now add each dependency in the order it was seen, but avoiding
236 // duplicates.
237 for (std::vector<std::string>::iterator I = Files.begin(),
238 E = Files.end(); I != E; ++I) {
239 // Start a new line if this would exceed the column limit. Make
240 // sure to leave space for a trailing " \" in case we need to
241 // break the line on the next iteration.
242 unsigned N = I->length();
243 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000244 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000245 Columns = 2;
246 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000247 OS << ' ';
248 PrintFilename(OS, *I);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000249 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000250 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000251 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000252
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000253 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000254 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000255 // Skip the first entry, this is always the input file itself.
256 for (std::vector<std::string>::iterator I = Files.begin() + 1,
257 E = Files.end(); I != E; ++I) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000258 OS << '\n';
259 PrintFilename(OS, *I);
260 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000261 }
262 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000263}
264
Ben Langmuircb69b572014-03-07 06:40:32 +0000265bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000266 bool IsSystem, bool IsOverridden) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000267 assert(!IsSystem || needsSystemInputFileVisitation());
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000268 if (IsOverridden)
269 return true;
270
Ben Langmuircb69b572014-03-07 06:40:32 +0000271 Parent.AddFilename(Filename);
272 return true;
273}
274
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000275void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) {
276 if (Parent.includeModuleFiles())
277 Parent.AddFilename(Filename);
278}