ModuleDependencyCollector.cpp 5.6 KB

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