blob: 0c6ab4a0bd8db6a812030049070a4e311ab15cec [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"
Rafael Espindola34392372013-06-11 19:59:07 +000025#include "llvm/Support/PathV1.h"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000026#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000027
28using namespace clang;
29
30namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000031class DependencyFileCallback : public PPCallbacks {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000032 std::vector<std::string> Files;
33 llvm::StringSet<> FilesSet;
Daniel Dunbar750c3582008-10-24 22:12:41 +000034 const Preprocessor *PP;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000035 std::string OutputFile;
Chris Lattner02fbb252009-01-11 19:28:34 +000036 std::vector<std::string> Targets;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000037 bool IncludeSystemHeaders;
38 bool PhonyTarget;
Peter Collingbournebb527862011-07-12 19:35:15 +000039 bool AddMissingHeaderDeps;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000040 bool SeenMissingHeader;
Daniel Dunbar750c3582008-10-24 22:12:41 +000041private:
42 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000043 SrcMgr::CharacteristicKind FileType);
Chris Lattner5f9e2722011-07-23 10:55:15 +000044 void AddFilename(StringRef Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +000045 void OutputDependencyFile();
46
47public:
Mike Stump1eb44332009-09-09 15:08:12 +000048 DependencyFileCallback(const Preprocessor *_PP,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000049 const DependencyOutputOptions &Opts)
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000050 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000051 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbournebb527862011-07-12 19:35:15 +000052 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000053 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
54 SeenMissingHeader(false) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000055
Daniel Dunbardbd82092010-03-23 05:09:10 +000056 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +000057 SrcMgr::CharacteristicKind FileType,
58 FileID PrevFID);
Peter Collingbournebb527862011-07-12 19:35:15 +000059 virtual void InclusionDirective(SourceLocation HashLoc,
60 const Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +000061 StringRef FileName,
Peter Collingbournebb527862011-07-12 19:35:15 +000062 bool IsAngled,
Argyrios Kyrtzidisda313592012-09-27 01:42:07 +000063 CharSourceRange FilenameRange,
Peter Collingbournebb527862011-07-12 19:35:15 +000064 const FileEntry *File,
Chris Lattner5f9e2722011-07-23 10:55:15 +000065 StringRef SearchPath,
Argyrios Kyrtzidisf8afcff2012-09-29 01:06:10 +000066 StringRef RelativePath,
67 const Module *Imported);
Daniel Dunbardbd82092010-03-23 05:09:10 +000068
69 virtual void EndOfMainFile() {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000070 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000071 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000072};
73}
74
Daniel Dunbarca11f612009-11-11 21:44:00 +000075void clang::AttachDependencyFileGen(Preprocessor &PP,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000076 const DependencyOutputOptions &Opts) {
77 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000078 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000079 return;
80 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000081
Peter Collingbournebb527862011-07-12 19:35:15 +000082 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedmanf84139a2011-08-30 23:07:51 +000083 if (Opts.AddMissingHeaderDeps)
84 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbournebb527862011-07-12 19:35:15 +000085
Peter Collingbourne1b91ab42011-11-21 00:01:14 +000086 PP.addPPCallbacks(new DependencyFileCallback(&PP, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000087}
88
89/// FileMatchesDepCriteria - Determine whether the given Filename should be
90/// considered as a dependency.
91bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000092 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000093 if (strcmp("<built-in>", Filename) == 0)
94 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +000095
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000096 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000097 return true;
98
99 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000100}
101
102void DependencyFileCallback::FileChanged(SourceLocation Loc,
103 FileChangeReason Reason,
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +0000104 SrcMgr::CharacteristicKind FileType,
105 FileID PrevFID) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000106 if (Reason != PPCallbacks::EnterFile)
107 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000109 // Dependency generation really does want to go all the way to the
110 // file entry for a source location to find out what is depended on.
111 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000112 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000114 const FileEntry *FE =
Chandler Carruth40278532011-07-25 16:49:02 +0000115 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000116 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner5f9e2722011-07-23 10:55:15 +0000118 StringRef Filename = FE->getName();
Eli Friedmana6e023c2011-07-08 20:17:28 +0000119 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000120 return;
121
Eli Friedmana6e023c2011-07-08 20:17:28 +0000122 // Remove leading "./" (or ".//" or "././" etc.)
123 while (Filename.size() > 2 && Filename[0] == '.' &&
124 llvm::sys::path::is_separator(Filename[1])) {
125 Filename = Filename.substr(1);
126 while (llvm::sys::path::is_separator(Filename[0]))
127 Filename = Filename.substr(1);
128 }
129
Peter Collingbournebb527862011-07-12 19:35:15 +0000130 AddFilename(Filename);
131}
Daniel Dunbar750c3582008-10-24 22:12:41 +0000132
Peter Collingbournebb527862011-07-12 19:35:15 +0000133void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
134 const Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000135 StringRef FileName,
Peter Collingbournebb527862011-07-12 19:35:15 +0000136 bool IsAngled,
Argyrios Kyrtzidisda313592012-09-27 01:42:07 +0000137 CharSourceRange FilenameRange,
Peter Collingbournebb527862011-07-12 19:35:15 +0000138 const FileEntry *File,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000139 StringRef SearchPath,
Argyrios Kyrtzidisf8afcff2012-09-29 01:06:10 +0000140 StringRef RelativePath,
141 const Module *Imported) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000142 if (!File) {
143 if (AddMissingHeaderDeps)
144 AddFilename(FileName);
145 else
146 SeenMissingHeader = true;
147 }
Peter Collingbournebb527862011-07-12 19:35:15 +0000148}
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
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000155/// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
156/// other 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) {
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000159 if (Filename[i] == ' ' || Filename[i] == '#')
Chris Lattner9d506342011-02-17 02:14:49 +0000160 OS << '\\';
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000161 else if (Filename[i] == '$') // $ is escaped by $$.
162 OS << '$';
Chris Lattner9d506342011-02-17 02:14:49 +0000163 OS << Filename[i];
164 }
165}
166
Daniel Dunbar750c3582008-10-24 22:12:41 +0000167void DependencyFileCallback::OutputDependencyFile() {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000168 if (SeenMissingHeader) {
169 llvm::sys::Path(OutputFile).eraseFromDisk();
170 return;
171 }
172
173 std::string Err;
174 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err);
175 if (!Err.empty()) {
176 PP->getDiagnostics().Report(diag::err_fe_error_opening)
177 << OutputFile << Err;
178 return;
179 }
180
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000181 // Write out the dependency targets, trying to avoid overly long
182 // lines when possible. We try our best to emit exactly the same
183 // dependency file as GCC (4.2), assuming the included files are the
184 // same.
185 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000186 unsigned Columns = 0;
187
188 for (std::vector<std::string>::iterator
189 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
190 unsigned N = I->length();
191 if (Columns == 0) {
192 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000193 } else if (Columns + N + 2 > MaxColumns) {
194 Columns = N + 2;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000195 OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000196 } else {
197 Columns += N + 1;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000198 OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000199 }
Chris Lattner9d506342011-02-17 02:14:49 +0000200 // Targets already quoted as needed.
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000201 OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000202 }
203
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000204 OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000205 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000207 // Now add each dependency in the order it was seen, but avoiding
208 // duplicates.
209 for (std::vector<std::string>::iterator I = Files.begin(),
210 E = Files.end(); I != E; ++I) {
211 // Start a new line if this would exceed the column limit. Make
212 // sure to leave space for a trailing " \" in case we need to
213 // break the line on the next iteration.
214 unsigned N = I->length();
215 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000216 OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000217 Columns = 2;
218 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000219 OS << ' ';
220 PrintFilename(OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000221 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000222 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000223 OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000224
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000225 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000226 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000227 // Skip the first entry, this is always the input file itself.
228 for (std::vector<std::string>::iterator I = Files.begin() + 1,
229 E = Files.end(); I != E; ++I) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000230 OS << '\n';
231 PrintFilename(OS, *I);
232 OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000233 }
234 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000235}
236