blob: 1ac7e36eff1f35fe4fae6a2bef02d49439e9a698 [file] [log] [blame]
Justin Bogner86d12592014-06-19 19:36:03 +00001//===--- 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
14#include "clang/Frontend/Utils.h"
15#include "clang/Serialization/ASTReader.h"
16#include "llvm/ADT/iterator_range.h"
17#include "llvm/ADT/StringSet.h"
Justin Bognercbda32f2014-06-19 19:49:28 +000018#include "llvm/Support/FileSystem.h"
Justin Bogner86d12592014-06-19 19:36:03 +000019#include "llvm/Support/Path.h"
20#include "llvm/Support/raw_ostream.h"
21
22using namespace clang;
23
24namespace {
25/// Private implementation for ModuleDependencyCollector
26class ModuleDependencyListener : public ASTReaderListener {
27 ModuleDependencyCollector &Collector;
28
29 std::error_code copyToRoot(StringRef Src);
30public:
31 ModuleDependencyListener(ModuleDependencyCollector &Collector)
32 : Collector(Collector) {}
33 bool needsInputFileVisitation() override { return true; }
34 bool needsSystemInputFileVisitation() override { return true; }
35 bool visitInputFile(StringRef Filename, bool IsSystem,
36 bool IsOverridden) override;
37};
38}
39
40void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +000041 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
Justin Bogner86d12592014-06-19 19:36:03 +000042}
43
44void ModuleDependencyCollector::writeFileMap() {
45 if (Seen.empty())
46 return;
47
48 SmallString<256> Dest = getDest();
49 llvm::sys::path::append(Dest, "vfs.yaml");
50
Rafael Espindoladae941a2014-08-25 18:17:04 +000051 std::error_code EC;
52 llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
53 if (EC) {
Justin Bogner86d12592014-06-19 19:36:03 +000054 setHasErrors();
55 return;
56 }
57 VFSWriter.write(OS);
58}
59
Justin Bogner86d12592014-06-19 19:36:03 +000060std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) {
61 using namespace llvm::sys;
62
63 // We need an absolute path to append to the root.
64 SmallString<256> AbsoluteSrc = Src;
65 fs::make_absolute(AbsoluteSrc);
Richard Smith54cc3c22014-12-11 20:50:24 +000066 FileManager::removeDotPaths(AbsoluteSrc);
Justin Bognere1552f62014-06-20 03:28:46 +000067
Justin Bogner86d12592014-06-19 19:36:03 +000068 // Build the destination path.
69 SmallString<256> Dest = Collector.getDest();
Justin Bognere1552f62014-06-20 03:28:46 +000070 path::append(Dest, path::relative_path(AbsoluteSrc));
Justin Bogner86d12592014-06-19 19:36:03 +000071
72 // Copy the file into place.
73 if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
74 /*IgnoreExisting=*/true))
75 return EC;
76 if (std::error_code EC = fs::copy_file(AbsoluteSrc.str(), Dest.str()))
77 return EC;
78 // Use the absolute path under the root for the file mapping.
Justin Bognere1552f62014-06-20 03:28:46 +000079 Collector.addFileMapping(AbsoluteSrc.str(), Dest.str());
Justin Bogner86d12592014-06-19 19:36:03 +000080 return std::error_code();
81}
82
83bool ModuleDependencyListener::visitInputFile(StringRef Filename, bool IsSystem,
84 bool IsOverridden) {
85 if (Collector.insertSeen(Filename))
86 if (copyToRoot(Filename))
87 Collector.setHasErrors();
88 return true;
89}