blob: 0d4ee91f01bb436569d1ac76f5cbd03c7bf6eff4 [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
14#include "clang.h"
15#include "clang/Basic/SourceManager.h"
Chris Lattnerb9c3f962009-01-27 07:57:44 +000016#include "clang/Basic/FileManager.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Lex/PPCallbacks.h"
19#include "clang/Lex/DirectoryLookup.h"
20#include "clang/Basic/SourceLocation.h"
21#include "llvm/ADT/StringSet.h"
22#include "llvm/System/Path.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/Compiler.h"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000026#include <fstream>
27#include <string>
28
29using namespace clang;
30
31namespace {
32class VISIBILITY_HIDDEN DependencyFileCallback : public PPCallbacks {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000033 std::vector<std::string> Files;
34 llvm::StringSet<> FilesSet;
Daniel Dunbar750c3582008-10-24 22:12:41 +000035 const Preprocessor *PP;
36 std::ofstream OS;
37 const std::string &InputFile;
Chris Lattner02fbb252009-01-11 19:28:34 +000038 std::vector<std::string> Targets;
Daniel Dunbar750c3582008-10-24 22:12:41 +000039
40private:
41 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000042 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +000043 void OutputDependencyFile();
44
45public:
46 DependencyFileCallback(const Preprocessor *PP,
47 const std::string &InputFile,
48 const std::string &DepFile,
Chris Lattner02fbb252009-01-11 19:28:34 +000049 const std::vector<std::string> &Targets,
Daniel Dunbar750c3582008-10-24 22:12:41 +000050 const char *&ErrStr);
51 ~DependencyFileCallback();
52 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +000053 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +000054};
55}
56
57static const char *DependencyFileExt = "d";
58static const char *ObjectFileExt = "o";
59
60//===----------------------------------------------------------------------===//
61// Dependency file options
62//===----------------------------------------------------------------------===//
63static llvm::cl::opt<bool>
64GenerateDependencyFile("MD",
65 llvm::cl::desc("Generate dependency for main source file "
66 "(system headers included)"));
67
68static llvm::cl::opt<bool>
69GenerateDependencyFileNoSysHeaders("MMD",
70 llvm::cl::desc("Generate dependency for main source file "
71 "(no system headers)"));
72
73static llvm::cl::opt<std::string>
74DependencyOutputFile("MF",
75 llvm::cl::desc("Specify dependency output file"));
76
Chris Lattner02fbb252009-01-11 19:28:34 +000077static llvm::cl::list<std::string>
78DependencyTargets("MT",
Daniel Dunbar750c3582008-10-24 22:12:41 +000079 llvm::cl::desc("Specify target for dependency"));
80
81// FIXME: Implement feature
82static llvm::cl::opt<bool>
83PhonyDependencyTarget("MP",
84 llvm::cl::desc("Create phony target for each dependency "
85 "(other than main file)"));
86
87bool clang::CreateDependencyFileGen(Preprocessor *PP,
88 std::string &OutputFile,
89 const std::string &InputFile,
90 const char *&ErrStr) {
91 assert(!InputFile.empty() && "No file given");
92
93 ErrStr = NULL;
94
95 if (!GenerateDependencyFile && !GenerateDependencyFileNoSysHeaders) {
Chris Lattner02fbb252009-01-11 19:28:34 +000096 if (!DependencyOutputFile.empty() || !DependencyTargets.empty() ||
Daniel Dunbar750c3582008-10-24 22:12:41 +000097 PhonyDependencyTarget)
98 ErrStr = "Error: to generate dependencies you must specify -MD or -MMD\n";
99 return false;
100 }
101
102 // Handle conflicting options
103 if (GenerateDependencyFileNoSysHeaders)
104 GenerateDependencyFile = false;
105
106 // Determine name of dependency output filename
107 llvm::sys::Path DepFile;
108 if (!DependencyOutputFile.empty())
109 DepFile = DependencyOutputFile;
110 else if (!OutputFile.empty()) {
111 DepFile = OutputFile;
112 DepFile.eraseSuffix();
113 DepFile.appendSuffix(DependencyFileExt);
114 }
115 else {
116 DepFile = InputFile;
117 DepFile.eraseSuffix();
118 DepFile.appendSuffix(DependencyFileExt);
119 }
120
Chris Lattner02fbb252009-01-11 19:28:34 +0000121 std::vector<std::string> Targets(DependencyTargets);
122
123 // Infer target name if unspecified
124 if (Targets.empty()) {
125 if (!OutputFile.empty()) {
126 llvm::sys::Path TargetPath(OutputFile);
127 TargetPath.eraseSuffix();
128 TargetPath.appendSuffix(ObjectFileExt);
129 Targets.push_back(TargetPath.toString());
130 } else {
131 llvm::sys::Path TargetPath(InputFile);
132 TargetPath.eraseSuffix();
133 TargetPath.appendSuffix(ObjectFileExt);
134 Targets.push_back(TargetPath.toString());
135 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000136 }
137
138 DependencyFileCallback *PPDep =
139 new DependencyFileCallback(PP, InputFile, DepFile.toString(),
Chris Lattner02fbb252009-01-11 19:28:34 +0000140 Targets, ErrStr);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000141 if (ErrStr){
142 delete PPDep;
143 return false;
144 }
145 else {
146 PP->setPPCallbacks(PPDep);
147 return true;
148 }
149}
150
151/// FileMatchesDepCriteria - Determine whether the given Filename should be
152/// considered as a dependency.
153bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +0000154 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000155 if (strcmp(InputFile.c_str(), Filename) != 0 &&
156 strcmp("<predefines>", Filename) != 0) {
157 if (GenerateDependencyFileNoSysHeaders)
158 return FileType == SrcMgr::C_User;
159 else
160 return true;
161 }
162
163 return false;
164}
165
166void DependencyFileCallback::FileChanged(SourceLocation Loc,
167 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000168 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000169 if (Reason != PPCallbacks::EnterFile)
170 return;
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000171
172 // Depedency generation really does want to go all the way to the file entry
173 // for a source location to find out what is depended on. We do not want
174 // #line markers to affect dependency generation!
175 SourceManager &SM = PP->getSourceManager();
176
177 FileID FID = SM.getFileID(SM.getInstantiationLoc(Loc));
178 const char *Filename = SM.getFileEntryForID(FID)->getName();
Daniel Dunbar750c3582008-10-24 22:12:41 +0000179 if (!FileMatchesDepCriteria(Filename, FileType))
180 return;
181
182 // Remove leading "./"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000183 if (Filename[0] == '.' && Filename[1] == '/')
Daniel Dunbar750c3582008-10-24 22:12:41 +0000184 Filename = &Filename[2];
185
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000186 if (FilesSet.insert(Filename))
187 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000188}
189
190void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000191 // Write out the dependency targets, trying to avoid overly long
192 // lines when possible. We try our best to emit exactly the same
193 // dependency file as GCC (4.2), assuming the included files are the
194 // same.
195 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000196 unsigned Columns = 0;
197
198 for (std::vector<std::string>::iterator
199 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
200 unsigned N = I->length();
201 if (Columns == 0) {
202 Columns += N;
203 OS << *I;
204 } else if (Columns + N + 2 > MaxColumns) {
205 Columns = N + 2;
206 OS << " \\\n " << *I;
207 } else {
208 Columns += N + 1;
209 OS << " " << *I;
210 }
211 }
212
213 OS << ":";
214 Columns += 1;
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000215
216 // Now add each dependency in the order it was seen, but avoiding
217 // duplicates.
218 for (std::vector<std::string>::iterator I = Files.begin(),
219 E = Files.end(); I != E; ++I) {
220 // Start a new line if this would exceed the column limit. Make
221 // sure to leave space for a trailing " \" in case we need to
222 // break the line on the next iteration.
223 unsigned N = I->length();
224 if (Columns + (N + 1) + 2 > MaxColumns) {
225 OS << " \\\n ";
226 Columns = 2;
227 }
228 OS << " " << *I;
229 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000230 }
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000231 OS << "\n";
Daniel Dunbar750c3582008-10-24 22:12:41 +0000232
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000233 // Create phony targets if requested.
234 if (PhonyDependencyTarget) {
235 // Skip the first entry, this is always the input file itself.
236 for (std::vector<std::string>::iterator I = Files.begin() + 1,
237 E = Files.end(); I != E; ++I) {
238 OS << "\n";
239 OS << *I << ":\n";
240 }
241 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000242}
243
244DependencyFileCallback::DependencyFileCallback(const Preprocessor *PP,
245 const std::string &InputFile,
246 const std::string &DepFile,
Chris Lattner02fbb252009-01-11 19:28:34 +0000247 const std::vector<std::string>
248 &Targets,
Daniel Dunbar750c3582008-10-24 22:12:41 +0000249 const char *&ErrStr)
Chris Lattner02fbb252009-01-11 19:28:34 +0000250 : PP(PP), InputFile(InputFile), Targets(Targets) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000251
252 OS.open(DepFile.c_str());
253 if (OS.fail())
254 ErrStr = "Could not open dependency output file\n";
255 else
256 ErrStr = NULL;
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000257
258 Files.push_back(InputFile);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000259}
260
261DependencyFileCallback::~DependencyFileCallback() {
262 if ((!GenerateDependencyFile && !GenerateDependencyFileNoSysHeaders) ||
263 OS.fail())
264 return;
265
266 OutputDependencyFile();
267 OS.close();
268}
269