clang_subcommand.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. arg_b.Default(true);
  27. arg_b.Set(&build_runtimes_on_demand);
  28. });
  29. b.AddStringPositionalArg(
  30. {
  31. .name = "ARG",
  32. .help = R"""(
  33. Arguments passed to Clang.
  34. )""",
  35. },
  36. [&](auto& arg_b) { arg_b.Append(&args); });
  37. }
  38. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  39. .name = "clang",
  40. .help = R"""(
  41. Runs Clang on arguments.
  42. This is equivalent to running the `clang` command line directly, and provides
  43. the full command line interface.
  44. Use `carbon clang -- ARGS` to pass flags to `clang`. Although there are
  45. currently no flags for `carbon clang`, the `--` reserves the ability to add
  46. flags in the future.
  47. This is provided to help guarantee consistent compilation of C++ files, both
  48. when Clang is invoked directly and when a Carbon file importing a C++ file
  49. results in an indirect Clang invocation.
  50. )""",
  51. };
  52. ClangSubcommand::ClangSubcommand() : DriverSubcommand(SubcommandInfo) {}
  53. // TODO: This lacks a lot of features from the main driver code. We may need to
  54. // add more.
  55. // https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
  56. auto ClangSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  57. ClangRunner runner(driver_env.installation, driver_env.fs,
  58. driver_env.vlog_stream);
  59. // Don't run Clang when fuzzing, it is known to not be reliable under fuzzing
  60. // due to many unfixed issues.
  61. if (TestAndDiagnoseIfFuzzingExternalLibraries(driver_env, "clang")) {
  62. return {.success = false};
  63. }
  64. ErrorOr<bool> run_result = false;
  65. if (driver_env.prebuilt_runtimes) {
  66. run_result = runner.RunWithPrebuiltRuntimes(options_.args,
  67. *driver_env.prebuilt_runtimes,
  68. driver_env.enable_leaking);
  69. } else if (options_.build_runtimes_on_demand) {
  70. run_result = runner.Run(options_.args, driver_env.runtimes_cache,
  71. *driver_env.thread_pool, driver_env.enable_leaking);
  72. } else {
  73. run_result =
  74. runner.RunWithNoRuntimes(options_.args, driver_env.enable_leaking);
  75. }
  76. if (!run_result.ok()) {
  77. // This is not a Clang failure, but a failure to even run Clang, so we need
  78. // to diagnose it here.
  79. CARBON_DIAGNOSTIC(FailureRunningClang, Error,
  80. "failure running `clang` subcommand: {0}", std::string);
  81. driver_env.emitter.Emit(FailureRunningClang, run_result.error().message());
  82. return {.success = false};
  83. }
  84. // Successfully ran Clang, but return whether Clang itself succeeded.
  85. return {.success = *run_result};
  86. }
  87. } // namespace Carbon