2008-10-25 06:12:41 +08:00
|
|
|
//===--- DependencyFile.cpp - Generate dependency file --------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2008-10-25 06:12:41 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
2015-08-09 12:46:57 +08:00
|
|
|
#include "clang/Lex/ModuleMap.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"
|
2014-10-28 06:31:50 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.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;
|
|
|
|
|
2014-07-01 04:04:14 +08:00
|
|
|
namespace {
|
|
|
|
struct DepCollectorPPCallbacks : public PPCallbacks {
|
|
|
|
DependencyCollector &DepCollector;
|
|
|
|
SourceManager &SM;
|
2019-06-20 01:07:36 +08:00
|
|
|
DiagnosticsEngine &Diags;
|
|
|
|
DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM,
|
|
|
|
DiagnosticsEngine &Diags)
|
|
|
|
: DepCollector(L), SM(SM), Diags(Diags) {}
|
2014-07-01 04:04:14 +08:00
|
|
|
|
|
|
|
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
|
|
|
SrcMgr::CharacteristicKind FileType,
|
|
|
|
FileID PrevFID) override {
|
|
|
|
if (Reason != PPCallbacks::EnterFile)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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!
|
Introduce FileEntryRef and use it when handling includes to report correct dependencies
when the FileManager is reused across invocations
This commit introduces a parallel API to FileManager's getFile: getFileEntryRef, which returns
a reference to the FileEntry, and the name that was used to access the file. In the case of
a VFS with 'use-external-names', the FileEntyRef contains the external name of the file,
not the filename that was used to access it.
The new API is adopted only in the HeaderSearch and Preprocessor for include file lookup, so that the
accessed path can be propagated to SourceManager's FileInfo. SourceManager's FileInfo now can report this accessed path, using
the new getName method. This API is then adopted in the dependency collector, which now correctly reports dependencies when a file
is included both using a symlink and a real path in the case when the FileManager is reused across multiple Preprocessor invocations.
Note that this patch does not fix all dependency collector issues, as the same problem is still present in other cases when dependencies
are obtained using FileSkipped, InclusionDirective, and HasInclude. This will be fixed in follow-up commits.
Differential Revision: https://reviews.llvm.org/D65907
llvm-svn: 369680
2019-08-23 02:15:50 +08:00
|
|
|
Optional<FileEntryRef> File =
|
|
|
|
SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(Loc)));
|
|
|
|
if (!File)
|
2014-07-01 04:04:14 +08:00
|
|
|
return;
|
|
|
|
|
2015-09-03 05:14:53 +08:00
|
|
|
StringRef Filename =
|
Introduce FileEntryRef and use it when handling includes to report correct dependencies
when the FileManager is reused across invocations
This commit introduces a parallel API to FileManager's getFile: getFileEntryRef, which returns
a reference to the FileEntry, and the name that was used to access the file. In the case of
a VFS with 'use-external-names', the FileEntyRef contains the external name of the file,
not the filename that was used to access it.
The new API is adopted only in the HeaderSearch and Preprocessor for include file lookup, so that the
accessed path can be propagated to SourceManager's FileInfo. SourceManager's FileInfo now can report this accessed path, using
the new getName method. This API is then adopted in the dependency collector, which now correctly reports dependencies when a file
is included both using a symlink and a real path in the case when the FileManager is reused across multiple Preprocessor invocations.
Note that this patch does not fix all dependency collector issues, as the same problem is still present in other cases when dependencies
are obtained using FileSkipped, InclusionDirective, and HasInclude. This will be fixed in follow-up commits.
Differential Revision: https://reviews.llvm.org/D65907
llvm-svn: 369680
2019-08-23 02:15:50 +08:00
|
|
|
llvm::sys::path::remove_leading_dotslash(File->getName());
|
2014-07-01 04:04:14 +08:00
|
|
|
|
|
|
|
DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
|
2017-06-29 10:19:42 +08:00
|
|
|
isSystem(FileType),
|
|
|
|
/*IsModuleFile*/false, /*IsMissing*/false);
|
2014-07-01 04:04:14 +08:00
|
|
|
}
|
|
|
|
|
2019-08-27 09:03:25 +08:00
|
|
|
void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
|
2019-06-20 01:07:36 +08:00
|
|
|
SrcMgr::CharacteristicKind FileType) override {
|
|
|
|
StringRef Filename =
|
|
|
|
llvm::sys::path::remove_leading_dotslash(SkippedFile.getName());
|
|
|
|
DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
|
|
|
|
/*IsSystem=*/isSystem(FileType),
|
|
|
|
/*IsModuleFile=*/false,
|
|
|
|
/*IsMissing=*/false);
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:04:14 +08:00
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange, const FileEntry *File,
|
|
|
|
StringRef SearchPath, StringRef RelativePath,
|
2018-05-11 03:05:36 +08:00
|
|
|
const Module *Imported,
|
|
|
|
SrcMgr::CharacteristicKind FileType) override {
|
2014-07-01 04:04:14 +08:00
|
|
|
if (!File)
|
|
|
|
DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
|
|
|
|
/*IsSystem*/false, /*IsModuleFile*/false,
|
|
|
|
/*IsMissing*/true);
|
|
|
|
// Files that actually exist are handled by FileChanged.
|
|
|
|
}
|
|
|
|
|
2019-06-20 01:07:36 +08:00
|
|
|
void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
|
2019-08-28 01:32:42 +08:00
|
|
|
Optional<FileEntryRef> File,
|
2019-06-20 01:07:36 +08:00
|
|
|
SrcMgr::CharacteristicKind FileType) override {
|
|
|
|
if (!File)
|
|
|
|
return;
|
|
|
|
StringRef Filename =
|
|
|
|
llvm::sys::path::remove_leading_dotslash(File->getName());
|
|
|
|
DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
|
|
|
|
/*IsSystem=*/isSystem(FileType),
|
|
|
|
/*IsModuleFile=*/false,
|
|
|
|
/*IsMissing=*/false);
|
2014-07-01 04:04:14 +08:00
|
|
|
}
|
2019-06-20 01:07:36 +08:00
|
|
|
|
|
|
|
void EndOfMainFile() override { DepCollector.finishedMainFile(Diags); }
|
2014-07-01 04:04:14 +08:00
|
|
|
};
|
|
|
|
|
2015-08-09 12:46:57 +08:00
|
|
|
struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
|
|
|
|
DependencyCollector &DepCollector;
|
|
|
|
DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
|
|
|
|
|
|
|
|
void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
|
|
|
|
bool IsSystem) override {
|
|
|
|
StringRef Filename = Entry.getName();
|
|
|
|
DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
|
|
|
|
/*IsSystem*/IsSystem,
|
|
|
|
/*IsModuleFile*/false,
|
|
|
|
/*IsMissing*/false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-01 04:04:14 +08:00
|
|
|
struct DepCollectorASTListener : public ASTReaderListener {
|
|
|
|
DependencyCollector &DepCollector;
|
|
|
|
DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
|
|
|
|
bool needsInputFileVisitation() override { return true; }
|
|
|
|
bool needsSystemInputFileVisitation() override {
|
|
|
|
return DepCollector.needSystemDependencies();
|
|
|
|
}
|
2015-08-14 01:57:10 +08:00
|
|
|
void visitModuleFile(StringRef Filename,
|
|
|
|
serialization::ModuleKind Kind) override {
|
2014-07-01 04:04:14 +08:00
|
|
|
DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
|
|
|
|
/*IsSystem*/false, /*IsModuleFile*/true,
|
|
|
|
/*IsMissing*/false);
|
|
|
|
}
|
|
|
|
bool visitInputFile(StringRef Filename, bool IsSystem,
|
2015-08-14 01:57:10 +08:00
|
|
|
bool IsOverridden, bool IsExplicitModule) override {
|
|
|
|
if (IsOverridden || IsExplicitModule)
|
2014-07-01 04:04:14 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
|
|
|
|
/*IsModuleFile*/false, /*IsMissing*/false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
|
|
|
|
bool IsSystem, bool IsModuleFile,
|
|
|
|
bool IsMissing) {
|
2019-06-20 01:07:36 +08:00
|
|
|
if (sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
|
|
|
|
addDependency(Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DependencyCollector::addDependency(StringRef Filename) {
|
|
|
|
if (Seen.insert(Filename).second) {
|
2014-07-01 04:04:14 +08:00
|
|
|
Dependencies.push_back(Filename);
|
2019-06-20 01:07:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-07-01 04:04:14 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 06:31:50 +08:00
|
|
|
static bool isSpecialFilename(StringRef Filename) {
|
|
|
|
return llvm::StringSwitch<bool>(Filename)
|
|
|
|
.Case("<built-in>", true)
|
|
|
|
.Case("<stdin>", true)
|
|
|
|
.Default(false);
|
|
|
|
}
|
|
|
|
|
2014-07-01 04:04:14 +08:00
|
|
|
bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
|
|
|
|
bool IsSystem, bool IsModuleFile,
|
|
|
|
bool IsMissing) {
|
2014-10-28 06:31:50 +08:00
|
|
|
return !isSpecialFilename(Filename) &&
|
|
|
|
(needSystemDependencies() || !IsSystem);
|
2014-07-01 04:04:14 +08:00
|
|
|
}
|
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
DependencyCollector::~DependencyCollector() { }
|
2014-07-01 04:04:14 +08:00
|
|
|
void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
|
2019-08-15 07:04:18 +08:00
|
|
|
PP.addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>(
|
2019-06-20 01:07:36 +08:00
|
|
|
*this, PP.getSourceManager(), PP.getDiagnostics()));
|
2015-08-09 12:46:57 +08:00
|
|
|
PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
|
2019-08-15 07:04:18 +08:00
|
|
|
std::make_unique<DepCollectorMMCallbacks>(*this));
|
2014-07-01 04:04:14 +08:00
|
|
|
}
|
|
|
|
void DependencyCollector::attachToASTReader(ASTReader &R) {
|
2019-08-15 07:04:18 +08:00
|
|
|
R.addListener(std::make_unique<DepCollectorASTListener>(*this));
|
2014-07-01 04:04:14 +08:00
|
|
|
}
|
|
|
|
|
2019-06-20 01:07:36 +08:00
|
|
|
DependencyFileGenerator::DependencyFileGenerator(
|
|
|
|
const DependencyOutputOptions &Opts)
|
|
|
|
: 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),
|
2019-06-20 01:07:36 +08:00
|
|
|
AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(false),
|
2015-04-28 02:14:32 +08:00
|
|
|
IncludeModuleFiles(Opts.IncludeModuleFiles),
|
2019-06-20 01:07:36 +08:00
|
|
|
OutputFormat(Opts.OutputFormat), InputFileIndex(0) {
|
|
|
|
for (const auto &ExtraDep : Opts.ExtraDeps) {
|
|
|
|
if (addDependency(ExtraDep))
|
|
|
|
++InputFileIndex;
|
2014-03-07 14:40:32 +08:00
|
|
|
}
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2008-10-25 06:12:41 +08:00
|
|
|
|
2019-06-20 01:07:36 +08:00
|
|
|
void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) {
|
2011-07-13 03:35:15 +08:00
|
|
|
// Disable the "file not found" diagnostic if the -MG option was given.
|
2019-06-20 01:07:36 +08:00
|
|
|
if (AddMissingHeaderDeps)
|
2011-08-31 07:07:51 +08:00
|
|
|
PP.SetSuppressIncludeNotFoundError(true);
|
2011-07-13 03:35:15 +08:00
|
|
|
|
2019-06-20 01:07:36 +08:00
|
|
|
DependencyCollector::attachToPreprocessor(PP);
|
2014-03-07 14:40:32 +08:00
|
|
|
}
|
|
|
|
|
2019-06-20 01:07:36 +08:00
|
|
|
bool DependencyFileGenerator::sawDependency(StringRef Filename, bool FromModule,
|
|
|
|
bool IsSystem, bool IsModuleFile,
|
|
|
|
bool IsMissing) {
|
|
|
|
if (IsMissing) {
|
|
|
|
// Handle the case of missing file from an inclusion directive.
|
|
|
|
if (AddMissingHeaderDeps)
|
|
|
|
return true;
|
|
|
|
SeenMissingHeader = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsModuleFile && !IncludeModuleFiles)
|
|
|
|
return false;
|
2008-10-25 06:12:41 +08:00
|
|
|
|
2014-10-28 06:31:50 +08:00
|
|
|
if (isSpecialFilename(Filename))
|
2009-03-30 08:34:04 +08:00
|
|
|
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
|
|
|
|
2019-06-20 01:07:36 +08:00
|
|
|
return !IsSystem;
|
2011-07-13 03:35:15 +08:00
|
|
|
}
|
|
|
|
|
2019-06-20 01:07:36 +08:00
|
|
|
void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) {
|
|
|
|
outputDependencyFile(Diags);
|
2008-10-25 06:12:41 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 05:18:15 +08:00
|
|
|
/// Print the filename, with escaping or quoting that accommodates the three
|
|
|
|
/// most likely tools that use dependency files: GNU Make, BSD Make, and
|
|
|
|
/// NMake/Jom.
|
|
|
|
///
|
|
|
|
/// BSD Make is the simplest case: It does no escaping at all. This means
|
|
|
|
/// characters that are normally delimiters, i.e. space and # (the comment
|
|
|
|
/// character) simply aren't supported in filenames.
|
|
|
|
///
|
|
|
|
/// GNU Make does allow space and # in filenames, but to avoid being treated
|
|
|
|
/// as a delimiter or comment, these must be escaped with a backslash. Because
|
|
|
|
/// backslash is itself the escape character, if a backslash appears in a
|
|
|
|
/// filename, it should be escaped as well. (As a special case, $ is escaped
|
|
|
|
/// as $$, which is the normal Make way to handle the $ character.)
|
|
|
|
/// For compatibility with BSD Make and historical practice, if GNU Make
|
|
|
|
/// un-escapes characters in a filename but doesn't find a match, it will
|
|
|
|
/// retry with the unmodified original string.
|
|
|
|
///
|
|
|
|
/// GCC tries to accommodate both Make formats by escaping any space or #
|
2015-05-14 06:33:50 +08:00
|
|
|
/// characters in the original filename, but not escaping backslashes. The
|
|
|
|
/// apparent intent is so that filenames with backslashes will be handled
|
2015-05-14 05:18:15 +08:00
|
|
|
/// correctly by BSD Make, and by GNU Make in its fallback mode of using the
|
|
|
|
/// unmodified original string; filenames with # or space characters aren't
|
|
|
|
/// supported by BSD Make at all, but will be handled correctly by GNU Make
|
|
|
|
/// due to the escaping.
|
|
|
|
///
|
2015-05-14 06:33:50 +08:00
|
|
|
/// A corner case that GCC gets only partly right is when the original filename
|
|
|
|
/// has a backslash immediately followed by space or #. GNU Make would expect
|
|
|
|
/// this backslash to be escaped; however GCC escapes the original backslash
|
|
|
|
/// only when followed by space, not #. It will therefore take a dependency
|
|
|
|
/// from a directive such as
|
|
|
|
/// #include "a\ b\#c.h"
|
2015-05-14 05:18:15 +08:00
|
|
|
/// and emit it as
|
2015-05-14 06:33:50 +08:00
|
|
|
/// a\\\ b\\#c.h
|
2015-05-14 05:18:15 +08:00
|
|
|
/// which GNU Make will interpret as
|
2015-05-14 06:33:50 +08:00
|
|
|
/// a\ b\
|
2015-05-14 05:18:15 +08:00
|
|
|
/// followed by a comment. Failing to find this file, it will fall back to the
|
2015-05-14 06:33:50 +08:00
|
|
|
/// original string, which probably doesn't exist either; in any case it won't
|
|
|
|
/// find
|
|
|
|
/// a\ b\#c.h
|
2015-05-14 05:18:15 +08:00
|
|
|
/// which is the actual filename specified by the include directive.
|
|
|
|
///
|
2015-05-14 06:33:50 +08:00
|
|
|
/// Clang does what GCC does, rather than what GNU Make expects.
|
2015-05-14 05:18:15 +08:00
|
|
|
///
|
|
|
|
/// NMake/Jom has a different set of scary characters, but wraps filespecs in
|
|
|
|
/// double-quotes to avoid misinterpreting them; see
|
2015-04-28 02:14:32 +08:00
|
|
|
/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
|
|
|
|
/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
|
|
|
|
/// for Windows file-naming info.
|
|
|
|
static void PrintFilename(raw_ostream &OS, StringRef Filename,
|
|
|
|
DependencyOutputFormat OutputFormat) {
|
2018-09-13 22:27:32 +08:00
|
|
|
// Convert filename to platform native path
|
|
|
|
llvm::SmallString<256> NativePath;
|
|
|
|
llvm::sys::path::native(Filename.str(), NativePath);
|
|
|
|
|
2015-04-28 02:14:32 +08:00
|
|
|
if (OutputFormat == DependencyOutputFormat::NMake) {
|
|
|
|
// Add quotes if needed. These are the characters listed as "special" to
|
|
|
|
// NMake, that are legal in a Windows filespec, and that could cause
|
|
|
|
// misinterpretation of the dependency string.
|
2018-09-13 22:27:32 +08:00
|
|
|
if (NativePath.find_first_of(" #${}^!") != StringRef::npos)
|
|
|
|
OS << '\"' << NativePath << '\"';
|
2015-04-28 02:14:32 +08:00
|
|
|
else
|
2018-09-13 22:27:32 +08:00
|
|
|
OS << NativePath;
|
2015-04-28 02:14:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-05-14 06:33:50 +08:00
|
|
|
assert(OutputFormat == DependencyOutputFormat::Make);
|
2018-09-13 22:27:32 +08:00
|
|
|
for (unsigned i = 0, e = NativePath.size(); i != e; ++i) {
|
|
|
|
if (NativePath[i] == '#') // Handle '#' the broken gcc way.
|
2015-05-14 06:33:50 +08:00
|
|
|
OS << '\\';
|
2018-09-13 22:27:32 +08:00
|
|
|
else if (NativePath[i] == ' ') { // Handle space correctly.
|
2011-02-17 10:14:49 +08:00
|
|
|
OS << '\\';
|
2015-05-14 05:18:15 +08:00
|
|
|
unsigned j = i;
|
2018-09-13 22:27:32 +08:00
|
|
|
while (j > 0 && NativePath[--j] == '\\')
|
2015-05-14 05:18:15 +08:00
|
|
|
OS << '\\';
|
2018-09-13 22:27:32 +08:00
|
|
|
} else if (NativePath[i] == '$') // $ is escaped by $$.
|
2013-04-02 21:38:48 +08:00
|
|
|
OS << '$';
|
2018-09-13 22:27:32 +08:00
|
|
|
OS << NativePath[i];
|
2011-02-17 10:14:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 01:07:36 +08:00
|
|
|
void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) {
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-26 02:17:04 +08:00
|
|
|
std::error_code EC;
|
2019-08-05 13:43:48 +08:00
|
|
|
llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_Text);
|
2014-08-26 02:17:04 +08:00
|
|
|
if (EC) {
|
2019-06-20 01:07:36 +08:00
|
|
|
Diags.Report(diag::err_fe_error_opening) << OutputFile << EC.message();
|
2011-11-21 08:01:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-22 02:24:55 +08:00
|
|
|
outputDependencyFile(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
|
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;
|
|
|
|
|
2016-11-17 03:24:10 +08:00
|
|
|
for (StringRef Target : Targets) {
|
|
|
|
unsigned N = Target.size();
|
2009-01-12 03:28:34 +08:00
|
|
|
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.
|
2016-11-17 03:24:10 +08:00
|
|
|
OS << Target;
|
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.
|
2019-06-20 01:07:36 +08:00
|
|
|
ArrayRef<std::string> Files = getDependencies();
|
2016-11-17 03:24:10 +08:00
|
|
|
for (StringRef File : Files) {
|
2008-10-28 04:01:06 +08:00
|
|
|
// 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.
|
2016-11-17 03:24:10 +08:00
|
|
|
unsigned N = File.size();
|
2008-10-28 04:01:06 +08:00
|
|
|
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 << ' ';
|
2016-11-17 03:24:10 +08:00
|
|
|
PrintFilename(OS, File, OutputFormat);
|
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()) {
|
2018-05-29 21:07:58 +08:00
|
|
|
unsigned Index = 0;
|
|
|
|
for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
|
|
|
|
if (Index++ == InputFileIndex)
|
|
|
|
continue;
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << '\n';
|
2017-01-15 05:12:08 +08:00
|
|
|
PrintFilename(OS, *I, OutputFormat);
|
2011-11-21 08:01:14 +08:00
|
|
|
OS << ":\n";
|
2008-10-28 04:01:06 +08:00
|
|
|
}
|
2008-10-25 06:12:41 +08:00
|
|
|
}
|
|
|
|
}
|