blob: 8457770abb9f0df465a8f2f8dab4ed20e00f6bd7 [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;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000043private:
44 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +000045 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000046 void OutputDependencyFile();
47
48public:
Ben Langmuircb69b572014-03-07 06:40:32 +000049 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +000050 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000051 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +000052 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +000053 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
54 SeenMissingHeader(false) {}
Daniel Dunbar52e96cc2009-03-30 00:34:04 +000055
Craig Topperafa7cb32014-03-13 06:07:04 +000056 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
57 SrcMgr::CharacteristicKind FileType,
58 FileID PrevFID) override;
59 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
60 StringRef FileName, bool IsAngled,
61 CharSourceRange FilenameRange, const FileEntry *File,
62 StringRef SearchPath, StringRef RelativePath,
63 const Module *Imported) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +000064
Craig Topperafa7cb32014-03-13 06:07:04 +000065 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +000066 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +000067 }
Ben Langmuircb69b572014-03-07 06:40:32 +000068
69 void AddFilename(StringRef Filename);
70 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
71};
72
73class DFGASTReaderListener : public ASTReaderListener {
74 DFGImpl &Parent;
75public:
76 DFGASTReaderListener(DFGImpl &Parent)
77 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +000078 bool needsInputFileVisitation() override { return true; }
79 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +000080 return Parent.includeSystemHeaders();
81 }
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +000082 bool visitInputFile(StringRef Filename, bool isSystem,
83 bool isOverridden) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000084};
85}
86
Ben Langmuircb69b572014-03-07 06:40:32 +000087DependencyFileGenerator::DependencyFileGenerator(void *Impl)
88: Impl(Impl) { }
89
90DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
91 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
92
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000093 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +000094 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Ben Langmuircb69b572014-03-07 06:40:32 +000095 return NULL;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000096 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +000097
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +000098 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +000099 if (Opts.AddMissingHeaderDeps)
100 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000101
Ben Langmuircb69b572014-03-07 06:40:32 +0000102 DFGImpl *Callback = new DFGImpl(&PP, Opts);
103 PP.addPPCallbacks(Callback); // PP owns the Callback
104 return new DependencyFileGenerator(Callback);
105}
106
107void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
108 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
109 assert(I && "missing implementation");
110 R.addListener(new DFGASTReaderListener(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000111}
112
113/// FileMatchesDepCriteria - Determine whether the given Filename should be
114/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000115bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
116 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000117 if (strcmp("<built-in>", Filename) == 0)
118 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000119
Eli Friedman33cd03c2009-05-19 03:35:57 +0000120 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000121 return true;
122
123 return FileType == SrcMgr::C_User;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000124}
125
Ben Langmuircb69b572014-03-07 06:40:32 +0000126void DFGImpl::FileChanged(SourceLocation Loc,
127 FileChangeReason Reason,
128 SrcMgr::CharacteristicKind FileType,
129 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000130 if (Reason != PPCallbacks::EnterFile)
131 return;
Mike Stump11289f42009-09-09 15:08:12 +0000132
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000133 // Dependency generation really does want to go all the way to the
134 // file entry for a source location to find out what is depended on.
135 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000136 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000137
Chris Lattner64d8fc22009-01-28 05:42:38 +0000138 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000139 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Chris Lattner64d8fc22009-01-28 05:42:38 +0000140 if (FE == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +0000141
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000142 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000143 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000144 return;
145
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000146 // Remove leading "./" (or ".//" or "././" etc.)
147 while (Filename.size() > 2 && Filename[0] == '.' &&
148 llvm::sys::path::is_separator(Filename[1])) {
149 Filename = Filename.substr(1);
150 while (llvm::sys::path::is_separator(Filename[0]))
151 Filename = Filename.substr(1);
152 }
153
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000154 AddFilename(Filename);
155}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000156
Ben Langmuircb69b572014-03-07 06:40:32 +0000157void DFGImpl::InclusionDirective(SourceLocation HashLoc,
158 const Token &IncludeTok,
159 StringRef FileName,
160 bool IsAngled,
161 CharSourceRange FilenameRange,
162 const FileEntry *File,
163 StringRef SearchPath,
164 StringRef RelativePath,
165 const Module *Imported) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000166 if (!File) {
167 if (AddMissingHeaderDeps)
168 AddFilename(FileName);
169 else
170 SeenMissingHeader = true;
171 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000172}
173
Ben Langmuircb69b572014-03-07 06:40:32 +0000174void DFGImpl::AddFilename(StringRef Filename) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000175 if (FilesSet.insert(Filename))
176 Files.push_back(Filename);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000177}
178
Benjamin Kramerae611512013-04-02 13:38:48 +0000179/// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
180/// other scary characters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000181static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000182 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Benjamin Kramerae611512013-04-02 13:38:48 +0000183 if (Filename[i] == ' ' || Filename[i] == '#')
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000184 OS << '\\';
Benjamin Kramerae611512013-04-02 13:38:48 +0000185 else if (Filename[i] == '$') // $ is escaped by $$.
186 OS << '$';
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000187 OS << Filename[i];
188 }
189}
190
Ben Langmuircb69b572014-03-07 06:40:32 +0000191void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000192 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000193 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000194 return;
195 }
196
197 std::string Err;
Rafael Espindola4fbd3732014-02-24 18:20:21 +0000198 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err, llvm::sys::fs::F_Text);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000199 if (!Err.empty()) {
200 PP->getDiagnostics().Report(diag::err_fe_error_opening)
201 << OutputFile << Err;
202 return;
203 }
204
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000205 // Write out the dependency targets, trying to avoid overly long
206 // lines when possible. We try our best to emit exactly the same
207 // dependency file as GCC (4.2), assuming the included files are the
208 // same.
209 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000210 unsigned Columns = 0;
211
212 for (std::vector<std::string>::iterator
213 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
214 unsigned N = I->length();
215 if (Columns == 0) {
216 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000217 } else if (Columns + N + 2 > MaxColumns) {
218 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000219 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000220 } else {
221 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000222 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000223 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000224 // Targets already quoted as needed.
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000225 OS << *I;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000226 }
227
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000228 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000229 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000230
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000231 // Now add each dependency in the order it was seen, but avoiding
232 // duplicates.
233 for (std::vector<std::string>::iterator I = Files.begin(),
234 E = Files.end(); I != E; ++I) {
235 // Start a new line if this would exceed the column limit. Make
236 // sure to leave space for a trailing " \" in case we need to
237 // break the line on the next iteration.
238 unsigned N = I->length();
239 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000240 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000241 Columns = 2;
242 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000243 OS << ' ';
244 PrintFilename(OS, *I);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000245 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000246 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000247 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000248
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000249 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000250 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000251 // Skip the first entry, this is always the input file itself.
252 for (std::vector<std::string>::iterator I = Files.begin() + 1,
253 E = Files.end(); I != E; ++I) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000254 OS << '\n';
255 PrintFilename(OS, *I);
256 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000257 }
258 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000259}
260
Ben Langmuircb69b572014-03-07 06:40:32 +0000261bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000262 bool IsSystem, bool IsOverridden) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000263 assert(!IsSystem || needsSystemInputFileVisitation());
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000264 if (IsOverridden)
265 return true;
266
Ben Langmuircb69b572014-03-07 06:40:32 +0000267 Parent.AddFilename(Filename);
268 return true;
269}
270