blob: 1edd09b06c4cd12b13204d31516ec37e44c19eb9 [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;
Chris Lattner02fbb252009-01-11 19:28:34 +000034 std::vector<std::string> Targets;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000035 llvm::raw_ostream *OS;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000036 bool IncludeSystemHeaders;
37 bool PhonyTarget;
Peter Collingbournebb527862011-07-12 19:35:15 +000038 bool AddMissingHeaderDeps;
Daniel Dunbar750c3582008-10-24 22:12:41 +000039private:
40 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000041 SrcMgr::CharacteristicKind FileType);
Peter Collingbournebb527862011-07-12 19:35:15 +000042 void AddFilename(llvm::StringRef Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +000043 void OutputDependencyFile();
44
45public:
Mike Stump1eb44332009-09-09 15:08:12 +000046 DependencyFileCallback(const Preprocessor *_PP,
47 llvm::raw_ostream *_OS,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000048 const DependencyOutputOptions &Opts)
49 : PP(_PP), Targets(Opts.Targets), OS(_OS),
50 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbournebb527862011-07-12 19:35:15 +000051 PhonyTarget(Opts.UsePhonyTargets),
52 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000053
Daniel Dunbardbd82092010-03-23 05:09:10 +000054 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
55 SrcMgr::CharacteristicKind FileType);
Peter Collingbournebb527862011-07-12 19:35:15 +000056 virtual void InclusionDirective(SourceLocation HashLoc,
57 const Token &IncludeTok,
58 llvm::StringRef FileName,
59 bool IsAngled,
60 const FileEntry *File,
61 SourceLocation EndLoc,
62 llvm::StringRef SearchPath,
63 llvm::StringRef RelativePath);
Daniel Dunbardbd82092010-03-23 05:09:10 +000064
65 virtual void EndOfMainFile() {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000066 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000067 delete OS;
Daniel Dunbardbd82092010-03-23 05:09:10 +000068 OS = 0;
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
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000080 std::string Err;
81 llvm::raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
82 if (!Err.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000083 PP.getDiagnostics().Report(diag::err_fe_error_opening)
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000084 << Opts.OutputFile << Err;
85 return;
86 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000087
Peter Collingbournebb527862011-07-12 19:35:15 +000088 // Disable the "file not found" diagnostic if the -MG option was given.
89 // FIXME: Ideally this would live in the driver, but we don't have the ability
90 // to remap individual diagnostics there without creating a DiagGroup, in
91 // which case we would need to prevent the group name from showing up in
92 // diagnostics.
93 if (Opts.AddMissingHeaderDeps) {
94 PP.getDiagnostics().setDiagnosticMapping(diag::warn_pp_file_not_found,
95 diag::MAP_IGNORE, SourceLocation());
96 }
97
Kovarththanan Rajaratnama5d10c42010-03-07 07:30:06 +000098 PP.addPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000099}
100
101/// FileMatchesDepCriteria - Determine whether the given Filename should be
102/// considered as a dependency.
103bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +0000104 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000105 if (strcmp("<built-in>", Filename) == 0)
106 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000107
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000108 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000109 return true;
110
111 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000112}
113
114void DependencyFileCallback::FileChanged(SourceLocation Loc,
115 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000116 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000117 if (Reason != PPCallbacks::EnterFile)
118 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000120 // Dependency generation really does want to go all the way to the
121 // file entry for a source location to find out what is depended on.
122 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000123 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000125 const FileEntry *FE =
126 SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc)));
127 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Eli Friedmana6e023c2011-07-08 20:17:28 +0000129 llvm::StringRef Filename = FE->getName();
130 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000131 return;
132
Eli Friedmana6e023c2011-07-08 20:17:28 +0000133 // Remove leading "./" (or ".//" or "././" etc.)
134 while (Filename.size() > 2 && Filename[0] == '.' &&
135 llvm::sys::path::is_separator(Filename[1])) {
136 Filename = Filename.substr(1);
137 while (llvm::sys::path::is_separator(Filename[0]))
138 Filename = Filename.substr(1);
139 }
140
Peter Collingbournebb527862011-07-12 19:35:15 +0000141 AddFilename(Filename);
142}
Daniel Dunbar750c3582008-10-24 22:12:41 +0000143
Peter Collingbournebb527862011-07-12 19:35:15 +0000144void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
145 const Token &IncludeTok,
146 llvm::StringRef FileName,
147 bool IsAngled,
148 const FileEntry *File,
149 SourceLocation EndLoc,
150 llvm::StringRef SearchPath,
151 llvm::StringRef RelativePath) {
152 if (AddMissingHeaderDeps && !File)
153 AddFilename(FileName);
154}
155
156void DependencyFileCallback::AddFilename(llvm::StringRef Filename) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000157 if (FilesSet.insert(Filename))
158 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000159}
160
Chris Lattner9d506342011-02-17 02:14:49 +0000161/// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
162/// scary characters.
163static void PrintFilename(llvm::raw_ostream &OS, llvm::StringRef Filename) {
164 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
165 if (Filename[i] == ' ')
166 OS << '\\';
167 OS << Filename[i];
168 }
169}
170
Daniel Dunbar750c3582008-10-24 22:12:41 +0000171void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000172 // Write out the dependency targets, trying to avoid overly long
173 // lines when possible. We try our best to emit exactly the same
174 // dependency file as GCC (4.2), assuming the included files are the
175 // same.
176 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000177 unsigned Columns = 0;
178
179 for (std::vector<std::string>::iterator
180 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
181 unsigned N = I->length();
182 if (Columns == 0) {
183 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000184 } else if (Columns + N + 2 > MaxColumns) {
185 Columns = N + 2;
Chris Lattner9d506342011-02-17 02:14:49 +0000186 *OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000187 } else {
188 Columns += N + 1;
Chris Lattner9d506342011-02-17 02:14:49 +0000189 *OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000190 }
Chris Lattner9d506342011-02-17 02:14:49 +0000191 // Targets already quoted as needed.
192 *OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000193 }
194
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000195 *OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000196 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000198 // Now add each dependency in the order it was seen, but avoiding
199 // duplicates.
200 for (std::vector<std::string>::iterator I = Files.begin(),
201 E = Files.end(); I != E; ++I) {
202 // Start a new line if this would exceed the column limit. Make
203 // sure to leave space for a trailing " \" in case we need to
204 // break the line on the next iteration.
205 unsigned N = I->length();
206 if (Columns + (N + 1) + 2 > MaxColumns) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000207 *OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000208 Columns = 2;
209 }
Chris Lattner9d506342011-02-17 02:14:49 +0000210 *OS << ' ';
211 PrintFilename(*OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000212 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000213 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000214 *OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000215
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000216 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000217 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000218 // Skip the first entry, this is always the input file itself.
219 for (std::vector<std::string>::iterator I = Files.begin() + 1,
220 E = Files.end(); I != E; ++I) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000221 *OS << '\n';
Chris Lattner9d506342011-02-17 02:14:49 +0000222 PrintFilename(*OS, *I);
223 *OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000224 }
225 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000226}
227