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; |
Eli Friedman | e595fe9 | 2009-05-19 03:35:57 +0000 | [diff] [blame^] | 37 | bool IncludeSystemHeaders; |
| 38 | bool PhonyTarget; |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 39 | private: |
| 40 | bool FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 41 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 42 | void OutputDependencyFile(); |
| 43 | |
| 44 | public: |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 45 | DependencyFileCallback(const Preprocessor *_PP, |
| 46 | llvm::raw_ostream *_OS, |
Eli Friedman | e595fe9 | 2009-05-19 03:35:57 +0000 | [diff] [blame^] | 47 | const std::vector<std::string> &_Targets, |
| 48 | bool _IncludeSystemHeaders, |
| 49 | bool _PhonyTarget) |
| 50 | : PP(_PP), Targets(_Targets), OS(_OS), |
| 51 | IncludeSystemHeaders(_IncludeSystemHeaders), PhonyTarget(_PhonyTarget) {} |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 52 | |
| 53 | ~DependencyFileCallback() { |
| 54 | OutputDependencyFile(); |
| 55 | OS->flush(); |
| 56 | delete OS; |
| 57 | } |
| 58 | |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 59 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 60 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 61 | }; |
| 62 | } |
| 63 | |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 64 | |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 65 | |
Eli Friedman | e595fe9 | 2009-05-19 03:35:57 +0000 | [diff] [blame^] | 66 | void clang::AttachDependencyFileGen(Preprocessor *PP, llvm::raw_ostream *OS, |
| 67 | std::vector<std::string> &Targets, |
| 68 | bool IncludeSystemHeaders, |
| 69 | bool PhonyTarget) { |
| 70 | assert(!Targets.empty() && "Target required for dependency generation"); |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 71 | |
| 72 | DependencyFileCallback *PPDep = |
Eli Friedman | e595fe9 | 2009-05-19 03:35:57 +0000 | [diff] [blame^] | 73 | new DependencyFileCallback(PP, OS, Targets, IncludeSystemHeaders, |
| 74 | PhonyTarget); |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 75 | PP->setPPCallbacks(PPDep); |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /// FileMatchesDepCriteria - Determine whether the given Filename should be |
| 79 | /// considered as a dependency. |
| 80 | bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 81 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 82 | if (strcmp("<built-in>", Filename) == 0) |
| 83 | return false; |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 84 | |
Eli Friedman | e595fe9 | 2009-05-19 03:35:57 +0000 | [diff] [blame^] | 85 | if (IncludeSystemHeaders) |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 86 | return true; |
| 87 | |
| 88 | return FileType == SrcMgr::C_User; |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void DependencyFileCallback::FileChanged(SourceLocation Loc, |
| 92 | FileChangeReason Reason, |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 93 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 94 | if (Reason != PPCallbacks::EnterFile) |
| 95 | return; |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 97 | // Dependency generation really does want to go all the way to the |
| 98 | // file entry for a source location to find out what is depended on. |
| 99 | // We do not want #line markers to affect dependency generation! |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 100 | SourceManager &SM = PP->getSourceManager(); |
| 101 | |
Chris Lattner | e88c195 | 2009-01-28 05:42:38 +0000 | [diff] [blame] | 102 | const FileEntry *FE = |
| 103 | SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc))); |
| 104 | if (FE == 0) return; |
| 105 | |
| 106 | const char *Filename = FE->getName(); |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 107 | if (!FileMatchesDepCriteria(Filename, FileType)) |
| 108 | return; |
| 109 | |
| 110 | // Remove leading "./" |
Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 111 | if (Filename[0] == '.' && Filename[1] == '/') |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 112 | Filename = &Filename[2]; |
| 113 | |
Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 114 | if (FilesSet.insert(Filename)) |
| 115 | Files.push_back(Filename); |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void DependencyFileCallback::OutputDependencyFile() { |
Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 119 | // Write out the dependency targets, trying to avoid overly long |
| 120 | // lines when possible. We try our best to emit exactly the same |
| 121 | // dependency file as GCC (4.2), assuming the included files are the |
| 122 | // same. |
| 123 | const unsigned MaxColumns = 75; |
Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 124 | unsigned Columns = 0; |
| 125 | |
| 126 | for (std::vector<std::string>::iterator |
| 127 | I = Targets.begin(), E = Targets.end(); I != E; ++I) { |
| 128 | unsigned N = I->length(); |
| 129 | if (Columns == 0) { |
| 130 | Columns += N; |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 131 | *OS << *I; |
Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 132 | } else if (Columns + N + 2 > MaxColumns) { |
| 133 | Columns = N + 2; |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 134 | *OS << " \\\n " << *I; |
Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 135 | } else { |
| 136 | Columns += N + 1; |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 137 | *OS << ' ' << *I; |
Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 141 | *OS << ':'; |
Chris Lattner | 6b0d6e2 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 142 | Columns += 1; |
Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 143 | |
| 144 | // Now add each dependency in the order it was seen, but avoiding |
| 145 | // duplicates. |
| 146 | for (std::vector<std::string>::iterator I = Files.begin(), |
| 147 | E = Files.end(); I != E; ++I) { |
| 148 | // Start a new line if this would exceed the column limit. Make |
| 149 | // sure to leave space for a trailing " \" in case we need to |
| 150 | // break the line on the next iteration. |
| 151 | unsigned N = I->length(); |
| 152 | if (Columns + (N + 1) + 2 > MaxColumns) { |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 153 | *OS << " \\\n "; |
Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 154 | Columns = 2; |
| 155 | } |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 156 | *OS << ' ' << *I; |
Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 157 | Columns += N + 1; |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 158 | } |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 159 | *OS << '\n'; |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 160 | |
Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 161 | // Create phony targets if requested. |
Eli Friedman | e595fe9 | 2009-05-19 03:35:57 +0000 | [diff] [blame^] | 162 | if (PhonyTarget) { |
Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 163 | // Skip the first entry, this is always the input file itself. |
| 164 | for (std::vector<std::string>::iterator I = Files.begin() + 1, |
| 165 | E = Files.end(); I != E; ++I) { |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 166 | *OS << '\n'; |
| 167 | *OS << *I << ":\n"; |
Daniel Dunbar | 4cdc4a5 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 170 | } |
| 171 | |