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 | |
| 14 | #include "clang.h" |
| 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 <fstream> |
| 27 | #include <string> |
| 28 | |
| 29 | using namespace clang; |
| 30 | |
| 31 | namespace { |
| 32 | class VISIBILITY_HIDDEN DependencyFileCallback : public PPCallbacks { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 33 | std::vector<std::string> Files; |
| 34 | llvm::StringSet<> FilesSet; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 35 | const Preprocessor *PP; |
| 36 | std::ofstream OS; |
| 37 | const std::string &InputFile; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 38 | std::vector<std::string> Targets; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | bool FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 42 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 43 | void OutputDependencyFile(); |
| 44 | |
| 45 | public: |
| 46 | DependencyFileCallback(const Preprocessor *PP, |
| 47 | const std::string &InputFile, |
| 48 | const std::string &DepFile, |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 49 | const std::vector<std::string> &Targets, |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 50 | const char *&ErrStr); |
| 51 | ~DependencyFileCallback(); |
| 52 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 53 | SrcMgr::CharacteristicKind FileType); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 54 | }; |
| 55 | } |
| 56 | |
| 57 | static const char *DependencyFileExt = "d"; |
| 58 | static const char *ObjectFileExt = "o"; |
| 59 | |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | // Dependency file options |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | static llvm::cl::opt<bool> |
| 64 | GenerateDependencyFile("MD", |
| 65 | llvm::cl::desc("Generate dependency for main source file " |
| 66 | "(system headers included)")); |
| 67 | |
| 68 | static llvm::cl::opt<bool> |
| 69 | GenerateDependencyFileNoSysHeaders("MMD", |
| 70 | llvm::cl::desc("Generate dependency for main source file " |
| 71 | "(no system headers)")); |
| 72 | |
| 73 | static llvm::cl::opt<std::string> |
| 74 | DependencyOutputFile("MF", |
| 75 | llvm::cl::desc("Specify dependency output file")); |
| 76 | |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 77 | static llvm::cl::list<std::string> |
| 78 | DependencyTargets("MT", |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 79 | llvm::cl::desc("Specify target for dependency")); |
| 80 | |
| 81 | // FIXME: Implement feature |
| 82 | static llvm::cl::opt<bool> |
| 83 | PhonyDependencyTarget("MP", |
| 84 | llvm::cl::desc("Create phony target for each dependency " |
| 85 | "(other than main file)")); |
| 86 | |
| 87 | bool clang::CreateDependencyFileGen(Preprocessor *PP, |
| 88 | std::string &OutputFile, |
| 89 | const std::string &InputFile, |
| 90 | const char *&ErrStr) { |
| 91 | assert(!InputFile.empty() && "No file given"); |
| 92 | |
| 93 | ErrStr = NULL; |
| 94 | |
| 95 | if (!GenerateDependencyFile && !GenerateDependencyFileNoSysHeaders) { |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 96 | if (!DependencyOutputFile.empty() || !DependencyTargets.empty() || |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 97 | PhonyDependencyTarget) |
| 98 | ErrStr = "Error: to generate dependencies you must specify -MD or -MMD\n"; |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | // Handle conflicting options |
| 103 | if (GenerateDependencyFileNoSysHeaders) |
| 104 | GenerateDependencyFile = false; |
| 105 | |
| 106 | // Determine name of dependency output filename |
| 107 | llvm::sys::Path DepFile; |
| 108 | if (!DependencyOutputFile.empty()) |
| 109 | DepFile = DependencyOutputFile; |
| 110 | else if (!OutputFile.empty()) { |
| 111 | DepFile = OutputFile; |
| 112 | DepFile.eraseSuffix(); |
| 113 | DepFile.appendSuffix(DependencyFileExt); |
| 114 | } |
| 115 | else { |
| 116 | DepFile = InputFile; |
| 117 | DepFile.eraseSuffix(); |
| 118 | DepFile.appendSuffix(DependencyFileExt); |
| 119 | } |
| 120 | |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 121 | std::vector<std::string> Targets(DependencyTargets); |
| 122 | |
| 123 | // Infer target name if unspecified |
| 124 | if (Targets.empty()) { |
| 125 | if (!OutputFile.empty()) { |
| 126 | llvm::sys::Path TargetPath(OutputFile); |
| 127 | TargetPath.eraseSuffix(); |
| 128 | TargetPath.appendSuffix(ObjectFileExt); |
| 129 | Targets.push_back(TargetPath.toString()); |
| 130 | } else { |
| 131 | llvm::sys::Path TargetPath(InputFile); |
| 132 | TargetPath.eraseSuffix(); |
| 133 | TargetPath.appendSuffix(ObjectFileExt); |
| 134 | Targets.push_back(TargetPath.toString()); |
| 135 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | DependencyFileCallback *PPDep = |
| 139 | new DependencyFileCallback(PP, InputFile, DepFile.toString(), |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 140 | Targets, ErrStr); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 141 | if (ErrStr){ |
| 142 | delete PPDep; |
| 143 | return false; |
| 144 | } |
| 145 | else { |
| 146 | PP->setPPCallbacks(PPDep); |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /// FileMatchesDepCriteria - Determine whether the given Filename should be |
| 152 | /// considered as a dependency. |
| 153 | bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 154 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 155 | if (strcmp(InputFile.c_str(), Filename) != 0 && |
| 156 | strcmp("<predefines>", Filename) != 0) { |
| 157 | if (GenerateDependencyFileNoSysHeaders) |
| 158 | return FileType == SrcMgr::C_User; |
| 159 | else |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | void DependencyFileCallback::FileChanged(SourceLocation Loc, |
| 167 | FileChangeReason Reason, |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 168 | SrcMgr::CharacteristicKind FileType) { |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 169 | if (Reason != PPCallbacks::EnterFile) |
| 170 | return; |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame^] | 171 | |
| 172 | // Depedency generation really does want to go all the way to the file entry |
| 173 | // for a source location to find out what is depended on. We do not want |
| 174 | // #line markers to affect dependency generation! |
| 175 | SourceManager &SM = PP->getSourceManager(); |
| 176 | |
| 177 | FileID FID = SM.getFileID(SM.getInstantiationLoc(Loc)); |
| 178 | const char *Filename = SM.getFileEntryForID(FID)->getName(); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 179 | if (!FileMatchesDepCriteria(Filename, FileType)) |
| 180 | return; |
| 181 | |
| 182 | // Remove leading "./" |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 183 | if (Filename[0] == '.' && Filename[1] == '/') |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 184 | Filename = &Filename[2]; |
| 185 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 186 | if (FilesSet.insert(Filename)) |
| 187 | Files.push_back(Filename); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void DependencyFileCallback::OutputDependencyFile() { |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 191 | // Write out the dependency targets, trying to avoid overly long |
| 192 | // lines when possible. We try our best to emit exactly the same |
| 193 | // dependency file as GCC (4.2), assuming the included files are the |
| 194 | // same. |
| 195 | const unsigned MaxColumns = 75; |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 196 | unsigned Columns = 0; |
| 197 | |
| 198 | for (std::vector<std::string>::iterator |
| 199 | I = Targets.begin(), E = Targets.end(); I != E; ++I) { |
| 200 | unsigned N = I->length(); |
| 201 | if (Columns == 0) { |
| 202 | Columns += N; |
| 203 | OS << *I; |
| 204 | } else if (Columns + N + 2 > MaxColumns) { |
| 205 | Columns = N + 2; |
| 206 | OS << " \\\n " << *I; |
| 207 | } else { |
| 208 | Columns += N + 1; |
| 209 | OS << " " << *I; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | OS << ":"; |
| 214 | Columns += 1; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 215 | |
| 216 | // Now add each dependency in the order it was seen, but avoiding |
| 217 | // duplicates. |
| 218 | for (std::vector<std::string>::iterator I = Files.begin(), |
| 219 | E = Files.end(); I != E; ++I) { |
| 220 | // Start a new line if this would exceed the column limit. Make |
| 221 | // sure to leave space for a trailing " \" in case we need to |
| 222 | // break the line on the next iteration. |
| 223 | unsigned N = I->length(); |
| 224 | if (Columns + (N + 1) + 2 > MaxColumns) { |
| 225 | OS << " \\\n "; |
| 226 | Columns = 2; |
| 227 | } |
| 228 | OS << " " << *I; |
| 229 | Columns += N + 1; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 230 | } |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 231 | OS << "\n"; |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 232 | |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 233 | // Create phony targets if requested. |
| 234 | if (PhonyDependencyTarget) { |
| 235 | // Skip the first entry, this is always the input file itself. |
| 236 | for (std::vector<std::string>::iterator I = Files.begin() + 1, |
| 237 | E = Files.end(); I != E; ++I) { |
| 238 | OS << "\n"; |
| 239 | OS << *I << ":\n"; |
| 240 | } |
| 241 | } |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | DependencyFileCallback::DependencyFileCallback(const Preprocessor *PP, |
| 245 | const std::string &InputFile, |
| 246 | const std::string &DepFile, |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 247 | const std::vector<std::string> |
| 248 | &Targets, |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 249 | const char *&ErrStr) |
Chris Lattner | 02fbb25 | 2009-01-11 19:28:34 +0000 | [diff] [blame] | 250 | : PP(PP), InputFile(InputFile), Targets(Targets) { |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 251 | |
| 252 | OS.open(DepFile.c_str()); |
| 253 | if (OS.fail()) |
| 254 | ErrStr = "Could not open dependency output file\n"; |
| 255 | else |
| 256 | ErrStr = NULL; |
Daniel Dunbar | 7e9f1f7 | 2008-10-27 20:01:06 +0000 | [diff] [blame] | 257 | |
| 258 | Files.push_back(InputFile); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | DependencyFileCallback::~DependencyFileCallback() { |
| 262 | if ((!GenerateDependencyFile && !GenerateDependencyFileNoSysHeaders) || |
| 263 | OS.fail()) |
| 264 | return; |
| 265 | |
| 266 | OutputDependencyFile(); |
| 267 | OS.close(); |
| 268 | } |
| 269 | |