TargetExecutionUtils.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //===--- TargetExecutionUtils.cpp - Execution utils for target processes --===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
  9. #include <vector>
  10. namespace llvm {
  11. namespace orc {
  12. int runAsMain(int (*Main)(int, char *[]), ArrayRef<std::string> Args,
  13. Optional<StringRef> ProgramName) {
  14. std::vector<std::unique_ptr<char[]>> ArgVStorage;
  15. std::vector<char *> ArgV;
  16. ArgVStorage.reserve(Args.size() + (ProgramName ? 1 : 0));
  17. ArgV.reserve(Args.size() + 1 + (ProgramName ? 1 : 0));
  18. if (ProgramName) {
  19. ArgVStorage.push_back(std::make_unique<char[]>(ProgramName->size() + 1));
  20. llvm::copy(*ProgramName, &ArgVStorage.back()[0]);
  21. ArgVStorage.back()[ProgramName->size()] = '\0';
  22. ArgV.push_back(ArgVStorage.back().get());
  23. }
  24. for (const auto &Arg : Args) {
  25. ArgVStorage.push_back(std::make_unique<char[]>(Arg.size() + 1));
  26. llvm::copy(Arg, &ArgVStorage.back()[0]);
  27. ArgVStorage.back()[Arg.size()] = '\0';
  28. ArgV.push_back(ArgVStorage.back().get());
  29. }
  30. ArgV.push_back(nullptr);
  31. return Main(Args.size() + !!ProgramName, ArgV.data());
  32. }
  33. } // End namespace orc.
  34. } // End namespace llvm.