blob: 3e6c0d2475015706b2779a2c8ee7d3dbfcb542c7 [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
Bruno Cardoso Lopes4775fcf2016-04-07 01:12:18 +000014#include "clang/Basic/CharInfo.h"
Justin Bogner86d12592014-06-19 19:36:03 +000015#include "clang/Frontend/Utils.h"
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000016#include "clang/Lex/Preprocessor.h"
Justin Bogner86d12592014-06-19 19:36:03 +000017#include "clang/Serialization/ASTReader.h"
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +000018#include "llvm/ADT/StringMap.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000019#include "llvm/ADT/iterator_range.h"
Justin Bognercbda32f2014-06-19 19:49:28 +000020#include "llvm/Support/FileSystem.h"
Justin Bogner86d12592014-06-19 19:36:03 +000021#include "llvm/Support/Path.h"
22#include "llvm/Support/raw_ostream.h"
23
24using namespace clang;
25
26namespace {
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000027/// Private implementations for ModuleDependencyCollector
Justin Bogner86d12592014-06-19 19:36:03 +000028class ModuleDependencyListener : public ASTReaderListener {
29 ModuleDependencyCollector &Collector;
Justin Bogner86d12592014-06-19 19:36:03 +000030public:
31 ModuleDependencyListener(ModuleDependencyCollector &Collector)
32 : Collector(Collector) {}
33 bool needsInputFileVisitation() override { return true; }
34 bool needsSystemInputFileVisitation() override { return true; }
Richard Smith216a3bd2015-08-13 17:57:10 +000035 bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +000036 bool IsExplicitModule) override {
37 Collector.addFile(Filename);
38 return true;
39 }
Justin Bogner86d12592014-06-19 19:36:03 +000040};
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000041
42struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
43 ModuleDependencyCollector &Collector;
44 ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
45 : Collector(Collector) {}
46
47 void moduleMapAddHeader(const FileEntry &File) override {
48 StringRef HeaderPath = File.getName();
49 if (llvm::sys::path::is_absolute(HeaderPath))
50 Collector.addFile(HeaderPath);
51 }
52};
53
Alexander Kornienkoab9db512015-06-22 23:07:51 +000054}
Justin Bogner86d12592014-06-19 19:36:03 +000055
Bruno Cardoso Lopes7caebc12016-04-07 00:00:42 +000056// TODO: move this to Support/Path.h and check for HAVE_REALPATH?
57static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
58#ifdef LLVM_ON_UNIX
59 char CanonicalPath[PATH_MAX];
60
61 // TODO: emit a warning in case this fails...?
62 if (!realpath(SrcPath.str().c_str(), CanonicalPath))
63 return false;
64
65 SmallString<256> RPath(CanonicalPath);
66 RealPath.swap(RPath);
67 return true;
68#else
69 // FIXME: Add support for systems without realpath.
70 return false;
71#endif
72}
73
Justin Bogner86d12592014-06-19 19:36:03 +000074void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +000075 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
Justin Bogner86d12592014-06-19 19:36:03 +000076}
77
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000078void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
79 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
80 llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
81}
82
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +000083static bool isCaseSensitivePath(StringRef Path) {
Sean Silva72af4722016-04-07 01:58:14 +000084 SmallString<256> TmpDest = Path, UpperDest, RealDest;
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +000085 // Remove component traversals, links, etc.
86 if (!real_path(Path, TmpDest))
87 return true; // Current default value in vfs.yaml
88 Path = TmpDest;
89
90 // Change path to all upper case and ask for its real path, if the latter
91 // exists and is equal to Path, it's not case sensitive. Default to case
92 // sensitive in the absense of realpath, since this is what the VFSWriter
93 // already expects when sensitivity isn't setup.
94 for (auto &C : Path)
Bruno Cardoso Lopes4775fcf2016-04-07 01:12:18 +000095 UpperDest.push_back(toUppercase(C));
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +000096 if (real_path(UpperDest, RealDest) && Path.equals(RealDest))
97 return false;
98 return true;
99}
100
Justin Bogner86d12592014-06-19 19:36:03 +0000101void ModuleDependencyCollector::writeFileMap() {
102 if (Seen.empty())
103 return;
104
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000105 StringRef VFSDir = getDest();
Justin Bogner86d12592014-06-19 19:36:03 +0000106
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +0000107 // Default to use relative overlay directories in the VFS yaml file. This
108 // allows crash reproducer scripts to work across machines.
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000109 VFSWriter.setOverlayDir(VFSDir);
110
111 // Explicitly set case sensitivity for the YAML writer. For that, find out
112 // the sensitivity at the path where the headers all collected to.
113 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +0000114
Rafael Espindoladae941a2014-08-25 18:17:04 +0000115 std::error_code EC;
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000116 SmallString<256> YAMLPath = VFSDir;
117 llvm::sys::path::append(YAMLPath, "vfs.yaml");
118 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text);
Rafael Espindoladae941a2014-08-25 18:17:04 +0000119 if (EC) {
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000120 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000121 return;
122 }
123 VFSWriter.write(OS);
124}
125
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000126bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
127 SmallVectorImpl<char> &Result) {
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000128 using namespace llvm::sys;
129 SmallString<256> RealPath;
130 StringRef FileName = path::filename(SrcPath);
131 std::string Dir = path::parent_path(SrcPath).str();
132 auto DirWithSymLink = SymLinkMap.find(Dir);
133
134 // Use real_path to fix any symbolic link component present in a path.
135 // Computing the real path is expensive, cache the search through the
136 // parent path directory.
137 if (DirWithSymLink == SymLinkMap.end()) {
138 if (!real_path(Dir, RealPath))
139 return false;
140 SymLinkMap[Dir] = RealPath.str();
141 } else {
142 RealPath = DirWithSymLink->second;
143 }
144
145 path::append(RealPath, FileName);
146 Result.swap(RealPath);
147 return true;
148}
149
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000150std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
Justin Bogner86d12592014-06-19 19:36:03 +0000151 using namespace llvm::sys;
152
153 // We need an absolute path to append to the root.
154 SmallString<256> AbsoluteSrc = Src;
155 fs::make_absolute(AbsoluteSrc);
Justin Bogner93e3cfc2014-12-12 23:12:27 +0000156 // Canonicalize to a native path to avoid mixed separator styles.
157 path::native(AbsoluteSrc);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000158 // Remove redundant leading "./" pieces and consecutive separators.
159 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
160
161 // Canonicalize path by removing "..", "." components.
162 SmallString<256> CanonicalPath = AbsoluteSrc;
163 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
164
165 // If a ".." component is present after a symlink component, remove_dots may
166 // lead to the wrong real destination path. Let the source be canonicalized
167 // like that but make sure the destination uses the real path.
168 bool HasDotDotInPath =
169 std::count(path::begin(AbsoluteSrc), path::end(AbsoluteSrc), "..") > 0;
170 SmallString<256> RealPath;
171 bool HasRemovedSymlinkComponent = HasDotDotInPath &&
172 getRealPath(AbsoluteSrc, RealPath) &&
173 !StringRef(CanonicalPath).equals(RealPath);
Justin Bognere1552f62014-06-20 03:28:46 +0000174
Justin Bogner86d12592014-06-19 19:36:03 +0000175 // Build the destination path.
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000176 SmallString<256> Dest = getDest();
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000177 path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath
178 : CanonicalPath));
Justin Bogner86d12592014-06-19 19:36:03 +0000179
180 // Copy the file into place.
181 if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
182 /*IgnoreExisting=*/true))
183 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000184 if (std::error_code EC = fs::copy_file(
185 HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest))
Justin Bogner86d12592014-06-19 19:36:03 +0000186 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000187
188 // Use the canonical path under the root for the file mapping. Also create
189 // an additional entry for the real path.
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000190 addFileMapping(CanonicalPath, Dest);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000191 if (HasRemovedSymlinkComponent)
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000192 addFileMapping(RealPath, Dest);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000193
Justin Bogner86d12592014-06-19 19:36:03 +0000194 return std::error_code();
195}
196
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000197void ModuleDependencyCollector::addFile(StringRef Filename) {
198 if (insertSeen(Filename))
Justin Bogner86d12592014-06-19 19:36:03 +0000199 if (copyToRoot(Filename))
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000200 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000201}