busybox_main.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <filesystem>
  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/install_paths.h"
  16. namespace Carbon {
  17. namespace {
  18. struct BusyboxInfo {
  19. // The path to `carbon-busybox`.
  20. std::filesystem::path bin_path;
  21. // The mode, such as `carbon` or `clang`.
  22. std::optional<std::string> mode;
  23. };
  24. } // namespace
  25. // Returns the busybox information, given argv[0]. This primarily handles
  26. // resolving symlinks that point at the busybox.
  27. static auto GetBusyboxInfo(llvm::StringRef argv0) -> ErrorOr<BusyboxInfo> {
  28. BusyboxInfo info = BusyboxInfo{argv0.str(), std::nullopt};
  29. while (true) {
  30. std::string filename = info.bin_path.filename();
  31. if (filename == "carbon-busybox") {
  32. return info;
  33. }
  34. std::error_code ec;
  35. auto symlink_target = std::filesystem::read_symlink(info.bin_path, ec);
  36. if (ec) {
  37. return ErrorBuilder()
  38. << "expected carbon-busybox symlink at `" << info.bin_path << "`";
  39. }
  40. info.mode = filename;
  41. info.bin_path = symlink_target;
  42. }
  43. }
  44. // The actual `main` implementation. Can return an exit code or an `Error`
  45. // (which causes EXIT_FAILRUE).
  46. static auto Main(int argc, char** argv) -> ErrorOr<int> {
  47. Carbon::InitLLVM init_llvm(argc, argv);
  48. // Start by resolving any symlinks.
  49. CARBON_ASSIGN_OR_RETURN(auto busybox_info, Carbon::GetBusyboxInfo(argv[0]));
  50. auto fs = llvm::vfs::getRealFileSystem();
  51. // Resolve paths before calling SetWorkingDirForBazel.
  52. std::string exe_path =
  53. Carbon::FindExecutablePath(busybox_info.bin_path.string());
  54. const auto install_paths = Carbon::InstallPaths::MakeExeRelative(exe_path);
  55. if (install_paths.error()) {
  56. return Error(*install_paths.error());
  57. }
  58. Carbon::SetWorkingDirForBazel();
  59. llvm::SmallVector<llvm::StringRef> args;
  60. args.reserve(argc + 1);
  61. if (busybox_info.mode && busybox_info.mode != "carbon") {
  62. args.append({*busybox_info.mode, "--"});
  63. }
  64. args.append(argv + 1, argv + argc);
  65. Carbon::Driver driver(*fs, &install_paths, llvm::outs(), llvm::errs());
  66. bool success = driver.RunCommand(args).success;
  67. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  68. }
  69. } // namespace Carbon
  70. auto main(int argc, char** argv) -> int {
  71. auto result = Carbon::Main(argc, argv);
  72. if (result.ok()) {
  73. return *result;
  74. } else {
  75. llvm::errs() << "error: " << result.error() << "\n";
  76. return EXIT_FAILURE;
  77. }
  78. }