Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1 | //===--- 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 Friedman | b09f6e1 | 2009-05-19 04:14:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/Utils.h" |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceLocation.h" |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSet.h" |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 25 | #include <string> |
| 26 | |
| 27 | using namespace clang; |
| 28 | |
| 29 | namespace { |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame^] | 30 | class DependencyFileCallback : public PPCallbacks { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 31 | std::vector<std::string> Files; |
| 32 | llvm::StringSet<> FilesSet; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 33 | const Preprocessor *PP; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 34 | std::vector<std::string> Targets; |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 35 | llvm::raw_ostream *OS; |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 36 | bool IncludeSystemHeaders; |
| 37 | bool PhonyTarget; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 38 | private: |
| 39 | bool FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 40 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 41 | void OutputDependencyFile(); |
| 42 | |
| 43 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | DependencyFileCallback(const Preprocessor *_PP, |
| 45 | llvm::raw_ostream *_OS, |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 46 | const DependencyOutputOptions &Opts) |
| 47 | : PP(_PP), Targets(Opts.Targets), OS(_OS), |
| 48 | IncludeSystemHeaders(Opts.IncludeSystemHeaders), |
| 49 | PhonyTarget(Opts.UsePhonyTargets) {} |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 50 | |
| 51 | ~DependencyFileCallback() { |
| 52 | OutputDependencyFile(); |
| 53 | OS->flush(); |
| 54 | delete OS; |
| 55 | } |
| 56 | |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 57 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 58 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 59 | }; |
| 60 | } |
| 61 | |
Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 62 | void clang::AttachDependencyFileGen(Preprocessor &PP, |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 63 | const DependencyOutputOptions &Opts) { |
| 64 | if (Opts.Targets.empty()) { |
Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 65 | PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT); |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 66 | return; |
| 67 | } |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 69 | std::string Err; |
| 70 | llvm::raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err)); |
| 71 | if (!Err.empty()) { |
Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 72 | PP.getDiagnostics().Report(diag::err_fe_error_opening) |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 73 | << Opts.OutputFile << Err; |
| 74 | return; |
| 75 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 76 | |
Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 77 | assert(!PP.getPPCallbacks() && "Preprocessor callbacks already registered!"); |
| 78 | PP.setPPCallbacks(new DependencyFileCallback(&PP, OS, Opts)); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /// FileMatchesDepCriteria - Determine whether the given Filename should be |
| 82 | /// considered as a dependency. |
| 83 | bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 84 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 85 | if (strcmp("<built-in>", Filename) == 0) |
| 86 | return false; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 87 | |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 88 | if (IncludeSystemHeaders) |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 89 | return true; |
| 90 | |
| 91 | return FileType == SrcMgr::C_User; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void DependencyFileCallback::FileChanged(SourceLocation Loc, |
| 95 | FileChangeReason Reason, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 96 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 97 | if (Reason != PPCallbacks::EnterFile) |
| 98 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 100 | // Dependency generation really does want to go all the way to the |
| 101 | // file entry for a source location to find out what is depended on. |
| 102 | // We do not want #line markers to affect dependency generation! |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 103 | SourceManager &SM = PP->getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Chris Lattner | e86e4cd0 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 105 | const FileEntry *FE = |
| 106 | SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc))); |
| 107 | if (FE == 0) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Chris Lattner | e86e4cd0 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 109 | const char *Filename = FE->getName(); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 110 | if (!FileMatchesDepCriteria(Filename, FileType)) |
| 111 | return; |
| 112 | |
| 113 | // Remove leading "./" |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 114 | if (Filename[0] == '.' && Filename[1] == '/') |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 115 | Filename = &Filename[2]; |
| 116 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 117 | if (FilesSet.insert(Filename)) |
| 118 | Files.push_back(Filename); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void DependencyFileCallback::OutputDependencyFile() { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 122 | // Write out the dependency targets, trying to avoid overly long |
| 123 | // lines when possible. We try our best to emit exactly the same |
| 124 | // dependency file as GCC (4.2), assuming the included files are the |
| 125 | // same. |
| 126 | const unsigned MaxColumns = 75; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 127 | unsigned Columns = 0; |
| 128 | |
| 129 | for (std::vector<std::string>::iterator |
| 130 | I = Targets.begin(), E = Targets.end(); I != E; ++I) { |
| 131 | unsigned N = I->length(); |
| 132 | if (Columns == 0) { |
| 133 | Columns += N; |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 134 | *OS << *I; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 135 | } else if (Columns + N + 2 > MaxColumns) { |
| 136 | Columns = N + 2; |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 137 | *OS << " \\\n " << *I; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 138 | } else { |
| 139 | Columns += N + 1; |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 140 | *OS << ' ' << *I; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 144 | *OS << ':'; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 145 | Columns += 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 147 | // Now add each dependency in the order it was seen, but avoiding |
| 148 | // duplicates. |
| 149 | for (std::vector<std::string>::iterator I = Files.begin(), |
| 150 | E = Files.end(); I != E; ++I) { |
| 151 | // Start a new line if this would exceed the column limit. Make |
| 152 | // sure to leave space for a trailing " \" in case we need to |
| 153 | // break the line on the next iteration. |
| 154 | unsigned N = I->length(); |
| 155 | if (Columns + (N + 1) + 2 > MaxColumns) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 156 | *OS << " \\\n "; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 157 | Columns = 2; |
| 158 | } |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 159 | *OS << ' ' << *I; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 160 | Columns += N + 1; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 161 | } |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 162 | *OS << '\n'; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 164 | // Create phony targets if requested. |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 165 | if (PhonyTarget) { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 166 | // Skip the first entry, this is always the input file itself. |
| 167 | for (std::vector<std::string>::iterator I = Files.begin() + 1, |
| 168 | E = Files.end(); I != E; ++I) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 169 | *OS << '\n'; |
| 170 | *OS << *I << ":\n"; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 173 | } |
| 174 | |