busybox_main.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. args.append({*busybox_info.mode, "--"});
  36. }
  37. args.append(argv + 1, argv + argc);
  38. Driver driver(fs, &install_paths, stdin, &llvm::outs(), &llvm::errs(),
  39. /*fuzzing=*/false, /*enable_leaking=*/true);
  40. bool success = driver.RunCommand(args).success;
  41. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  42. }
  43. } // namespace Carbon
  44. auto main(int argc, char** argv) -> int {
  45. auto result = Carbon::Main(argc, argv);
  46. if (result.ok()) {
  47. return *result;
  48. } else {
  49. llvm::errs() << "error: " << result.error() << "\n";
  50. return EXIT_FAILURE;
  51. }
  52. }