blob: ff3a12392c9f8f9660cfcfd5e53906d44f995899 [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;
Chris Lattner5f9e2722011-07-23 10:55:15 +000035 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);
Chris Lattner5f9e2722011-07-23 10:55:15 +000042 void AddFilename(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,
Chris Lattner5f9e2722011-07-23 10:55:15 +000047 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,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +000055 SrcMgr::CharacteristicKind FileType,
56 FileID PrevFID);
Peter Collingbournebb527862011-07-12 19:35:15 +000057 virtual void InclusionDirective(SourceLocation HashLoc,
58 const Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +000059 StringRef FileName,
Peter Collingbournebb527862011-07-12 19:35:15 +000060 bool IsAngled,
61 const FileEntry *File,
62 SourceLocation EndLoc,
Chris Lattner5f9e2722011-07-23 10:55:15 +000063 StringRef SearchPath,
64 StringRef RelativePath);
Daniel Dunbardbd82092010-03-23 05:09:10 +000065
66 virtual void EndOfMainFile() {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000067 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000068 delete OS;
Daniel Dunbardbd82092010-03-23 05:09:10 +000069 OS = 0;
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
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000081 std::string Err;
Chris Lattner5f9e2722011-07-23 10:55:15 +000082 raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000083 if (!Err.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000084 PP.getDiagnostics().Report(diag::err_fe_error_opening)
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000085 << Opts.OutputFile << Err;
86 return;
87 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000088
Peter Collingbournebb527862011-07-12 19:35:15 +000089 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedmanf84139a2011-08-30 23:07:51 +000090 if (Opts.AddMissingHeaderDeps)
91 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbournebb527862011-07-12 19:35:15 +000092
Kovarththanan Rajaratnama5d10c42010-03-07 07:30:06 +000093 PP.addPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000094}
95
96/// FileMatchesDepCriteria - Determine whether the given Filename should be
97/// considered as a dependency.
98bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000099 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000100 if (strcmp("<built-in>", Filename) == 0)
101 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000102
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000103 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000104 return true;
105
106 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000107}
108
109void DependencyFileCallback::FileChanged(SourceLocation Loc,
110 FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +0000111 SrcMgr::CharacteristicKind FileType,
112 FileID PrevFID) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000113 if (Reason != PPCallbacks::EnterFile)
114 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000116 // Dependency generation really does want to go all the way to the
117 // file entry for a source location to find out what is depended on.
118 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000119 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000121 const FileEntry *FE =
Chandler Carruth40278532011-07-25 16:49:02 +0000122 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000123 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner5f9e2722011-07-23 10:55:15 +0000125 StringRef Filename = FE->getName();
Eli Friedmana6e023c2011-07-08 20:17:28 +0000126 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000127 return;
128
Eli Friedmana6e023c2011-07-08 20:17:28 +0000129 // Remove leading "./" (or ".//" or "././" etc.)
130 while (Filename.size() > 2 && Filename[0] == '.' &&
131 llvm::sys::path::is_separator(Filename[1])) {
132 Filename = Filename.substr(1);
133 while (llvm::sys::path::is_separator(Filename[0]))
134 Filename = Filename.substr(1);
135 }
136
Peter Collingbournebb527862011-07-12 19:35:15 +0000137 AddFilename(Filename);
138}
Daniel Dunbar750c3582008-10-24 22:12:41 +0000139
Peter Collingbournebb527862011-07-12 19:35:15 +0000140void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
141 const Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 StringRef FileName,
Peter Collingbournebb527862011-07-12 19:35:15 +0000143 bool IsAngled,
144 const FileEntry *File,
145 SourceLocation EndLoc,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000146 StringRef SearchPath,
147 StringRef RelativePath) {
Peter Collingbournebb527862011-07-12 19:35:15 +0000148 if (AddMissingHeaderDeps && !File)
149 AddFilename(FileName);
150}
151
Chris Lattner5f9e2722011-07-23 10:55:15 +0000152void DependencyFileCallback::AddFilename(StringRef Filename) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000153 if (FilesSet.insert(Filename))
154 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000155}
156
Chris Lattner9d506342011-02-17 02:14:49 +0000157/// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
158/// scary characters.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000159static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner9d506342011-02-17 02:14:49 +0000160 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
161 if (Filename[i] == ' ')
162 OS << '\\';
163 OS << Filename[i];
164 }
165}
166
Daniel Dunbar750c3582008-10-24 22:12:41 +0000167void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000168 // Write out the dependency targets, trying to avoid overly long
169 // lines when possible. We try our best to emit exactly the same
170 // dependency file as GCC (4.2), assuming the included files are the
171 // same.
172 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000173 unsigned Columns = 0;
174
175 for (std::vector<std::string>::iterator
176 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
177 unsigned N = I->length();
178 if (Columns == 0) {
179 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000180 } else if (Columns + N + 2 > MaxColumns) {
181 Columns = N + 2;
Chris Lattner9d506342011-02-17 02:14:49 +0000182 *OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000183 } else {
184 Columns += N + 1;
Chris Lattner9d506342011-02-17 02:14:49 +0000185 *OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000186 }
Chris Lattner9d506342011-02-17 02:14:49 +0000187 // Targets already quoted as needed.
188 *OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000189 }
190
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000191 *OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000192 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000194 // Now add each dependency in the order it was seen, but avoiding
195 // duplicates.
196 for (std::vector<std::string>::iterator I = Files.begin(),
197 E = Files.end(); I != E; ++I) {
198 // Start a new line if this would exceed the column limit. Make
199 // sure to leave space for a trailing " \" in case we need to
200 // break the line on the next iteration.
201 unsigned N = I->length();
202 if (Columns + (N + 1) + 2 > MaxColumns) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000203 *OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000204 Columns = 2;
205 }
Chris Lattner9d506342011-02-17 02:14:49 +0000206 *OS << ' ';
207 PrintFilename(*OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000208 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000209 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000210 *OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000211
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000212 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000213 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000214 // Skip the first entry, this is always the input file itself.
215 for (std::vector<std::string>::iterator I = Files.begin() + 1,
216 E = Files.end(); I != E; ++I) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000217 *OS << '\n';
Chris Lattner9d506342011-02-17 02:14:49 +0000218 PrintFilename(*OS, *I);
219 *OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000220 }
221 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000222}
223