blob: 53ea8befbc09fff0c846344df87b6c7db8f74ad8 [file] [log] [blame]
Daniel Dunbar750c3582008-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 Friedmanb09f6e12009-05-19 04:14:29 +000014#include "clang/Frontend/Utils.h"
Chris Lattnerb9c3f962009-01-27 07:57:44 +000015#include "clang/Basic/FileManager.h"
Daniel Dunbar0e0bae82009-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 Collingbournebb527862011-07-12 19:35:15 +000020#include "clang/Lex/LexDiagnostic.h"
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000021#include "clang/Lex/PPCallbacks.h"
22#include "clang/Lex/Preprocessor.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000023#include "llvm/ADT/StringSet.h"
Eli Friedmana6e023c2011-07-08 20:17:28 +000024#include "llvm/Support/Path.h"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000026
27using namespace clang;
28
29namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000030class DependencyFileCallback : public PPCallbacks {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000031 std::vector<std::string> Files;
32 llvm::StringSet<> FilesSet;
Daniel Dunbar750c3582008-10-24 22:12:41 +000033 const Preprocessor *PP;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000034 std::string OutputFile;
Chris Lattner02fbb252009-01-11 19:28:34 +000035 std::vector<std::string> Targets;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000036 bool IncludeSystemHeaders;
37 bool PhonyTarget;
Peter Collingbournebb527862011-07-12 19:35:15 +000038 bool AddMissingHeaderDeps;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000039 bool SeenMissingHeader;
Daniel Dunbar750c3582008-10-24 22:12:41 +000040private:
41 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000042 SrcMgr::CharacteristicKind FileType);
Chris Lattner5f9e2722011-07-23 10:55:15 +000043 void AddFilename(StringRef Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +000044 void OutputDependencyFile();
45
46public:
Mike Stump1eb44332009-09-09 15:08:12 +000047 DependencyFileCallback(const Preprocessor *_PP,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000048 const DependencyOutputOptions &Opts)
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000049 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000050 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbournebb527862011-07-12 19:35:15 +000051 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000052 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
53 SeenMissingHeader(false) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000054
Daniel Dunbardbd82092010-03-23 05:09:10 +000055 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +000056 SrcMgr::CharacteristicKind FileType,
57 FileID PrevFID);
Peter Collingbournebb527862011-07-12 19:35:15 +000058 virtual void InclusionDirective(SourceLocation HashLoc,
59 const Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 StringRef FileName,
Peter Collingbournebb527862011-07-12 19:35:15 +000061 bool IsAngled,
Argyrios Kyrtzidisda313592012-09-27 01:42:07 +000062 CharSourceRange FilenameRange,
Peter Collingbournebb527862011-07-12 19:35:15 +000063 const FileEntry *File,
Chris Lattner5f9e2722011-07-23 10:55:15 +000064 StringRef SearchPath,
Argyrios Kyrtzidisf8afcff2012-09-29 01:06:10 +000065 StringRef RelativePath,
66 const Module *Imported);
Daniel Dunbardbd82092010-03-23 05:09:10 +000067
68 virtual void EndOfMainFile() {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000069 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000070 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000071};
72}
73
Daniel Dunbarca11f612009-11-11 21:44:00 +000074void clang::AttachDependencyFileGen(Preprocessor &PP,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000075 const DependencyOutputOptions &Opts) {
76 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000077 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000078 return;
79 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000080
Peter Collingbournebb527862011-07-12 19:35:15 +000081 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedmanf84139a2011-08-30 23:07:51 +000082 if (Opts.AddMissingHeaderDeps)
83 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbournebb527862011-07-12 19:35:15 +000084
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000085 PP.addPPCallbacks(new DependencyFileCallback(&PP, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000086}
87
88/// FileMatchesDepCriteria - Determine whether the given Filename should be
89/// considered as a dependency.
90bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000091 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000092 if (strcmp("<built-in>", Filename) == 0)
93 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +000094
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000095 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000096 return true;
97
98 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +000099}
100
101void DependencyFileCallback::FileChanged(SourceLocation Loc,
102 FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +0000103 SrcMgr::CharacteristicKind FileType,
104 FileID PrevFID) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000105 if (Reason != PPCallbacks::EnterFile)
106 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000108 // Dependency generation really does want to go all the way to the
109 // file entry for a source location to find out what is depended on.
110 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000111 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000113 const FileEntry *FE =
Chandler Carruth40278532011-07-25 16:49:02 +0000114 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000115 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner5f9e2722011-07-23 10:55:15 +0000117 StringRef Filename = FE->getName();
Eli Friedmana6e023c2011-07-08 20:17:28 +0000118 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000119 return;
120
Eli Friedmana6e023c2011-07-08 20:17:28 +0000121 // Remove leading "./" (or ".//" or "././" etc.)
122 while (Filename.size() > 2 && Filename[0] == '.' &&
123 llvm::sys::path::is_separator(Filename[1])) {
124 Filename = Filename.substr(1);
125 while (llvm::sys::path::is_separator(Filename[0]))
126 Filename = Filename.substr(1);
127 }
128
Peter Collingbournebb527862011-07-12 19:35:15 +0000129 AddFilename(Filename);
130}
Daniel Dunbar750c3582008-10-24 22:12:41 +0000131
Peter Collingbournebb527862011-07-12 19:35:15 +0000132void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
133 const Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134 StringRef FileName,
Peter Collingbournebb527862011-07-12 19:35:15 +0000135 bool IsAngled,
Argyrios Kyrtzidisda313592012-09-27 01:42:07 +0000136 CharSourceRange FilenameRange,
Peter Collingbournebb527862011-07-12 19:35:15 +0000137 const FileEntry *File,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000138 StringRef SearchPath,
Argyrios Kyrtzidisf8afcff2012-09-29 01:06:10 +0000139 StringRef RelativePath,
140 const Module *Imported) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000141 if (!File) {
142 if (AddMissingHeaderDeps)
143 AddFilename(FileName);
144 else
145 SeenMissingHeader = true;
146 }
Peter Collingbournebb527862011-07-12 19:35:15 +0000147}
148
Chris Lattner5f9e2722011-07-23 10:55:15 +0000149void DependencyFileCallback::AddFilename(StringRef Filename) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000150 if (FilesSet.insert(Filename))
151 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000152}
153
Chris Lattner9d506342011-02-17 02:14:49 +0000154/// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
155/// scary characters.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000156static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner9d506342011-02-17 02:14:49 +0000157 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
158 if (Filename[i] == ' ')
159 OS << '\\';
160 OS << Filename[i];
161 }
162}
163
Daniel Dunbar750c3582008-10-24 22:12:41 +0000164void DependencyFileCallback::OutputDependencyFile() {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000165 if (SeenMissingHeader) {
166 llvm::sys::Path(OutputFile).eraseFromDisk();
167 return;
168 }
169
170 std::string Err;
171 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err);
172 if (!Err.empty()) {
173 PP->getDiagnostics().Report(diag::err_fe_error_opening)
174 << OutputFile << Err;
175 return;
176 }
177
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000178 // Write out the dependency targets, trying to avoid overly long
179 // lines when possible. We try our best to emit exactly the same
180 // dependency file as GCC (4.2), assuming the included files are the
181 // same.
182 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000183 unsigned Columns = 0;
184
185 for (std::vector<std::string>::iterator
186 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
187 unsigned N = I->length();
188 if (Columns == 0) {
189 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000190 } else if (Columns + N + 2 > MaxColumns) {
191 Columns = N + 2;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000192 OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000193 } else {
194 Columns += N + 1;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000195 OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000196 }
Chris Lattner9d506342011-02-17 02:14:49 +0000197 // Targets already quoted as needed.
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000198 OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000199 }
200
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000201 OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000202 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000204 // Now add each dependency in the order it was seen, but avoiding
205 // duplicates.
206 for (std::vector<std::string>::iterator I = Files.begin(),
207 E = Files.end(); I != E; ++I) {
208 // Start a new line if this would exceed the column limit. Make
209 // sure to leave space for a trailing " \" in case we need to
210 // break the line on the next iteration.
211 unsigned N = I->length();
212 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000213 OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000214 Columns = 2;
215 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000216 OS << ' ';
217 PrintFilename(OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000218 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000219 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000220 OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000221
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000222 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000223 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000224 // Skip the first entry, this is always the input file itself.
225 for (std::vector<std::string>::iterator I = Files.begin() + 1,
226 E = Files.end(); I != E; ++I) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000227 OS << '\n';
228 PrintFilename(OS, *I);
229 OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000230 }
231 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000232}
233