2014-06-20 03:36:03 +08:00
|
|
|
//===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Collect the dependencies of a set of modules.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/Utils.h"
|
|
|
|
#include "clang/Serialization/ASTReader.h"
|
2016-03-17 10:20:43 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2014-06-20 03:49:28 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2014-06-20 03:36:03 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Private implementation for ModuleDependencyCollector
|
|
|
|
class ModuleDependencyListener : public ASTReaderListener {
|
|
|
|
ModuleDependencyCollector &Collector;
|
2016-03-17 10:20:43 +08:00
|
|
|
llvm::StringMap<std::string> SymLinkMap;
|
2014-06-20 03:36:03 +08:00
|
|
|
|
2016-03-17 10:20:43 +08:00
|
|
|
bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result);
|
2014-06-20 03:36:03 +08:00
|
|
|
std::error_code copyToRoot(StringRef Src);
|
|
|
|
public:
|
|
|
|
ModuleDependencyListener(ModuleDependencyCollector &Collector)
|
|
|
|
: Collector(Collector) {}
|
|
|
|
bool needsInputFileVisitation() override { return true; }
|
|
|
|
bool needsSystemInputFileVisitation() override { return true; }
|
2015-08-14 01:57:10 +08:00
|
|
|
bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
|
|
|
|
bool IsExplicitModule) override;
|
2014-06-20 03:36:03 +08:00
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2014-06-20 03:36:03 +08:00
|
|
|
|
|
|
|
void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
|
2014-08-11 00:54:39 +08:00
|
|
|
R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
|
2014-06-20 03:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleDependencyCollector::writeFileMap() {
|
|
|
|
if (Seen.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallString<256> Dest = getDest();
|
|
|
|
llvm::sys::path::append(Dest, "vfs.yaml");
|
|
|
|
|
2014-08-26 02:17:04 +08:00
|
|
|
std::error_code EC;
|
|
|
|
llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
|
|
|
|
if (EC) {
|
2014-06-20 03:36:03 +08:00
|
|
|
setHasErrors();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VFSWriter.write(OS);
|
|
|
|
}
|
|
|
|
|
2016-03-17 10:20:43 +08:00
|
|
|
// TODO: move this to Support/Path.h and check for HAVE_REALPATH?
|
|
|
|
static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
|
|
|
|
#ifdef LLVM_ON_UNIX
|
|
|
|
char CanonicalPath[PATH_MAX];
|
|
|
|
|
|
|
|
// TODO: emit a warning in case this fails...?
|
|
|
|
if (!realpath(SrcPath.str().c_str(), CanonicalPath))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallString<256> RPath(CanonicalPath);
|
|
|
|
RealPath.swap(RPath);
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
// FIXME: Add support for systems without realpath.
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleDependencyListener::getRealPath(StringRef SrcPath,
|
|
|
|
SmallVectorImpl<char> &Result) {
|
|
|
|
using namespace llvm::sys;
|
|
|
|
SmallString<256> RealPath;
|
|
|
|
StringRef FileName = path::filename(SrcPath);
|
|
|
|
std::string Dir = path::parent_path(SrcPath).str();
|
|
|
|
auto DirWithSymLink = SymLinkMap.find(Dir);
|
|
|
|
|
|
|
|
// Use real_path to fix any symbolic link component present in a path.
|
|
|
|
// Computing the real path is expensive, cache the search through the
|
|
|
|
// parent path directory.
|
|
|
|
if (DirWithSymLink == SymLinkMap.end()) {
|
|
|
|
if (!real_path(Dir, RealPath))
|
|
|
|
return false;
|
|
|
|
SymLinkMap[Dir] = RealPath.str();
|
|
|
|
} else {
|
|
|
|
RealPath = DirWithSymLink->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
path::append(RealPath, FileName);
|
|
|
|
Result.swap(RealPath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-20 03:36:03 +08:00
|
|
|
std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) {
|
|
|
|
using namespace llvm::sys;
|
|
|
|
|
|
|
|
// We need an absolute path to append to the root.
|
|
|
|
SmallString<256> AbsoluteSrc = Src;
|
|
|
|
fs::make_absolute(AbsoluteSrc);
|
2014-12-13 07:12:27 +08:00
|
|
|
// Canonicalize to a native path to avoid mixed separator styles.
|
|
|
|
path::native(AbsoluteSrc);
|
2016-03-17 10:20:43 +08:00
|
|
|
// Remove redundant leading "./" pieces and consecutive separators.
|
|
|
|
AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
|
|
|
|
|
|
|
|
// Canonicalize path by removing "..", "." components.
|
|
|
|
SmallString<256> CanonicalPath = AbsoluteSrc;
|
|
|
|
path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
|
|
|
|
|
|
|
|
// If a ".." component is present after a symlink component, remove_dots may
|
|
|
|
// lead to the wrong real destination path. Let the source be canonicalized
|
|
|
|
// like that but make sure the destination uses the real path.
|
|
|
|
bool HasDotDotInPath =
|
|
|
|
std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0;
|
|
|
|
SmallString<256> RealPath;
|
|
|
|
bool HasRemovedSymlinkComponent = HasDotDotInPath &&
|
|
|
|
getRealPath(AbsoluteSrc, RealPath) &&
|
|
|
|
!StringRef(CanonicalPath).equals(RealPath);
|
2014-06-20 11:28:46 +08:00
|
|
|
|
2014-06-20 03:36:03 +08:00
|
|
|
// Build the destination path.
|
|
|
|
SmallString<256> Dest = Collector.getDest();
|
2016-03-17 10:20:43 +08:00
|
|
|
path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath
|
|
|
|
: CanonicalPath));
|
2014-06-20 03:36:03 +08:00
|
|
|
|
|
|
|
// Copy the file into place.
|
|
|
|
if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
|
|
|
|
/*IgnoreExisting=*/true))
|
|
|
|
return EC;
|
2016-03-17 10:20:43 +08:00
|
|
|
if (std::error_code EC = fs::copy_file(
|
|
|
|
HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest))
|
2014-06-20 03:36:03 +08:00
|
|
|
return EC;
|
2016-03-17 10:20:43 +08:00
|
|
|
|
|
|
|
// Use the canonical path under the root for the file mapping. Also create
|
|
|
|
// an additional entry for the real path.
|
|
|
|
Collector.addFileMapping(CanonicalPath, Dest);
|
|
|
|
if (HasRemovedSymlinkComponent)
|
|
|
|
Collector.addFileMapping(RealPath, Dest);
|
|
|
|
|
2014-06-20 03:36:03 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleDependencyListener::visitInputFile(StringRef Filename, bool IsSystem,
|
2015-08-14 01:57:10 +08:00
|
|
|
bool IsOverridden,
|
|
|
|
bool IsExplicitModule) {
|
2014-06-20 03:36:03 +08:00
|
|
|
if (Collector.insertSeen(Filename))
|
|
|
|
if (copyToRoot(Filename))
|
|
|
|
Collector.setHasErrors();
|
|
|
|
return true;
|
|
|
|
}
|