2008-10-25 06:12:41 +08:00
|
|
|
//===--- DependencyFile.cpp - Generate dependency file --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This code generates dependency files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-19 12:14:29 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2009-01-27 15:57:44 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2009-11-12 05:43:12 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Frontend/DependencyOutputOptions.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Lex/DirectoryLookup.h"
|
2011-07-13 03:35:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2009-11-12 05:43:12 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2014-03-07 14:40:32 +08:00
|
|
|
#include "clang/Serialization/ASTReader.h"
|
2008-10-25 06:12:41 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2013-06-13 22:26:04 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2011-07-09 04:17:28 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2008-10-28 04:01:06 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-10-25 06:12:41 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2014-03-07 14:40:32 +08:00
|
|
|
/// Private implementation for DependencyFileGenerator
|
|
|
|
class DFGImpl : public PPCallbacks {
|
2008-10-28 04:01:06 +08:00
|
|
|
std::vector<std::string> Files;
|
|
|
|
llvm::StringSet<> FilesSet;
|
2008-10-25 06:12:41 +08:00
|
|
|
const Preprocessor *PP;
|
2011-11-21 08:01:14 +08:00
|
|
|
std::string OutputFile;
|
2009-01-12 03:28:34 +08:00
|
|
|
std::vector<std::string> Targets;
|
2009-05-19 11:35:57 +08:00
|
|
|
bool IncludeSystemHeaders;
|
|
|
|
bool PhonyTarget;
|
2011-07-13 03:35:15 +08:00
|
|
|
bool AddMissingHeaderDeps;
|
2011-11-21 08:01:14 +08:00
|
|
|
bool SeenMissingHeader;
|
2014-03-14 11:07:38 +08:00
|
|
|
bool IncludeModuleFiles;
|
2008-10-25 06:12:41 +08:00
|
|
|
private:
|
|
|
|
bool FileMatchesDepCriteria(const char *Filename,
|
2008-10-27 09:19:25 +08:00
|
|
|
SrcMgr::CharacteristicKind FileType);
|
2008-10-25 06:12:41 +08:00
|
|
|
void OutputDependencyFile();
|
|
|
|
|
|
|
|
public:
|
2014-03-07 14:40:32 +08:00
|
|
|
DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
|
2011-11-21 08:01:14 +08:00
|
|
|
: PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
|
2009-11-12 05:43:12 +08:00
|
|
|
IncludeSystemHeaders(Opts.IncludeSystemHeaders),
|
2011-07-13 03:35:15 +08:00
|
|
|
PhonyTarget(Opts.UsePhonyTargets),
|
2011-11-21 08:01:14 +08:00
|
|
|
AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
|
2014-03-14 11:07:38 +08:00
|
|
|
SeenMissingHeader(false),
|
|
|
|
IncludeModuleFiles(Opts.IncludeModuleFiles) {}
|
2009-03-30 08:34:04 +08:00
|
|
|
|
2014-03-13 14:07:04 +08:00
|
|
|
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
|
|
|
SrcMgr::CharacteristicKind FileType,
|
|
|
|
FileID PrevFID) override;
|
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange, const FileEntry *File,
|
|
|
|
StringRef SearchPath, StringRef RelativePath,
|
|
|
|
const Module *Imported) override;
|
2010-03-23 13:09:10 +08:00
|
|
|
|
2014-03-13 14:07:04 +08:00
|
|
|
void EndOfMainFile() override {
|
2009-03-30 08:34:04 +08:00
|
|
|
OutputDependencyFile();
|
|
|
|
}
|
2014-03-07 14:40:32 +08:00
|
|
|
|
|
|
|
void AddFilename(StringRef Filename);
|
|
|
|
bool includeSystemHeaders() const { return IncludeSystemHeaders; }
|
2014-03-14 11:07:38 +08:00
|
|
|
bool includeModuleFiles() const { return IncludeModuleFiles; }
|
2014-03-07 14:40:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DFGASTReaderListener : public ASTReaderListener {
|
|
|
|
DFGImpl &Parent;
|
|
|
|
public:
|
|
|
|
DFGASTReaderListener(DFGImpl &Parent)
|
|
|
|
: Parent(Parent) { }
|
2014-03-13 14:07:04 +08:00
|
|
|
bool needsInputFileVisitation() override { return true; }
|
|
|
|
bool needsSystemInputFileVisitation() override {
|
2014-03-07 14:40:32 +08:00
|
|
|
return Parent.includeSystemHeaders();
|
|
|
|
}
|
2014-03-14 11:07:38 +08:00
|
|
|
void visitModuleFile(StringRef Filename) override;
|
2014-03-14 10:26:31 +08:00
|
|
|
bool visitInputFile(StringRef Filename, bool isSystem,
|
|
|
|
bool isOverridden) override;
|
2008-10-25 06:12:41 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-03-07 14:40:32 +08:00
|
|
|
DependencyFileGenerator::DependencyFileGenerator(void *Impl)
|
|
|
|
: Impl(Impl) { }
|
|
|
|
|
|
|
|
DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
|
|
|
|
clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
|
|
|
|
|
2009-11-12 05:43:12 +08:00
|
|
|
if (Opts.Targets.empty()) {
|
2009-11-12 05:44:00 +08:00
|
|
|
PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
|
2014-03-07 14:40:32 +08:00
|
|
|
return NULL;
|
2009-11-12 05:43:12 +08:00
|
|
|
}
|
2008-10-25 06:12:41 +08:00
|
|
|
|
2011-07-13 03:35:15 +08:00
|
|
|
// Disable the "file not found" diagnostic if the -MG option was given.
|
2011-08-31 07:07:51 +08:00
|
|
|
if (Opts.AddMissingHeaderDeps)
|
|
|
|
PP.SetSuppressIncludeNotFoundError(true);
|
2011-07-13 03:35:15 +08:00
|
|
|
|
2014-03-07 14:40:32 +08:00
|
|
|
DFGImpl *Callback = new DFGImpl(&PP, Opts);
|
|
|
|
PP.addPPCallbacks(Callback); // PP owns the Callback
|
|
|
|
return new DependencyFileGenerator(Callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
|
|
|
|
DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
|
|
|
|
assert(I && "missing implementation");
|
|
|
|
R.addListener(new DFGASTReaderListener(*I));
|
2008-10-25 06:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FileMatchesDepCriteria - Determine whether the given Filename should be
|
|
|
|
/// considered as a dependency.
|
2014-03-07 14:40:32 +08:00
|
|
|
bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
|
|
|
|
SrcMgr::CharacteristicKind FileType) {
|
2009-03-30 08:34:04 +08:00
|
|
|
if (strcmp("<built-in>", Filename) == 0)
|
|
|
|
return false;
|
|
|
|
|
2009-05-19 11:35:57 +08:00
|
|
|
if (IncludeSystemHeaders)
|
2009-03-30 08:34:04 +08:00
|
|
|
return true;
|
2008-10-25 06:12:41 +08:00
|
|
|
|
2009-03-30 08:34:04 +08:00
|
|
|
return FileType == SrcMgr::C_User;
|
2008-10-25 06:12:41 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 14:40:32 +08:00
|
|
|
void DFGImpl::FileChanged(SourceLocation Loc,
|
|
|
|
FileChangeReason Reason,
|
|
|
|
SrcMgr::CharacteristicKind FileType,
|
|
|
|
FileID PrevFID) {
|
2008-10-25 06:12:41 +08:00
|
|
|
if (Reason != PPCallbacks::EnterFile)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-30 08:34:04 +08:00
|
|
|
// Dependency generation really does want to go all the way to the
|
|
|
|
// file entry for a source location to find out what is depended on.
|
|
|
|
// We do not want #line markers to affect dependency generation!
|
2009-01-27 15:57:44 +08:00
|
|
|
SourceManager &SM = PP->getSourceManager();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-28 13:42:38 +08:00
|
|
|
const FileEntry *FE =
|
2011-07-26 00:49:02 +08:00
|
|
|
SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
|
2009-01-28 13:42:38 +08:00
|
|
|
if (FE == 0) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Filename = FE->getName();
|
2011-07-09 04:17:28 +08:00
|
|
|
if (!FileMatchesDepCriteria(Filename.data(), FileType))
|
2008-10-25 06:12:41 +08:00
|
|
|
return;
|
|
|
|
|
2011-07-09 04:17:28 +08:00
|
|
|
// Remove leading "./" (or ".//" or "././" etc.)
|
|
|
|
while (Filename.size() > 2 && Filename[0] == '.' &&
|
|
|
|
llvm::sys::path::is_separator(Filename[1])) {
|
|
|
|
Filename = Filename.substr(1);
|
|
|
|
while (llvm::sys::path::is_separator(Filename[0]))
|
|
|
|
Filename = Filename.substr(1);
|
|
|
|
}
|
|
|
|
|
2011-07-13 03:35:15 +08:00
|
|
|
AddFilename(Filename);
|
|
|
|
}
|
|
|
|
|
2014-03-07 14:40:32 +08:00
|
|
|
void DFGImpl::InclusionDirective(SourceLocation HashLoc,
|
|
|
|
const Token &IncludeTok,
|
|
|
|
StringRef FileName,
|
|
|
|
bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange,
|
|
|
|
const FileEntry *File,
|
|
|
|
StringRef SearchPath,
|
|
|
|
StringRef RelativePath,
|
|
|
|
const Module *Imported) {
|
2011-11-21 08:01:14 +08:00
|
|
|
if (!File) {
|
|
|
|
if (AddMissingHeaderDeps)
|
|
|
|
AddFilename(FileName);
|
|
|
|
else
|
|
|
|
SeenMissingHeader = true;
|
|
|
|
}
|
2011-07-13 03:35:15 +08:00
|
|
|
}
|
2008-10-25 06:12:41 +08:00
|
|
|
|
2014-03-07 14:40:32 +08:00
|
|
|
void DFGImpl::AddFilename(StringRef Filename) {
|
2008-10-28 04:01:06 +08:00
|
|
|
if (FilesSet.insert(Filename))
|
|
|
|
Files.push_back(Filename);
|
2008-10-25 06:12:41 +08:00
|
|
|
}
|
|
|
|
|
2013-04-02 21:38:48 +08:00
|
|
|
/// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
|
|
|
|
/// other scary characters.
|
2011-07-23 18:55:15 +08:00
|
|
|
static void PrintFilename(raw_ostream &OS, StringRef Filename) {
|
2011-02-17 10:14:49 +08:00
|
|
|
for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
|
2013-04-02 21:38:48 +08:00
|
|
|
if (Filename[i] == ' ' || Filename[i] == '#')
|
2011-02-17 10:14:49 +08:00
|
|
|
OS << '\\';
|
2013-04-02 21:38:48 +08:00
|
|
|
else if (Filename[i] == '$') // $ is escaped by $$.
|
|
|
|
OS << '$';
|
2011-02-17 10:14:49 +08:00
|
|
|
OS << Filename[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 14:40:32 +08:00
|
|
|
void DFGImpl::OutputDependencyFile() {
|
2011-11-21 08:01:14 +08:00
|
|
|
if (SeenMissingHeader) {
|
2014-01-11 05:32:14 +08:00
|
|
|
llvm::sys::fs::remove(OutputFile);
|
2011-11-21 08:01:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Err;
|
2014-02-25 02:20:21 +08:00
|
|
|
llvm::raw_fd_ostream OS(OutputFile.c_str(), Err, llvm::sys::fs::F_Text);
|
2011-11-21 08:01:14 +08:00
|
|
|
if (!Err.empty()) {
|
|
|
|
PP->getDiagnostics().Report(diag::err_fe_error_opening)
|
|
|
|
<< OutputFile << Err;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-28 04:01:06 +08:00
|
|
|
// Write out the dependency targets, trying to avoid overly long
|
|
|
|
// lines when possible. We try our best to emit exactly the same
|
|
|
|
// dependency file as GCC (4.2), assuming the included files are the
|
|
|
|
// same.
|
|
|
|
const unsigned MaxColumns = 75;
|
2009-01-12 03:28:34 +08:00
|
|
|
unsigned Columns = 0;
|
|
|
|
|
|
|
|
for (std::vector<std::string>::iterator
|
|
|
|
I = Targets.begin(), E = Targets.end(); I != E; ++I) {
|
|
|
|
unsigned N = I->length();
|
|
|
|
if (Columns == 0) {
|
|
|
|
Columns += N;
|
|
|
|
} else if (Columns + N + 2 > MaxColumns) {
|
|
|
|
Columns = N + 2;
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << " \\\n ";
|
2009-01-12 03:28:34 +08:00
|
|
|
} else {
|
|
|
|
Columns += N + 1;
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << ' ';
|
2009-01-12 03:28:34 +08:00
|
|
|
}
|
2011-02-17 10:14:49 +08:00
|
|
|
// Targets already quoted as needed.
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << *I;
|
2009-01-12 03:28:34 +08:00
|
|
|
}
|
|
|
|
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << ':';
|
2009-01-12 03:28:34 +08:00
|
|
|
Columns += 1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-28 04:01:06 +08:00
|
|
|
// Now add each dependency in the order it was seen, but avoiding
|
|
|
|
// duplicates.
|
|
|
|
for (std::vector<std::string>::iterator I = Files.begin(),
|
|
|
|
E = Files.end(); I != E; ++I) {
|
|
|
|
// Start a new line if this would exceed the column limit. Make
|
|
|
|
// sure to leave space for a trailing " \" in case we need to
|
|
|
|
// break the line on the next iteration.
|
|
|
|
unsigned N = I->length();
|
|
|
|
if (Columns + (N + 1) + 2 > MaxColumns) {
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << " \\\n ";
|
2008-10-28 04:01:06 +08:00
|
|
|
Columns = 2;
|
|
|
|
}
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << ' ';
|
|
|
|
PrintFilename(OS, *I);
|
2008-10-28 04:01:06 +08:00
|
|
|
Columns += N + 1;
|
|
|
|
}
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << '\n';
|
2008-10-28 04:01:06 +08:00
|
|
|
|
|
|
|
// Create phony targets if requested.
|
2011-04-16 02:49:23 +08:00
|
|
|
if (PhonyTarget && !Files.empty()) {
|
2008-10-28 04:01:06 +08:00
|
|
|
// Skip the first entry, this is always the input file itself.
|
|
|
|
for (std::vector<std::string>::iterator I = Files.begin() + 1,
|
|
|
|
E = Files.end(); I != E; ++I) {
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << '\n';
|
|
|
|
PrintFilename(OS, *I);
|
|
|
|
OS << ":\n";
|
2008-10-28 04:01:06 +08:00
|
|
|
}
|
2008-10-25 06:12:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 14:40:32 +08:00
|
|
|
bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
|
2014-03-14 10:26:31 +08:00
|
|
|
bool IsSystem, bool IsOverridden) {
|
2014-03-07 14:40:32 +08:00
|
|
|
assert(!IsSystem || needsSystemInputFileVisitation());
|
2014-03-14 10:26:31 +08:00
|
|
|
if (IsOverridden)
|
|
|
|
return true;
|
|
|
|
|
2014-03-07 14:40:32 +08:00
|
|
|
Parent.AddFilename(Filename);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-14 11:07:38 +08:00
|
|
|
void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) {
|
|
|
|
if (Parent.includeModuleFiles())
|
|
|
|
Parent.AddFilename(Filename);
|
|
|
|
}
|