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