blob: c7f93595e1ea3704b17714f37d591f586e035d0a [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 Dunbar750c3582008-10-24 22:12:41 +000016#include "clang/Basic/SourceLocation.h"
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Frontend/DependencyOutputOptions.h"
19#include "clang/Frontend/FrontendDiagnostic.h"
20#include "clang/Lex/DirectoryLookup.h"
21#include "clang/Lex/PPCallbacks.h"
22#include "clang/Lex/Preprocessor.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000023#include "llvm/ADT/StringSet.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000024#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 <string>
27
28using namespace clang;
29
30namespace {
31class VISIBILITY_HIDDEN 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;
Chris Lattner02fbb252009-01-11 19:28:34 +000035 std::vector<std::string> Targets;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000036 llvm::raw_ostream *OS;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000037 bool IncludeSystemHeaders;
38 bool PhonyTarget;
Daniel Dunbar750c3582008-10-24 22:12:41 +000039private:
40 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000041 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +000042 void OutputDependencyFile();
43
44public:
Mike Stump1eb44332009-09-09 15:08:12 +000045 DependencyFileCallback(const Preprocessor *_PP,
46 llvm::raw_ostream *_OS,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000047 const DependencyOutputOptions &Opts)
48 : PP(_PP), Targets(Opts.Targets), OS(_OS),
49 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
50 PhonyTarget(Opts.UsePhonyTargets) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000051
52 ~DependencyFileCallback() {
53 OutputDependencyFile();
54 OS->flush();
55 delete OS;
56 }
57
Daniel Dunbar750c3582008-10-24 22:12:41 +000058 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +000059 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +000060};
61}
62
Daniel Dunbarca11f612009-11-11 21:44:00 +000063void clang::AttachDependencyFileGen(Preprocessor &PP,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000064 const DependencyOutputOptions &Opts) {
65 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000066 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000067 return;
68 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000069
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000070 std::string Err;
71 llvm::raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
72 if (!Err.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000073 PP.getDiagnostics().Report(diag::err_fe_error_opening)
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000074 << Opts.OutputFile << Err;
75 return;
76 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000077
Daniel Dunbarca11f612009-11-11 21:44:00 +000078 assert(!PP.getPPCallbacks() && "Preprocessor callbacks already registered!");
79 PP.setPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000080}
81
82/// FileMatchesDepCriteria - Determine whether the given Filename should be
83/// considered as a dependency.
84bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000085 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000086 if (strcmp("<built-in>", Filename) == 0)
87 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +000088
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000089 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000090 return true;
91
92 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +000093}
94
95void DependencyFileCallback::FileChanged(SourceLocation Loc,
96 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +000097 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar750c3582008-10-24 22:12:41 +000098 if (Reason != PPCallbacks::EnterFile)
99 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000101 // Dependency generation really does want to go all the way to the
102 // file entry for a source location to find out what is depended on.
103 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000104 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000106 const FileEntry *FE =
107 SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc)));
108 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000110 const char *Filename = FE->getName();
Daniel Dunbar750c3582008-10-24 22:12:41 +0000111 if (!FileMatchesDepCriteria(Filename, FileType))
112 return;
113
114 // Remove leading "./"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000115 if (Filename[0] == '.' && Filename[1] == '/')
Daniel Dunbar750c3582008-10-24 22:12:41 +0000116 Filename = &Filename[2];
117
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000118 if (FilesSet.insert(Filename))
119 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000120}
121
122void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000123 // Write out the dependency targets, trying to avoid overly long
124 // lines when possible. We try our best to emit exactly the same
125 // dependency file as GCC (4.2), assuming the included files are the
126 // same.
127 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000128 unsigned Columns = 0;
129
130 for (std::vector<std::string>::iterator
131 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
132 unsigned N = I->length();
133 if (Columns == 0) {
134 Columns += N;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000135 *OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000136 } else if (Columns + N + 2 > MaxColumns) {
137 Columns = N + 2;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000138 *OS << " \\\n " << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000139 } else {
140 Columns += N + 1;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000141 *OS << ' ' << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000142 }
143 }
144
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000145 *OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000146 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000148 // Now add each dependency in the order it was seen, but avoiding
149 // duplicates.
150 for (std::vector<std::string>::iterator I = Files.begin(),
151 E = Files.end(); I != E; ++I) {
152 // Start a new line if this would exceed the column limit. Make
153 // sure to leave space for a trailing " \" in case we need to
154 // break the line on the next iteration.
155 unsigned N = I->length();
156 if (Columns + (N + 1) + 2 > MaxColumns) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000157 *OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000158 Columns = 2;
159 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000160 *OS << ' ' << *I;
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000161 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000162 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000163 *OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000164
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000165 // Create phony targets if requested.
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000166 if (PhonyTarget) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000167 // Skip the first entry, this is always the input file itself.
168 for (std::vector<std::string>::iterator I = Files.begin() + 1,
169 E = Files.end(); I != E; ++I) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000170 *OS << '\n';
171 *OS << *I << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000172 }
173 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000174}
175