ModuleDependencyCollector.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/StringSet.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. std::error_code copyToRoot(StringRef Src);
  26. public:
  27. ModuleDependencyListener(ModuleDependencyCollector &Collector)
  28. : Collector(Collector) {}
  29. bool needsInputFileVisitation() override { return true; }
  30. bool needsSystemInputFileVisitation() override { return true; }
  31. bool visitInputFile(StringRef Filename, bool IsSystem,
  32. bool IsOverridden) override;
  33. };
  34. }
  35. void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
  36. R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
  37. }
  38. void ModuleDependencyCollector::writeFileMap() {
  39. if (Seen.empty())
  40. return;
  41. SmallString<256> Dest = getDest();
  42. llvm::sys::path::append(Dest, "vfs.yaml");
  43. std::error_code EC;
  44. llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
  45. if (EC) {
  46. setHasErrors();
  47. return;
  48. }
  49. VFSWriter.write(OS);
  50. }
  51. std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) {
  52. using namespace llvm::sys;
  53. // We need an absolute path to append to the root.
  54. SmallString<256> AbsoluteSrc = Src;
  55. fs::make_absolute(AbsoluteSrc);
  56. // Canonicalize to a native path to avoid mixed separator styles.
  57. path::native(AbsoluteSrc);
  58. // TODO: We probably need to handle .. as well as . in order to have valid
  59. // input to the YAMLVFSWriter.
  60. FileManager::removeDotPaths(AbsoluteSrc);
  61. // Build the destination path.
  62. SmallString<256> Dest = Collector.getDest();
  63. path::append(Dest, path::relative_path(AbsoluteSrc));
  64. // Copy the file into place.
  65. if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
  66. /*IgnoreExisting=*/true))
  67. return EC;
  68. if (std::error_code EC = fs::copy_file(AbsoluteSrc, Dest))
  69. return EC;
  70. // Use the absolute path under the root for the file mapping.
  71. Collector.addFileMapping(AbsoluteSrc, Dest);
  72. return std::error_code();
  73. }
  74. bool ModuleDependencyListener::visitInputFile(StringRef Filename, bool IsSystem,
  75. bool IsOverridden) {
  76. if (Collector.insertSeen(Filename))
  77. if (copyToRoot(Filename))
  78. Collector.setHasErrors();
  79. return true;
  80. }