driver.cpp 9.5 KB

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