ModuleDependencyCollector.cpp 5.4 KB

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