1 //===--- DependencyFile.cpp - Generate dependency file --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This code generates dependency files.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/Utils.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/SourceLocation.h"
17 #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"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/raw_ostream.h"
28 using namespace clang;
31 class VISIBILITY_HIDDEN DependencyFileCallback : public PPCallbacks {
32 std::vector<std::string> Files;
33 llvm::StringSet<> FilesSet;
34 const Preprocessor *PP;
35 std::vector<std::string> Targets;
36 llvm::raw_ostream *OS;
37 bool IncludeSystemHeaders;
40 bool FileMatchesDepCriteria(const char *Filename,
41 SrcMgr::CharacteristicKind FileType);
42 void OutputDependencyFile();
45 DependencyFileCallback(const Preprocessor *_PP,
46 llvm::raw_ostream *_OS,
47 const DependencyOutputOptions &Opts)
48 : PP(_PP), Targets(Opts.Targets), OS(_OS),
49 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
50 PhonyTarget(Opts.UsePhonyTargets) {}
52 ~DependencyFileCallback() {
53 OutputDependencyFile();
58 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
59 SrcMgr::CharacteristicKind FileType);
63 void clang::AttachDependencyFileGen(Preprocessor &PP,
64 const DependencyOutputOptions &Opts) {
65 if (Opts.Targets.empty()) {
66 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
71 llvm::raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
73 PP.getDiagnostics().Report(diag::err_fe_error_opening)
74 << Opts.OutputFile << Err;
78 assert(!PP.getPPCallbacks() && "Preprocessor callbacks already registered!");
79 PP.setPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
82 /// FileMatchesDepCriteria - Determine whether the given Filename should be
83 /// considered as a dependency.
84 bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
85 SrcMgr::CharacteristicKind FileType) {
86 if (strcmp("<built-in>", Filename) == 0)
89 if (IncludeSystemHeaders)
92 return FileType == SrcMgr::C_User;
95 void DependencyFileCallback::FileChanged(SourceLocation Loc,
96 FileChangeReason Reason,
97 SrcMgr::CharacteristicKind FileType) {
98 if (Reason != PPCallbacks::EnterFile)
101 // 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!
104 SourceManager &SM = PP->getSourceManager();
106 const FileEntry *FE =
107 SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc)));
110 const char *Filename = FE->getName();
111 if (!FileMatchesDepCriteria(Filename, FileType))
114 // Remove leading "./"
115 if (Filename[0] == '.' && Filename[1] == '/')
116 Filename = &Filename[2];
118 if (FilesSet.insert(Filename))
119 Files.push_back(Filename);
122 void DependencyFileCallback::OutputDependencyFile() {
123 // 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
127 const unsigned MaxColumns = 75;
128 unsigned Columns = 0;
130 for (std::vector<std::string>::iterator
131 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
132 unsigned N = I->length();
136 } else if (Columns + N + 2 > MaxColumns) {
138 *OS << " \\\n " << *I;
148 // Now add each dependency in the order it was seen, but avoiding
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) {
165 // Create phony targets if requested.
167 // 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) {