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