link_subcommand.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/link_subcommand.h"
  5. #include "llvm/TargetParser/Triple.h"
  6. #include "toolchain/driver/clang_runner.h"
  7. namespace Carbon {
  8. auto LinkOptions::Build(CommandLine::CommandBuilder& b) -> void {
  9. b.AddStringPositionalArg(
  10. {
  11. .name = "OBJECT_FILE",
  12. .help = R"""(
  13. The input object files.
  14. )""",
  15. },
  16. [&](auto& arg_b) {
  17. arg_b.Required(true);
  18. arg_b.Append(&object_filenames);
  19. });
  20. b.AddStringOption(
  21. {
  22. .name = "output",
  23. .value_name = "FILE",
  24. .help = R"""(
  25. The linked file name. The output is always a linked binary.
  26. )""",
  27. },
  28. [&](auto& arg_b) {
  29. arg_b.Required(true);
  30. arg_b.Set(&output_filename);
  31. });
  32. codegen_options.Build(b);
  33. }
  34. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  35. .name = "link",
  36. .help = R"""(
  37. Link Carbon executables.
  38. This subcommand links Carbon executables by combining object files.
  39. TODO: Support linking binary libraries, both archives and shared libraries.
  40. TODO: Support linking against binary libraries.
  41. )""",
  42. };
  43. LinkSubcommand::LinkSubcommand() : DriverSubcommand(SubcommandInfo) {}
  44. auto LinkSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  45. // TODO: Currently we use the Clang driver to link. This works well on Unix
  46. // OSes but we likely need to directly build logic to invoke `link.exe` on
  47. // Windows where `cl.exe` doesn't typically cover that logic.
  48. // Use a reasonably large small vector here to minimize allocations. We expect
  49. // to link reasonably large numbers of object files.
  50. llvm::SmallVector<llvm::StringRef, 128> clang_args;
  51. // We link using a C++ mode of the driver.
  52. clang_args.push_back("--driver-mode=g++");
  53. // Pass the target down to Clang to pick up the correct defaults.
  54. std::string target_arg =
  55. llvm::formatv("--target={0}", options_.codegen_options.target).str();
  56. clang_args.push_back(target_arg);
  57. clang_args.push_back("-o");
  58. clang_args.push_back(options_.output_filename);
  59. clang_args.append(options_.object_filenames.begin(),
  60. options_.object_filenames.end());
  61. ClangRunner runner(driver_env.installation, driver_env.fs,
  62. driver_env.vlog_stream);
  63. ErrorOr<bool> run_result =
  64. driver_env.prebuilt_runtimes
  65. ? runner.RunWithPrebuiltRuntimes(clang_args,
  66. *driver_env.prebuilt_runtimes,
  67. driver_env.enable_leaking)
  68. : runner.Run(clang_args, driver_env.runtimes_cache,
  69. *driver_env.thread_pool, driver_env.enable_leaking);
  70. if (!run_result.ok()) {
  71. // This is not a Clang failure, but a failure to even run Clang, so we need
  72. // to diagnose it here.
  73. CARBON_DIAGNOSTIC(FailureRunningClangToLink, Error,
  74. "failure running `clang` to perform linking: {0}",
  75. std::string);
  76. driver_env.emitter.Emit(FailureRunningClangToLink,
  77. run_result.error().message());
  78. return {.success = false};
  79. }
  80. // Successfully ran Clang to perform the link, return its result.
  81. return {.success = *run_result};
  82. }
  83. } // namespace Carbon