blob: dde3454aba6390f8880f1b46fa86171a62100149 [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"
20#include "clang/Lex/PPCallbacks.h"
21#include "clang/Lex/Preprocessor.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000022#include "llvm/ADT/StringSet.h"
Eli Friedmana6e023c2011-07-08 20:17:28 +000023#include "llvm/Support/Path.h"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000024#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000025
26using namespace clang;
27
28namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000029class DependencyFileCallback : public PPCallbacks {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000030 std::vector<std::string> Files;
31 llvm::StringSet<> FilesSet;
Daniel Dunbar750c3582008-10-24 22:12:41 +000032 const Preprocessor *PP;
Chris Lattner02fbb252009-01-11 19:28:34 +000033 std::vector<std::string> Targets;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000034 llvm::raw_ostream *OS;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000035 bool IncludeSystemHeaders;
36 bool PhonyTarget;
Daniel Dunbar750c3582008-10-24 22:12:41 +000037private:
38 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000039 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +000040 void OutputDependencyFile();
41
42public:
Mike Stump1eb44332009-09-09 15:08:12 +000043 DependencyFileCallback(const Preprocessor *_PP,
44 llvm::raw_ostream *_OS,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000045 const DependencyOutputOptions &Opts)
46 : PP(_PP), Targets(Opts.Targets), OS(_OS),
47 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
48 PhonyTarget(Opts.UsePhonyTargets) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000049
Daniel Dunbardbd82092010-03-23 05:09:10 +000050 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
51 SrcMgr::CharacteristicKind FileType);
52
53 virtual void EndOfMainFile() {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000054 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000055 delete OS;
Daniel Dunbardbd82092010-03-23 05:09:10 +000056 OS = 0;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000057 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000058};
59}
60
Daniel Dunbarca11f612009-11-11 21:44:00 +000061void clang::AttachDependencyFileGen(Preprocessor &PP,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000062 const DependencyOutputOptions &Opts) {
63 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000064 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000065 return;
66 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000067
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000068 std::string Err;
69 llvm::raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
70 if (!Err.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000071 PP.getDiagnostics().Report(diag::err_fe_error_opening)
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000072 << Opts.OutputFile << Err;
73 return;
74 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000075
Kovarththanan Rajaratnama5d10c42010-03-07 07:30:06 +000076 PP.addPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000077}
78
79/// FileMatchesDepCriteria - Determine whether the given Filename should be
80/// considered as a dependency.
81bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000082 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000083 if (strcmp("<built-in>", Filename) == 0)
84 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +000085
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000086 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000087 return true;
88
89 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +000090}
91
92void DependencyFileCallback::FileChanged(SourceLocation Loc,
93 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +000094 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar750c3582008-10-24 22:12:41 +000095 if (Reason != PPCallbacks::EnterFile)
96 return;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000098 // Dependency generation really does want to go all the way to the
99 // file entry for a source location to find out what is depended on.
100 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000101 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000103 const FileEntry *FE =
104 SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc)));
105 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Eli Friedmana6e023c2011-07-08 20:17:28 +0000107 llvm::StringRef Filename = FE->getName();
108 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000109 return;
110
Eli Friedmana6e023c2011-07-08 20:17:28 +0000111 // Remove leading "./" (or ".//" or "././" etc.)
112 while (Filename.size() > 2 && Filename[0] == '.' &&
113 llvm::sys::path::is_separator(Filename[1])) {
114 Filename = Filename.substr(1);
115 while (llvm::sys::path::is_separator(Filename[0]))
116 Filename = Filename.substr(1);
117 }
118
Daniel Dunbar750c3582008-10-24 22:12:41 +0000119
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000120 if (FilesSet.insert(Filename))
121 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000122}
123
Chris Lattner9d506342011-02-17 02:14:49 +0000124/// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
125/// scary characters.
126static void PrintFilename(llvm::raw_ostream &OS, llvm::StringRef Filename) {
127 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
128 if (Filename[i] == ' ')
129 OS << '\\';
130 OS << Filename[i];
131 }
132}
133
Daniel Dunbar750c3582008-10-24 22:12:41 +0000134void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000135 // Write out the dependency targets, trying to avoid overly long
136 // lines when possible. We try our best to emit exactly the same
137 // dependency file as GCC (4.2), assuming the included files are the
138 // same.
139 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000140 unsigned Columns = 0;
141
142 for (std::vector<std::string>::iterator
143 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
144 unsigned N = I->length();
145 if (Columns == 0) {
146 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000147 } else if (Columns + N + 2 > MaxColumns) {
148 Columns = N + 2;
Chris Lattner9d506342011-02-17 02:14:49 +0000149 *OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000150 } else {
151 Columns += N + 1;
Chris Lattner9d506342011-02-17 02:14:49 +0000152 *OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000153 }
Chris Lattner9d506342011-02-17 02:14:49 +0000154 // Targets already quoted as needed.
155 *OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000156 }
157
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000158 *OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000159 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000161 // Now add each dependency in the order it was seen, but avoiding
162 // duplicates.
163 for (std::vector<std::string>::iterator I = Files.begin(),
164 E = Files.end(); I != E; ++I) {
165 // Start a new line if this would exceed the column limit. Make
166 // sure to leave space for a trailing " \" in case we need to
167 // break the line on the next iteration.
168 unsigned N = I->length();
169 if (Columns + (N + 1) + 2 > MaxColumns) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000170 *OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000171 Columns = 2;
172 }
Chris Lattner9d506342011-02-17 02:14:49 +0000173 *OS << ' ';
174 PrintFilename(*OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000175 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000176 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000177 *OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000178
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000179 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000180 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000181 // Skip the first entry, this is always the input file itself.
182 for (std::vector<std::string>::iterator I = Files.begin() + 1,
183 E = Files.end(); I != E; ++I) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000184 *OS << '\n';
Chris Lattner9d506342011-02-17 02:14:49 +0000185 PrintFilename(*OS, *I);
186 *OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000187 }
188 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000189}
190