busybox_main.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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, GetBusyboxInfo(argv[0]));
  23. auto fs = llvm::vfs::getRealFileSystem();
  24. // Resolve paths before calling SetWorkingDirForBazel.
  25. std::string exe_path = FindExecutablePath(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. SetWorkingDirForBazel();
  31. llvm::SmallVector<llvm::StringRef> args;
  32. args.reserve(argc + 1);
  33. if (busybox_info.mode) {
  34. args.append({*busybox_info.mode, "--"});
  35. }
  36. args.append(argv + 1, argv + argc);
  37. Driver driver(fs, &install_paths, stdin, &llvm::outs(), &llvm::errs());
  38. bool success = driver.RunCommand(args).success;
  39. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  40. }
  41. } // namespace Carbon
  42. auto main(int argc, char** argv) -> int {
  43. auto result = Carbon::Main(argc, argv);
  44. if (result.ok()) {
  45. return *result;
  46. } else {
  47. llvm::errs() << "error: " << result.error() << "\n";
  48. return EXIT_FAILURE;
  49. }
  50. }