link_subcommand.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. constexpr CommandLine::CommandInfo LinkOptions::Info = {
  9. .name = "link",
  10. .help = R"""(
  11. Link Carbon executables.
  12. This subcommand links Carbon executables by combining object files.
  13. TODO: Support linking binary libraries, both archives and shared libraries.
  14. TODO: Support linking against binary libraries.
  15. )""",
  16. };
  17. auto LinkOptions::Build(CommandLine::CommandBuilder& b) -> void {
  18. b.AddStringPositionalArg(
  19. {
  20. .name = "OBJECT_FILE",
  21. .help = R"""(
  22. The input object files.
  23. )""",
  24. },
  25. [&](auto& arg_b) {
  26. arg_b.Required(true);
  27. arg_b.Append(&object_filenames);
  28. });
  29. b.AddStringOption(
  30. {
  31. .name = "output",
  32. .value_name = "FILE",
  33. .help = R"""(
  34. The linked file name. The output is always a linked binary.
  35. )""",
  36. },
  37. [&](auto& arg_b) {
  38. arg_b.Required(true);
  39. arg_b.Set(&output_filename);
  40. });
  41. codegen_options.Build(b);
  42. }
  43. static void AddOSFlags(llvm::StringRef target,
  44. llvm::SmallVectorImpl<llvm::StringRef>& args) {
  45. llvm::Triple triple(target);
  46. switch (triple.getOS()) {
  47. case llvm::Triple::Darwin:
  48. case llvm::Triple::MacOSX:
  49. // On macOS we need to set the sysroot to a viable SDK. Currently, this
  50. // hard codes the path to be the unversioned symlink. The prefix is also
  51. // hard coded in Homebrew and so this seems likely to work reasonably
  52. // well. Homebrew and I suspect the Xcode Clang both have this hard coded
  53. // at build time, so this seems reasonably safe but we can revisit if/when
  54. // needed.
  55. args.push_back(
  56. "--sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk");
  57. // We also need to insist on a modern linker, otherwise the driver tries
  58. // too old and deprecated flags. The specific number here comes from an
  59. // inspection of the Clang driver source code to understand where features
  60. // were enabled, and this appears to be the latest version to control
  61. // driver behavior.
  62. //
  63. // TODO: We should replace this with use of `lld` eventually.
  64. args.push_back("-mlinker-version=705");
  65. break;
  66. default:
  67. // By default, just let the Clang driver handle everything.
  68. break;
  69. }
  70. }
  71. auto LinkSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  72. // TODO: Currently we use the Clang driver to link. This works well on Unix
  73. // OSes but we likely need to directly build logic to invoke `link.exe` on
  74. // Windows where `cl.exe` doesn't typically cover that logic.
  75. // Use a reasonably large small vector here to minimize allocations. We expect
  76. // to link reasonably large numbers of object files.
  77. llvm::SmallVector<llvm::StringRef, 128> clang_args;
  78. // We link using a C++ mode of the driver.
  79. clang_args.push_back("--driver-mode=g++");
  80. // Use LLD, which we provide in our install directory, for linking.
  81. clang_args.push_back("-fuse-ld=lld");
  82. // Disable linking the C++ standard library until can build and ship it as
  83. // part of the Carbon toolchain. This clearly won't work once we get into
  84. // interop, but for now it avoids spurious failures and distraction. The plan
  85. // is to build and bundle libc++ at which point we can replace this with
  86. // pointing at our bundled library.
  87. // TODO: Replace this when ready.
  88. clang_args.push_back("-nostdlib++");
  89. // Add OS-specific flags based on the target.
  90. AddOSFlags(options_.codegen_options.target, clang_args);
  91. clang_args.push_back("-o");
  92. clang_args.push_back(options_.output_filename);
  93. clang_args.append(options_.object_filenames.begin(),
  94. options_.object_filenames.end());
  95. ClangRunner runner(driver_env.installation, options_.codegen_options.target,
  96. driver_env.vlog_stream);
  97. return {.success = runner.Run(clang_args)};
  98. }
  99. } // namespace Carbon