driver_main.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 <cstdlib>
  5. #include "common/bazel_working_dir.h"
  6. #include "common/init_llvm.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/Support/Path.h"
  10. #include "llvm/Support/Program.h"
  11. #include "toolchain/driver/driver.h"
  12. auto main(int argc, char** argv) -> int {
  13. Carbon::InitLLVM init_llvm(argc, argv);
  14. if (argc < 1) {
  15. return EXIT_FAILURE;
  16. }
  17. // Find the executable without resolving symlinks. Do a PATH lookup if argv[0]
  18. // isn't a valid path.
  19. llvm::SmallString<128> exe_path(argv[0]);
  20. if (!llvm::sys::fs::exists(exe_path)) {
  21. if (llvm::ErrorOr<std::string> path =
  22. llvm::sys::findProgramByName(exe_path)) {
  23. exe_path = *path;
  24. }
  25. }
  26. Carbon::SetWorkingDirForBazel();
  27. // Printing to stderr should flush stdout. This is most noticeable when stderr
  28. // is piped to stdout.
  29. llvm::errs().tie(&llvm::outs());
  30. llvm::SmallVector<llvm::StringRef> args(argv + 1, argv + argc);
  31. auto fs = llvm::vfs::getRealFileSystem();
  32. // Construct the data directory relative to the executable location.
  33. llvm::SmallString<256> data_dir(llvm::sys::path::parent_path(exe_path));
  34. llvm::sys::path::append(data_dir, llvm::sys::path::Style::posix,
  35. "carbon.runfiles/_main/");
  36. Carbon::Driver driver(*fs, data_dir, llvm::outs(), llvm::errs());
  37. bool success = driver.RunCommand(args).success;
  38. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  39. }