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 | |
Ted Kremenek | c2542b6 | 2009-03-31 18:58:14 +0000 | [diff] [blame] | 14 | #include "clang-cc.h" |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Daniel Dunbar | 750c358 | 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 | 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 | #include <string> |
| 27 | |
| 28 | using namespace clang; |
| 29 | |
| 30 | namespace { |
| 31 | class VISIBILITY_HIDDEN 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; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 35 | std::vector<std::string> Targets; |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 36 | llvm::raw_ostream *OS; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 37 | |
| 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: |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 44 | DependencyFileCallback(const Preprocessor *_PP, |
| 45 | llvm::raw_ostream *_OS, |
Daniel Dunbar | e5393fb | 2009-05-03 10:04:17 +0000 | [diff] [blame] | 46 | const std::vector<std::string> &_Targets) |
| 47 | : PP(_PP), Targets(_Targets), OS(_OS) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | ~DependencyFileCallback() { |
| 51 | OutputDependencyFile(); |
| 52 | OS->flush(); |
| 53 | delete OS; |
| 54 | } |
| 55 | |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 56 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 57 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 58 | }; |
| 59 | } |
| 60 | |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 61 | //===----------------------------------------------------------------------===// |
| 62 | // Dependency file options |
| 63 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 64 | static llvm::cl::opt<std::string> |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 65 | DependencyFile("dependency-file", |
| 66 | llvm::cl::desc("Filename (or -) to write dependency output to")); |
| 67 | |
| 68 | static llvm::cl::opt<bool> |
| 69 | DependenciesIncludeSystemHeaders("sys-header-deps", |
| 70 | llvm::cl::desc("Include system headers in dependency output")); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 72 | static llvm::cl::list<std::string> |
| 73 | DependencyTargets("MT", |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 74 | llvm::cl::desc("Specify target for dependency")); |
| 75 | |
| 76 | // FIXME: Implement feature |
| 77 | static llvm::cl::opt<bool> |
| 78 | PhonyDependencyTarget("MP", |
| 79 | llvm::cl::desc("Create phony target for each dependency " |
| 80 | "(other than main file)")); |
| 81 | |
| 82 | bool clang::CreateDependencyFileGen(Preprocessor *PP, |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 83 | std::string &ErrStr) { |
| 84 | ErrStr = ""; |
| 85 | if (DependencyFile.empty()) |
| 86 | return false; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 88 | if (DependencyTargets.empty()) { |
| 89 | ErrStr = "-dependency-file requires at least one -MT option\n"; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 90 | return false; |
| 91 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 92 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 93 | std::string ErrMsg; |
Daniel Dunbar | cd8e4c4 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 94 | llvm::raw_ostream *OS; |
| 95 | if (DependencyFile == "-") { |
| 96 | OS = new llvm::raw_stdout_ostream(); |
| 97 | } else { |
| 98 | OS = new llvm::raw_fd_ostream(DependencyFile.c_str(), false, ErrStr); |
| 99 | if (!ErrMsg.empty()) { |
| 100 | ErrStr = "unable to open dependency file: " + ErrMsg; |
| 101 | return false; |
| 102 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | DependencyFileCallback *PPDep = |
Daniel Dunbar | e5393fb | 2009-05-03 10:04:17 +0000 | [diff] [blame] | 106 | new DependencyFileCallback(PP, OS, DependencyTargets); |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 107 | PP->setPPCallbacks(PPDep); |
| 108 | return true; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /// FileMatchesDepCriteria - Determine whether the given Filename should be |
| 112 | /// considered as a dependency. |
| 113 | bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 114 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 115 | if (strcmp("<built-in>", Filename) == 0) |
| 116 | return false; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 117 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 118 | if (DependenciesIncludeSystemHeaders) |
| 119 | return true; |
| 120 | |
| 121 | return FileType == SrcMgr::C_User; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void DependencyFileCallback::FileChanged(SourceLocation Loc, |
| 125 | FileChangeReason Reason, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 126 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 127 | if (Reason != PPCallbacks::EnterFile) |
| 128 | return; |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 129 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 130 | // Dependency generation really does want to go all the way to the |
| 131 | // file entry for a source location to find out what is depended on. |
| 132 | // We do not want #line markers to affect dependency generation! |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 133 | SourceManager &SM = PP->getSourceManager(); |
| 134 | |
Chris Lattner | e86e4cd0 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 135 | const FileEntry *FE = |
| 136 | SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc))); |
| 137 | if (FE == 0) return; |
| 138 | |
| 139 | const char *Filename = FE->getName(); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 140 | if (!FileMatchesDepCriteria(Filename, FileType)) |
| 141 | return; |
| 142 | |
| 143 | // Remove leading "./" |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 144 | if (Filename[0] == '.' && Filename[1] == '/') |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 145 | Filename = &Filename[2]; |
| 146 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 147 | if (FilesSet.insert(Filename)) |
| 148 | Files.push_back(Filename); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void DependencyFileCallback::OutputDependencyFile() { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 152 | // Write out the dependency targets, trying to avoid overly long |
| 153 | // lines when possible. We try our best to emit exactly the same |
| 154 | // dependency file as GCC (4.2), assuming the included files are the |
| 155 | // same. |
| 156 | const unsigned MaxColumns = 75; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 157 | unsigned Columns = 0; |
| 158 | |
| 159 | for (std::vector<std::string>::iterator |
| 160 | I = Targets.begin(), E = Targets.end(); I != E; ++I) { |
| 161 | unsigned N = I->length(); |
| 162 | if (Columns == 0) { |
| 163 | Columns += N; |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 164 | *OS << *I; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 165 | } else if (Columns + N + 2 > MaxColumns) { |
| 166 | Columns = N + 2; |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 167 | *OS << " \\\n " << *I; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 168 | } else { |
| 169 | Columns += N + 1; |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 170 | *OS << ' ' << *I; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 174 | *OS << ':'; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 175 | Columns += 1; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 176 | |
| 177 | // Now add each dependency in the order it was seen, but avoiding |
| 178 | // duplicates. |
| 179 | for (std::vector<std::string>::iterator I = Files.begin(), |
| 180 | E = Files.end(); I != E; ++I) { |
| 181 | // Start a new line if this would exceed the column limit. Make |
| 182 | // sure to leave space for a trailing " \" in case we need to |
| 183 | // break the line on the next iteration. |
| 184 | unsigned N = I->length(); |
| 185 | if (Columns + (N + 1) + 2 > MaxColumns) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 186 | *OS << " \\\n "; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 187 | Columns = 2; |
| 188 | } |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 189 | *OS << ' ' << *I; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 190 | Columns += N + 1; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 191 | } |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 192 | *OS << '\n'; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 193 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 194 | // Create phony targets if requested. |
| 195 | if (PhonyDependencyTarget) { |
| 196 | // Skip the first entry, this is always the input file itself. |
| 197 | for (std::vector<std::string>::iterator I = Files.begin() + 1, |
| 198 | E = Files.end(); I != E; ++I) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 199 | *OS << '\n'; |
| 200 | *OS << *I << ":\n"; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 203 | } |
| 204 | |