blob: fc3958f601a4a986fb59976e0ca7d16e742cd889 [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"
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +000016#include "llvm/ADT/StringMap.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000017#include "llvm/ADT/iterator_range.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;
Justin Bogner86d12592014-06-19 19:36:03 +000028public:
29 ModuleDependencyListener(ModuleDependencyCollector &Collector)
30 : Collector(Collector) {}
31 bool needsInputFileVisitation() override { return true; }
32 bool needsSystemInputFileVisitation() override { return true; }
Richard Smith216a3bd2015-08-13 17:57:10 +000033 bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +000034 bool IsExplicitModule) override {
35 Collector.addFile(Filename);
36 return true;
37 }
Justin Bogner86d12592014-06-19 19:36:03 +000038};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000039}
Justin Bogner86d12592014-06-19 19:36:03 +000040
41void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +000042 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
Justin Bogner86d12592014-06-19 19:36:03 +000043}
44
45void ModuleDependencyCollector::writeFileMap() {
46 if (Seen.empty())
47 return;
48
49 SmallString<256> Dest = getDest();
50 llvm::sys::path::append(Dest, "vfs.yaml");
51
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +000052 // Default to use relative overlay directories in the VFS yaml file. This
53 // allows crash reproducer scripts to work across machines.
54 VFSWriter.setOverlayDir(getDest());
55
Rafael Espindoladae941a2014-08-25 18:17:04 +000056 std::error_code EC;
57 llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
58 if (EC) {
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +000059 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +000060 return;
61 }
62 VFSWriter.write(OS);
63}
64
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +000065// TODO: move this to Support/Path.h and check for HAVE_REALPATH?
66static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
67#ifdef LLVM_ON_UNIX
68 char CanonicalPath[PATH_MAX];
69
70 // TODO: emit a warning in case this fails...?
71 if (!realpath(SrcPath.str().c_str(), CanonicalPath))
72 return false;
73
74 SmallString<256> RPath(CanonicalPath);
75 RealPath.swap(RPath);
76 return true;
77#else
78 // FIXME: Add support for systems without realpath.
79 return false;
80#endif
81}
82
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +000083bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
84 SmallVectorImpl<char> &Result) {
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +000085 using namespace llvm::sys;
86 SmallString<256> RealPath;
87 StringRef FileName = path::filename(SrcPath);
88 std::string Dir = path::parent_path(SrcPath).str();
89 auto DirWithSymLink = SymLinkMap.find(Dir);
90
91 // Use real_path to fix any symbolic link component present in a path.
92 // Computing the real path is expensive, cache the search through the
93 // parent path directory.
94 if (DirWithSymLink == SymLinkMap.end()) {
95 if (!real_path(Dir, RealPath))
96 return false;
97 SymLinkMap[Dir] = RealPath.str();
98 } else {
99 RealPath = DirWithSymLink->second;
100 }
101
102 path::append(RealPath, FileName);
103 Result.swap(RealPath);
104 return true;
105}
106
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000107std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
Justin Bogner86d12592014-06-19 19:36:03 +0000108 using namespace llvm::sys;
109
110 // We need an absolute path to append to the root.
111 SmallString<256> AbsoluteSrc = Src;
112 fs::make_absolute(AbsoluteSrc);
Justin Bogner93e3cfc2014-12-12 23:12:27 +0000113 // Canonicalize to a native path to avoid mixed separator styles.
114 path::native(AbsoluteSrc);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000115 // Remove redundant leading "./" pieces and consecutive separators.
116 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
117
118 // Canonicalize path by removing "..", "." components.
119 SmallString<256> CanonicalPath = AbsoluteSrc;
120 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
121
122 // If a ".." component is present after a symlink component, remove_dots may
123 // lead to the wrong real destination path. Let the source be canonicalized
124 // like that but make sure the destination uses the real path.
125 bool HasDotDotInPath =
126 std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0;
127 SmallString<256> RealPath;
128 bool HasRemovedSymlinkComponent = HasDotDotInPath &&
129 getRealPath(AbsoluteSrc, RealPath) &&
130 !StringRef(CanonicalPath).equals(RealPath);
Justin Bognere1552f62014-06-20 03:28:46 +0000131
Justin Bogner86d12592014-06-19 19:36:03 +0000132 // Build the destination path.
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000133 SmallString<256> Dest = getDest();
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000134 path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath
135 : CanonicalPath));
Justin Bogner86d12592014-06-19 19:36:03 +0000136
137 // Copy the file into place.
138 if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
139 /*IgnoreExisting=*/true))
140 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000141 if (std::error_code EC = fs::copy_file(
142 HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest))
Justin Bogner86d12592014-06-19 19:36:03 +0000143 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000144
145 // Use the canonical path under the root for the file mapping. Also create
146 // an additional entry for the real path.
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000147 addFileMapping(CanonicalPath, Dest);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000148 if (HasRemovedSymlinkComponent)
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000149 addFileMapping(RealPath, Dest);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000150
Justin Bogner86d12592014-06-19 19:36:03 +0000151 return std::error_code();
152}
153
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000154void ModuleDependencyCollector::addFile(StringRef Filename) {
155 if (insertSeen(Filename))
Justin Bogner86d12592014-06-19 19:36:03 +0000156 if (copyToRoot(Filename))
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000157 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000158}