busybox_main.cpp 3.6 KB

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