blob: 81d1179f28e36b382e0d657e0527216792f39376 [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"
Daniel Dunbar750c3582008-10-24 22:12:41 +000015#include "clang/Basic/SourceManager.h"
Chris Lattnerb9c3f962009-01-27 07:57:44 +000016#include "clang/Basic/FileManager.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Lex/PPCallbacks.h"
19#include "clang/Lex/DirectoryLookup.h"
20#include "clang/Basic/SourceLocation.h"
21#include "llvm/ADT/StringSet.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000022#include "llvm/Support/Compiler.h"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000023#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000024#include <string>
25
26using namespace clang;
27
28namespace {
29class VISIBILITY_HIDDEN 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,
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000045 const std::vector<std::string> &_Targets,
46 bool _IncludeSystemHeaders,
47 bool _PhonyTarget)
48 : PP(_PP), Targets(_Targets), OS(_OS),
49 IncludeSystemHeaders(_IncludeSystemHeaders), PhonyTarget(_PhonyTarget) {}
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 Dunbara5a7bd02009-03-30 00:34:04 +000062
Daniel Dunbar750c3582008-10-24 22:12:41 +000063
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000064void clang::AttachDependencyFileGen(Preprocessor *PP, llvm::raw_ostream *OS,
65 std::vector<std::string> &Targets,
66 bool IncludeSystemHeaders,
67 bool PhonyTarget) {
68 assert(!Targets.empty() && "Target required for dependency generation");
Daniel Dunbar750c3582008-10-24 22:12:41 +000069
Mike Stump1eb44332009-09-09 15:08:12 +000070 DependencyFileCallback *PPDep =
71 new DependencyFileCallback(PP, OS, Targets, IncludeSystemHeaders,
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000072 PhonyTarget);
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000073 PP->setPPCallbacks(PPDep);
Daniel Dunbar750c3582008-10-24 22:12:41 +000074}
75
76/// FileMatchesDepCriteria - Determine whether the given Filename should be
77/// considered as a dependency.
78bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +000079 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000080 if (strcmp("<built-in>", Filename) == 0)
81 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +000082
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +000083 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000084 return true;
85
86 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +000087}
88
89void DependencyFileCallback::FileChanged(SourceLocation Loc,
90 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +000091 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar750c3582008-10-24 22:12:41 +000092 if (Reason != PPCallbacks::EnterFile)
93 return;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Daniel Dunbara5a7bd02009-03-30 00:34:04 +000095 // Dependency generation really does want to go all the way to the
96 // file entry for a source location to find out what is depended on.
97 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +000098 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +000099
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000100 const FileEntry *FE =
101 SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc)));
102 if (FE == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000104 const char *Filename = FE->getName();
Daniel Dunbar750c3582008-10-24 22:12:41 +0000105 if (!FileMatchesDepCriteria(Filename, FileType))
106 return;
107
108 // Remove leading "./"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000109 if (Filename[0] == '.' && Filename[1] == '/')
Daniel Dunbar750c3582008-10-24 22:12:41 +0000110 Filename = &Filename[2];
111
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000112 if (FilesSet.insert(Filename))
113 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000114}
115
116void DependencyFileCallback::OutputDependencyFile() {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000117 // Write out the dependency targets, trying to avoid overly long
118 // lines when possible. We try our best to emit exactly the same
119 // dependency file as GCC (4.2), assuming the included files are the
120 // same.
121 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000122 unsigned Columns = 0;
123
124 for (std::vector<std::string>::iterator
125 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
126 unsigned N = I->length();
127 if (Columns == 0) {
128 Columns += N;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000129 *OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000130 } else if (Columns + N + 2 > MaxColumns) {
131 Columns = N + 2;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000132 *OS << " \\\n " << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000133 } else {
134 Columns += N + 1;
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000135 *OS << ' ' << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000136 }
137 }
138
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000139 *OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000140 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000142 // Now add each dependency in the order it was seen, but avoiding
143 // duplicates.
144 for (std::vector<std::string>::iterator I = Files.begin(),
145 E = Files.end(); I != E; ++I) {
146 // Start a new line if this would exceed the column limit. Make
147 // sure to leave space for a trailing " \" in case we need to
148 // break the line on the next iteration.
149 unsigned N = I->length();
150 if (Columns + (N + 1) + 2 > MaxColumns) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000151 *OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000152 Columns = 2;
153 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000154 *OS << ' ' << *I;
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000155 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000156 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000157 *OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000158
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000159 // Create phony targets if requested.
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000160 if (PhonyTarget) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000161 // Skip the first entry, this is always the input file itself.
162 for (std::vector<std::string>::iterator I = Files.begin() + 1,
163 E = Files.end(); I != E; ++I) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000164 *OS << '\n';
165 *OS << *I << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000166 }
167 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000168}
169