blob: fc64557e444ac80c627ed0a81e4cedd2b258f559 [file] [log] [blame]
Daniel Dunbar35fe5de2008-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"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Lex/PPCallbacks.h"
18#include "clang/Lex/DirectoryLookup.h"
19#include "clang/Basic/SourceLocation.h"
20#include "llvm/ADT/StringSet.h"
21#include "llvm/System/Path.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Compiler.h"
Daniel Dunbar4cdc4a52008-10-27 20:01:06 +000024#include "llvm/Support/raw_ostream.h"
Daniel Dunbar35fe5de2008-10-24 22:12:41 +000025#include <fstream>
26#include <string>
27
28using namespace clang;
29
30namespace {
31class VISIBILITY_HIDDEN DependencyFileCallback : public PPCallbacks {
Daniel Dunbar4cdc4a52008-10-27 20:01:06 +000032 std::vector<std::string> Files;
33 llvm::StringSet<> FilesSet;
Daniel Dunbar35fe5de2008-10-24 22:12:41 +000034 const Preprocessor *PP;
35 std::ofstream OS;
36 const std::string &InputFile;
37 std::string Target;
38
39private:
40 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner7a4864e2008-10-27 01:19:25 +000041 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar35fe5de2008-10-24 22:12:41 +000042 void OutputDependencyFile();
43
44public:
45 DependencyFileCallback(const Preprocessor *PP,
46 const std::string &InputFile,
47 const std::string &DepFile,
48 const std::string &Target,
49 const char *&ErrStr);
50 ~DependencyFileCallback();
51 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner7a4864e2008-10-27 01:19:25 +000052 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar35fe5de2008-10-24 22:12:41 +000053};
54}
55
56static const char *DependencyFileExt = "d";
57static const char *ObjectFileExt = "o";
58
59//===----------------------------------------------------------------------===//
60// Dependency file options
61//===----------------------------------------------------------------------===//
62static llvm::cl::opt<bool>
63GenerateDependencyFile("MD",
64 llvm::cl::desc("Generate dependency for main source file "
65 "(system headers included)"));
66
67static llvm::cl::opt<bool>
68GenerateDependencyFileNoSysHeaders("MMD",
69 llvm::cl::desc("Generate dependency for main source file "
70 "(no system headers)"));
71
72static llvm::cl::opt<std::string>
73DependencyOutputFile("MF",
74 llvm::cl::desc("Specify dependency output file"));
75
76static llvm::cl::opt<std::string>
77DependencyTarget("MT",
78 llvm::cl::desc("Specify target for dependency"));
79
80// FIXME: Implement feature
81static llvm::cl::opt<bool>
82PhonyDependencyTarget("MP",
83 llvm::cl::desc("Create phony target for each dependency "
84 "(other than main file)"));
85
86bool clang::CreateDependencyFileGen(Preprocessor *PP,
87 std::string &OutputFile,
88 const std::string &InputFile,
89 const char *&ErrStr) {
90 assert(!InputFile.empty() && "No file given");
91
92 ErrStr = NULL;
93
94 if (!GenerateDependencyFile && !GenerateDependencyFileNoSysHeaders) {
95 if (!DependencyOutputFile.empty() || !DependencyTarget.empty() ||
96 PhonyDependencyTarget)
97 ErrStr = "Error: to generate dependencies you must specify -MD or -MMD\n";
98 return false;
99 }
100
101 // Handle conflicting options
102 if (GenerateDependencyFileNoSysHeaders)
103 GenerateDependencyFile = false;
104
105 // Determine name of dependency output filename
106 llvm::sys::Path DepFile;
107 if (!DependencyOutputFile.empty())
108 DepFile = DependencyOutputFile;
109 else if (!OutputFile.empty()) {
110 DepFile = OutputFile;
111 DepFile.eraseSuffix();
112 DepFile.appendSuffix(DependencyFileExt);
113 }
114 else {
115 DepFile = InputFile;
116 DepFile.eraseSuffix();
117 DepFile.appendSuffix(DependencyFileExt);
118 }
119
120 // Determine name of target
121 std::string Target;
122 if (!DependencyTarget.empty())
123 Target = DependencyTarget;
124 else if (!OutputFile.empty()) {
125 llvm::sys::Path TargetPath(OutputFile);
126 TargetPath.eraseSuffix();
127 TargetPath.appendSuffix(ObjectFileExt);
128 Target = TargetPath.toString();
129 }
130 else {
131 llvm::sys::Path TargetPath(InputFile);
132 TargetPath.eraseSuffix();
133 TargetPath.appendSuffix(ObjectFileExt);
134 Target = TargetPath.toString();
135 }
136
137 DependencyFileCallback *PPDep =
138 new DependencyFileCallback(PP, InputFile, DepFile.toString(),
139 Target, ErrStr);
140 if (ErrStr){
141 delete PPDep;
142 return false;
143 }
144 else {
145 PP->setPPCallbacks(PPDep);
146 return true;
147 }
148}
149
150/// FileMatchesDepCriteria - Determine whether the given Filename should be
151/// considered as a dependency.
152bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner7a4864e2008-10-27 01:19:25 +0000153 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar35fe5de2008-10-24 22:12:41 +0000154 if (strcmp(InputFile.c_str(), Filename) != 0 &&
155 strcmp("<predefines>", Filename) != 0) {
156 if (GenerateDependencyFileNoSysHeaders)
157 return FileType == SrcMgr::C_User;
158 else
159 return true;
160 }
161
162 return false;
163}
164
165void DependencyFileCallback::FileChanged(SourceLocation Loc,
166 FileChangeReason Reason,
Chris Lattner7a4864e2008-10-27 01:19:25 +0000167 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar35fe5de2008-10-24 22:12:41 +0000168 if (Reason != PPCallbacks::EnterFile)
169 return;
170
171 const char *Filename = PP->getSourceManager().getSourceName(Loc);
172 if (!FileMatchesDepCriteria(Filename, FileType))
173 return;
174
175 // Remove leading "./"
Daniel Dunbar4cdc4a52008-10-27 20:01:06 +0000176 if (Filename[0] == '.' && Filename[1] == '/')
Daniel Dunbar35fe5de2008-10-24 22:12:41 +0000177 Filename = &Filename[2];
178
Daniel Dunbar4cdc4a52008-10-27 20:01:06 +0000179 if (FilesSet.insert(Filename))
180 Files.push_back(Filename);
Daniel Dunbar35fe5de2008-10-24 22:12:41 +0000181}
182
183void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar4cdc4a52008-10-27 20:01:06 +0000184 // Write out the dependency targets, trying to avoid overly long
185 // lines when possible. We try our best to emit exactly the same
186 // dependency file as GCC (4.2), assuming the included files are the
187 // same.
188 const unsigned MaxColumns = 75;
189
190 OS << Target << ":";
191 unsigned Columns = Target.length() + 1;
192
193 // Now add each dependency in the order it was seen, but avoiding
194 // duplicates.
195 for (std::vector<std::string>::iterator I = Files.begin(),
196 E = Files.end(); I != E; ++I) {
197 // Start a new line if this would exceed the column limit. Make
198 // sure to leave space for a trailing " \" in case we need to
199 // break the line on the next iteration.
200 unsigned N = I->length();
201 if (Columns + (N + 1) + 2 > MaxColumns) {
202 OS << " \\\n ";
203 Columns = 2;
204 }
205 OS << " " << *I;
206 Columns += N + 1;
Daniel Dunbar35fe5de2008-10-24 22:12:41 +0000207 }
Daniel Dunbar4cdc4a52008-10-27 20:01:06 +0000208 OS << "\n";
Daniel Dunbar35fe5de2008-10-24 22:12:41 +0000209
Daniel Dunbar4cdc4a52008-10-27 20:01:06 +0000210 // Create phony targets if requested.
211 if (PhonyDependencyTarget) {
212 // 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) {
215 OS << "\n";
216 OS << *I << ":\n";
217 }
218 }
Daniel Dunbar35fe5de2008-10-24 22:12:41 +0000219}
220
221DependencyFileCallback::DependencyFileCallback(const Preprocessor *PP,
222 const std::string &InputFile,
223 const std::string &DepFile,
224 const std::string &Target,
225 const char *&ErrStr)
226 : PP(PP), InputFile(InputFile), Target(Target) {
227
228 OS.open(DepFile.c_str());
229 if (OS.fail())
230 ErrStr = "Could not open dependency output file\n";
231 else
232 ErrStr = NULL;
Daniel Dunbar4cdc4a52008-10-27 20:01:06 +0000233
234 Files.push_back(InputFile);
Daniel Dunbar35fe5de2008-10-24 22:12:41 +0000235}
236
237DependencyFileCallback::~DependencyFileCallback() {
238 if ((!GenerateDependencyFile && !GenerateDependencyFileNoSysHeaders) ||
239 OS.fail())
240 return;
241
242 OutputDependencyFile();
243 OS.close();
244}
245