LLVM 22.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
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Config/llvm-config.h"
17using namespace llvm;
18using namespace sys;
19
20//===----------------------------------------------------------------------===//
21//=== WARNING: Implementation here must contain only TRULY operating system
22//=== independent code.
23//===----------------------------------------------------------------------===//
24
25static bool Execute(ProcessInfo &PI, StringRef Program,
27 std::optional<ArrayRef<StringRef>> Env,
28 ArrayRef<std::optional<StringRef>> Redirects,
29 unsigned MemoryLimit, std::string *ErrMsg,
30 BitVector *AffinityMask, bool DetachProcess);
31
33 std::optional<ArrayRef<StringRef>> Env,
34 ArrayRef<std::optional<StringRef>> Redirects,
35 unsigned SecondsToWait, unsigned MemoryLimit,
36 std::string *ErrMsg, bool *ExecutionFailed,
37 std::optional<ProcessStatistics> *ProcStat,
38 BitVector *AffinityMask) {
39 assert(Redirects.empty() || Redirects.size() == 3);
40 ProcessInfo PI;
41 if (Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg,
42 AffinityMask, /*DetachProcess=*/false)) {
43 if (ExecutionFailed)
44 *ExecutionFailed = false;
45 ProcessInfo Result = Wait(
46 PI, SecondsToWait == 0 ? std::nullopt : std::optional(SecondsToWait),
47 ErrMsg, ProcStat);
48 return Result.ReturnCode;
49 }
50
51 if (ExecutionFailed)
52 *ExecutionFailed = true;
53
54 return -1;
55}
56
58 std::optional<ArrayRef<StringRef>> Env,
59 ArrayRef<std::optional<StringRef>> Redirects,
60 unsigned MemoryLimit, std::string *ErrMsg,
61 bool *ExecutionFailed, BitVector *AffinityMask,
62 bool DetachProcess) {
63 assert(Redirects.empty() || Redirects.size() == 3);
64 ProcessInfo PI;
65 if (ExecutionFailed)
66 *ExecutionFailed = false;
67 if (!Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg,
68 AffinityMask, DetachProcess))
69 if (ExecutionFailed)
70 *ExecutionFailed = true;
71
72 return PI;
73}
74
77 SmallVector<StringRef, 8> StringRefArgs(Args);
78 return commandLineFitsWithinSystemLimits(Program, StringRefArgs);
79}
80
81void sys::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
82 const bool Escape = Arg.find_first_of(" \"\\$") != StringRef::npos;
83
84 if (!Quote && !Escape) {
85 OS << Arg;
86 return;
87 }
88
89 // Quote and escape. This isn't really complete, but good enough.
90 OS << '"';
91 for (const auto c : Arg) {
92 if (c == '"' || c == '\\' || c == '$')
93 OS << '\\';
94 OS << c;
95 }
96 OS << '"';
97}
98
99// Include the platform-specific parts of this class.
100#ifdef LLVM_ON_UNIX
101#include "Unix/Program.inc"
102#endif
103#ifdef _WIN32
104#include "Windows/Program.inc"
105#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool Execute(ProcessInfo &PI, StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env, ArrayRef< std::optional< StringRef > > Redirects, unsigned MemoryLimit, std::string *ErrMsg, BitVector *AffinityMask, bool DetachProcess)
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
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:384
static constexpr size_t npos
Definition: StringRef.h:57
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
LLVM_ABI void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote)
Print a command argument, and optionally quote it.
Definition: Program.cpp:81
LLVM_ABI bool commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef< StringRef > Args)
Return true if the given arguments fit within system-specific argument length limits.
LLVM_ABI ProcessInfo ExecuteNoWait(StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env, ArrayRef< std::optional< StringRef > > Redirects={}, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, BitVector *AffinityMask=nullptr, bool DetachProcess=false)
Similar to ExecuteAndWait, but returns immediately.
Definition: Program.cpp:57
LLVM_ABI int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env=std::nullopt, ArrayRef< std::optional< StringRef > > Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, std::optional< ProcessStatistics > *ProcStat=nullptr, BitVector *AffinityMask=nullptr)
This function executes the program using the arguments provided.
Definition: Program.cpp:32
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Wait
Definition: Threading.h:60
This struct encapsulates information about a process.
Definition: Program.h:47