blob: db43ab9bbe11a163e6e3ca035dc28c0fe6b29988 [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,
55 SrcMgr::CharacteristicKind FileType);
Peter Collingbournebb527862011-07-12 19:35:15 +000056 virtual void InclusionDirective(SourceLocation HashLoc,
57 const Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +000058 StringRef FileName,
Peter Collingbournebb527862011-07-12 19:35:15 +000059 bool IsAngled,
60 const FileEntry *File,
61 SourceLocation EndLoc,
Chris Lattner5f9e2722011-07-23 10:55:15 +000062 StringRef SearchPath,
63 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;
Chris Lattner5f9e2722011-07-23 10:55:15 +000081 raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000082 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.
Eli Friedmanf84139a2011-08-30 23:07:51 +000089 if (Opts.AddMissingHeaderDeps)
90 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbournebb527862011-07-12 19:35:15 +000091
Kovarththanan Rajaratnama5d10c42010-03-07 07:30:06 +000092 PP.addPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000093}
94
95/// FileMatchesDepCriteria - Determine whether the given Filename should be
96/// considered as a dependency.
97bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000098 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000099 if (strcmp("<built-in>", Filename) == 0)
100 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000101
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000102 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000103 return true;
104
105 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000106}
107
108void DependencyFileCallback::FileChanged(SourceLocation Loc,
109 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000110 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000111 if (Reason != PPCallbacks::EnterFile)
112 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000114 // Dependency generation really does want to go all the way to the
115 // file entry for a source location to find out what is depended on.
116 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000117 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000119 const FileEntry *FE =
Chandler Carruth40278532011-07-25 16:49:02 +0000120 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000121 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 StringRef Filename = FE->getName();
Eli Friedmana6e023c2011-07-08 20:17:28 +0000124 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000125 return;
126
Eli Friedmana6e023c2011-07-08 20:17:28 +0000127 // Remove leading "./" (or ".//" or "././" etc.)
128 while (Filename.size() > 2 && Filename[0] == '.' &&
129 llvm::sys::path::is_separator(Filename[1])) {
130 Filename = Filename.substr(1);
131 while (llvm::sys::path::is_separator(Filename[0]))
132 Filename = Filename.substr(1);
133 }
134
Peter Collingbournebb527862011-07-12 19:35:15 +0000135 AddFilename(Filename);
136}
Daniel Dunbar750c3582008-10-24 22:12:41 +0000137
Peter Collingbournebb527862011-07-12 19:35:15 +0000138void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
139 const Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000140 StringRef FileName,
Peter Collingbournebb527862011-07-12 19:35:15 +0000141 bool IsAngled,
142 const FileEntry *File,
143 SourceLocation EndLoc,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000144 StringRef SearchPath,
145 StringRef RelativePath) {
Peter Collingbournebb527862011-07-12 19:35:15 +0000146 if (AddMissingHeaderDeps && !File)
147 AddFilename(FileName);
148}
149
Chris Lattner5f9e2722011-07-23 10:55:15 +0000150void DependencyFileCallback::AddFilename(StringRef Filename) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000151 if (FilesSet.insert(Filename))
152 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000153}
154
Chris Lattner9d506342011-02-17 02:14:49 +0000155/// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
156/// scary characters.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000157static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner9d506342011-02-17 02:14:49 +0000158 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
159 if (Filename[i] == ' ')
160 OS << '\\';
161 OS << Filename[i];
162 }
163}
164
Daniel Dunbar750c3582008-10-24 22:12:41 +0000165void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000166 // Write out the dependency targets, trying to avoid overly long
167 // lines when possible. We try our best to emit exactly the same
168 // dependency file as GCC (4.2), assuming the included files are the
169 // same.
170 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000171 unsigned Columns = 0;
172
173 for (std::vector<std::string>::iterator
174 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
175 unsigned N = I->length();
176 if (Columns == 0) {
177 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000178 } else if (Columns + N + 2 > MaxColumns) {
179 Columns = N + 2;
Chris Lattner9d506342011-02-17 02:14:49 +0000180 *OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000181 } else {
182 Columns += N + 1;
Chris Lattner9d506342011-02-17 02:14:49 +0000183 *OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000184 }
Chris Lattner9d506342011-02-17 02:14:49 +0000185 // Targets already quoted as needed.
186 *OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000187 }
188
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000189 *OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000190 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000192 // Now add each dependency in the order it was seen, but avoiding
193 // duplicates.
194 for (std::vector<std::string>::iterator I = Files.begin(),
195 E = Files.end(); I != E; ++I) {
196 // Start a new line if this would exceed the column limit. Make
197 // sure to leave space for a trailing " \" in case we need to
198 // break the line on the next iteration.
199 unsigned N = I->length();
200 if (Columns + (N + 1) + 2 > MaxColumns) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000201 *OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000202 Columns = 2;
203 }
Chris Lattner9d506342011-02-17 02:14:49 +0000204 *OS << ' ';
205 PrintFilename(*OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000206 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000207 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000208 *OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000209
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000210 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000211 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000212 // Skip the first entry, this is always the input file itself.
213 for (std::vector<std::string>::iterator I = Files.begin() + 1,
214 E = Files.end(); I != E; ++I) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000215 *OS << '\n';
Chris Lattner9d506342011-02-17 02:14:49 +0000216 PrintFilename(*OS, *I);
217 *OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000218 }
219 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000220}
221