busybox_main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <unistd.h>
  5. #include <cstdlib>
  6. #include <string>
  7. #include "clang/Driver/Driver.h"
  8. #include "common/bazel_working_dir.h"
  9. #include "common/error.h"
  10. #include "common/init_llvm.h"
  11. #include "llvm/ADT/STLExtras.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/Support/Allocator.h"
  15. #include "llvm/Support/Error.h"
  16. #include "llvm/Support/LLVMDriver.h"
  17. #include "toolchain/base/install_paths.h"
  18. #include "toolchain/driver/driver.h"
  19. #include "toolchain/install/busybox_info.h"
  20. namespace Carbon {
  21. // The actual `main` implementation. Can return an exit code or an `Error`
  22. // (which causes EXIT_FAILRUE).
  23. static auto Main(int argc, char** argv) -> ErrorOr<int> {
  24. InitLLVM init_llvm(argc, argv);
  25. // Start by resolving any symlinks.
  26. CARBON_ASSIGN_OR_RETURN(auto busybox_info, GetBusyboxInfo(argv[0]));
  27. std::filesystem::path exe_path = busybox_info.bin_path.string();
  28. exe_path = SetWorkingDirForBazelRun(exe_path);
  29. const auto install_paths = InstallPaths::MakeExeRelative(exe_path.native());
  30. if (install_paths.error()) {
  31. return Error(*install_paths.error());
  32. }
  33. // If `LLVM_SYMBOLIZER_PATH` is unset, sets it. Signals.cpp would do some more
  34. // path resolution which this overrides in favor of using the busybox itself
  35. // for symbolization.
  36. setenv(
  37. "LLVM_SYMBOLIZER_PATH",
  38. (install_paths.llvm_install_bin() / "llvm-symbolizer").native().c_str(),
  39. /*overwrite=*/0);
  40. auto fs = llvm::vfs::getRealFileSystem();
  41. llvm::SmallVector<const char*> raw_args;
  42. raw_args.append(argv + 1, argv + argc);
  43. // Expand any response files in the arguments.
  44. llvm::BumpPtrAllocator alloc;
  45. if (llvm::Error error = clang::driver::expandResponseFiles(
  46. raw_args, busybox_info.mode && *busybox_info.mode == "clang-cl",
  47. alloc, fs.get())) {
  48. return Error(llvm::toString(std::move(error)));
  49. }
  50. llvm::SmallVector<llvm::StringRef> args;
  51. args.reserve(argc + 1);
  52. if (busybox_info.mode) {
  53. // Map busybox modes to the relevant subcommands with any flags needed to
  54. // emulate the requested command. Typically, our busyboxed binaries redirect
  55. // to a specific subcommand with some flags set and then pass the remaining
  56. // busybox arguments as positional arguments to that subcommand.
  57. //
  58. // TODO: Add relevant flags to the `clang` subcommand and add `clang`-based
  59. // symlinks to this like `clang++`.
  60. auto subcommand_args =
  61. llvm::StringSwitch<llvm::SmallVector<llvm::StringRef>>(
  62. *busybox_info.mode)
  63. // The `clang` program name used configures the default for its
  64. // `--driver-mode` flag. The first of these is redundant with the
  65. // default, but we group it here for clarity.
  66. .Case("clang", {"clang", "--"})
  67. .Case("clang++", {"clang", "--", "--driver-mode=g++"})
  68. .Case("clang-cl", {"clang", "--", "--driver-mode=cl"})
  69. .Case("clang-cpp", {"clang", "--", "--driver-mode=cpp"})
  70. // LLD has platform-specific program names that we translate into
  71. // platform flags.
  72. .Case("ld.lld", {"lld", "--platform=gnu", "--"})
  73. .Case("ld64.lld", {"lld", "--platform=darwin", "--"})
  74. // We also support a number of LLVM tools with a trivial translation
  75. // to subcommands. If any of these end up needing more advanced
  76. // translation, that can be factored into the `.def` file to provide custom
  77. // expansion here.
  78. #define CARBON_LLVM_TOOL(Id, Name, BinName, MainFn) \
  79. .Case(BinName, {"llvm", Name, "--"})
  80. #include "toolchain/base/llvm_tools.def"
  81. .Default({*busybox_info.mode, "--"});
  82. // And now append the subcommand args.
  83. args.append(subcommand_args);
  84. }
  85. llvm::append_range(args, raw_args);
  86. // We also support a special command line syntax for passing flags to the base
  87. // Carbon driver as `-Xcarbon=--some-carbon-flag=some-value`. This is
  88. // important when build systems only allow appending custom user flags to
  89. // allow them to be used for driver.
  90. //
  91. // Extract any arguments of that form, remove the prefix, and insert them to
  92. // the argument list just before the first positional parameter or subcommand.
  93. // This let's them come after any other flags to the base driver and override
  94. // them if needed.
  95. llvm::SmallVector<llvm::StringRef> extra_driver_args;
  96. llvm::erase_if(args, [&extra_driver_args](llvm::StringRef arg) {
  97. if (arg.consume_front("-Xcarbon=")) {
  98. extra_driver_args.push_back(arg);
  99. return true;
  100. }
  101. return false;
  102. });
  103. if (!extra_driver_args.empty()) {
  104. auto* subcommand_it = llvm::find_if(args, [](llvm::StringRef arg) {
  105. // Flags start with `-`, unless it is the string `-` or `--`.
  106. return !arg.starts_with("-") || arg == "-" || arg == "--";
  107. });
  108. args.insert(subcommand_it, extra_driver_args.begin(),
  109. extra_driver_args.end());
  110. }
  111. Driver driver(fs, &install_paths, stdin, &llvm::outs(), &llvm::errs(),
  112. /*fuzzing=*/false, /*enable_leaking=*/true);
  113. bool success = driver.RunCommand(args).success;
  114. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  115. }
  116. } // namespace Carbon
  117. auto main(int argc, char** argv) -> int {
  118. auto result = Carbon::Main(argc, argv);
  119. if (result.ok()) {
  120. return *result;
  121. } else {
  122. llvm::errs() << "error: " << result.error() << "\n";
  123. return EXIT_FAILURE;
  124. }
  125. }