busybox_main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/init_llvm.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/Support/LLVMDriver.h"
  14. #include "toolchain/base/install_paths.h"
  15. #include "toolchain/driver/driver.h"
  16. #include "toolchain/install/busybox_info.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, GetBusyboxInfo(argv[0]));
  24. std::filesystem::path exe_path = busybox_info.bin_path.string();
  25. exe_path = SetWorkingDirForBazelRun(exe_path);
  26. const auto install_paths = InstallPaths::MakeExeRelative(exe_path.native());
  27. if (install_paths.error()) {
  28. return Error(*install_paths.error());
  29. }
  30. // If `LLVM_SYMBOLIZER_PATH` is unset, sets it. Signals.cpp would do some more
  31. // path resolution which this overrides in favor of using the busybox itself
  32. // for symbolization.
  33. setenv(
  34. "LLVM_SYMBOLIZER_PATH",
  35. (install_paths.llvm_install_bin().native() + "llvm-symbolizer").c_str(),
  36. /*overwrite=*/0);
  37. auto fs = llvm::vfs::getRealFileSystem();
  38. llvm::SmallVector<llvm::StringRef> raw_args;
  39. raw_args.append(argv + 1, argv + argc);
  40. llvm::SmallVector<llvm::StringRef> args;
  41. args.reserve(argc + 1);
  42. if (busybox_info.mode) {
  43. // Map busybox modes to the relevant subcommands with any flags needed to
  44. // emulate the requested command. Typically, our busyboxed binaries redirect
  45. // to a specific subcommand with some flags set and then pass the remaining
  46. // busybox arguments as positional arguments to that subcommand.
  47. //
  48. // TODO: Add relevant flags to the `clang` subcommand and add `clang`-based
  49. // symlinks to this like `clang++`.
  50. auto subcommand_args =
  51. llvm::StringSwitch<llvm::SmallVector<llvm::StringRef>>(
  52. *busybox_info.mode)
  53. // The `clang` program name used configures the default for its
  54. // `--driver-mode` flag. The first of these is redundant with the
  55. // default, but we group it here for clarity.
  56. .Case("clang", {"clang", "--"})
  57. .Case("clang++", {"clang", "--", "--driver-mode=g++"})
  58. .Case("clang-cl", {"clang", "--", "--driver-mode=cl"})
  59. .Case("clang-cpp", {"clang", "--", "--driver-mode=cpp"})
  60. // LLD has platform-specific program names that we translate into
  61. // platform flags.
  62. .Case("ld.lld", {"lld", "--platform=gnu", "--"})
  63. .Case("ld64.lld", {"lld", "--platform=darwin", "--"})
  64. // We also support a number of LLVM tools with a trivial translation
  65. // to subcommands. If any of these end up needing more advanced
  66. // translation, that can be factored into the `.def` file to provide custom
  67. // expansion here.
  68. #define CARBON_LLVM_TOOL(Id, Name, BinName, MainFn) \
  69. .Case(BinName, {"llvm", Name, "--"})
  70. #include "toolchain/base/llvm_tools.def"
  71. .Default({*busybox_info.mode, "--"});
  72. // When we're operating as a busybox, we also support a special command line
  73. // syntax for passing flags to the base Carbon driver as
  74. // `-Xcarbon=--some-carbon-flag=some-value`. Extract any arguments of that
  75. // form, remove the prefix, and prepend them to the arg list prior to the
  76. // busybox subcommand arguments.
  77. llvm::erase_if(raw_args, [&args](llvm::StringRef raw_arg) {
  78. if (raw_arg.consume_front("-Xcarbon=")) {
  79. args.push_back(raw_arg);
  80. return true;
  81. }
  82. return false;
  83. });
  84. // And now append the subcommand args.
  85. args.append(subcommand_args);
  86. }
  87. args.append(raw_args);
  88. Driver driver(fs, &install_paths, stdin, &llvm::outs(), &llvm::errs(),
  89. /*fuzzing=*/false, /*enable_leaking=*/true);
  90. bool success = driver.RunCommand(args).success;
  91. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  92. }
  93. } // namespace Carbon
  94. auto main(int argc, char** argv) -> int {
  95. auto result = Carbon::Main(argc, argv);
  96. if (result.ok()) {
  97. return *result;
  98. } else {
  99. llvm::errs() << "error: " << result.error() << "\n";
  100. return EXIT_FAILURE;
  101. }
  102. }