link_subcommand.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // Disable linking the C++ standard library until can build and ship it as
  58. // part of the Carbon toolchain. This clearly won't work once we get into
  59. // interop, but for now it avoids spurious failures and distraction. The plan
  60. // is to build and bundle libc++ at which point we can replace this with
  61. // pointing at our bundled library.
  62. // TODO: Replace this when ready.
  63. clang_args.push_back("-nostdlib++");
  64. clang_args.push_back("-o");
  65. clang_args.push_back(options_.output_filename);
  66. clang_args.append(options_.object_filenames.begin(),
  67. options_.object_filenames.end());
  68. ClangRunner runner(driver_env.installation, driver_env.fs,
  69. driver_env.vlog_stream);
  70. ErrorOr<bool> run_result = runner.Run(clang_args, driver_env.runtimes_cache,
  71. *driver_env.thread_pool);
  72. if (!run_result.ok()) {
  73. // This is not a Clang failure, but a failure to even run Clang, so we need
  74. // to diagnose it here.
  75. CARBON_DIAGNOSTIC(FailureRunningClangToLink, Error,
  76. "failure running `clang` to perform linking: {0}",
  77. std::string);
  78. driver_env.emitter.Emit(FailureRunningClangToLink,
  79. run_result.error().message());
  80. return {.success = false};
  81. }
  82. // Successfully ran Clang to perform the link, return its result.
  83. return {.success = *run_result};
  84. }
  85. } // namespace Carbon