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 | |
Bruno Cardoso Lopes | 4775fcf | 2016-04-07 01:12:18 +0000 | [diff] [blame] | 14 | #include "clang/Basic/CharInfo.h" |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/Utils.h" |
Bruno Cardoso Lopes | e62cfd7 | 2016-03-30 23:54:25 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 17 | #include "clang/Serialization/ASTReader.h" |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/iterator_range.h" |
Justin Bogner | cbda32f | 2014-06-19 19:49:28 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FileSystem.h" |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
Bruno Cardoso Lopes | e62cfd7 | 2016-03-30 23:54:25 +0000 | [diff] [blame] | 27 | /// Private implementations for ModuleDependencyCollector |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 28 | class ModuleDependencyListener : public ASTReaderListener { |
| 29 | ModuleDependencyCollector &Collector; |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 30 | public: |
| 31 | ModuleDependencyListener(ModuleDependencyCollector &Collector) |
| 32 | : Collector(Collector) {} |
| 33 | bool needsInputFileVisitation() override { return true; } |
| 34 | bool needsSystemInputFileVisitation() override { return true; } |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 35 | bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden, |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 36 | bool IsExplicitModule) override { |
| 37 | Collector.addFile(Filename); |
| 38 | return true; |
| 39 | } |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 40 | }; |
Bruno Cardoso Lopes | e62cfd7 | 2016-03-30 23:54:25 +0000 | [diff] [blame] | 41 | |
| 42 | struct 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 Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 54 | } |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 55 | |
Bruno Cardoso Lopes | 7caebc1 | 2016-04-07 00:00:42 +0000 | [diff] [blame] | 56 | // TODO: move this to Support/Path.h and check for HAVE_REALPATH? |
| 57 | static 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 Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 74 | void ModuleDependencyCollector::attachToASTReader(ASTReader &R) { |
David Blaikie | 2721c32 | 2014-08-10 16:54:39 +0000 | [diff] [blame] | 75 | R.addListener(llvm::make_unique<ModuleDependencyListener>(*this)); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Bruno Cardoso Lopes | e62cfd7 | 2016-03-30 23:54:25 +0000 | [diff] [blame] | 78 | void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) { |
| 79 | PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( |
| 80 | llvm::make_unique<ModuleDependencyMMCallbacks>(*this)); |
| 81 | } |
| 82 | |
Bruno Cardoso Lopes | 4c20bef | 2016-04-07 00:00:57 +0000 | [diff] [blame] | 83 | static bool isCaseSensitivePath(StringRef Path) { |
Sean Silva | 72af472 | 2016-04-07 01:58:14 +0000 | [diff] [blame^] | 84 | SmallString<256> TmpDest = Path, UpperDest, RealDest; |
Bruno Cardoso Lopes | 4c20bef | 2016-04-07 00:00:57 +0000 | [diff] [blame] | 85 | // 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 Lopes | 4775fcf | 2016-04-07 01:12:18 +0000 | [diff] [blame] | 95 | UpperDest.push_back(toUppercase(C)); |
Bruno Cardoso Lopes | 4c20bef | 2016-04-07 00:00:57 +0000 | [diff] [blame] | 96 | if (real_path(UpperDest, RealDest) && Path.equals(RealDest)) |
| 97 | return false; |
| 98 | return true; |
| 99 | } |
| 100 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 101 | void ModuleDependencyCollector::writeFileMap() { |
| 102 | if (Seen.empty()) |
| 103 | return; |
| 104 | |
Bruno Cardoso Lopes | 4c20bef | 2016-04-07 00:00:57 +0000 | [diff] [blame] | 105 | StringRef VFSDir = getDest(); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 106 | |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 107 | // Default to use relative overlay directories in the VFS yaml file. This |
| 108 | // allows crash reproducer scripts to work across machines. |
Bruno Cardoso Lopes | 4c20bef | 2016-04-07 00:00:57 +0000 | [diff] [blame] | 109 | 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 Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 114 | |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 115 | std::error_code EC; |
Bruno Cardoso Lopes | 4c20bef | 2016-04-07 00:00:57 +0000 | [diff] [blame] | 116 | 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 Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 119 | if (EC) { |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 120 | HasErrors = true; |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | VFSWriter.write(OS); |
| 124 | } |
| 125 | |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 126 | bool ModuleDependencyCollector::getRealPath(StringRef SrcPath, |
| 127 | SmallVectorImpl<char> &Result) { |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 128 | 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 Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 150 | std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) { |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 151 | 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 Bogner | 93e3cfc | 2014-12-12 23:12:27 +0000 | [diff] [blame] | 156 | // Canonicalize to a native path to avoid mixed separator styles. |
| 157 | path::native(AbsoluteSrc); |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 158 | // 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 Bogner | e1552f6 | 2014-06-20 03:28:46 +0000 | [diff] [blame] | 174 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 175 | // Build the destination path. |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 176 | SmallString<256> Dest = getDest(); |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 177 | path::append(Dest, path::relative_path(HasRemovedSymlinkComponent ? RealPath |
| 178 | : CanonicalPath)); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 179 | |
| 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 Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 184 | if (std::error_code EC = fs::copy_file( |
| 185 | HasRemovedSymlinkComponent ? RealPath : CanonicalPath, Dest)) |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 186 | return EC; |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 187 | |
| 188 | // Use the canonical path under the root for the file mapping. Also create |
| 189 | // an additional entry for the real path. |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 190 | addFileMapping(CanonicalPath, Dest); |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 191 | if (HasRemovedSymlinkComponent) |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 192 | addFileMapping(RealPath, Dest); |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 193 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 194 | return std::error_code(); |
| 195 | } |
| 196 | |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 197 | void ModuleDependencyCollector::addFile(StringRef Filename) { |
| 198 | if (insertSeen(Filename)) |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 199 | if (copyToRoot(Filename)) |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 200 | HasErrors = true; |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 201 | } |