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"
|
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 {
|
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;
|
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;
|
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);
|
2011-07-23 18:55:15 +08:00
|
|
|
void AddFilename(StringRef Filename);
|
2008-10-25 06:12:41 +08:00
|
|
|
void OutputDependencyFile();
|
|
|
|
|
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
DependencyFileCallback(const Preprocessor *_PP,
|
2009-11-12 05:43:12 +08:00
|
|
|
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),
|
|
|
|
SeenMissingHeader(false) {}
|
2009-03-30 08:34:04 +08:00
|
|
|
|
2010-03-23 13:09:10 +08:00
|
|
|
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
2011-10-12 01:29:44 +08:00
|
|
|
SrcMgr::CharacteristicKind FileType,
|
|
|
|
FileID PrevFID);
|
2011-07-13 03:35:15 +08:00
|
|
|
virtual void InclusionDirective(SourceLocation HashLoc,
|
|
|
|
const Token &IncludeTok,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef FileName,
|
2011-07-13 03:35:15 +08:00
|
|
|
bool IsAngled,
|
2012-09-27 09:42:07 +08:00
|
|
|
CharSourceRange FilenameRange,
|
2011-07-13 03:35:15 +08:00
|
|
|
const FileEntry *File,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef SearchPath,
|
2012-09-29 09:06:10 +08:00
|
|
|
StringRef RelativePath,
|
|
|
|
const Module *Imported);
|
2010-03-23 13:09:10 +08:00
|
|
|
|
|
|
|
virtual void EndOfMainFile() {
|
2009-03-30 08:34:04 +08:00
|
|
|
OutputDependencyFile();
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
|
2011-11-21 08:01:14 +08:00
|
|
|
PP.addPPCallbacks(new DependencyFileCallback(&PP, 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,
|
2011-10-12 01:29:44 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
|
|
|
|
const Token &IncludeTok,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef FileName,
|
2011-07-13 03:35:15 +08:00
|
|
|
bool IsAngled,
|
2012-09-27 09:42:07 +08:00
|
|
|
CharSourceRange FilenameRange,
|
2011-07-13 03:35:15 +08:00
|
|
|
const FileEntry *File,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef SearchPath,
|
2012-09-29 09:06:10 +08:00
|
|
|
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
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void DependencyFileCallback::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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-25 06:12:41 +08:00
|
|
|
void DependencyFileCallback::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;
|
|
|
|
llvm::raw_fd_ostream OS(OutputFile.c_str(), Err);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|