blob: adc96604e7e4b534165b29982637c822d5e433df [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,
65 StringRef RelativePath);
Daniel Dunbardbd82092010-03-23 05:09:10 +000066
67 virtual void EndOfMainFile() {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000068 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000069 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000070};
71}
72
Daniel Dunbarca11f612009-11-11 21:44:00 +000073void clang::AttachDependencyFileGen(Preprocessor &PP,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000074 const DependencyOutputOptions &Opts) {
75 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000076 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000077 return;
78 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000079
Peter Collingbournebb527862011-07-12 19:35:15 +000080 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedmanf84139a2011-08-30 23:07:51 +000081 if (Opts.AddMissingHeaderDeps)
82 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbournebb527862011-07-12 19:35:15 +000083
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000084 PP.addPPCallbacks(new DependencyFileCallback(&PP, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000085}
86
87/// FileMatchesDepCriteria - Determine whether the given Filename should be
88/// considered as a dependency.
89bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000090 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000091 if (strcmp("<built-in>", Filename) == 0)
92 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +000093
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000094 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000095 return true;
96
97 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +000098}
99
100void DependencyFileCallback::FileChanged(SourceLocation Loc,
101 FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +0000102 SrcMgr::CharacteristicKind FileType,
103 FileID PrevFID) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000104 if (Reason != PPCallbacks::EnterFile)
105 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000107 // Dependency generation really does want to go all the way to the
108 // file entry for a source location to find out what is depended on.
109 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000110 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000112 const FileEntry *FE =
Chandler Carruth40278532011-07-25 16:49:02 +0000113 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000114 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattner5f9e2722011-07-23 10:55:15 +0000116 StringRef Filename = FE->getName();
Eli Friedmana6e023c2011-07-08 20:17:28 +0000117 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000118 return;
119
Eli Friedmana6e023c2011-07-08 20:17:28 +0000120 // Remove leading "./" (or ".//" or "././" etc.)
121 while (Filename.size() > 2 && Filename[0] == '.' &&
122 llvm::sys::path::is_separator(Filename[1])) {
123 Filename = Filename.substr(1);
124 while (llvm::sys::path::is_separator(Filename[0]))
125 Filename = Filename.substr(1);
126 }
127
Peter Collingbournebb527862011-07-12 19:35:15 +0000128 AddFilename(Filename);
129}
Daniel Dunbar750c3582008-10-24 22:12:41 +0000130
Peter Collingbournebb527862011-07-12 19:35:15 +0000131void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
132 const Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000133 StringRef FileName,
Peter Collingbournebb527862011-07-12 19:35:15 +0000134 bool IsAngled,
Argyrios Kyrtzidisda313592012-09-27 01:42:07 +0000135 CharSourceRange FilenameRange,
Peter Collingbournebb527862011-07-12 19:35:15 +0000136 const FileEntry *File,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137 StringRef SearchPath,
138 StringRef RelativePath) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000139 if (!File) {
140 if (AddMissingHeaderDeps)
141 AddFilename(FileName);
142 else
143 SeenMissingHeader = true;
144 }
Peter Collingbournebb527862011-07-12 19:35:15 +0000145}
146
Chris Lattner5f9e2722011-07-23 10:55:15 +0000147void DependencyFileCallback::AddFilename(StringRef Filename) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000148 if (FilesSet.insert(Filename))
149 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000150}
151
Chris Lattner9d506342011-02-17 02:14:49 +0000152/// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
153/// scary characters.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000154static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner9d506342011-02-17 02:14:49 +0000155 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
156 if (Filename[i] == ' ')
157 OS << '\\';
158 OS << Filename[i];
159 }
160}
161
Daniel Dunbar750c3582008-10-24 22:12:41 +0000162void DependencyFileCallback::OutputDependencyFile() {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000163 if (SeenMissingHeader) {
164 llvm::sys::Path(OutputFile).eraseFromDisk();
165 return;
166 }
167
168 std::string Err;
169 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err);
170 if (!Err.empty()) {
171 PP->getDiagnostics().Report(diag::err_fe_error_opening)
172 << OutputFile << Err;
173 return;
174 }
175
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000176 // Write out the dependency targets, trying to avoid overly long
177 // lines when possible. We try our best to emit exactly the same
178 // dependency file as GCC (4.2), assuming the included files are the
179 // same.
180 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000181 unsigned Columns = 0;
182
183 for (std::vector<std::string>::iterator
184 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
185 unsigned N = I->length();
186 if (Columns == 0) {
187 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000188 } else if (Columns + N + 2 > MaxColumns) {
189 Columns = N + 2;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000190 OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000191 } else {
192 Columns += N + 1;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000193 OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000194 }
Chris Lattner9d506342011-02-17 02:14:49 +0000195 // Targets already quoted as needed.
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000196 OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000197 }
198
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000199 OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000200 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000202 // Now add each dependency in the order it was seen, but avoiding
203 // duplicates.
204 for (std::vector<std::string>::iterator I = Files.begin(),
205 E = Files.end(); I != E; ++I) {
206 // Start a new line if this would exceed the column limit. Make
207 // sure to leave space for a trailing " \" in case we need to
208 // break the line on the next iteration.
209 unsigned N = I->length();
210 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000211 OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000212 Columns = 2;
213 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000214 OS << ' ';
215 PrintFilename(OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000216 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000217 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000218 OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000219
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000220 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000221 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000222 // Skip the first entry, this is always the input file itself.
223 for (std::vector<std::string>::iterator I = Files.begin() + 1,
224 E = Files.end(); I != E; ++I) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000225 OS << '\n';
226 PrintFilename(OS, *I);
227 OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000228 }
229 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000230}
231