blob: df5e19df91ddb5aefc1da245186becc57aafd344 [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 Lopesc9daaae2016-03-16 04:39:38 +000016#include "llvm/ADT/StringMap.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000017#include "llvm/ADT/iterator_range.h"
Bruno Cardoso Lopesc9daaae2016-03-16 04:39:38 +000018#include "llvm/Config/config.h"
Justin Bognercbda32f2014-06-19 19:49:28 +000019#include "llvm/Support/FileSystem.h"
Justin Bogner86d12592014-06-19 19:36:03 +000020#include "llvm/Support/Path.h"
21#include "llvm/Support/raw_ostream.h"
22
23using namespace clang;
24
25namespace {
26/// Private implementation for ModuleDependencyCollector
27class ModuleDependencyListener : public ASTReaderListener {
28 ModuleDependencyCollector &Collector;
Bruno Cardoso Lopesc9daaae2016-03-16 04:39:38 +000029 llvm::StringMap<std::string> SymLinkMap;
Justin Bogner86d12592014-06-19 19:36:03 +000030
Bruno Cardoso Lopesc9daaae2016-03-16 04:39:38 +000031 bool getRealPath(StringRef SrcPath, SmallVectorImpl<char> &Result);
Justin Bogner86d12592014-06-19 19:36:03 +000032 std::error_code copyToRoot(StringRef Src);
33public:
34 ModuleDependencyListener(ModuleDependencyCollector &Collector)
35 : Collector(Collector) {}
36 bool needsInputFileVisitation() override { return true; }
37 bool needsSystemInputFileVisitation() override { return true; }
Richard Smith216a3bd2015-08-13 17:57:10 +000038 bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
39 bool IsExplicitModule) override;
Justin Bogner86d12592014-06-19 19:36:03 +000040};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000041}
Justin Bogner86d12592014-06-19 19:36:03 +000042
43void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +000044 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
Justin Bogner86d12592014-06-19 19:36:03 +000045}
46
47void ModuleDependencyCollector::writeFileMap() {
48 if (Seen.empty())
49 return;
50
51 SmallString<256> Dest = getDest();
52 llvm::sys::path::append(Dest, "vfs.yaml");
53
Rafael Espindoladae941a2014-08-25 18:17:04 +000054 std::error_code EC;
55 llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
56 if (EC) {
Justin Bogner86d12592014-06-19 19:36:03 +000057 setHasErrors();
58 return;
59 }
60 VFSWriter.write(OS);
61}
62
Bruno Cardoso Lopesc9daaae2016-03-16 04:39:38 +000063// TODO: move this to Support/Path.h?
64static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
65#ifdef HAVE_REALPATH
66 char CanonicalPath[PATH_MAX];
67
68 // TODO: emit a warning in case this fails...?
69 if (!realpath(SrcPath.str().c_str(), CanonicalPath))
70 return false;
71
72 SmallString<256> RPath(CanonicalPath);
73 RealPath.swap(RPath);
74 return true;
75#else
76 // FIXME: Add support for systems without realpath.
77 return false;
78#endif
79}
80
81bool ModuleDependencyListener::getRealPath(StringRef SrcPath,
82 SmallVectorImpl<char> &Result) {
83 using namespace llvm::sys;
84 SmallString<256> RealPath;
85 StringRef FileName = path::filename(SrcPath);
86 std::string Dir = path::parent_path(SrcPath).str();
87 auto DirWithSymLink = SymLinkMap.find(Dir);
88
89 // Use real_path to fix any symbolic link component present in a path.
90 // Computing the real path is expensive, cache the search through the
91 // parent path directory.
92 if (DirWithSymLink == SymLinkMap.end()) {
93 if (!real_path(Dir, RealPath))
94 return false;
95 SymLinkMap[Dir] = RealPath.str();
96 } else {
97 RealPath = DirWithSymLink->second;
98 }
99
100 path::append(RealPath, FileName);
101 Result.swap(RealPath);
102 return true;
103}
104
Justin Bogner86d12592014-06-19 19:36:03 +0000105std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) {
106 using namespace llvm::sys;
107
108 // We need an absolute path to append to the root.
109 SmallString<256> AbsoluteSrc = Src;
110 fs::make_absolute(AbsoluteSrc);
Justin Bogner93e3cfc2014-12-12 23:12:27 +0000111 // Canonicalize to a native path to avoid mixed separator styles.
112 path::native(AbsoluteSrc);
Bruno Cardoso Lopesc9daaae2016-03-16 04:39:38 +0000113 // Remove redundant leading "./" pieces and consecutive separators.
114 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
115
116 // Canonicalize path by removing "..", "." components.
117 SmallString<256> CanonicalPath = AbsoluteSrc;
118 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
119
120 // If a ".." component is present after a symlink component, remove_dots may
121 // lead to the wrong real destination path. Let the source be canonicalized
122 // like that but make sure the destination uses the real path.
123 bool HasDotDotInPath =
124 std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0;
125 SmallString<256> RealPath;
126 bool HasRemovedSymlinkComponent = HasDotDotInPath &&
127 getRealPath(AbsoluteSrc, RealPath) &&
128 !StringRef(CanonicalPath).equals(RealPath);
Justin Bognere1552f62014-06-20 03:28:46 +0000129
Justin Bogner86d12592014-06-19 19:36:03 +0000130 // Build the destination path.
131 SmallString<256> Dest = Collector.getDest();
Bruno Cardoso Lopesc9daaae2016-03-16 04:39:38 +0000132 path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath
133 : CanonicalPath));
Justin Bogner86d12592014-06-19 19:36:03 +0000134
135 // Copy the file into place.
136 if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
137 /*IgnoreExisting=*/true))
138 return EC;
Bruno Cardoso Lopesc9daaae2016-03-16 04:39:38 +0000139 if (std::error_code EC = fs::copy_file(
140 HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest))
Justin Bogner86d12592014-06-19 19:36:03 +0000141 return EC;
Bruno Cardoso Lopesc9daaae2016-03-16 04:39:38 +0000142
143 // Use the canonical path under the root for the file mapping. Also create
144 // an additional entry for the real path.
145 Collector.addFileMapping(CanonicalPath, Dest);
146 if (HasRemovedSymlinkComponent)
147 Collector.addFileMapping(RealPath, Dest);
148
Justin Bogner86d12592014-06-19 19:36:03 +0000149 return std::error_code();
150}
151
152bool ModuleDependencyListener::visitInputFile(StringRef Filename, bool IsSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000153 bool IsOverridden,
154 bool IsExplicitModule) {
Justin Bogner86d12592014-06-19 19:36:03 +0000155 if (Collector.insertSeen(Filename))
156 if (copyToRoot(Filename))
157 Collector.setHasErrors();
158 return true;
159}