blob: 5c3a23128a147de5cc74e664066c1417ab86be1e [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"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000023#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000024
25using namespace clang;
26
27namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000028class DependencyFileCallback : public PPCallbacks {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000029 std::vector<std::string> Files;
30 llvm::StringSet<> FilesSet;
Daniel Dunbar750c3582008-10-24 22:12:41 +000031 const Preprocessor *PP;
Chris Lattner02fbb252009-01-11 19:28:34 +000032 std::vector<std::string> Targets;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000033 llvm::raw_ostream *OS;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000034 bool IncludeSystemHeaders;
35 bool PhonyTarget;
Daniel Dunbar750c3582008-10-24 22:12:41 +000036private:
37 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000038 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +000039 void OutputDependencyFile();
40
41public:
Mike Stump1eb44332009-09-09 15:08:12 +000042 DependencyFileCallback(const Preprocessor *_PP,
43 llvm::raw_ostream *_OS,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000044 const DependencyOutputOptions &Opts)
45 : PP(_PP), Targets(Opts.Targets), OS(_OS),
46 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
47 PhonyTarget(Opts.UsePhonyTargets) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000048
Daniel Dunbardbd82092010-03-23 05:09:10 +000049 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
50 SrcMgr::CharacteristicKind FileType);
51
52 virtual void EndOfMainFile() {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000053 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000054 delete OS;
Daniel Dunbardbd82092010-03-23 05:09:10 +000055 OS = 0;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000056 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000057};
58}
59
Daniel Dunbarca11f612009-11-11 21:44:00 +000060void clang::AttachDependencyFileGen(Preprocessor &PP,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000061 const DependencyOutputOptions &Opts) {
62 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000063 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000064 return;
65 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000066
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000067 std::string Err;
68 llvm::raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
69 if (!Err.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000070 PP.getDiagnostics().Report(diag::err_fe_error_opening)
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000071 << Opts.OutputFile << Err;
72 return;
73 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000074
Kovarththanan Rajaratnama5d10c42010-03-07 07:30:06 +000075 PP.addPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000076}
77
78/// FileMatchesDepCriteria - Determine whether the given Filename should be
79/// considered as a dependency.
80bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000081 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000082 if (strcmp("<built-in>", Filename) == 0)
83 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +000084
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000085 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000086 return true;
87
88 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +000089}
90
91void DependencyFileCallback::FileChanged(SourceLocation Loc,
92 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +000093 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar750c3582008-10-24 22:12:41 +000094 if (Reason != PPCallbacks::EnterFile)
95 return;
Mike Stump1eb44332009-09-09 15:08:12 +000096
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000097 // Dependency generation really does want to go all the way to the
98 // file entry for a source location to find out what is depended on.
99 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000100 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000102 const FileEntry *FE =
103 SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc)));
104 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000106 const char *Filename = FE->getName();
Daniel Dunbar750c3582008-10-24 22:12:41 +0000107 if (!FileMatchesDepCriteria(Filename, FileType))
108 return;
109
110 // Remove leading "./"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000111 if (Filename[0] == '.' && Filename[1] == '/')
Daniel Dunbar750c3582008-10-24 22:12:41 +0000112 Filename = &Filename[2];
113
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000114 if (FilesSet.insert(Filename))
115 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000116}
117
Chris Lattner9d506342011-02-17 02:14:49 +0000118/// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
119/// scary characters.
120static void PrintFilename(llvm::raw_ostream &OS, llvm::StringRef Filename) {
121 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
122 if (Filename[i] == ' ')
123 OS << '\\';
124 OS << Filename[i];
125 }
126}
127
Daniel Dunbar750c3582008-10-24 22:12:41 +0000128void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000129 // Write out the dependency targets, trying to avoid overly long
130 // lines when possible. We try our best to emit exactly the same
131 // dependency file as GCC (4.2), assuming the included files are the
132 // same.
133 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000134 unsigned Columns = 0;
135
136 for (std::vector<std::string>::iterator
137 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
138 unsigned N = I->length();
139 if (Columns == 0) {
140 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000141 } else if (Columns + N + 2 > MaxColumns) {
142 Columns = N + 2;
Chris Lattner9d506342011-02-17 02:14:49 +0000143 *OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000144 } else {
145 Columns += N + 1;
Chris Lattner9d506342011-02-17 02:14:49 +0000146 *OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000147 }
Chris Lattner9d506342011-02-17 02:14:49 +0000148 // Targets already quoted as needed.
149 *OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000150 }
151
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000152 *OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000153 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000155 // Now add each dependency in the order it was seen, but avoiding
156 // duplicates.
157 for (std::vector<std::string>::iterator I = Files.begin(),
158 E = Files.end(); I != E; ++I) {
159 // Start a new line if this would exceed the column limit. Make
160 // sure to leave space for a trailing " \" in case we need to
161 // break the line on the next iteration.
162 unsigned N = I->length();
163 if (Columns + (N + 1) + 2 > MaxColumns) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000164 *OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000165 Columns = 2;
166 }
Chris Lattner9d506342011-02-17 02:14:49 +0000167 *OS << ' ';
168 PrintFilename(*OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000169 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000170 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000171 *OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000172
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000173 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000174 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000175 // Skip the first entry, this is always the input file itself.
176 for (std::vector<std::string>::iterator I = Files.begin() + 1,
177 E = Files.end(); I != E; ++I) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000178 *OS << '\n';
Chris Lattner9d506342011-02-17 02:14:49 +0000179 PrintFilename(*OS, *I);
180 *OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000181 }
182 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000183}
184