driver.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/version.h"
  10. #include "toolchain/driver/clang_subcommand.h"
  11. #include "toolchain/driver/compile_subcommand.h"
  12. #include "toolchain/driver/format_subcommand.h"
  13. #include "toolchain/driver/language_server_subcommand.h"
  14. #include "toolchain/driver/link_subcommand.h"
  15. namespace Carbon {
  16. namespace {
  17. struct Options {
  18. static const CommandLine::CommandInfo Info;
  19. auto Build(CommandLine::CommandBuilder& b) -> void;
  20. bool verbose;
  21. bool fuzzing;
  22. ClangSubcommand clang;
  23. CompileSubcommand compile;
  24. FormatSubcommand format;
  25. LanguageServerSubcommand language_server;
  26. LinkSubcommand link;
  27. // On success, this is set to the subcommand to run.
  28. DriverSubcommand* subcommand = nullptr;
  29. };
  30. } // namespace
  31. // Note that this is not constexpr so that it can include information generated
  32. // in separate translation units and potentially overridden at link time in the
  33. // version string.
  34. const CommandLine::CommandInfo Options::Info = {
  35. .name = "carbon",
  36. .version = Version::ToolchainInfo,
  37. .help = R"""(
  38. This is the unified Carbon Language toolchain driver. Its subcommands provide
  39. all of the core behavior of the toolchain, including compilation, linking, and
  40. developer tools. Each of these has its own subcommand, and you can pass a
  41. specific subcommand to the `help` subcommand to get details about its usage.
  42. )""",
  43. .help_epilogue = R"""(
  44. For questions, issues, or bug reports, please use our GitHub project:
  45. https://github.com/carbon-language/carbon-lang
  46. )""",
  47. };
  48. auto Options::Build(CommandLine::CommandBuilder& b) -> void {
  49. b.AddFlag(
  50. {
  51. .name = "verbose",
  52. .short_name = "v",
  53. .help = "Enable verbose logging to the stderr stream.",
  54. },
  55. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&verbose); });
  56. b.AddFlag(
  57. {
  58. .name = "fuzzing",
  59. .help = "Configure the command line for fuzzing.",
  60. },
  61. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&fuzzing); });
  62. b.AddSubcommand(ClangOptions::Info, [&](CommandLine::CommandBuilder& sub_b) {
  63. clang.BuildOptions(sub_b);
  64. sub_b.Do([&] { subcommand = &clang; });
  65. });
  66. b.AddSubcommand(CompileOptions::Info,
  67. [&](CommandLine::CommandBuilder& sub_b) {
  68. compile.BuildOptions(sub_b);
  69. sub_b.Do([&] { subcommand = &compile; });
  70. });
  71. b.AddSubcommand(FormatOptions::Info, [&](CommandLine::CommandBuilder& sub_b) {
  72. format.BuildOptions(sub_b);
  73. sub_b.Do([&] { subcommand = &format; });
  74. });
  75. b.AddSubcommand(LanguageServerSubcommand::Info,
  76. [&](CommandLine::CommandBuilder& sub_b) {
  77. sub_b.Do([&] { subcommand = &language_server; });
  78. });
  79. b.AddSubcommand(LinkOptions::Info, [&](CommandLine::CommandBuilder& sub_b) {
  80. link.BuildOptions(sub_b);
  81. sub_b.Do([&] { subcommand = &link; });
  82. });
  83. b.RequiresSubcommand();
  84. }
  85. auto Driver::RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> DriverResult {
  86. if (driver_env_.installation->error()) {
  87. llvm::errs() << "error: " << *driver_env_.installation->error() << "\n";
  88. return {.success = false};
  89. }
  90. Options options;
  91. CommandLine::ParseResult result = CommandLine::Parse(
  92. args, driver_env_.output_stream, driver_env_.error_stream, Options::Info,
  93. [&](CommandLine::CommandBuilder& b) { options.Build(b); });
  94. if (result == CommandLine::ParseResult::Error) {
  95. return {.success = false};
  96. } else if (result == CommandLine::ParseResult::MetaSuccess) {
  97. return {.success = true};
  98. }
  99. if (options.verbose) {
  100. // Note this implies streamed output in order to interleave.
  101. driver_env_.vlog_stream = &driver_env_.error_stream;
  102. }
  103. if (options.fuzzing) {
  104. SetFuzzing();
  105. }
  106. CARBON_CHECK(options.subcommand != nullptr);
  107. return options.subcommand->Run(driver_env_);
  108. }
  109. auto Driver::SetFuzzing() -> void { driver_env_.fuzzing = true; }
  110. } // namespace Carbon