| Daniel Dunbar | 35fe5de | 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 | |
| Ted Kremenek | 377a319 | 2009-03-31 18:58:14 +0000 | [diff] [blame] | 14 | #include "clang-cc.h" |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
| Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 17 | #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" |
| 22 | #include "llvm/System/Path.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Compiler.h" |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 26 | #include <string> |
| 27 | |
| 28 | using namespace clang; |
| 29 | |
| 30 | namespace { |
| 31 | class VISIBILITY_HIDDEN DependencyFileCallback : public PPCallbacks { |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 32 | std::vector<std::string> Files; |
| 33 | llvm::StringSet<> FilesSet; |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 34 | const Preprocessor *PP; |
| Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 35 | std::vector<std::string> Targets; |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 36 | llvm::raw_ostream *OS; |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 37 | |
| Daniel Dunbar | ebfb1bc | 2009-05-03 09:35:25 +0000 | [diff] [blame^] | 38 | // FIXME: This functionality should be moved into a common class for |
| 39 | // chaining callbacks. |
| 40 | PPCallbacks *PrevCallbacks; |
| 41 | |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 42 | private: |
| 43 | bool FileMatchesDepCriteria(const char *Filename, |
| Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 44 | SrcMgr::CharacteristicKind FileType); |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 45 | void OutputDependencyFile(); |
| 46 | |
| 47 | public: |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 48 | DependencyFileCallback(const Preprocessor *_PP, |
| 49 | llvm::raw_ostream *_OS, |
| Daniel Dunbar | ebfb1bc | 2009-05-03 09:35:25 +0000 | [diff] [blame^] | 50 | const std::vector<std::string> &_Targets, |
| 51 | PPCallbacks *_PrevCallbacks) |
| 52 | : PP(_PP), Targets(_Targets), OS(_OS), PrevCallbacks(_PrevCallbacks) { |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ~DependencyFileCallback() { |
| Daniel Dunbar | ebfb1bc | 2009-05-03 09:35:25 +0000 | [diff] [blame^] | 56 | if (PrevCallbacks) |
| 57 | delete PrevCallbacks; |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 58 | OutputDependencyFile(); |
| 59 | OS->flush(); |
| 60 | delete OS; |
| 61 | } |
| 62 | |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 63 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 64 | SrcMgr::CharacteristicKind FileType); |
| Daniel Dunbar | ebfb1bc | 2009-05-03 09:35:25 +0000 | [diff] [blame^] | 65 | |
| 66 | virtual void Ident(SourceLocation Loc, const std::string &str) { |
| 67 | if (PrevCallbacks) |
| 68 | PrevCallbacks->Ident(Loc, str); |
| 69 | } |
| 70 | |
| 71 | virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, |
| 72 | const std::string &Str) { |
| 73 | if (PrevCallbacks) |
| 74 | PrevCallbacks->PragmaComment(Loc, Kind, Str); |
| 75 | } |
| 76 | |
| 77 | virtual void MacroExpands(const Token &Id, const MacroInfo* MI) { |
| 78 | if (PrevCallbacks) |
| 79 | PrevCallbacks->MacroExpands(Id, MI); |
| 80 | } |
| 81 | |
| 82 | virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) { |
| 83 | if (PrevCallbacks) |
| 84 | PrevCallbacks->MacroDefined(II, MI); |
| 85 | } |
| 86 | |
| 87 | virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) { |
| 88 | if (PrevCallbacks) |
| 89 | PrevCallbacks->MacroUndefined(II, MI); |
| 90 | } |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 91 | }; |
| 92 | } |
| 93 | |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 94 | //===----------------------------------------------------------------------===// |
| 95 | // Dependency file options |
| 96 | //===----------------------------------------------------------------------===// |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 97 | static llvm::cl::opt<std::string> |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 98 | DependencyFile("dependency-file", |
| 99 | llvm::cl::desc("Filename (or -) to write dependency output to")); |
| 100 | |
| 101 | static llvm::cl::opt<bool> |
| 102 | DependenciesIncludeSystemHeaders("sys-header-deps", |
| 103 | llvm::cl::desc("Include system headers in dependency output")); |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 104 | |
| Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 105 | static llvm::cl::list<std::string> |
| 106 | DependencyTargets("MT", |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 107 | llvm::cl::desc("Specify target for dependency")); |
| 108 | |
| 109 | // FIXME: Implement feature |
| 110 | static llvm::cl::opt<bool> |
| 111 | PhonyDependencyTarget("MP", |
| 112 | llvm::cl::desc("Create phony target for each dependency " |
| 113 | "(other than main file)")); |
| 114 | |
| 115 | bool clang::CreateDependencyFileGen(Preprocessor *PP, |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 116 | std::string &ErrStr) { |
| 117 | ErrStr = ""; |
| 118 | if (DependencyFile.empty()) |
| 119 | return false; |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 120 | |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 121 | if (DependencyTargets.empty()) { |
| 122 | ErrStr = "-dependency-file requires at least one -MT option\n"; |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 123 | return false; |
| 124 | } |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 125 | |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 126 | std::string ErrMsg; |
| Daniel Dunbar | 049b724 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 127 | llvm::raw_ostream *OS; |
| 128 | if (DependencyFile == "-") { |
| 129 | OS = new llvm::raw_stdout_ostream(); |
| 130 | } else { |
| 131 | OS = new llvm::raw_fd_ostream(DependencyFile.c_str(), false, ErrStr); |
| 132 | if (!ErrMsg.empty()) { |
| 133 | ErrStr = "unable to open dependency file: " + ErrMsg; |
| 134 | return false; |
| 135 | } |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| Daniel Dunbar | ebfb1bc | 2009-05-03 09:35:25 +0000 | [diff] [blame^] | 138 | // Claim any previous callbacks. |
| 139 | PPCallbacks *Prev = PP->getPPCallbacks(); |
| 140 | if (Prev) |
| 141 | PP->setPPCallbacks(0); |
| 142 | |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 143 | DependencyFileCallback *PPDep = |
| Daniel Dunbar | ebfb1bc | 2009-05-03 09:35:25 +0000 | [diff] [blame^] | 144 | new DependencyFileCallback(PP, OS, DependencyTargets, Prev); |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 145 | PP->setPPCallbacks(PPDep); |
| 146 | return true; |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /// FileMatchesDepCriteria - Determine whether the given Filename should be |
| 150 | /// considered as a dependency. |
| 151 | bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename, |
| Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 152 | SrcMgr::CharacteristicKind FileType) { |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 153 | if (strcmp("<built-in>", Filename) == 0) |
| 154 | return false; |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 155 | |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 156 | if (DependenciesIncludeSystemHeaders) |
| 157 | return true; |
| 158 | |
| 159 | return FileType == SrcMgr::C_User; |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void DependencyFileCallback::FileChanged(SourceLocation Loc, |
| 163 | FileChangeReason Reason, |
| Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 164 | SrcMgr::CharacteristicKind FileType) { |
| Daniel Dunbar | ebfb1bc | 2009-05-03 09:35:25 +0000 | [diff] [blame^] | 165 | if (PrevCallbacks) |
| 166 | PrevCallbacks->FileChanged(Loc, Reason, FileType); |
| 167 | |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 168 | if (Reason != PPCallbacks::EnterFile) |
| 169 | return; |
| Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 170 | |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 171 | // Dependency generation really does want to go all the way to the |
| 172 | // file entry for a source location to find out what is depended on. |
| 173 | // We do not want #line markers to affect dependency generation! |
| Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 174 | SourceManager &SM = PP->getSourceManager(); |
| 175 | |
| Chris Lattner | e88c195 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 176 | const FileEntry *FE = |
| 177 | SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc))); |
| 178 | if (FE == 0) return; |
| 179 | |
| 180 | const char *Filename = FE->getName(); |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 181 | if (!FileMatchesDepCriteria(Filename, FileType)) |
| 182 | return; |
| 183 | |
| 184 | // Remove leading "./" |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 185 | if (Filename[0] == '.' && Filename[1] == '/') |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 186 | Filename = &Filename[2]; |
| 187 | |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 188 | if (FilesSet.insert(Filename)) |
| 189 | Files.push_back(Filename); |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void DependencyFileCallback::OutputDependencyFile() { |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 193 | // Write out the dependency targets, trying to avoid overly long |
| 194 | // lines when possible. We try our best to emit exactly the same |
| 195 | // dependency file as GCC (4.2), assuming the included files are the |
| 196 | // same. |
| 197 | const unsigned MaxColumns = 75; |
| Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 198 | unsigned Columns = 0; |
| 199 | |
| 200 | for (std::vector<std::string>::iterator |
| 201 | I = Targets.begin(), E = Targets.end(); I != E; ++I) { |
| 202 | unsigned N = I->length(); |
| 203 | if (Columns == 0) { |
| 204 | Columns += N; |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 205 | *OS << *I; |
| Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 206 | } else if (Columns + N + 2 > MaxColumns) { |
| 207 | Columns = N + 2; |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 208 | *OS << " \\\n " << *I; |
| Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 209 | } else { |
| 210 | Columns += N + 1; |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 211 | *OS << ' ' << *I; |
| Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 215 | *OS << ':'; |
| Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 216 | Columns += 1; |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 217 | |
| 218 | // Now add each dependency in the order it was seen, but avoiding |
| 219 | // duplicates. |
| 220 | for (std::vector<std::string>::iterator I = Files.begin(), |
| 221 | E = Files.end(); I != E; ++I) { |
| 222 | // Start a new line if this would exceed the column limit. Make |
| 223 | // sure to leave space for a trailing " \" in case we need to |
| 224 | // break the line on the next iteration. |
| 225 | unsigned N = I->length(); |
| 226 | if (Columns + (N + 1) + 2 > MaxColumns) { |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 227 | *OS << " \\\n "; |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 228 | Columns = 2; |
| 229 | } |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 230 | *OS << ' ' << *I; |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 231 | Columns += N + 1; |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 232 | } |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 233 | *OS << '\n'; |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 234 | |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 235 | // Create phony targets if requested. |
| 236 | if (PhonyDependencyTarget) { |
| 237 | // Skip the first entry, this is always the input file itself. |
| 238 | for (std::vector<std::string>::iterator I = Files.begin() + 1, |
| 239 | E = Files.end(); I != E; ++I) { |
| Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 240 | *OS << '\n'; |
| 241 | *OS << *I << ":\n"; |
| Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 244 | } |
| 245 | |