clang_runner.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/driver/clang_runner.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <numeric>
  8. #include <optional>
  9. #include "clang/Basic/Diagnostic.h"
  10. #include "clang/Basic/DiagnosticOptions.h"
  11. #include "clang/Driver/Compilation.h"
  12. #include "clang/Driver/Driver.h"
  13. #include "clang/Frontend/CompilerInvocation.h"
  14. #include "clang/Frontend/TextDiagnosticPrinter.h"
  15. #include "common/command_line.h"
  16. #include "common/vlog.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/ScopeExit.h"
  19. #include "llvm/ADT/StringExtras.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/Support/FileSystem.h"
  23. #include "llvm/Support/LLVMDriver.h"
  24. #include "llvm/Support/Path.h"
  25. #include "llvm/Support/Program.h"
  26. #include "llvm/TargetParser/Host.h"
  27. // Defined in:
  28. // https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
  29. //
  30. // While not in a header, this is the API used by llvm-driver.cpp for
  31. // busyboxing.
  32. //
  33. // NOLINTNEXTLINE(readability-identifier-naming)
  34. auto clang_main(int Argc, char** Argv, const llvm::ToolContext& ToolContext)
  35. -> int;
  36. namespace Carbon {
  37. ClangRunner::ClangRunner(const InstallPaths* install_paths,
  38. llvm::StringRef target,
  39. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  40. llvm::raw_ostream* vlog_stream)
  41. : installation_(install_paths),
  42. target_(target),
  43. fs_(std::move(fs)),
  44. vlog_stream_(vlog_stream),
  45. diagnostic_ids_(new clang::DiagnosticIDs()) {}
  46. auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  47. // TODO: Maybe handle response file expansion similar to the Clang CLI?
  48. // If we have a verbose logging stream, and that stream is the same as
  49. // `llvm::errs`, then add the `-v` flag so that the driver also prints verbose
  50. // information.
  51. bool inject_v_arg = vlog_stream_ == &llvm::errs();
  52. std::array<llvm::StringRef, 1> v_arg_storage;
  53. llvm::ArrayRef<llvm::StringRef> maybe_v_arg;
  54. if (inject_v_arg) {
  55. v_arg_storage[0] = "-v";
  56. maybe_v_arg = v_arg_storage;
  57. }
  58. CARBON_VLOG("Running Clang driver with arguments: \n");
  59. // Render the arguments into null-terminated C-strings for use by the Clang
  60. // driver. Command lines can get quite long in build systems so this tries to
  61. // minimize the memory allocation overhead.
  62. // Provide the wrapped `clang` path in order to support subprocessing. We also
  63. // set the install directory below.
  64. std::string clang_path = installation_->clang_path();
  65. std::array<llvm::StringRef, 1> exe_arg = {clang_path};
  66. auto args_range =
  67. llvm::concat<const llvm::StringRef>(exe_arg, maybe_v_arg, args);
  68. int total_size = 0;
  69. for (llvm::StringRef arg : args_range) {
  70. // Accumulate both the string size and a null terminator byte.
  71. total_size += arg.size() + 1;
  72. }
  73. // Allocate one chunk of storage for the actual C-strings and a vector of
  74. // pointers into the storage.
  75. llvm::OwningArrayRef<char> cstr_arg_storage(total_size);
  76. llvm::SmallVector<const char*, 64> cstr_args;
  77. cstr_args.reserve(args.size() + inject_v_arg + 1);
  78. for (ssize_t i = 0; llvm::StringRef arg : args_range) {
  79. cstr_args.push_back(&cstr_arg_storage[i]);
  80. memcpy(&cstr_arg_storage[i], arg.data(), arg.size());
  81. i += arg.size();
  82. cstr_arg_storage[i] = '\0';
  83. ++i;
  84. }
  85. for (const char* cstr_arg : llvm::ArrayRef(cstr_args)) {
  86. CARBON_VLOG(" '{0}'\n", cstr_arg);
  87. }
  88. if (!args.empty() && args[0].starts_with("-cc1")) {
  89. CARBON_VLOG("Calling clang_main for cc1...");
  90. // cstr_args[0] will be the `clang_path` so we don't need the prepend arg.
  91. llvm::ToolContext tool_context = {
  92. .Path = cstr_args[0], .PrependArg = "clang", .NeedsPrependArg = false};
  93. int exit_code = clang_main(
  94. cstr_args.size(), const_cast<char**>(cstr_args.data()), tool_context);
  95. // TODO: Should this be forwarding the full exit code?
  96. return exit_code == 0;
  97. }
  98. CARBON_VLOG("Preparing Clang driver...\n");
  99. // Create the diagnostic options and parse arguments controlling them out of
  100. // our arguments.
  101. llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnostic_options =
  102. clang::CreateAndPopulateDiagOpts(cstr_args);
  103. // TODO: We don't yet support serializing diagnostics the way the actual
  104. // `clang` command line does. Unclear if we need to or not, but it would need
  105. // a bit more logic here to set up chained consumers.
  106. clang::TextDiagnosticPrinter diagnostic_client(llvm::errs(),
  107. diagnostic_options.get());
  108. clang::DiagnosticsEngine diagnostics(
  109. diagnostic_ids_, diagnostic_options.get(), &diagnostic_client,
  110. /*ShouldOwnClient=*/false);
  111. clang::ProcessWarningOptions(diagnostics, *diagnostic_options, *fs_);
  112. clang::driver::Driver driver(clang_path, target_, diagnostics,
  113. "clang LLVM compiler", fs_);
  114. // Configure the install directory to find other tools and data files.
  115. //
  116. // We directly override the detected directory as we use a synthetic path
  117. // above. This makes it appear that our binary was in the installed binaries
  118. // directory, and allows finding tools relative to it.
  119. driver.Dir = installation_->llvm_install_bin();
  120. CARBON_VLOG("Setting bin directory to: {0}\n", driver.Dir);
  121. // When there's only one command being run, this will run it in-process.
  122. // However, a `clang` invocation may cause multiple `cc1` invocations, which
  123. // still subprocess. See `InProcess` comment at:
  124. // https://github.com/llvm/llvm-project/blob/86ce8e4504c06ecc3cc42f002ad4eb05cac10925/clang/lib/Driver/Job.cpp#L411-L413
  125. //
  126. // Note the subprocessing will effectively call `clang -cc1`, which turns into
  127. // `carbon-busybox clang -cc1`, which results in an equivalent `clang_main`
  128. // call.
  129. auto cc1_main = [](llvm::SmallVectorImpl<const char*>& cc1_args) -> int {
  130. // cc1_args[0] will be the `clang_path` so we don't need the prepend arg.
  131. llvm::ToolContext tool_context = {
  132. .Path = cc1_args[0], .PrependArg = "clang", .NeedsPrependArg = false};
  133. return clang_main(cc1_args.size(), const_cast<char**>(cc1_args.data()),
  134. tool_context);
  135. };
  136. driver.CC1Main = cc1_main;
  137. std::unique_ptr<clang::driver::Compilation> compilation(
  138. driver.BuildCompilation(cstr_args));
  139. CARBON_CHECK(compilation, "Should always successfully allocate!");
  140. if (compilation->containsError()) {
  141. // These should have been diagnosed by the driver.
  142. return false;
  143. }
  144. CARBON_VLOG("Running Clang driver...\n");
  145. llvm::SmallVector<std::pair<int, const clang::driver::Command*>>
  146. failing_commands;
  147. int result = driver.ExecuteCompilation(*compilation, failing_commands);
  148. // Finish diagnosing any failures before we verbosely log the source of those
  149. // failures.
  150. diagnostic_client.finish();
  151. CARBON_VLOG("Execution result code: {0}\n", result);
  152. for (const auto& [command_result, failing_command] : failing_commands) {
  153. CARBON_VLOG("Failing command '{0}' with code '{1}' was:\n",
  154. failing_command->getExecutable(), command_result);
  155. if (vlog_stream_) {
  156. failing_command->Print(*vlog_stream_, "\n\n", /*Quote=*/true);
  157. }
  158. }
  159. // Return whether the command was executed successfully.
  160. return result == 0 && failing_commands.empty();
  161. }
  162. } // namespace Carbon