blob: d9cf40af0f699d4b3a68d939c611c4b2ecca579d [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"
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000015#include "clang/Lex/Preprocessor.h"
Justin Bogner86d12592014-06-19 19:36:03 +000016#include "clang/Serialization/ASTReader.h"
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +000017#include "llvm/ADT/StringMap.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000018#include "llvm/ADT/iterator_range.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 {
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000026/// Private implementations for ModuleDependencyCollector
Justin Bogner86d12592014-06-19 19:36:03 +000027class ModuleDependencyListener : public ASTReaderListener {
28 ModuleDependencyCollector &Collector;
Justin Bogner86d12592014-06-19 19:36:03 +000029public:
30 ModuleDependencyListener(ModuleDependencyCollector &Collector)
31 : Collector(Collector) {}
32 bool needsInputFileVisitation() override { return true; }
33 bool needsSystemInputFileVisitation() override { return true; }
Richard Smith216a3bd2015-08-13 17:57:10 +000034 bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +000035 bool IsExplicitModule) override {
36 Collector.addFile(Filename);
37 return true;
38 }
Justin Bogner86d12592014-06-19 19:36:03 +000039};
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000040
41struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
42 ModuleDependencyCollector &Collector;
43 ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
44 : Collector(Collector) {}
45
46 void moduleMapAddHeader(const FileEntry &File) override {
47 StringRef HeaderPath = File.getName();
48 if (llvm::sys::path::is_absolute(HeaderPath))
49 Collector.addFile(HeaderPath);
50 }
51};
52
Alexander Kornienkoab9db512015-06-22 23:07:51 +000053}
Justin Bogner86d12592014-06-19 19:36:03 +000054
Bruno Cardoso Lopes7caebc12016-04-07 00:00:42 +000055// TODO: move this to Support/Path.h and check for HAVE_REALPATH?
56static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
57#ifdef LLVM_ON_UNIX
58 char CanonicalPath[PATH_MAX];
59
60 // TODO: emit a warning in case this fails...?
61 if (!realpath(SrcPath.str().c_str(), CanonicalPath))
62 return false;
63
64 SmallString<256> RPath(CanonicalPath);
65 RealPath.swap(RPath);
66 return true;
67#else
68 // FIXME: Add support for systems without realpath.
69 return false;
70#endif
71}
72
Justin Bogner86d12592014-06-19 19:36:03 +000073void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +000074 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
Justin Bogner86d12592014-06-19 19:36:03 +000075}
76
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000077void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
78 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
79 llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
80}
81
Justin Bogner86d12592014-06-19 19:36:03 +000082void ModuleDependencyCollector::writeFileMap() {
83 if (Seen.empty())
84 return;
85
86 SmallString<256> Dest = getDest();
87 llvm::sys::path::append(Dest, "vfs.yaml");
88
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +000089 // Default to use relative overlay directories in the VFS yaml file. This
90 // allows crash reproducer scripts to work across machines.
91 VFSWriter.setOverlayDir(getDest());
92
Rafael Espindoladae941a2014-08-25 18:17:04 +000093 std::error_code EC;
94 llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
95 if (EC) {
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +000096 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +000097 return;
98 }
99 VFSWriter.write(OS);
100}
101
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000102bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
103 SmallVectorImpl<char> &Result) {
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000104 using namespace llvm::sys;
105 SmallString<256> RealPath;
106 StringRef FileName = path::filename(SrcPath);
107 std::string Dir = path::parent_path(SrcPath).str();
108 auto DirWithSymLink = SymLinkMap.find(Dir);
109
110 // Use real_path to fix any symbolic link component present in a path.
111 // Computing the real path is expensive, cache the search through the
112 // parent path directory.
113 if (DirWithSymLink == SymLinkMap.end()) {
114 if (!real_path(Dir, RealPath))
115 return false;
116 SymLinkMap[Dir] = RealPath.str();
117 } else {
118 RealPath = DirWithSymLink->second;
119 }
120
121 path::append(RealPath, FileName);
122 Result.swap(RealPath);
123 return true;
124}
125
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000126std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
Justin Bogner86d12592014-06-19 19:36:03 +0000127 using namespace llvm::sys;
128
129 // We need an absolute path to append to the root.
130 SmallString<256> AbsoluteSrc = Src;
131 fs::make_absolute(AbsoluteSrc);
Justin Bogner93e3cfc2014-12-12 23:12:27 +0000132 // Canonicalize to a native path to avoid mixed separator styles.
133 path::native(AbsoluteSrc);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000134 // Remove redundant leading "./" pieces and consecutive separators.
135 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
136
137 // Canonicalize path by removing "..", "." components.
138 SmallString<256> CanonicalPath = AbsoluteSrc;
139 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
140
141 // If a ".." component is present after a symlink component, remove_dots may
142 // lead to the wrong real destination path. Let the source be canonicalized
143 // like that but make sure the destination uses the real path.
144 bool HasDotDotInPath =
145 std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0;
146 SmallString<256> RealPath;
147 bool HasRemovedSymlinkComponent = HasDotDotInPath &&
148 getRealPath(AbsoluteSrc, RealPath) &&
149 !StringRef(CanonicalPath).equals(RealPath);
Justin Bognere1552f62014-06-20 03:28:46 +0000150
Justin Bogner86d12592014-06-19 19:36:03 +0000151 // Build the destination path.
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000152 SmallString<256> Dest = getDest();
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000153 path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath
154 : CanonicalPath));
Justin Bogner86d12592014-06-19 19:36:03 +0000155
156 // Copy the file into place.
157 if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
158 /*IgnoreExisting=*/true))
159 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000160 if (std::error_code EC = fs::copy_file(
161 HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest))
Justin Bogner86d12592014-06-19 19:36:03 +0000162 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000163
164 // Use the canonical path under the root for the file mapping. Also create
165 // an additional entry for the real path.
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000166 addFileMapping(CanonicalPath, Dest);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000167 if (HasRemovedSymlinkComponent)
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000168 addFileMapping(RealPath, Dest);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000169
Justin Bogner86d12592014-06-19 19:36:03 +0000170 return std::error_code();
171}
172
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000173void ModuleDependencyCollector::addFile(StringRef Filename) {
174 if (insertSeen(Filename))
Justin Bogner86d12592014-06-19 19:36:03 +0000175 if (copyToRoot(Filename))
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000176 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000177}