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"
|
2008-10-25 06:12:41 +08:00
|
|
|
#include "clang/Basic/SourceLocation.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"
|
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2008-10-25 06:12:41 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2008-10-28 04:01:06 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-10-25 06:12:41 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-28 18:07:24 +08:00
|
|
|
class DependencyFileCallback : 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;
|
2009-01-12 03:28:34 +08:00
|
|
|
std::vector<std::string> Targets;
|
2009-03-30 08:34:04 +08:00
|
|
|
llvm::raw_ostream *OS;
|
2009-05-19 11:35:57 +08:00
|
|
|
bool IncludeSystemHeaders;
|
|
|
|
bool PhonyTarget;
|
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:
|
2009-09-09 23:08:12 +08:00
|
|
|
DependencyFileCallback(const Preprocessor *_PP,
|
|
|
|
llvm::raw_ostream *_OS,
|
2009-11-12 05:43:12 +08:00
|
|
|
const DependencyOutputOptions &Opts)
|
|
|
|
: PP(_PP), Targets(Opts.Targets), OS(_OS),
|
|
|
|
IncludeSystemHeaders(Opts.IncludeSystemHeaders),
|
|
|
|
PhonyTarget(Opts.UsePhonyTargets) {}
|
2009-03-30 08:34:04 +08:00
|
|
|
|
|
|
|
~DependencyFileCallback() {
|
|
|
|
OutputDependencyFile();
|
|
|
|
OS->flush();
|
|
|
|
delete OS;
|
|
|
|
}
|
|
|
|
|
2008-10-25 06:12:41 +08:00
|
|
|
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
2008-10-27 09:19:25 +08:00
|
|
|
SrcMgr::CharacteristicKind FileType);
|
2008-10-25 06:12:41 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-11-12 05:44:00 +08:00
|
|
|
void clang::AttachDependencyFileGen(Preprocessor &PP,
|
2009-11-12 05:43:12 +08:00
|
|
|
const DependencyOutputOptions &Opts) {
|
|
|
|
if (Opts.Targets.empty()) {
|
2009-11-12 05:44:00 +08:00
|
|
|
PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
|
2009-11-12 05:43:12 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-10-25 06:12:41 +08:00
|
|
|
|
2009-11-12 05:43:12 +08:00
|
|
|
std::string Err;
|
|
|
|
llvm::raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
|
|
|
|
if (!Err.empty()) {
|
2009-11-12 05:44:00 +08:00
|
|
|
PP.getDiagnostics().Report(diag::err_fe_error_opening)
|
2009-11-12 05:43:12 +08:00
|
|
|
<< Opts.OutputFile << Err;
|
|
|
|
return;
|
|
|
|
}
|
2008-10-25 06:12:41 +08:00
|
|
|
|
2009-11-12 05:44:00 +08:00
|
|
|
assert(!PP.getPPCallbacks() && "Preprocessor callbacks already registered!");
|
|
|
|
PP.setPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
|
2008-10-25 06:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FileMatchesDepCriteria - Determine whether the given Filename should be
|
|
|
|
/// considered as a dependency.
|
|
|
|
bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
|
2008-10-27 09:19:25 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void DependencyFileCallback::FileChanged(SourceLocation Loc,
|
|
|
|
FileChangeReason Reason,
|
2008-10-27 09:19:25 +08:00
|
|
|
SrcMgr::CharacteristicKind FileType) {
|
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 =
|
|
|
|
SM.getFileEntryForID(SM.getFileID(SM.getInstantiationLoc(Loc)));
|
|
|
|
if (FE == 0) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-28 13:42:38 +08:00
|
|
|
const char *Filename = FE->getName();
|
2008-10-25 06:12:41 +08:00
|
|
|
if (!FileMatchesDepCriteria(Filename, FileType))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Remove leading "./"
|
2008-10-28 04:01:06 +08:00
|
|
|
if (Filename[0] == '.' && Filename[1] == '/')
|
2008-10-25 06:12:41 +08:00
|
|
|
Filename = &Filename[2];
|
|
|
|
|
2008-10-28 04:01:06 +08:00
|
|
|
if (FilesSet.insert(Filename))
|
|
|
|
Files.push_back(Filename);
|
2008-10-25 06:12:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DependencyFileCallback::OutputDependencyFile() {
|
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;
|
2009-03-30 08:34:04 +08:00
|
|
|
*OS << *I;
|
2009-01-12 03:28:34 +08:00
|
|
|
} else if (Columns + N + 2 > MaxColumns) {
|
|
|
|
Columns = N + 2;
|
2009-03-30 08:34:04 +08:00
|
|
|
*OS << " \\\n " << *I;
|
2009-01-12 03:28:34 +08:00
|
|
|
} else {
|
|
|
|
Columns += N + 1;
|
2009-03-30 08:34:04 +08:00
|
|
|
*OS << ' ' << *I;
|
2009-01-12 03:28:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-30 08:34:04 +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) {
|
2009-03-30 08:34:04 +08:00
|
|
|
*OS << " \\\n ";
|
2008-10-28 04:01:06 +08:00
|
|
|
Columns = 2;
|
|
|
|
}
|
2009-03-30 08:34:04 +08:00
|
|
|
*OS << ' ' << *I;
|
2008-10-28 04:01:06 +08:00
|
|
|
Columns += N + 1;
|
|
|
|
}
|
2009-03-30 08:34:04 +08:00
|
|
|
*OS << '\n';
|
2008-10-28 04:01:06 +08:00
|
|
|
|
|
|
|
// Create phony targets if requested.
|
2009-05-19 11:35:57 +08:00
|
|
|
if (PhonyTarget) {
|
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) {
|
2009-03-30 08:34:04 +08:00
|
|
|
*OS << '\n';
|
|
|
|
*OS << *I << ":\n";
|
2008-10-28 04:01:06 +08:00
|
|
|
}
|
2008-10-25 06:12:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|