LLVM  9.0.0svn
Program.cpp
Go to the documentation of this file.
1 //===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===//
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 //
9 // This file implements the operating system Program concept.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/Program.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Config/llvm-config.h"
16 #include <system_error>
17 using namespace llvm;
18 using namespace sys;
19 
20 //===----------------------------------------------------------------------===//
21 //=== WARNING: Implementation here must contain only TRULY operating system
22 //=== independent code.
23 //===----------------------------------------------------------------------===//
24 
25 static bool Execute(ProcessInfo &PI, StringRef Program,
27  ArrayRef<Optional<StringRef>> Redirects,
28  unsigned MemoryLimit, std::string *ErrMsg);
29 
32  ArrayRef<Optional<StringRef>> Redirects,
33  unsigned SecondsToWait, unsigned MemoryLimit,
34  std::string *ErrMsg, bool *ExecutionFailed) {
35  assert(Redirects.empty() || Redirects.size() == 3);
36  ProcessInfo PI;
37  if (Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg)) {
38  if (ExecutionFailed)
39  *ExecutionFailed = false;
40  ProcessInfo Result = Wait(
41  PI, SecondsToWait, /*WaitUntilTerminates=*/SecondsToWait == 0, ErrMsg);
42  return Result.ReturnCode;
43  }
44 
45  if (ExecutionFailed)
46  *ExecutionFailed = true;
47 
48  return -1;
49 }
50 
53  ArrayRef<Optional<StringRef>> Redirects,
54  unsigned MemoryLimit, std::string *ErrMsg,
55  bool *ExecutionFailed) {
56  assert(Redirects.empty() || Redirects.size() == 3);
57  ProcessInfo PI;
58  if (ExecutionFailed)
59  *ExecutionFailed = false;
60  if (!Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg))
61  if (ExecutionFailed)
62  *ExecutionFailed = true;
63 
64  return PI;
65 }
66 
69  SmallVector<StringRef, 8> StringRefArgs;
70  StringRefArgs.reserve(Args.size());
71  for (const char *A : Args)
72  StringRefArgs.emplace_back(A);
73  return commandLineFitsWithinSystemLimits(Program, StringRefArgs);
74 }
75 
76 // Include the platform-specific parts of this class.
77 #ifdef LLVM_ON_UNIX
78 #include "Unix/Program.inc"
79 #endif
80 #ifdef _WIN32
81 #include "Windows/Program.inc"
82 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
ProcessInfo ExecuteNoWait(StringRef Program, ArrayRef< StringRef > Args, Optional< ArrayRef< StringRef >> Env, ArrayRef< Optional< StringRef >> Redirects={}, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
Similar to ExecuteAndWait, but returns immediately.
Definition: Program.cpp:51
void reserve(size_type N)
Definition: SmallVector.h:368
int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, Optional< ArrayRef< StringRef >> Env=None, ArrayRef< Optional< StringRef >> Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
This function executes the program using the arguments provided.
Definition: Program.cpp:30
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
bool commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef< StringRef > Args)
Return true if the given arguments fit within system-specific argument length limits.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
int ReturnCode
Platform-dependent process object.
Definition: Program.h:50
void emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:644
This struct encapsulates information about a process.
Definition: Program.h:43
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static bool Execute(ProcessInfo &PI, StringRef Program, ArrayRef< StringRef > Args, Optional< ArrayRef< StringRef >> Env, ArrayRef< Optional< StringRef >> Redirects, unsigned MemoryLimit, std::string *ErrMsg)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
ProcessInfo Wait(const ProcessInfo &PI, unsigned SecondsToWait, bool WaitUntilTerminates, std::string *ErrMsg=nullptr)
This function waits for the process specified by PI to finish.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.