driver.cpp 8.0 KB

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