driver.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/driver.h"
  5. #include <algorithm>
  6. #include <filesystem>
  7. #include <memory>
  8. #include <optional>
  9. #include "common/command_line.h"
  10. #include "common/pretty_stack_trace_function.h"
  11. #include "common/version.h"
  12. #include "toolchain/driver/build_runtimes_subcommand.h"
  13. #include "toolchain/driver/clang_subcommand.h"
  14. #include "toolchain/driver/compile_subcommand.h"
  15. #include "toolchain/driver/format_subcommand.h"
  16. #include "toolchain/driver/language_server_subcommand.h"
  17. #include "toolchain/driver/link_subcommand.h"
  18. #include "toolchain/driver/lld_subcommand.h"
  19. #include "toolchain/driver/llvm_subcommand.h"
  20. namespace Carbon {
  21. namespace {
  22. struct Options {
  23. static const CommandLine::CommandInfo Info;
  24. auto Build(CommandLine::CommandBuilder& b) -> void;
  25. bool verbose = false;
  26. bool fuzzing = false;
  27. bool include_diagnostic_kind = false;
  28. llvm::StringRef runtimes_cache_path;
  29. llvm::StringRef prebuilt_runtimes_path;
  30. BuildRuntimesSubcommand runtimes;
  31. ClangSubcommand clang;
  32. CompileSubcommand compile;
  33. FormatSubcommand format;
  34. LanguageServerSubcommand language_server;
  35. LinkSubcommand link;
  36. LldSubcommand lld;
  37. LLVMSubcommand llvm;
  38. // On success, this is set to the subcommand to run.
  39. DriverSubcommand* selected_subcommand = nullptr;
  40. };
  41. } // namespace
  42. // Note that this is not constexpr so that it can include information generated
  43. // in separate translation units and potentially overridden at link time in the
  44. // version string.
  45. const CommandLine::CommandInfo Options::Info = {
  46. .name = "carbon",
  47. .version = Version::ToolchainInfo,
  48. .help = R"""(
  49. This is the unified Carbon Language toolchain driver. Its subcommands provide
  50. all of the core behavior of the toolchain, including compilation, linking, and
  51. developer tools. Each of these has its own subcommand, and you can pass a
  52. specific subcommand to the `help` subcommand to get details about its usage.
  53. )""",
  54. .help_epilogue = R"""(
  55. For questions, issues, or bug reports, please use our GitHub project:
  56. https://github.com/carbon-language/carbon-lang
  57. )""",
  58. };
  59. auto Options::Build(CommandLine::CommandBuilder& b) -> void {
  60. b.AddFlag(
  61. {
  62. .name = "verbose",
  63. .short_name = "v",
  64. .help = "Enable verbose logging to the stderr stream.",
  65. },
  66. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&verbose); });
  67. b.AddStringOption(
  68. {
  69. .name = "runtimes-cache",
  70. .value_name = "PATH",
  71. .help = R"""(
  72. Specify a custom runtimes cache location.
  73. By default, the runtimes cache is located in the `carbon_runtimes` subdirectory
  74. of `$XDG_CACHE_HOME` (or `$HOME/.cache` if not set). If unable to use either, it
  75. will be placed in a temporary directory that is removed when the command
  76. completes. This flag overrides that logic with a specific path. It has no effect
  77. if --prebuilt-runtimes is set.
  78. )""",
  79. },
  80. [&](auto& arg_b) { arg_b.Set(&runtimes_cache_path); });
  81. b.AddStringOption(
  82. {
  83. .name = "prebuilt-runtimes",
  84. .value_name = "PATH",
  85. .help = R"""(
  86. Path to prebuilt runtimes tree.
  87. If this option is provided, runtimes will not be built on demand and this path
  88. will be used instead.
  89. )""",
  90. },
  91. [&](auto& arg_b) { arg_b.Set(&prebuilt_runtimes_path); });
  92. b.AddFlag(
  93. {
  94. .name = "fuzzing",
  95. .help = "Configure the command line for fuzzing.",
  96. },
  97. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&fuzzing); });
  98. b.AddFlag(
  99. {
  100. .name = "include-diagnostic-kind",
  101. .help = R"""(
  102. When printing diagnostics, include the diagnostic kind as part of output. This
  103. applies to each message that forms a diagnostic, not just the primary message.
  104. )""",
  105. },
  106. [&](auto& arg_b) { arg_b.Set(&include_diagnostic_kind); });
  107. runtimes.AddTo(b, &selected_subcommand);
  108. clang.AddTo(b, &selected_subcommand);
  109. compile.AddTo(b, &selected_subcommand);
  110. format.AddTo(b, &selected_subcommand);
  111. language_server.AddTo(b, &selected_subcommand);
  112. link.AddTo(b, &selected_subcommand);
  113. lld.AddTo(b, &selected_subcommand);
  114. llvm.AddTo(b, &selected_subcommand);
  115. b.RequiresSubcommand();
  116. }
  117. auto Driver::RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> DriverResult {
  118. PrettyStackTraceFunction trace_version([&](llvm::raw_ostream& out) {
  119. out << "Carbon version: " << Version::String << "\n";
  120. });
  121. if (driver_env_.installation->error()) {
  122. CARBON_DIAGNOSTIC(DriverInstallInvalid, Error, "{0}", std::string);
  123. driver_env_.emitter.Emit(DriverInstallInvalid,
  124. driver_env_.installation->error()->str());
  125. return {.success = false};
  126. }
  127. Options options;
  128. ErrorOr<CommandLine::ParseResult> result = CommandLine::Parse(
  129. args, *driver_env_.output_stream, Options::Info,
  130. [&](CommandLine::CommandBuilder& b) { options.Build(b); });
  131. // Regardless of whether the parse succeeded, try to use the diagnostic kind
  132. // flag.
  133. driver_env_.consumer.set_include_diagnostic_kind(
  134. options.include_diagnostic_kind);
  135. if (!result.ok()) {
  136. CARBON_DIAGNOSTIC(DriverCommandLineParseFailed, Error, "{0}", std::string);
  137. driver_env_.emitter.Emit(DriverCommandLineParseFailed,
  138. PrintToString(result.error()));
  139. return {.success = false};
  140. } else if (*result == CommandLine::ParseResult::MetaSuccess) {
  141. return {.success = true};
  142. }
  143. auto cache_result =
  144. options.runtimes_cache_path.empty()
  145. ? Runtimes::Cache::MakeSystem(*driver_env_.installation,
  146. driver_env_.vlog_stream)
  147. : Runtimes::Cache::MakeCustom(
  148. *driver_env_.installation,
  149. std::filesystem::absolute(options.runtimes_cache_path.str()),
  150. driver_env_.vlog_stream);
  151. if (!cache_result.ok()) {
  152. // TODO: We should provide a better diagnostic than the raw error.
  153. CARBON_DIAGNOSTIC(DriverRuntimesCacheInvalid, Error, "{0}", std::string);
  154. driver_env_.emitter.Emit(DriverRuntimesCacheInvalid,
  155. cache_result.error().message());
  156. return {.success = false};
  157. }
  158. driver_env_.runtimes_cache = std::move(*cache_result);
  159. if (!options.prebuilt_runtimes_path.empty()) {
  160. auto result = Runtimes::Make(
  161. std::filesystem::absolute(options.prebuilt_runtimes_path.str()),
  162. driver_env_.vlog_stream);
  163. if (!result.ok()) {
  164. // TODO: We should provide a better diagnostic than the raw error.
  165. CARBON_DIAGNOSTIC(DriverPrebuiltRuntimesInvalid, Error, "{0}",
  166. std::string);
  167. driver_env_.emitter.Emit(DriverPrebuiltRuntimesInvalid,
  168. result.error().message());
  169. return {.success = false};
  170. }
  171. driver_env_.prebuilt_runtimes = *std::move(result);
  172. }
  173. if (options.verbose) {
  174. // Note this implies streamed output in order to interleave.
  175. driver_env_.vlog_stream = driver_env_.error_stream;
  176. }
  177. if (options.fuzzing) {
  178. driver_env_.fuzzing = true;
  179. }
  180. CARBON_CHECK(options.selected_subcommand != nullptr);
  181. return options.selected_subcommand->Run(driver_env_);
  182. }
  183. } // namespace Carbon