clang_subcommand.cpp 4.2 KB

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