blob: cc655f6eb0021f269114f537bf1a377f46e786d8 [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"
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
Bruno Cardoso Lopesf0841792016-05-06 23:21:50 +000046 void moduleMapAddHeader(StringRef HeaderPath) override {
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000047 if (llvm::sys::path::is_absolute(HeaderPath))
48 Collector.addFile(HeaderPath);
49 }
Bruno Cardoso Lopesb3a0fa42016-05-13 22:21:51 +000050 void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
51 const FileEntry *Header) override {
52 StringRef HeaderFilename = Header->getName();
53 moduleMapAddHeader(HeaderFilename);
54 // The FileManager can find and cache the symbolic link for a framework
55 // header before its real path, this means a module can have some of its
56 // headers to use other paths. Although this is usually not a problem, it's
57 // inconsistent, and not collecting the original path header leads to
58 // umbrella clashes while rebuilding modules in the crash reproducer. For
59 // example:
60 // ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h
61 // instead of:
62 // ImageIO.framework/ImageIO.h
63 //
64 // FIXME: this shouldn't be necessary once we have FileName instances
65 // around instead of FileEntry ones. For now, make sure we collect all
66 // that we need for the reproducer to work correctly.
67 StringRef UmbreallDirFromHeader =
68 llvm::sys::path::parent_path(HeaderFilename);
69 StringRef UmbrellaDir = Header->getDir()->getName();
70 if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
71 SmallString<128> AltHeaderFilename;
72 llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
73 llvm::sys::path::filename(HeaderFilename));
74 if (FileMgr->getFile(AltHeaderFilename))
75 moduleMapAddHeader(AltHeaderFilename);
76 }
77 }
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000078};
79
Alexander Kornienkoab9db512015-06-22 23:07:51 +000080}
Justin Bogner86d12592014-06-19 19:36:03 +000081
Bruno Cardoso Lopes7caebc12016-04-07 00:00:42 +000082// TODO: move this to Support/Path.h and check for HAVE_REALPATH?
83static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
84#ifdef LLVM_ON_UNIX
85 char CanonicalPath[PATH_MAX];
86
87 // TODO: emit a warning in case this fails...?
88 if (!realpath(SrcPath.str().c_str(), CanonicalPath))
89 return false;
90
91 SmallString<256> RPath(CanonicalPath);
92 RealPath.swap(RPath);
93 return true;
94#else
95 // FIXME: Add support for systems without realpath.
96 return false;
97#endif
98}
99
Justin Bogner86d12592014-06-19 19:36:03 +0000100void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000101 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
Justin Bogner86d12592014-06-19 19:36:03 +0000102}
103
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +0000104void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
105 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
106 llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
107}
108
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000109static bool isCaseSensitivePath(StringRef Path) {
Sean Silva72af4722016-04-07 01:58:14 +0000110 SmallString<256> TmpDest = Path, UpperDest, RealDest;
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000111 // Remove component traversals, links, etc.
112 if (!real_path(Path, TmpDest))
113 return true; // Current default value in vfs.yaml
114 Path = TmpDest;
115
116 // Change path to all upper case and ask for its real path, if the latter
117 // exists and is equal to Path, it's not case sensitive. Default to case
118 // sensitive in the absense of realpath, since this is what the VFSWriter
119 // already expects when sensitivity isn't setup.
120 for (auto &C : Path)
Bruno Cardoso Lopes4775fcf2016-04-07 01:12:18 +0000121 UpperDest.push_back(toUppercase(C));
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000122 if (real_path(UpperDest, RealDest) && Path.equals(RealDest))
123 return false;
124 return true;
125}
126
Justin Bogner86d12592014-06-19 19:36:03 +0000127void ModuleDependencyCollector::writeFileMap() {
128 if (Seen.empty())
129 return;
130
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000131 StringRef VFSDir = getDest();
Justin Bogner86d12592014-06-19 19:36:03 +0000132
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +0000133 // Default to use relative overlay directories in the VFS yaml file. This
134 // allows crash reproducer scripts to work across machines.
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000135 VFSWriter.setOverlayDir(VFSDir);
136
Bruno Cardoso Lopesb40d8ad2016-08-12 01:50:53 +0000137 // Do not ignore non existent contents otherwise we might skip something
138 // that should have been collected here.
139 VFSWriter.setIgnoreNonExistentContents(false);
140
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000141 // Explicitly set case sensitivity for the YAML writer. For that, find out
142 // the sensitivity at the path where the headers all collected to.
143 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +0000144
Bruno Cardoso Lopesfc8644c2016-04-13 19:28:21 +0000145 // Do not rely on real path names when executing the crash reproducer scripts
146 // since we only want to actually use the files we have on the VFS cache.
147 VFSWriter.setUseExternalNames(false);
148
Rafael Espindoladae941a2014-08-25 18:17:04 +0000149 std::error_code EC;
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000150 SmallString<256> YAMLPath = VFSDir;
151 llvm::sys::path::append(YAMLPath, "vfs.yaml");
152 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text);
Rafael Espindoladae941a2014-08-25 18:17:04 +0000153 if (EC) {
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000154 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000155 return;
156 }
157 VFSWriter.write(OS);
158}
159
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000160bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
161 SmallVectorImpl<char> &Result) {
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000162 using namespace llvm::sys;
163 SmallString<256> RealPath;
164 StringRef FileName = path::filename(SrcPath);
165 std::string Dir = path::parent_path(SrcPath).str();
166 auto DirWithSymLink = SymLinkMap.find(Dir);
167
168 // Use real_path to fix any symbolic link component present in a path.
169 // Computing the real path is expensive, cache the search through the
170 // parent path directory.
171 if (DirWithSymLink == SymLinkMap.end()) {
172 if (!real_path(Dir, RealPath))
173 return false;
174 SymLinkMap[Dir] = RealPath.str();
175 } else {
176 RealPath = DirWithSymLink->second;
177 }
178
179 path::append(RealPath, FileName);
180 Result.swap(RealPath);
181 return true;
182}
183
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000184std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
Justin Bogner86d12592014-06-19 19:36:03 +0000185 using namespace llvm::sys;
186
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000187 // We need an absolute src path to append to the root.
Justin Bogner86d12592014-06-19 19:36:03 +0000188 SmallString<256> AbsoluteSrc = Src;
189 fs::make_absolute(AbsoluteSrc);
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000190 // Canonicalize src to a native path to avoid mixed separator styles.
Justin Bogner93e3cfc2014-12-12 23:12:27 +0000191 path::native(AbsoluteSrc);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000192 // Remove redundant leading "./" pieces and consecutive separators.
193 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
194
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000195 // Canonicalize the source path by removing "..", "." components.
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000196 SmallString<256> CanonicalPath = AbsoluteSrc;
197 path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
198
199 // If a ".." component is present after a symlink component, remove_dots may
200 // lead to the wrong real destination path. Let the source be canonicalized
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000201 // like that but make sure we always use the real path for the destination.
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000202 SmallString<256> RealPath;
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000203 if (!getRealPath(AbsoluteSrc, RealPath))
204 RealPath = CanonicalPath;
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000205 SmallString<256> Dest = getDest();
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000206 path::append(Dest, path::relative_path(RealPath));
Justin Bogner86d12592014-06-19 19:36:03 +0000207
208 // Copy the file into place.
209 if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
210 /*IgnoreExisting=*/true))
211 return EC;
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000212 if (std::error_code EC = fs::copy_file(RealPath, Dest))
Justin Bogner86d12592014-06-19 19:36:03 +0000213 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000214
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000215 // Always map a canonical src path to its real path into the YAML, by doing
216 // this we map different virtual src paths to the same entry in the VFS
217 // overlay, which is a way to emulate symlink inside the VFS; this is also
218 // needed for correctness, not doing that can lead to module redifinition
219 // errors.
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000220 addFileMapping(CanonicalPath, Dest);
Justin Bogner86d12592014-06-19 19:36:03 +0000221 return std::error_code();
222}
223
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000224void ModuleDependencyCollector::addFile(StringRef Filename) {
225 if (insertSeen(Filename))
Justin Bogner86d12592014-06-19 19:36:03 +0000226 if (copyToRoot(Filename))
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000227 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000228}