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