driver.cpp 3.4 KB

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