blob: ede12aab6e695144774167d1c6d0facd14d2cbb5 [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
Bruno Cardoso Lopes66e96272016-12-11 04:27:31 +000041struct ModuleDependencyPPCallbacks : public PPCallbacks {
42 ModuleDependencyCollector &Collector;
43 SourceManager &SM;
44 ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
45 SourceManager &SM)
46 : Collector(Collector), SM(SM) {}
47
48 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
49 StringRef FileName, bool IsAngled,
50 CharSourceRange FilenameRange, const FileEntry *File,
51 StringRef SearchPath, StringRef RelativePath,
52 const Module *Imported) override {
53 if (!File)
54 return;
55 Collector.addFile(File->getName());
56 }
57};
58
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000059struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
60 ModuleDependencyCollector &Collector;
61 ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
62 : Collector(Collector) {}
63
Bruno Cardoso Lopesf0841792016-05-06 23:21:50 +000064 void moduleMapAddHeader(StringRef HeaderPath) override {
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000065 if (llvm::sys::path::is_absolute(HeaderPath))
66 Collector.addFile(HeaderPath);
67 }
Bruno Cardoso Lopesb3a0fa42016-05-13 22:21:51 +000068 void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
69 const FileEntry *Header) override {
70 StringRef HeaderFilename = Header->getName();
71 moduleMapAddHeader(HeaderFilename);
72 // The FileManager can find and cache the symbolic link for a framework
73 // header before its real path, this means a module can have some of its
74 // headers to use other paths. Although this is usually not a problem, it's
75 // inconsistent, and not collecting the original path header leads to
76 // umbrella clashes while rebuilding modules in the crash reproducer. For
77 // example:
78 // ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h
79 // instead of:
80 // ImageIO.framework/ImageIO.h
81 //
82 // FIXME: this shouldn't be necessary once we have FileName instances
83 // around instead of FileEntry ones. For now, make sure we collect all
84 // that we need for the reproducer to work correctly.
85 StringRef UmbreallDirFromHeader =
86 llvm::sys::path::parent_path(HeaderFilename);
87 StringRef UmbrellaDir = Header->getDir()->getName();
88 if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
89 SmallString<128> AltHeaderFilename;
90 llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
91 llvm::sys::path::filename(HeaderFilename));
92 if (FileMgr->getFile(AltHeaderFilename))
93 moduleMapAddHeader(AltHeaderFilename);
94 }
95 }
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +000096};
97
Alexander Kornienkoab9db512015-06-22 23:07:51 +000098}
Justin Bogner86d12592014-06-19 19:36:03 +000099
Bruno Cardoso Lopes7caebc12016-04-07 00:00:42 +0000100// TODO: move this to Support/Path.h and check for HAVE_REALPATH?
101static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
102#ifdef LLVM_ON_UNIX
103 char CanonicalPath[PATH_MAX];
104
105 // TODO: emit a warning in case this fails...?
106 if (!realpath(SrcPath.str().c_str(), CanonicalPath))
107 return false;
108
109 SmallString<256> RPath(CanonicalPath);
110 RealPath.swap(RPath);
111 return true;
112#else
113 // FIXME: Add support for systems without realpath.
114 return false;
115#endif
116}
117
Justin Bogner86d12592014-06-19 19:36:03 +0000118void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000119 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
Justin Bogner86d12592014-06-19 19:36:03 +0000120}
121
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +0000122void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
Bruno Cardoso Lopes66e96272016-12-11 04:27:31 +0000123 PP.addPPCallbacks(llvm::make_unique<ModuleDependencyPPCallbacks>(
124 *this, PP.getSourceManager()));
Bruno Cardoso Lopese62cfd72016-03-30 23:54:25 +0000125 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
126 llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
127}
128
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000129static bool isCaseSensitivePath(StringRef Path) {
Sean Silva72af4722016-04-07 01:58:14 +0000130 SmallString<256> TmpDest = Path, UpperDest, RealDest;
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000131 // Remove component traversals, links, etc.
132 if (!real_path(Path, TmpDest))
133 return true; // Current default value in vfs.yaml
134 Path = TmpDest;
135
136 // Change path to all upper case and ask for its real path, if the latter
137 // exists and is equal to Path, it's not case sensitive. Default to case
138 // sensitive in the absense of realpath, since this is what the VFSWriter
139 // already expects when sensitivity isn't setup.
140 for (auto &C : Path)
Bruno Cardoso Lopes4775fcf2016-04-07 01:12:18 +0000141 UpperDest.push_back(toUppercase(C));
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000142 if (real_path(UpperDest, RealDest) && Path.equals(RealDest))
143 return false;
144 return true;
145}
146
Justin Bogner86d12592014-06-19 19:36:03 +0000147void ModuleDependencyCollector::writeFileMap() {
148 if (Seen.empty())
149 return;
150
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000151 StringRef VFSDir = getDest();
Justin Bogner86d12592014-06-19 19:36:03 +0000152
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +0000153 // Default to use relative overlay directories in the VFS yaml file. This
154 // allows crash reproducer scripts to work across machines.
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000155 VFSWriter.setOverlayDir(VFSDir);
156
Bruno Cardoso Lopesb40d8ad2016-08-12 01:50:53 +0000157 // Do not ignore non existent contents otherwise we might skip something
158 // that should have been collected here.
159 VFSWriter.setIgnoreNonExistentContents(false);
160
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000161 // Explicitly set case sensitivity for the YAML writer. For that, find out
162 // the sensitivity at the path where the headers all collected to.
163 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
Bruno Cardoso Lopesd878e282016-03-20 02:08:48 +0000164
Bruno Cardoso Lopesfc8644c2016-04-13 19:28:21 +0000165 // Do not rely on real path names when executing the crash reproducer scripts
166 // since we only want to actually use the files we have on the VFS cache.
167 VFSWriter.setUseExternalNames(false);
168
Rafael Espindoladae941a2014-08-25 18:17:04 +0000169 std::error_code EC;
Bruno Cardoso Lopes4c20bef2016-04-07 00:00:57 +0000170 SmallString<256> YAMLPath = VFSDir;
171 llvm::sys::path::append(YAMLPath, "vfs.yaml");
172 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text);
Rafael Espindoladae941a2014-08-25 18:17:04 +0000173 if (EC) {
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000174 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000175 return;
176 }
177 VFSWriter.write(OS);
178}
179
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000180bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
181 SmallVectorImpl<char> &Result) {
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000182 using namespace llvm::sys;
183 SmallString<256> RealPath;
184 StringRef FileName = path::filename(SrcPath);
185 std::string Dir = path::parent_path(SrcPath).str();
186 auto DirWithSymLink = SymLinkMap.find(Dir);
187
188 // Use real_path to fix any symbolic link component present in a path.
189 // Computing the real path is expensive, cache the search through the
190 // parent path directory.
191 if (DirWithSymLink == SymLinkMap.end()) {
192 if (!real_path(Dir, RealPath))
193 return false;
194 SymLinkMap[Dir] = RealPath.str();
195 } else {
196 RealPath = DirWithSymLink->second;
197 }
198
199 path::append(RealPath, FileName);
200 Result.swap(RealPath);
201 return true;
202}
203
Bruno Cardoso Lopes82ec4fde2016-12-22 07:06:03 +0000204std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
205 StringRef Dst) {
Justin Bogner86d12592014-06-19 19:36:03 +0000206 using namespace llvm::sys;
207
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000208 // We need an absolute src path to append to the root.
Justin Bogner86d12592014-06-19 19:36:03 +0000209 SmallString<256> AbsoluteSrc = Src;
210 fs::make_absolute(AbsoluteSrc);
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000211 // Canonicalize src to a native path to avoid mixed separator styles.
Justin Bogner93e3cfc2014-12-12 23:12:27 +0000212 path::native(AbsoluteSrc);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000213 // Remove redundant leading "./" pieces and consecutive separators.
214 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
215
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000216 // Canonicalize the source path by removing "..", "." components.
Bruno Cardoso Lopes82ec4fde2016-12-22 07:06:03 +0000217 SmallString<256> VirtualPath = AbsoluteSrc;
218 path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000219
220 // If a ".." component is present after a symlink component, remove_dots may
221 // lead to the wrong real destination path. Let the source be canonicalized
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000222 // like that but make sure we always use the real path for the destination.
Bruno Cardoso Lopes82ec4fde2016-12-22 07:06:03 +0000223 SmallString<256> CopyFrom;
224 if (!getRealPath(AbsoluteSrc, CopyFrom))
225 CopyFrom = VirtualPath;
226 SmallString<256> CacheDst = getDest();
227
228 if (Dst.empty()) {
229 // The common case is to map the virtual path to the same path inside the
230 // cache.
231 path::append(CacheDst, path::relative_path(CopyFrom));
232 } else {
233 // When collecting entries from input vfsoverlays, copy the external
234 // contents into the cache but still map from the source.
235 if (!fs::exists(Dst))
236 return std::error_code();
237 path::append(CacheDst, Dst);
238 CopyFrom = Dst;
239 }
Justin Bogner86d12592014-06-19 19:36:03 +0000240
241 // Copy the file into place.
Bruno Cardoso Lopes82ec4fde2016-12-22 07:06:03 +0000242 if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
243 /*IgnoreExisting=*/true))
Justin Bogner86d12592014-06-19 19:36:03 +0000244 return EC;
Bruno Cardoso Lopes82ec4fde2016-12-22 07:06:03 +0000245 if (std::error_code EC = fs::copy_file(CopyFrom, CacheDst))
Justin Bogner86d12592014-06-19 19:36:03 +0000246 return EC;
Bruno Cardoso Lopesb76c0272016-03-17 02:20:43 +0000247
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000248 // Always map a canonical src path to its real path into the YAML, by doing
249 // this we map different virtual src paths to the same entry in the VFS
250 // overlay, which is a way to emulate symlink inside the VFS; this is also
Hiroshi Inoue3170de02017-07-01 08:46:43 +0000251 // needed for correctness, not doing that can lead to module redefinition
Bruno Cardoso Lopes0df3e042016-05-06 23:58:58 +0000252 // errors.
Bruno Cardoso Lopes82ec4fde2016-12-22 07:06:03 +0000253 addFileMapping(VirtualPath, CacheDst);
Justin Bogner86d12592014-06-19 19:36:03 +0000254 return std::error_code();
255}
256
Bruno Cardoso Lopes82ec4fde2016-12-22 07:06:03 +0000257void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000258 if (insertSeen(Filename))
Bruno Cardoso Lopes82ec4fde2016-12-22 07:06:03 +0000259 if (copyToRoot(Filename, FileDst))
Bruno Cardoso Lopesb1631d92016-03-29 23:47:40 +0000260 HasErrors = true;
Justin Bogner86d12592014-06-19 19:36:03 +0000261}