| 1 | //===- DependencyScanningTool.cpp - clang-scan-deps service ---------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/Tooling/DependencyScanning/DependencyScanningTool.h" |
| 10 | #include "clang/Frontend/Utils.h" |
| 11 | #include <optional> |
| 12 | |
| 13 | using namespace clang; |
| 14 | using namespace tooling; |
| 15 | using namespace dependencies; |
| 16 | |
| 17 | DependencyScanningTool::DependencyScanningTool( |
| 18 | DependencyScanningService &Service, |
| 19 | llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) |
| 20 | : Worker(Service, std::move(FS)) {} |
| 21 | |
| 22 | namespace { |
| 23 | /// Prints out all of the gathered dependencies into a string. |
| 24 | class MakeDependencyPrinterConsumer : public DependencyConsumer { |
| 25 | public: |
| 26 | void handleBuildCommand(Command) override {} |
| 27 | |
| 28 | void |
| 29 | handleDependencyOutputOpts(const DependencyOutputOptions &Opts) override { |
| 30 | this->Opts = std::make_unique<DependencyOutputOptions>(args: Opts); |
| 31 | } |
| 32 | |
| 33 | void handleFileDependency(StringRef File) override { |
| 34 | Dependencies.push_back(x: std::string(File)); |
| 35 | } |
| 36 | |
| 37 | // These are ignored for the make format as it can't support the full |
| 38 | // set of deps, and handleFileDependency handles enough for implicitly |
| 39 | // built modules to work. |
| 40 | void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override {} |
| 41 | void handleModuleDependency(ModuleDeps MD) override {} |
| 42 | void handleDirectModuleDependency(ModuleID ID) override {} |
| 43 | void handleContextHash(std::string Hash) override {} |
| 44 | |
| 45 | void printDependencies(std::string &S) { |
| 46 | assert(Opts && "Handled dependency output options." ); |
| 47 | |
| 48 | class DependencyPrinter : public DependencyFileGenerator { |
| 49 | public: |
| 50 | DependencyPrinter(DependencyOutputOptions &Opts, |
| 51 | ArrayRef<std::string> Dependencies) |
| 52 | : DependencyFileGenerator(Opts) { |
| 53 | for (const auto &Dep : Dependencies) |
| 54 | addDependency(Filename: Dep); |
| 55 | } |
| 56 | |
| 57 | void printDependencies(std::string &S) { |
| 58 | llvm::raw_string_ostream OS(S); |
| 59 | outputDependencyFile(OS); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | DependencyPrinter Generator(*Opts, Dependencies); |
| 64 | Generator.printDependencies(S); |
| 65 | } |
| 66 | |
| 67 | protected: |
| 68 | std::unique_ptr<DependencyOutputOptions> Opts; |
| 69 | std::vector<std::string> Dependencies; |
| 70 | }; |
| 71 | } // anonymous namespace |
| 72 | |
| 73 | llvm::Expected<std::string> DependencyScanningTool::getDependencyFile( |
| 74 | const std::vector<std::string> &CommandLine, StringRef CWD) { |
| 75 | MakeDependencyPrinterConsumer Consumer; |
| 76 | CallbackActionController Controller(nullptr); |
| 77 | auto Result = |
| 78 | Worker.computeDependencies(WorkingDirectory: CWD, CommandLine, Consumer, Controller); |
| 79 | if (Result) |
| 80 | return std::move(Result); |
| 81 | std::string Output; |
| 82 | Consumer.printDependencies(S&: Output); |
| 83 | return Output; |
| 84 | } |
| 85 | |
| 86 | llvm::Expected<P1689Rule> DependencyScanningTool::getP1689ModuleDependencyFile( |
| 87 | const CompileCommand &Command, StringRef CWD, std::string &MakeformatOutput, |
| 88 | std::string &MakeformatOutputPath) { |
| 89 | class P1689ModuleDependencyPrinterConsumer |
| 90 | : public MakeDependencyPrinterConsumer { |
| 91 | public: |
| 92 | P1689ModuleDependencyPrinterConsumer(P1689Rule &Rule, |
| 93 | const CompileCommand &Command) |
| 94 | : Filename(Command.Filename), Rule(Rule) { |
| 95 | Rule.PrimaryOutput = Command.Output; |
| 96 | } |
| 97 | |
| 98 | void handleProvidedAndRequiredStdCXXModules( |
| 99 | std::optional<P1689ModuleInfo> Provided, |
| 100 | std::vector<P1689ModuleInfo> Requires) override { |
| 101 | Rule.Provides = Provided; |
| 102 | if (Rule.Provides) |
| 103 | Rule.Provides->SourcePath = Filename.str(); |
| 104 | Rule.Requires = Requires; |
| 105 | } |
| 106 | |
| 107 | StringRef getMakeFormatDependencyOutputPath() { |
| 108 | if (Opts->OutputFormat != DependencyOutputFormat::Make) |
| 109 | return {}; |
| 110 | return Opts->OutputFile; |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | StringRef Filename; |
| 115 | P1689Rule &Rule; |
| 116 | }; |
| 117 | |
| 118 | class P1689ActionController : public DependencyActionController { |
| 119 | public: |
| 120 | // The lookupModuleOutput is for clang modules. P1689 format don't need it. |
| 121 | std::string lookupModuleOutput(const ModuleDeps &, |
| 122 | ModuleOutputKind Kind) override { |
| 123 | return "" ; |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | P1689Rule Rule; |
| 128 | P1689ModuleDependencyPrinterConsumer Consumer(Rule, Command); |
| 129 | P1689ActionController Controller; |
| 130 | auto Result = Worker.computeDependencies(WorkingDirectory: CWD, CommandLine: Command.CommandLine, Consumer, |
| 131 | Controller); |
| 132 | if (Result) |
| 133 | return std::move(Result); |
| 134 | |
| 135 | MakeformatOutputPath = Consumer.getMakeFormatDependencyOutputPath(); |
| 136 | if (!MakeformatOutputPath.empty()) |
| 137 | Consumer.printDependencies(S&: MakeformatOutput); |
| 138 | return Rule; |
| 139 | } |
| 140 | |
| 141 | llvm::Expected<TranslationUnitDeps> |
| 142 | DependencyScanningTool::getTranslationUnitDependencies( |
| 143 | const std::vector<std::string> &CommandLine, StringRef CWD, |
| 144 | const llvm::DenseSet<ModuleID> &AlreadySeen, |
| 145 | LookupModuleOutputCallback LookupModuleOutput, |
| 146 | std::optional<llvm::MemoryBufferRef> TUBuffer) { |
| 147 | FullDependencyConsumer Consumer(AlreadySeen); |
| 148 | CallbackActionController Controller(LookupModuleOutput); |
| 149 | llvm::Error Result = Worker.computeDependencies(WorkingDirectory: CWD, CommandLine, Consumer, |
| 150 | Controller, TUBuffer); |
| 151 | |
| 152 | if (Result) |
| 153 | return std::move(Result); |
| 154 | return Consumer.takeTranslationUnitDeps(); |
| 155 | } |
| 156 | |
| 157 | llvm::Expected<ModuleDepsGraph> DependencyScanningTool::getModuleDependencies( |
| 158 | StringRef ModuleName, const std::vector<std::string> &CommandLine, |
| 159 | StringRef CWD, const llvm::DenseSet<ModuleID> &AlreadySeen, |
| 160 | LookupModuleOutputCallback LookupModuleOutput) { |
| 161 | FullDependencyConsumer Consumer(AlreadySeen); |
| 162 | CallbackActionController Controller(LookupModuleOutput); |
| 163 | llvm::Error Result = Worker.computeDependencies(WorkingDirectory: CWD, CommandLine, Consumer, |
| 164 | Controller, ModuleName); |
| 165 | if (Result) |
| 166 | return std::move(Result); |
| 167 | return Consumer.takeModuleGraphDeps(); |
| 168 | } |
| 169 | |
| 170 | TranslationUnitDeps FullDependencyConsumer::takeTranslationUnitDeps() { |
| 171 | TranslationUnitDeps TU; |
| 172 | |
| 173 | TU.ID.ContextHash = std::move(ContextHash); |
| 174 | TU.ID.ModuleName = std::move(ModuleName); |
| 175 | TU.NamedModuleDeps = std::move(NamedModuleDeps); |
| 176 | TU.FileDeps = std::move(Dependencies); |
| 177 | TU.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps); |
| 178 | TU.Commands = std::move(Commands); |
| 179 | |
| 180 | for (auto &&M : ClangModuleDeps) { |
| 181 | auto &MD = M.second; |
| 182 | // TODO: Avoid handleModuleDependency even being called for modules |
| 183 | // we've already seen. |
| 184 | if (AlreadySeen.count(V: M.first)) |
| 185 | continue; |
| 186 | TU.ModuleGraph.push_back(x: std::move(MD)); |
| 187 | } |
| 188 | TU.ClangModuleDeps = std::move(DirectModuleDeps); |
| 189 | |
| 190 | return TU; |
| 191 | } |
| 192 | |
| 193 | ModuleDepsGraph FullDependencyConsumer::takeModuleGraphDeps() { |
| 194 | ModuleDepsGraph ModuleGraph; |
| 195 | |
| 196 | for (auto &&M : ClangModuleDeps) { |
| 197 | auto &MD = M.second; |
| 198 | // TODO: Avoid handleModuleDependency even being called for modules |
| 199 | // we've already seen. |
| 200 | if (AlreadySeen.count(V: M.first)) |
| 201 | continue; |
| 202 | ModuleGraph.push_back(x: std::move(MD)); |
| 203 | } |
| 204 | |
| 205 | return ModuleGraph; |
| 206 | } |
| 207 | |
| 208 | CallbackActionController::~CallbackActionController() {} |
| 209 | |