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