ModuleDependencyCollector.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Collect the dependencies of a set of modules.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Frontend/Utils.h"
  14. #include "clang/Serialization/ASTReader.h"
  15. #include "llvm/ADT/StringMap.h"
  16. #include "llvm/ADT/iterator_range.h"
  17. #include "llvm/Support/FileSystem.h"
  18. #include "llvm/Support/Path.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace clang;
  21. namespace {
  22. /// Private implementation for ModuleDependencyCollector
  23. class ModuleDependencyListener : public ASTReaderListener {
  24. ModuleDependencyCollector &Collector;
  25. public:
  26. ModuleDependencyListener(ModuleDependencyCollector &Collector)
  27. : Collector(Collector) {}
  28. bool needsInputFileVisitation() override { return true; }
  29. bool needsSystemInputFileVisitation() override { return true; }
  30. bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
  31. bool IsExplicitModule) override {
  32. Collector.addFile(Filename);
  33. return true;
  34. }
  35. };
  36. }
  37. void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
  38. R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
  39. }
  40. void ModuleDependencyCollector::writeFileMap() {
  41. if (Seen.empty())
  42. return;
  43. SmallString<256> Dest = getDest();
  44. llvm::sys::path::append(Dest, "vfs.yaml");
  45. // Default to use relative overlay directories in the VFS yaml file. This
  46. // allows crash reproducer scripts to work across machines.
  47. VFSWriter.setOverlayDir(getDest());
  48. std::error_code EC;
  49. llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
  50. if (EC) {
  51. HasErrors = true;
  52. return;
  53. }
  54. VFSWriter.write(OS);
  55. }
  56. // TODO: move this to Support/Path.h and check for HAVE_REALPATH?
  57. static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
  58. #ifdef LLVM_ON_UNIX
  59. char CanonicalPath[PATH_MAX];
  60. // TODO: emit a warning in case this fails...?
  61. if (!realpath(SrcPath.str().c_str(), CanonicalPath))
  62. return false;
  63. SmallString<256> RPath(CanonicalPath);
  64. RealPath.swap(RPath);
  65. return true;
  66. #else
  67. // FIXME: Add support for systems without realpath.
  68. return false;
  69. #endif
  70. }
  71. bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
  72. SmallVectorImpl<char> &Result) {
  73. using namespace llvm::sys;
  74. SmallString<256> RealPath;
  75. StringRef FileName = path::filename(SrcPath);
  76. std::string Dir = path::parent_path(SrcPath).str();
  77. auto DirWithSymLink = SymLinkMap.find(Dir);
  78. // Use real_path to fix any symbolic link component present in a path.
  79. // Computing the real path is expensive, cache the search through the
  80. // parent path directory.
  81. if (DirWithSymLink == SymLinkMap.end()) {
  82. if (!real_path(Dir, RealPath))
  83. return false;
  84. SymLinkMap[Dir] = RealPath.str();
  85. } else {
  86. RealPath = DirWithSymLink->second;
  87. }
  88. path::append(RealPath, FileName);
  89. Result.swap(RealPath);
  90. return true;
  91. }
  92. std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
  93. using namespace llvm::sys;
  94. // We need an absolute path to append to the root.
  95. SmallString<256> AbsoluteSrc = Src;
  96. fs::make_absolute(AbsoluteSrc);
  97. // Canonicalize to a native path to avoid mixed separator styles.
  98. path::native(AbsoluteSrc);
  99. // Remove redundant leading "./" pieces and consecutive separators.
  100. AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
  101. // Canonicalize path by removing "..", "." components.
  102. SmallString<256> CanonicalPath = AbsoluteSrc;
  103. path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
  104. // If a ".." component is present after a symlink component, remove_dots may
  105. // lead to the wrong real destination path. Let the source be canonicalized
  106. // like that but make sure the destination uses the real path.
  107. bool HasDotDotInPath =
  108. std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0;
  109. SmallString<256> RealPath;
  110. bool HasRemovedSymlinkComponent = HasDotDotInPath &&
  111. getRealPath(AbsoluteSrc, RealPath) &&
  112. !StringRef(CanonicalPath).equals(RealPath);
  113. // Build the destination path.
  114. SmallString<256> Dest = getDest();
  115. path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath
  116. : CanonicalPath));
  117. // Copy the file into place.
  118. if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
  119. /*IgnoreExisting=*/true))
  120. return EC;
  121. if (std::error_code EC = fs::copy_file(
  122. HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest))
  123. return EC;
  124. // Use the canonical path under the root for the file mapping. Also create
  125. // an additional entry for the real path.
  126. addFileMapping(CanonicalPath, Dest);
  127. if (HasRemovedSymlinkComponent)
  128. addFileMapping(RealPath, Dest);
  129. return std::error_code();
  130. }
  131. void ModuleDependencyCollector::addFile(StringRef Filename) {
  132. if (insertSeen(Filename))
  133. if (copyToRoot(Filename))
  134. HasErrors = true;
  135. }