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 | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Frontend/DependencyOutputOptions.h" |
| 18 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 19 | #include "clang/Lex/DirectoryLookup.h" |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LexDiagnostic.h" |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 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" |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 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; |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 34 | std::string OutputFile; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 35 | std::vector<std::string> Targets; |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 36 | bool IncludeSystemHeaders; |
| 37 | bool PhonyTarget; |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 38 | bool AddMissingHeaderDeps; |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 39 | bool SeenMissingHeader; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 40 | private: |
| 41 | bool FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 42 | SrcMgr::CharacteristicKind FileType); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 43 | void AddFilename(StringRef Filename); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 44 | void OutputDependencyFile(); |
| 45 | |
| 46 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | DependencyFileCallback(const Preprocessor *_PP, |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 48 | const DependencyOutputOptions &Opts) |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 49 | : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets), |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 50 | IncludeSystemHeaders(Opts.IncludeSystemHeaders), |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 51 | PhonyTarget(Opts.UsePhonyTargets), |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 52 | AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), |
| 53 | SeenMissingHeader(false) {} |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 54 | |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 55 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 56 | SrcMgr::CharacteristicKind FileType, |
| 57 | FileID PrevFID); |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 58 | virtual void InclusionDirective(SourceLocation HashLoc, |
| 59 | const Token &IncludeTok, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 60 | StringRef FileName, |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 61 | bool IsAngled, |
Argyrios Kyrtzidis | da31359 | 2012-09-27 01:42:07 +0000 | [diff] [blame^] | 62 | CharSourceRange FilenameRange, |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 63 | const FileEntry *File, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 64 | StringRef SearchPath, |
| 65 | StringRef RelativePath); |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 66 | |
| 67 | virtual void EndOfMainFile() { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 68 | OutputDependencyFile(); |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 69 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 70 | }; |
| 71 | } |
| 72 | |
Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 73 | void clang::AttachDependencyFileGen(Preprocessor &PP, |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 74 | const DependencyOutputOptions &Opts) { |
| 75 | if (Opts.Targets.empty()) { |
Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 76 | PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT); |
Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 79 | |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 80 | // Disable the "file not found" diagnostic if the -MG option was given. |
Eli Friedman | f84139a | 2011-08-30 23:07:51 +0000 | [diff] [blame] | 81 | if (Opts.AddMissingHeaderDeps) |
| 82 | PP.SetSuppressIncludeNotFoundError(true); |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 83 | |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 84 | PP.addPPCallbacks(new DependencyFileCallback(&PP, Opts)); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /// FileMatchesDepCriteria - Determine whether the given Filename should be |
| 88 | /// considered as a dependency. |
| 89 | bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 90 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 91 | if (strcmp("<built-in>", Filename) == 0) |
| 92 | return false; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 93 | |
Eli Friedman | b5c8f8b | 2009-05-19 03:35:57 +0000 | [diff] [blame] | 94 | if (IncludeSystemHeaders) |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 95 | return true; |
| 96 | |
| 97 | return FileType == SrcMgr::C_User; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void DependencyFileCallback::FileChanged(SourceLocation Loc, |
| 101 | FileChangeReason Reason, |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 102 | SrcMgr::CharacteristicKind FileType, |
| 103 | FileID PrevFID) { |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 104 | if (Reason != PPCallbacks::EnterFile) |
| 105 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 107 | // Dependency generation really does want to go all the way to the |
| 108 | // file entry for a source location to find out what is depended on. |
| 109 | // We do not want #line markers to affect dependency generation! |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 110 | SourceManager &SM = PP->getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Chris Lattner | e86e4cd0 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 112 | const FileEntry *FE = |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 113 | SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc))); |
Chris Lattner | e86e4cd0 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 114 | if (FE == 0) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 116 | StringRef Filename = FE->getName(); |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 117 | if (!FileMatchesDepCriteria(Filename.data(), FileType)) |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 118 | return; |
| 119 | |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 120 | // Remove leading "./" (or ".//" or "././" etc.) |
| 121 | while (Filename.size() > 2 && Filename[0] == '.' && |
| 122 | llvm::sys::path::is_separator(Filename[1])) { |
| 123 | Filename = Filename.substr(1); |
| 124 | while (llvm::sys::path::is_separator(Filename[0])) |
| 125 | Filename = Filename.substr(1); |
| 126 | } |
| 127 | |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 128 | AddFilename(Filename); |
| 129 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 130 | |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 131 | void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc, |
| 132 | const Token &IncludeTok, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 133 | StringRef FileName, |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 134 | bool IsAngled, |
Argyrios Kyrtzidis | da31359 | 2012-09-27 01:42:07 +0000 | [diff] [blame^] | 135 | CharSourceRange FilenameRange, |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 136 | const FileEntry *File, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 137 | StringRef SearchPath, |
| 138 | StringRef RelativePath) { |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 139 | if (!File) { |
| 140 | if (AddMissingHeaderDeps) |
| 141 | AddFilename(FileName); |
| 142 | else |
| 143 | SeenMissingHeader = true; |
| 144 | } |
Peter Collingbourne | bb52786 | 2011-07-12 19:35:15 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 147 | void DependencyFileCallback::AddFilename(StringRef Filename) { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 148 | if (FilesSet.insert(Filename)) |
| 149 | Files.push_back(Filename); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chris Lattner | 9d50634 | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 152 | /// PrintFilename - GCC escapes spaces, but apparently not ' or " or other |
| 153 | /// scary characters. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 154 | static void PrintFilename(raw_ostream &OS, StringRef Filename) { |
Chris Lattner | 9d50634 | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 155 | for (unsigned i = 0, e = Filename.size(); i != e; ++i) { |
| 156 | if (Filename[i] == ' ') |
| 157 | OS << '\\'; |
| 158 | OS << Filename[i]; |
| 159 | } |
| 160 | } |
| 161 | |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 162 | void DependencyFileCallback::OutputDependencyFile() { |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 163 | if (SeenMissingHeader) { |
| 164 | llvm::sys::Path(OutputFile).eraseFromDisk(); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | std::string Err; |
| 169 | llvm::raw_fd_ostream OS(OutputFile.c_str(), Err); |
| 170 | if (!Err.empty()) { |
| 171 | PP->getDiagnostics().Report(diag::err_fe_error_opening) |
| 172 | << OutputFile << Err; |
| 173 | return; |
| 174 | } |
| 175 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 176 | // Write out the dependency targets, trying to avoid overly long |
| 177 | // lines when possible. We try our best to emit exactly the same |
| 178 | // dependency file as GCC (4.2), assuming the included files are the |
| 179 | // same. |
| 180 | const unsigned MaxColumns = 75; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 181 | unsigned Columns = 0; |
| 182 | |
| 183 | for (std::vector<std::string>::iterator |
| 184 | I = Targets.begin(), E = Targets.end(); I != E; ++I) { |
| 185 | unsigned N = I->length(); |
| 186 | if (Columns == 0) { |
| 187 | Columns += N; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 188 | } else if (Columns + N + 2 > MaxColumns) { |
| 189 | Columns = N + 2; |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 190 | OS << " \\\n "; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 191 | } else { |
| 192 | Columns += N + 1; |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 193 | OS << ' '; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 194 | } |
Chris Lattner | 9d50634 | 2011-02-17 02:14:49 +0000 | [diff] [blame] | 195 | // Targets already quoted as needed. |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 196 | OS << *I; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 199 | OS << ':'; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 200 | Columns += 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 202 | // Now add each dependency in the order it was seen, but avoiding |
| 203 | // duplicates. |
| 204 | for (std::vector<std::string>::iterator I = Files.begin(), |
| 205 | E = Files.end(); I != E; ++I) { |
| 206 | // Start a new line if this would exceed the column limit. Make |
| 207 | // sure to leave space for a trailing " \" in case we need to |
| 208 | // break the line on the next iteration. |
| 209 | unsigned N = I->length(); |
| 210 | if (Columns + (N + 1) + 2 > MaxColumns) { |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 211 | OS << " \\\n "; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 212 | Columns = 2; |
| 213 | } |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 214 | OS << ' '; |
| 215 | PrintFilename(OS, *I); |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 216 | Columns += N + 1; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 217 | } |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 218 | OS << '\n'; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 219 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 220 | // Create phony targets if requested. |
Fariborz Jahanian | ccad3db | 2011-04-15 18:49:23 +0000 | [diff] [blame] | 221 | if (PhonyTarget && !Files.empty()) { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 222 | // Skip the first entry, this is always the input file itself. |
| 223 | for (std::vector<std::string>::iterator I = Files.begin() + 1, |
| 224 | E = Files.end(); I != E; ++I) { |
Peter Collingbourne | 1b91ab4 | 2011-11-21 00:01:14 +0000 | [diff] [blame] | 225 | OS << '\n'; |
| 226 | PrintFilename(OS, *I); |
| 227 | OS << ":\n"; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 230 | } |
| 231 | |