lld_subcommand.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/lld_subcommand.h"
  5. #include "llvm/TargetParser/Host.h"
  6. #include "llvm/TargetParser/Triple.h"
  7. #include "toolchain/driver/lld_runner.h"
  8. namespace Carbon {
  9. auto LldOptions::Build(CommandLine::CommandBuilder& b) -> void {
  10. // We want to select a default platform based on the default target. Since
  11. // that requires some dynamic inspection of the target, do that here.
  12. std::string default_target = llvm::sys::getDefaultTargetTriple();
  13. llvm::Triple default_triple(default_target);
  14. switch (default_triple.getObjectFormat()) {
  15. case llvm::Triple::MachO:
  16. platform = Platform::MachO;
  17. break;
  18. // We default to the GNU or Unix platform as ELF is a plausible default
  19. // and LLD doesn't support any generic invocations.
  20. default:
  21. case llvm::Triple::ELF:
  22. platform = Platform::Elf;
  23. break;
  24. }
  25. b.AddOneOfOption(
  26. {
  27. .name = "platform",
  28. .help = R"""(
  29. Platform linking style to use. The default is selected to match the default
  30. target's platform.
  31. )""",
  32. },
  33. [&](auto& arg_b) {
  34. arg_b.SetOneOf(
  35. {
  36. arg_b.OneOfValue("elf", Platform::Elf),
  37. // Some of LLD documentation uses "Unix" or "GNU", so
  38. // include an alias here.
  39. arg_b.OneOfValue("gnu", Platform::Elf),
  40. arg_b.OneOfValue("unix", Platform::Elf),
  41. arg_b.OneOfValue("macho", Platform::MachO),
  42. // Darwin is also sometimes used, include it as an alias here.
  43. arg_b.OneOfValue("darwin", Platform::MachO),
  44. },
  45. &platform);
  46. });
  47. b.AddStringPositionalArg(
  48. {
  49. .name = "ARG",
  50. .help = R"""(
  51. Arguments passed to LLD.
  52. )""",
  53. },
  54. [&](auto& arg_b) { arg_b.Append(&args); });
  55. }
  56. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  57. .name = "lld",
  58. .help = R"""(
  59. Runs LLD with the provided arguments.
  60. Note that a specific LLD platform must be selected, and it is actually that
  61. particular platform's LLD-driver that is run with the arguments. There is no
  62. generic LLD command line.
  63. For a given platform, this is equivalent to running that platform's LLD alias
  64. directly, and provides the full command line interface.
  65. Use `carbon lld --platform=elf -- ARGS` to separate the `ARGS` forwarded to LLD
  66. from the flags passed to the Carbon subcommand.
  67. Note that typically it is better to use a higher level command to link code,
  68. such as invoking `carbon link` with the relevant flags. However, this subcommand
  69. supports when you already have a specific invocation using existing command line
  70. syntaxes, as well as testing and debugging of the underlying tool.
  71. )""",
  72. };
  73. LldSubcommand::LldSubcommand() : DriverSubcommand(SubcommandInfo) {}
  74. // TODO: This lacks a lot of features from the main driver code. We may need to
  75. // add more.
  76. // https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
  77. auto LldSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  78. LldRunner runner(driver_env.installation, driver_env.vlog_stream);
  79. // Don't run LLD when fuzzing, as we're not currently in a good position to
  80. // debug and fix fuzzer-found bugs within LLD.
  81. if (!DisableFuzzingExternalLibraries(driver_env, "lld")) {
  82. return {.success = false};
  83. }
  84. switch (options_.platform) {
  85. case LldOptions::Platform::Elf:
  86. return {.success = runner.ElfLink(options_.args)};
  87. case LldOptions::Platform::MachO:
  88. return {.success = runner.MachOLink(options_.args)};
  89. }
  90. CARBON_FATAL("Failed to find and run a valid LLD platform link!");
  91. }
  92. } // namespace Carbon