busybox_main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/base/install_paths.h"
  14. #include "toolchain/driver/driver.h"
  15. #include "toolchain/install/busybox_info.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(
  34. "LLVM_SYMBOLIZER_PATH",
  35. (install_paths.llvm_install_bin().native() + "llvm-symbolizer").c_str(),
  36. /*overwrite=*/0);
  37. SetWorkingDirForBazel();
  38. llvm::SmallVector<llvm::StringRef> args;
  39. args.reserve(argc + 1);
  40. if (busybox_info.mode) {
  41. // Map busybox modes to the relevant subcommands with any flags needed to
  42. // emulate the requested command. Typically, our busyboxed binaries redirect
  43. // to a specific subcommand with some flags set and then pass the remaining
  44. // busybox arguments as positional arguments to that subcommand.
  45. //
  46. // TODO: Add relevant flags to the `clang` subcommand and add `clang`-based
  47. // symlinks to this like `clang++`.
  48. auto subcommand_args =
  49. llvm::StringSwitch<llvm::SmallVector<llvm::StringRef>>(
  50. *busybox_info.mode)
  51. // The `clang` program name used configures the default for its
  52. // `--driver-mode` flag. The first of these is redundant with the
  53. // default, but we group it here for clarity.
  54. .Case("clang", {"clang", "--"})
  55. .Case("clang++", {"clang", "--", "--driver-mode=g++"})
  56. .Case("clang-cl", {"clang", "--", "--driver-mode=cl"})
  57. .Case("clang-cpp", {"clang", "--", "--driver-mode=cpp"})
  58. // LLD has platform-specific program names that we translate into
  59. // platform flags.
  60. .Case("ld.lld", {"lld", "--platform=gnu", "--"})
  61. .Case("ld64.lld", {"lld", "--platform=darwin", "--"})
  62. // We also support a number of LLVM tools with a trivial translation
  63. // to subcommands. If any of these end up needing more advanced
  64. // translation, that can be factored into the `.def` file to provide custom
  65. // expansion here.
  66. #define CARBON_LLVM_TOOL(Id, Name, BinName, MainFn) \
  67. .Case(BinName, {"llvm", Name, "--"})
  68. #include "toolchain/base/llvm_tools.def"
  69. .Default({*busybox_info.mode, "--"});
  70. args.append(subcommand_args);
  71. }
  72. args.append(argv + 1, argv + argc);
  73. Driver driver(fs, &install_paths, stdin, &llvm::outs(), &llvm::errs(),
  74. /*fuzzing=*/false, /*enable_leaking=*/true);
  75. bool success = driver.RunCommand(args).success;
  76. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  77. }
  78. } // namespace Carbon
  79. auto main(int argc, char** argv) -> int {
  80. auto result = Carbon::Main(argc, argv);
  81. if (result.ok()) {
  82. return *result;
  83. } else {
  84. llvm::errs() << "error: " << result.error() << "\n";
  85. return EXIT_FAILURE;
  86. }
  87. }