llvm_subcommand.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/llvm_subcommand.h"
  5. #include "common/command_line.h"
  6. #include "llvm/TargetParser/Host.h"
  7. #include "llvm/TargetParser/Triple.h"
  8. #include "toolchain/base/llvm_tools.h"
  9. #include "toolchain/driver/llvm_runner.h"
  10. namespace Carbon {
  11. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  12. .name = "llvm",
  13. .help = R"""(
  14. Runs LLVM's command line tools with the provided arguments.
  15. This subcommand provides access to a collection of LLVM's command line tools via
  16. further subcommands. For each of these tools, their command line can be provided
  17. using positional arguments.
  18. )""",
  19. };
  20. auto LLVMOptions::Build(CommandLine::CommandBuilder& b,
  21. DriverSubcommand* subcommand,
  22. DriverSubcommand** selected_subcommand) -> void {
  23. // Add further subcommands for each LLVM tool.
  24. for (LLVMTool tool : LLVMTool::Tools) {
  25. // TODO: The subcommand info for each tool is weirdly stored in the
  26. // `LLVMTool` class instead of here where it makes more logical sense.
  27. // Either we should figure out how to move it to here or we should more
  28. // fully document the oddity of having it in the `LLVMTool` class.
  29. //
  30. // TODO: Currently, the command line subsystem's help isn't as user friendly
  31. // for the generated subcommands below this as it could be. Because each of
  32. // these has a completely generic and stamped out info, the `help` output is
  33. // very repetitive and doesn't actually contribute much information.
  34. b.AddSubcommand(tool.subcommand_info(), [&](auto& sub_b) {
  35. sub_b.AddStringPositionalArg(
  36. {
  37. .name = "ARG",
  38. .help = R"""(
  39. Arguments passed to the LLVM tool.
  40. )""",
  41. },
  42. [&](auto& arg_b) { arg_b.Append(&args); });
  43. sub_b.Do([=, this] {
  44. subcommand_tool = tool;
  45. *selected_subcommand = subcommand;
  46. });
  47. });
  48. }
  49. // The `llvm` subcommand can't be used directly.
  50. b.RequiresSubcommand();
  51. }
  52. LLVMSubcommand::LLVMSubcommand() : DriverSubcommand(SubcommandInfo) {}
  53. auto LLVMSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  54. LLVMRunner runner(driver_env.installation, driver_env.vlog_stream);
  55. // Don't run arbitrary LLVM tools and libraries when fuzzing.
  56. if (TestAndDiagnoseIfFuzzingExternalLibraries(driver_env, "llvm")) {
  57. return {.success = false};
  58. }
  59. return {.success = runner.Run(options_.subcommand_tool, options_.args)};
  60. }
  61. } // namespace Carbon