busybox_main.cpp 3.6 KB

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