link_subcommand.cpp 4.3 KB

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