Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 1 | //===--- 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 Lopes | e62cfd7 | 2016-03-30 23:54:25 +0000 | [diff] [blame] | 15 | #include "clang/Lex/Preprocessor.h" |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 16 | #include "clang/Serialization/ASTReader.h" |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/iterator_range.h" |
Justin Bogner | cbda32f | 2014-06-19 19:49:28 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Path.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | namespace { |
Bruno Cardoso Lopes | e62cfd7 | 2016-03-30 23:54:25 +0000 | [diff] [blame] | 26 | /// Private implementations for ModuleDependencyCollector |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 27 | class ModuleDependencyListener : public ASTReaderListener { |
| 28 | ModuleDependencyCollector &Collector; |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 29 | public: |
| 30 | ModuleDependencyListener(ModuleDependencyCollector &Collector) |
| 31 | : Collector(Collector) {} |
| 32 | bool needsInputFileVisitation() override { return true; } |
| 33 | bool needsSystemInputFileVisitation() override { return true; } |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 34 | bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden, |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 35 | bool IsExplicitModule) override { |
| 36 | Collector.addFile(Filename); |
| 37 | return true; |
| 38 | } |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 39 | }; |
Bruno Cardoso Lopes | e62cfd7 | 2016-03-30 23:54:25 +0000 | [diff] [blame] | 40 | |
| 41 | struct 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 Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 53 | } |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 54 | |
Bruno Cardoso Lopes | 7caebc1 | 2016-04-07 00:00:42 +0000 | [diff] [blame^] | 55 | // TODO: move this to Support/Path.h and check for HAVE_REALPATH? |
| 56 | static 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 Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 73 | void ModuleDependencyCollector::attachToASTReader(ASTReader &R) { |
David Blaikie | 2721c32 | 2014-08-10 16:54:39 +0000 | [diff] [blame] | 74 | R.addListener(llvm::make_unique<ModuleDependencyListener>(*this)); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Bruno Cardoso Lopes | e62cfd7 | 2016-03-30 23:54:25 +0000 | [diff] [blame] | 77 | void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) { |
| 78 | PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( |
| 79 | llvm::make_unique<ModuleDependencyMMCallbacks>(*this)); |
| 80 | } |
| 81 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 82 | void 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 Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 89 | // 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 Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 93 | std::error_code EC; |
| 94 | llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text); |
| 95 | if (EC) { |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 96 | HasErrors = true; |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | VFSWriter.write(OS); |
| 100 | } |
| 101 | |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 102 | bool ModuleDependencyCollector::getRealPath(StringRef SrcPath, |
| 103 | SmallVectorImpl<char> &Result) { |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 104 | 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 Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 126 | std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) { |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 127 | 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 Bogner | 93e3cfc | 2014-12-12 23:12:27 +0000 | [diff] [blame] | 132 | // Canonicalize to a native path to avoid mixed separator styles. |
| 133 | path::native(AbsoluteSrc); |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 134 | // 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 Bogner | e1552f6 | 2014-06-20 03:28:46 +0000 | [diff] [blame] | 150 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 151 | // Build the destination path. |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 152 | SmallString<256> Dest = getDest(); |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 153 | path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath |
| 154 | : CanonicalPath)); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 155 | |
| 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 Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 160 | if (std::error_code EC = fs::copy_file( |
| 161 | HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest)) |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 162 | return EC; |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 163 | |
| 164 | // Use the canonical path under the root for the file mapping. Also create |
| 165 | // an additional entry for the real path. |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 166 | addFileMapping(CanonicalPath, Dest); |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 167 | if (HasRemovedSymlinkComponent) |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 168 | addFileMapping(RealPath, Dest); |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 169 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 170 | return std::error_code(); |
| 171 | } |
| 172 | |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 173 | void ModuleDependencyCollector::addFile(StringRef Filename) { |
| 174 | if (insertSeen(Filename)) |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 175 | if (copyToRoot(Filename)) |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 176 | HasErrors = true; |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 177 | } |