driver.cpp 3.4 KB

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