driver.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <memory>
  7. #include <optional>
  8. #include "common/command_line.h"
  9. #include "common/pretty_stack_trace_function.h"
  10. #include "common/version.h"
  11. #include "toolchain/driver/build_runtimes_subcommand.h"
  12. #include "toolchain/driver/clang_subcommand.h"
  13. #include "toolchain/driver/compile_subcommand.h"
  14. #include "toolchain/driver/format_subcommand.h"
  15. #include "toolchain/driver/language_server_subcommand.h"
  16. #include "toolchain/driver/link_subcommand.h"
  17. #include "toolchain/driver/lld_subcommand.h"
  18. #include "toolchain/driver/llvm_subcommand.h"
  19. namespace Carbon {
  20. namespace {
  21. struct Options {
  22. static const CommandLine::CommandInfo Info;
  23. auto Build(CommandLine::CommandBuilder& b) -> void;
  24. bool verbose = false;
  25. bool fuzzing = false;
  26. bool include_diagnostic_kind = false;
  27. BuildRuntimesSubcommand runtimes;
  28. ClangSubcommand clang;
  29. CompileSubcommand compile;
  30. FormatSubcommand format;
  31. LanguageServerSubcommand language_server;
  32. LinkSubcommand link;
  33. LldSubcommand lld;
  34. LLVMSubcommand llvm;
  35. // On success, this is set to the subcommand to run.
  36. DriverSubcommand* selected_subcommand = nullptr;
  37. };
  38. } // namespace
  39. // Note that this is not constexpr so that it can include information generated
  40. // in separate translation units and potentially overridden at link time in the
  41. // version string.
  42. const CommandLine::CommandInfo Options::Info = {
  43. .name = "carbon",
  44. .version = Version::ToolchainInfo,
  45. .help = R"""(
  46. This is the unified Carbon Language toolchain driver. Its subcommands provide
  47. all of the core behavior of the toolchain, including compilation, linking, and
  48. developer tools. Each of these has its own subcommand, and you can pass a
  49. specific subcommand to the `help` subcommand to get details about its usage.
  50. )""",
  51. .help_epilogue = R"""(
  52. For questions, issues, or bug reports, please use our GitHub project:
  53. https://github.com/carbon-language/carbon-lang
  54. )""",
  55. };
  56. auto Options::Build(CommandLine::CommandBuilder& b) -> void {
  57. b.AddFlag(
  58. {
  59. .name = "verbose",
  60. .short_name = "v",
  61. .help = "Enable verbose logging to the stderr stream.",
  62. },
  63. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&verbose); });
  64. b.AddFlag(
  65. {
  66. .name = "fuzzing",
  67. .help = "Configure the command line for fuzzing.",
  68. },
  69. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&fuzzing); });
  70. b.AddFlag(
  71. {
  72. .name = "include-diagnostic-kind",
  73. .help = R"""(
  74. When printing diagnostics, include the diagnostic kind as part of output. This
  75. applies to each message that forms a diagnostic, not just the primary message.
  76. )""",
  77. },
  78. [&](auto& arg_b) { arg_b.Set(&include_diagnostic_kind); });
  79. runtimes.AddTo(b, &selected_subcommand);
  80. clang.AddTo(b, &selected_subcommand);
  81. compile.AddTo(b, &selected_subcommand);
  82. format.AddTo(b, &selected_subcommand);
  83. language_server.AddTo(b, &selected_subcommand);
  84. link.AddTo(b, &selected_subcommand);
  85. lld.AddTo(b, &selected_subcommand);
  86. llvm.AddTo(b, &selected_subcommand);
  87. b.RequiresSubcommand();
  88. }
  89. auto Driver::RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> DriverResult {
  90. PrettyStackTraceFunction trace_version([&](llvm::raw_ostream& out) {
  91. out << "Carbon version: " << Version::String << "\n";
  92. });
  93. if (driver_env_.installation->error()) {
  94. CARBON_DIAGNOSTIC(DriverInstallInvalid, Error, "{0}", std::string);
  95. driver_env_.emitter.Emit(DriverInstallInvalid,
  96. driver_env_.installation->error()->str());
  97. return {.success = false};
  98. }
  99. Options options;
  100. ErrorOr<CommandLine::ParseResult> result = CommandLine::Parse(
  101. args, *driver_env_.output_stream, Options::Info,
  102. [&](CommandLine::CommandBuilder& b) { options.Build(b); });
  103. // Regardless of whether the parse succeeded, try to use the diagnostic kind
  104. // flag.
  105. driver_env_.consumer.set_include_diagnostic_kind(
  106. options.include_diagnostic_kind);
  107. if (!result.ok()) {
  108. CARBON_DIAGNOSTIC(DriverCommandLineParseFailed, Error, "{0}", std::string);
  109. driver_env_.emitter.Emit(DriverCommandLineParseFailed,
  110. PrintToString(result.error()));
  111. return {.success = false};
  112. } else if (*result == CommandLine::ParseResult::MetaSuccess) {
  113. return {.success = true};
  114. }
  115. if (options.verbose) {
  116. // Note this implies streamed output in order to interleave.
  117. driver_env_.vlog_stream = driver_env_.error_stream;
  118. }
  119. if (options.fuzzing) {
  120. driver_env_.fuzzing = true;
  121. }
  122. CARBON_CHECK(options.selected_subcommand != nullptr);
  123. return options.selected_subcommand->Run(driver_env_);
  124. }
  125. } // namespace Carbon