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" |
| 15 | #include "clang/Serialization/ASTReader.h" |
NAKAMURA Takumi | 9ed443c | 2016-03-16 12:15:29 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/iterator_range.h" |
Justin Bogner | cbda32f | 2014-06-19 19:49:28 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | /// Private implementation for ModuleDependencyCollector |
| 26 | class ModuleDependencyListener : public ASTReaderListener { |
| 27 | ModuleDependencyCollector &Collector; |
| 28 | |
| 29 | std::error_code copyToRoot(StringRef Src); |
| 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, |
| 36 | bool IsExplicitModule) override; |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 37 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 38 | } |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 39 | |
| 40 | void ModuleDependencyCollector::attachToASTReader(ASTReader &R) { |
David Blaikie | 2721c32 | 2014-08-10 16:54:39 +0000 | [diff] [blame] | 41 | R.addListener(llvm::make_unique<ModuleDependencyListener>(*this)); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void ModuleDependencyCollector::writeFileMap() { |
| 45 | if (Seen.empty()) |
| 46 | return; |
| 47 | |
| 48 | SmallString<256> Dest = getDest(); |
| 49 | llvm::sys::path::append(Dest, "vfs.yaml"); |
| 50 | |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 51 | std::error_code EC; |
| 52 | llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text); |
| 53 | if (EC) { |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 54 | setHasErrors(); |
| 55 | return; |
| 56 | } |
| 57 | VFSWriter.write(OS); |
| 58 | } |
| 59 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 60 | std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) { |
| 61 | using namespace llvm::sys; |
| 62 | |
| 63 | // We need an absolute path to append to the root. |
| 64 | SmallString<256> AbsoluteSrc = Src; |
| 65 | fs::make_absolute(AbsoluteSrc); |
Justin Bogner | 93e3cfc | 2014-12-12 23:12:27 +0000 | [diff] [blame] | 66 | // Canonicalize to a native path to avoid mixed separator styles. |
| 67 | path::native(AbsoluteSrc); |
NAKAMURA Takumi | 9ed443c | 2016-03-16 12:15:29 +0000 | [diff] [blame] | 68 | // TODO: We probably need to handle .. as well as . in order to have valid |
| 69 | // input to the YAMLVFSWriter. |
| 70 | path::remove_dots(AbsoluteSrc); |
Justin Bogner | e1552f6 | 2014-06-20 03:28:46 +0000 | [diff] [blame] | 71 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 72 | // Build the destination path. |
| 73 | SmallString<256> Dest = Collector.getDest(); |
NAKAMURA Takumi | 9ed443c | 2016-03-16 12:15:29 +0000 | [diff] [blame] | 74 | path::append(Dest, path::relative_path(AbsoluteSrc)); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 75 | |
| 76 | // Copy the file into place. |
| 77 | if (std::error_code EC = fs::create_directories(path::parent_path(Dest), |
| 78 | /*IgnoreExisting=*/true)) |
| 79 | return EC; |
NAKAMURA Takumi | 9ed443c | 2016-03-16 12:15:29 +0000 | [diff] [blame] | 80 | if (std::error_code EC = fs::copy_file(AbsoluteSrc, Dest)) |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 81 | return EC; |
NAKAMURA Takumi | 9ed443c | 2016-03-16 12:15:29 +0000 | [diff] [blame] | 82 | // Use the absolute path under the root for the file mapping. |
| 83 | Collector.addFileMapping(AbsoluteSrc, Dest); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 84 | return std::error_code(); |
| 85 | } |
| 86 | |
| 87 | bool ModuleDependencyListener::visitInputFile(StringRef Filename, bool IsSystem, |
Richard Smith | 216a3bd | 2015-08-13 17:57:10 +0000 | [diff] [blame] | 88 | bool IsOverridden, |
| 89 | bool IsExplicitModule) { |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 90 | if (Collector.insertSeen(Filename)) |
| 91 | if (copyToRoot(Filename)) |
| 92 | Collector.setHasErrors(); |
| 93 | return true; |
| 94 | } |