clang_subcommand.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/clang_subcommand.h"
  5. #include <string>
  6. #include "llvm/TargetParser/Host.h"
  7. #include "toolchain/driver/clang_runner.h"
  8. namespace Carbon {
  9. auto ClangOptions::Build(CommandLine::CommandBuilder& b) -> void {
  10. b.AddFlag(
  11. {
  12. .name = "build-runtimes",
  13. .help = R"""(
  14. Enables on-demand building of target-specific runtimes.
  15. When enabled, any link actions using `clang` will build the necessary runtimes
  16. on-demand. This build will use any customization it can from the link command
  17. line flags to build the runtimes for the correct target and with any desired
  18. features enabled.
  19. Note: this only has an effect when `--prebuilt-runtimes` are not provided. If
  20. there are no prebuilt runtimes and building runtimes is disabled, then it is
  21. assumed the installed toolchain has had the necessary target runtimes added to
  22. the installation tree in the default searched locations.
  23. )""",
  24. },
  25. [&](auto& arg_b) {
  26. // TODO: Once runtimes are cached properly, the plan is to enable this
  27. // by default.
  28. arg_b.Default(false);
  29. arg_b.Set(&build_runtimes_on_demand);
  30. });
  31. b.AddStringPositionalArg(
  32. {
  33. .name = "ARG",
  34. .help = R"""(
  35. Arguments passed to Clang.
  36. )""",
  37. },
  38. [&](auto& arg_b) { arg_b.Append(&args); });
  39. }
  40. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  41. .name = "clang",
  42. .help = R"""(
  43. Runs Clang on arguments.
  44. This is equivalent to running the `clang` command line directly, and provides
  45. the full command line interface.
  46. Use `carbon clang -- ARGS` to pass flags to `clang`. Although there are
  47. currently no flags for `carbon clang`, the `--` reserves the ability to add
  48. flags in the future.
  49. This is provided to help guarantee consistent compilation of C++ files, both
  50. when Clang is invoked directly and when a Carbon file importing a C++ file
  51. results in an indirect Clang invocation.
  52. )""",
  53. };
  54. ClangSubcommand::ClangSubcommand() : DriverSubcommand(SubcommandInfo) {}
  55. // TODO: This lacks a lot of features from the main driver code. We may need to
  56. // add more.
  57. // https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
  58. auto ClangSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  59. ClangRunner runner(driver_env.installation, driver_env.fs,
  60. driver_env.vlog_stream);
  61. // Don't run Clang when fuzzing, it is known to not be reliable under fuzzing
  62. // due to many unfixed issues.
  63. if (TestAndDiagnoseIfFuzzingExternalLibraries(driver_env, "clang")) {
  64. return {.success = false};
  65. }
  66. ErrorOr<bool> run_result = false;
  67. if (driver_env.prebuilt_runtimes) {
  68. run_result = runner.RunWithPrebuiltRuntimes(options_.args,
  69. *driver_env.prebuilt_runtimes,
  70. driver_env.enable_leaking);
  71. } else if (options_.build_runtimes_on_demand) {
  72. run_result = runner.Run(options_.args, driver_env.runtimes_cache,
  73. *driver_env.thread_pool, driver_env.enable_leaking);
  74. } else {
  75. run_result =
  76. runner.RunWithNoRuntimes(options_.args, driver_env.enable_leaking);
  77. }
  78. if (!run_result.ok()) {
  79. // This is not a Clang failure, but a failure to even run Clang, so we need
  80. // to diagnose it here.
  81. CARBON_DIAGNOSTIC(FailureRunningClang, Error,
  82. "failure running `clang` subcommand: {0}", std::string);
  83. driver_env.emitter.Emit(FailureRunningClang, run_result.error().message());
  84. return {.success = false};
  85. }
  86. // Successfully ran Clang, but return whether Clang itself succeeded.
  87. return {.success = *run_result};
  88. }
  89. } // namespace Carbon