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