LLVM  11.0.0git
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"
17 #include <system_error>
18 using namespace llvm;
19 using namespace sys;
20 
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //=== independent code.
24 //===----------------------------------------------------------------------===//
25 
26 static bool Execute(ProcessInfo &PI, StringRef Program,
28  ArrayRef<Optional<StringRef>> Redirects,
29  unsigned MemoryLimit, std::string *ErrMsg);
30 
33  ArrayRef<Optional<StringRef>> Redirects,
34  unsigned SecondsToWait, unsigned MemoryLimit,
35  std::string *ErrMsg, bool *ExecutionFailed,
36  Optional<ProcessStatistics> *ProcStat) {
37  assert(Redirects.empty() || Redirects.size() == 3);
38  ProcessInfo PI;
39  if (Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg)) {
40  if (ExecutionFailed)
41  *ExecutionFailed = false;
42  ProcessInfo Result =
43  Wait(PI, SecondsToWait, /*WaitUntilTerminates=*/SecondsToWait == 0,
44  ErrMsg, ProcStat);
45  return Result.ReturnCode;
46  }
47 
48  if (ExecutionFailed)
49  *ExecutionFailed = true;
50 
51  return -1;
52 }
53 
56  ArrayRef<Optional<StringRef>> Redirects,
57  unsigned MemoryLimit, std::string *ErrMsg,
58  bool *ExecutionFailed) {
59  assert(Redirects.empty() || Redirects.size() == 3);
60  ProcessInfo PI;
61  if (ExecutionFailed)
62  *ExecutionFailed = false;
63  if (!Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg))
64  if (ExecutionFailed)
65  *ExecutionFailed = true;
66 
67  return PI;
68 }
69 
72  SmallVector<StringRef, 8> StringRefArgs;
73  StringRefArgs.reserve(Args.size());
74  for (const char *A : Args)
75  StringRefArgs.emplace_back(A);
76  return commandLineFitsWithinSystemLimits(Program, StringRefArgs);
77 }
78 
79 void sys::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
80  const bool Escape = Arg.find_first_of(" \"\\$") != StringRef::npos;
81 
82  if (!Quote && !Escape) {
83  OS << Arg;
84  return;
85  }
86 
87  // Quote and escape. This isn't really complete, but good enough.
88  OS << '"';
89  for (const auto c : Arg) {
90  if (c == '"' || c == '\\' || c == '$')
91  OS << '\\';
92  OS << c;
93  }
94  OS << '"';
95 }
96 
97 // Include the platform-specific parts of this class.
98 #ifdef LLVM_ON_UNIX
99 #include "Unix/Program.inc"
100 #endif
101 #ifdef _WIN32
102 #include "Windows/Program.inc"
103 #endif
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:687
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:54
void reserve(size_type N)
Definition: SmallVector.h:415
static constexpr size_t npos
Definition: StringRef.h:59
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
ProcessInfo Wait(const ProcessInfo &PI, unsigned SecondsToWait, bool WaitUntilTerminates, std::string *ErrMsg=nullptr, Optional< ProcessStatistics > *ProcStat=nullptr)
This function waits for the process specified by PI to finish.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:156
bool commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef< StringRef > Args)
Return true if the given arguments fit within system-specific argument length limits.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
int ReturnCode
Platform-dependent process object.
Definition: Program.h:51
This struct encapsulates information about a process.
Definition: Program.h:44
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:413
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)
void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote)
Print a command argument, and optionally quote it.
Definition: Program.cpp:79
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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, Optional< ProcessStatistics > *ProcStat=nullptr)
This function executes the program using the arguments provided.
Definition: Program.cpp:31
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
constexpr char Args[]
Key for Kernel::Metadata::mArgs.