blob: 9487b1a06d1d6aad4e127a338ab9c8ef62f69c83 [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 Dunbar7e9f1f72008-10-27 20:01:06 +000024#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000025#include <string>
26
27using namespace clang;
28
29namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000030class DependencyFileCallback : public PPCallbacks {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000031 std::vector<std::string> Files;
32 llvm::StringSet<> FilesSet;
Daniel Dunbar750c3582008-10-24 22:12:41 +000033 const Preprocessor *PP;
Chris Lattner02fbb252009-01-11 19:28:34 +000034 std::vector<std::string> Targets;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000035 llvm::raw_ostream *OS;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000036 bool IncludeSystemHeaders;
37 bool PhonyTarget;
Daniel Dunbar750c3582008-10-24 22:12:41 +000038private:
39 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000040 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +000041 void OutputDependencyFile();
42
43public:
Mike Stump1eb44332009-09-09 15:08:12 +000044 DependencyFileCallback(const Preprocessor *_PP,
45 llvm::raw_ostream *_OS,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000046 const DependencyOutputOptions &Opts)
47 : PP(_PP), Targets(Opts.Targets), OS(_OS),
48 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
49 PhonyTarget(Opts.UsePhonyTargets) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000050
51 ~DependencyFileCallback() {
52 OutputDependencyFile();
53 OS->flush();
54 delete OS;
55 }
56
Daniel Dunbar750c3582008-10-24 22:12:41 +000057 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +000058 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +000059};
60}
61
Daniel Dunbarca11f612009-11-11 21:44:00 +000062void clang::AttachDependencyFileGen(Preprocessor &PP,
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000063 const DependencyOutputOptions &Opts) {
64 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000065 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000066 return;
67 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000068
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000069 std::string Err;
70 llvm::raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
71 if (!Err.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +000072 PP.getDiagnostics().Report(diag::err_fe_error_opening)
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000073 << Opts.OutputFile << Err;
74 return;
75 }
Daniel Dunbar750c3582008-10-24 22:12:41 +000076
Daniel Dunbarca11f612009-11-11 21:44:00 +000077 PP.setPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
Daniel Dunbar750c3582008-10-24 22:12:41 +000078}
79
80/// FileMatchesDepCriteria - Determine whether the given Filename should be
81/// considered as a dependency.
82bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000083 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000084 if (strcmp("<built-in>", Filename) == 0)
85 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +000086
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000087 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000088 return true;
89
90 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +000091}
92
93void DependencyFileCallback::FileChanged(SourceLocation Loc,
94 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +000095 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar750c3582008-10-24 22:12:41 +000096 if (Reason != PPCallbacks::EnterFile)
97 return;
Mike Stump1eb44332009-09-09 15:08:12 +000098
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000099 // Dependency generation really does want to go all the way to the
100 // file entry for a source location to find out what is depended on.
101 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000102 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000104 const FileEntry *FE =
105 SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc)));
106 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000108 const char *Filename = FE->getName();
Daniel Dunbar750c3582008-10-24 22:12:41 +0000109 if (!FileMatchesDepCriteria(Filename, FileType))
110 return;
111
112 // Remove leading "./"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000113 if (Filename[0] == '.' && Filename[1] == '/')
Daniel Dunbar750c3582008-10-24 22:12:41 +0000114 Filename = &Filename[2];
115
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000116 if (FilesSet.insert(Filename))
117 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000118}
119
120void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000121 // Write out the dependency targets, trying to avoid overly long
122 // lines when possible. We try our best to emit exactly the same
123 // dependency file as GCC (4.2), assuming the included files are the
124 // same.
125 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000126 unsigned Columns = 0;
127
128 for (std::vector<std::string>::iterator
129 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
130 unsigned N = I->length();
131 if (Columns == 0) {
132 Columns += N;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000133 *OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000134 } else if (Columns + N + 2 > MaxColumns) {
135 Columns = N + 2;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000136 *OS << " \\\n " << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000137 } else {
138 Columns += N + 1;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000139 *OS << ' ' << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000140 }
141 }
142
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000143 *OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000144 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000146 // Now add each dependency in the order it was seen, but avoiding
147 // duplicates.
148 for (std::vector<std::string>::iterator I = Files.begin(),
149 E = Files.end(); I != E; ++I) {
150 // Start a new line if this would exceed the column limit. Make
151 // sure to leave space for a trailing " \" in case we need to
152 // break the line on the next iteration.
153 unsigned N = I->length();
154 if (Columns + (N + 1) + 2 > MaxColumns) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000155 *OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000156 Columns = 2;
157 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000158 *OS << ' ' << *I;
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000159 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000160 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000161 *OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000162
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000163 // Create phony targets if requested.
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000164 if (PhonyTarget) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000165 // Skip the first entry, this is always the input file itself.
166 for (std::vector<std::string>::iterator I = Files.begin() + 1,
167 E = Files.end(); I != E; ++I) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000168 *OS << '\n';
169 *OS << *I << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000170 }
171 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000172}
173