busybox_main.cpp 3.8 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 <unistd.h>
  5. #include <cstdlib>
  6. #include <string>
  7. #include "common/bazel_working_dir.h"
  8. #include "common/error.h"
  9. #include "common/init_llvm.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/LLVMDriver.h"
  13. #include "toolchain/driver/driver.h"
  14. #include "toolchain/install/busybox_info.h"
  15. #include "toolchain/install/install_paths.h"
  16. namespace Carbon {
  17. // The actual `main` implementation. Can return an exit code or an `Error`
  18. // (which causes EXIT_FAILRUE).
  19. static auto Main(int argc, char** argv) -> ErrorOr<int> {
  20. InitLLVM init_llvm(argc, argv);
  21. // Start by resolving any symlinks.
  22. CARBON_ASSIGN_OR_RETURN(auto busybox_info, GetBusyboxInfo(argv[0]));
  23. auto fs = llvm::vfs::getRealFileSystem();
  24. // Resolve paths before calling SetWorkingDirForBazel.
  25. std::string exe_path = busybox_info.bin_path.string();
  26. const auto install_paths = InstallPaths::MakeExeRelative(exe_path);
  27. if (install_paths.error()) {
  28. return Error(*install_paths.error());
  29. }
  30. // If `LLVM_SYMBOLIZER_PATH` is unset, sets it. Signals.cpp would do some more
  31. // path resolution which this overrides in favor of using the busybox itself
  32. // for symbolization.
  33. setenv("LLVM_SYMBOLIZER_PATH",
  34. (install_paths.llvm_install_bin() + "llvm-symbolizer").c_str(),
  35. /*overwrite=*/0);
  36. SetWorkingDirForBazel();
  37. llvm::SmallVector<llvm::StringRef> args;
  38. args.reserve(argc + 1);
  39. if (busybox_info.mode) {
  40. // Map busybox modes to the relevant subcommands with any flags needed to
  41. // emulate the requested command. Typically, our busyboxed binaries redirect
  42. // to a specific subcommand with some flags set and then pass the remaining
  43. // busybox arguments as positional arguments to that subcommand.
  44. //
  45. // TODO: Add relevant flags to the `clang` subcommand and add `clang`-based
  46. // symlinks to this like `clang++`.
  47. auto subcommand_args =
  48. llvm::StringSwitch<llvm::SmallVector<llvm::StringRef>>(
  49. *busybox_info.mode)
  50. // The `clang` program name used configures the default for its
  51. // `--driver-mode` flag. The first of these is redundant with the
  52. // default, but we group it here for clarity.
  53. .Case("clang", {"clang", "--"})
  54. .Case("clang++", {"clang", "--", "--driver-mode=g++"})
  55. .Case("clang-cl", {"clang", "--", "--driver-mode=cl"})
  56. .Case("clang-cpp", {"clang", "--", "--driver-mode=cpp"})
  57. // LLD has platform-specific program names that we translate into
  58. // platform flags.
  59. .Case("ld.lld", {"lld", "--platform=gnu", "--"})
  60. .Case("ld64.lld", {"lld", "--platform=darwin", "--"})
  61. // We also support a number of LLVM tools with a trivial translation
  62. // to subcommands. If any of these end up needing more advanced
  63. // translation, that can be factored into the `.def` file to provide custom
  64. // expansion here.
  65. #define CARBON_LLVM_TOOL(Id, Name, BinName, MainFn) \
  66. .Case(BinName, {"llvm", Name, "--"})
  67. #include "toolchain/base/llvm_tools.def"
  68. .Default({*busybox_info.mode, "--"});
  69. args.append(subcommand_args);
  70. }
  71. args.append(argv + 1, argv + argc);
  72. Driver driver(fs, &install_paths, stdin, &llvm::outs(), &llvm::errs(),
  73. /*fuzzing=*/false, /*enable_leaking=*/true);
  74. bool success = driver.RunCommand(args).success;
  75. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  76. }
  77. } // namespace Carbon
  78. auto main(int argc, char** argv) -> int {
  79. auto result = Carbon::Main(argc, argv);
  80. if (result.ok()) {
  81. return *result;
  82. } else {
  83. llvm::errs() << "error: " << result.error() << "\n";
  84. return EXIT_FAILURE;
  85. }
  86. }