blob: 1678d1801d2d2aea3d9fd9200f316802ea34f60a [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
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +000082static bool isCaseSensitivePath(StringRef Path) {
83 SmallString<PATH_MAX> TmpDest = Path, UpperDest, RealDest;
84 // Remove component traversals, links, etc.
85 if (!real_path(Path, TmpDest))
86 return true; // Current default value in vfs.yaml
87 Path = TmpDest;
88
89 // Change path to all upper case and ask for its real path, if the latter
90 // exists and is equal to Path, it's not case sensitive. Default to case
91 // sensitive in the absense of realpath, since this is what the VFSWriter
92 // already expects when sensitivity isn't setup.
93 for (auto &C : Path)
94 UpperDest.push_back(std::toupper(C));
95 if (real_path(UpperDest, RealDest) && Path.equals(RealDest))
96 return false;
97 return true;
98}
99
Justin Bogner86d12592014-06-19 19:36:03 +0000100void ModuleDependencyCollector::writeFileMap() {
101 if (Seen.empty())
102 return;
103
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000104 StringRef VFSDir = getDest();
Justin Bogner86d12592014-06-19 19:36:03 +0000105
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +0000106 // Default to use relative overlay directories in the VFS yaml file. This
107 // allows crash reproducer scripts to work across machines.
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000108 VFSWriter.setOverlayDir(VFSDir);
109
110 // Explicitly set case sensitivity for the YAML writer. For that, find out
111 // the sensitivity at the path where the headers all collected to.
112 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +0000113
Rafael Espindoladae941a2014-08-25 18:17:04 +0000114 std::error_code EC;
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000115 SmallString<256> YAMLPath = VFSDir;
116 llvm::sys::path::append(YAMLPath, "vfs.yaml");
117 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text);
Rafael Espindoladae941a2014-08-25 18:17:04 +0000118 if (EC) {
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000119 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000120 return;
121 }
122 VFSWriter.write(OS);
123}
124
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000125bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
126 SmallVectorImpl<char> &Result) {
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000127 using namespace llvm::sys;
128 SmallString<256> RealPath;
129 StringRef FileName = path::filename(SrcPath);
130 std::string Dir = path::parent_path(SrcPath).str();
131 auto DirWithSymLink = SymLinkMap.find(Dir);
132
133 // Use real_path to fix any symbolic link component present in a path.
134 // Computing the real path is expensive, cache the search through the
135 // parent path directory.
136 if (DirWithSymLink == SymLinkMap.end()) {
137 if (!real_path(Dir, RealPath))
138 return false;
139 SymLinkMap[Dir] = RealPath.str();
140 } else {
141 RealPath = DirWithSymLink->second;
142 }
143
144 path::append(RealPath, FileName);
145 Result.swap(RealPath);
146 return true;
147}
148
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000149std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
Justin Bogner86d12592014-06-19 19:36:03 +0000150 using namespace llvm::sys;
151
152 // We need an absolute path to append to the root.
153 SmallString<256> AbsoluteSrc = Src;
154 fs::make_absolute(AbsoluteSrc);
Justin Bogner93e3cfc2014-12-12 23:12:27 +0000155 // Canonicalize to a native path to avoid mixed separator styles.
156 path::native(AbsoluteSrc);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000157 // Remove redundant leading "./" pieces and consecutive separators.
158 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
159
160 // Canonicalize path by removing "..", "." components.
161 SmallString<256> CanonicalPath = AbsoluteSrc;
162 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
163
164 // If a ".." component is present after a symlink component, remove_dots may
165 // lead to the wrong real destination path. Let the source be canonicalized
166 // like that but make sure the destination uses the real path.
167 bool HasDotDotInPath =
168 std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0;
169 SmallString<256> RealPath;
170 bool HasRemovedSymlinkComponent = HasDotDotInPath &&
171 getRealPath(AbsoluteSrc, RealPath) &&
172 !StringRef(CanonicalPath).equals(RealPath);
Justin Bognere1552f62014-06-20 03:28:46 +0000173
Justin Bogner86d12592014-06-19 19:36:03 +0000174 // Build the destination path.
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000175 SmallString<256> Dest = getDest();
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000176 path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath
177 : CanonicalPath));
Justin Bogner86d12592014-06-19 19:36:03 +0000178
179 // Copy the file into place.
180 if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
181 /*IgnoreExisting=*/true))
182 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000183 if (std::error_code EC = fs::copy_file(
184 HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest))
Justin Bogner86d12592014-06-19 19:36:03 +0000185 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000186
187 // Use the canonical path under the root for the file mapping. Also create
188 // an additional entry for the real path.
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000189 addFileMapping(CanonicalPath, Dest);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000190 if (HasRemovedSymlinkComponent)
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000191 addFileMapping(RealPath, Dest);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000192
Justin Bogner86d12592014-06-19 19:36:03 +0000193 return std::error_code();
194}
195
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000196void ModuleDependencyCollector::addFile(StringRef Filename) {
197 if (insertSeen(Filename))
Justin Bogner86d12592014-06-19 19:36:03 +0000198 if (copyToRoot(Filename))
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000199 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000200}