LLVM 20.0.0git
Program.h
Go to the documentation of this file.
1//===- llvm/Support/Program.h ------------------------------------*- 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 declares the llvm::sys::Program class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_PROGRAM_H
14#define LLVM_SUPPORT_PROGRAM_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Config/llvm-config.h"
21#include <chrono>
22#include <optional>
23#include <system_error>
24
25namespace llvm {
26class BitVector;
27namespace sys {
28
29 /// This is the OS-specific separator for PATH like environment variables:
30 // a colon on Unix or a semicolon on Windows.
31#if defined(LLVM_ON_UNIX)
32 const char EnvPathSeparator = ':';
33#elif defined (_WIN32)
34 const char EnvPathSeparator = ';';
35#endif
36
37#if defined(_WIN32)
38 typedef unsigned long procid_t; // Must match the type of DWORD on Windows.
39 typedef void *process_t; // Must match the type of HANDLE on Windows.
40#else
41 typedef ::pid_t procid_t;
43#endif
44
45 /// This struct encapsulates information about a process.
46 struct ProcessInfo {
47 enum : procid_t { InvalidPid = 0 };
48
49 procid_t Pid; /// The process identifier.
50 process_t Process; /// Platform-dependent process object.
51
52 /// The return code, set after execution.
54
56 };
57
58 /// This struct encapsulates information about a process execution.
60 std::chrono::microseconds TotalTime;
61 std::chrono::microseconds UserTime;
62 uint64_t PeakMemory = 0; ///< Maximum resident set size in KiB.
63 };
64
65 /// Find the first executable file \p Name in \p Paths.
66 ///
67 /// This does not perform hashing as a shell would but instead stats each PATH
68 /// entry individually so should generally be avoided. Core LLVM library
69 /// functions and options should instead require fully specified paths.
70 ///
71 /// \param Name name of the executable to find. If it contains any system
72 /// slashes, it will be returned as is.
73 /// \param Paths optional list of paths to search for \p Name. If empty it
74 /// will use the system PATH environment instead.
75 ///
76 /// \returns The fully qualified path to the first \p Name in \p Paths if it
77 /// exists. \p Name if \p Name has slashes in it. Otherwise an error.
80
81 // These functions change the specified standard stream (stdin or stdout) mode
82 // based on the Flags. They return errc::success if the specified stream was
83 // changed. Otherwise, a platform dependent error is returned.
84 std::error_code ChangeStdinMode(fs::OpenFlags Flags);
85 std::error_code ChangeStdoutMode(fs::OpenFlags Flags);
86
87 // These functions change the specified standard stream (stdin or stdout) to
88 // binary mode. They return errc::success if the specified stream
89 // was changed. Otherwise a platform dependent error is returned.
90 std::error_code ChangeStdinToBinary();
91 std::error_code ChangeStdoutToBinary();
92
93 /// This function executes the program using the arguments provided. The
94 /// invoked program will inherit the stdin, stdout, and stderr file
95 /// descriptors, the environment and other configuration settings of the
96 /// invoking program.
97 /// This function waits for the program to finish, so should be avoided in
98 /// library functions that aren't expected to block. Consider using
99 /// ExecuteNoWait() instead.
100 /// \returns an integer result code indicating the status of the program.
101 /// A zero or positive value indicates the result code of the program.
102 /// -1 indicates failure to execute
103 /// -2 indicates a crash during execution or timeout
104 int ExecuteAndWait(
105 StringRef Program, ///< Path of the program to be executed. It is
106 ///< presumed this is the result of the findProgramByName method.
107 ArrayRef<StringRef> Args, ///< An array of strings that are passed to the
108 ///< program. The first element should be the name of the program.
109 ///< The array should **not** be terminated by an empty StringRef.
110 std::optional<ArrayRef<StringRef>> Env =
111 std::nullopt, ///< An optional vector of
112 ///< strings to use for the program's environment. If not provided, the
113 ///< current program's environment will be used. If specified, the
114 ///< vector should **not** be terminated by an empty StringRef.
115 ArrayRef<std::optional<StringRef>> Redirects = {}, ///<
116 ///< An array of optional paths. Should have a size of zero or three.
117 ///< If the array is empty, no redirections are performed.
118 ///< Otherwise, the inferior process's stdin(0), stdout(1), and stderr(2)
119 ///< will be redirected to the corresponding paths, if the optional path
120 ///< is present (not \c std::nullopt).
121 ///< When an empty path is passed in, the corresponding file descriptor
122 ///< will be disconnected (ie, /dev/null'd) in a portable way.
123 unsigned SecondsToWait = 0, ///< If non-zero, this specifies the amount
124 ///< of time to wait for the child process to exit. If the time
125 ///< expires, the child is killed and this call returns. If zero,
126 ///< this function will wait until the child finishes or forever if
127 ///< it doesn't.
128 unsigned MemoryLimit = 0, ///< If non-zero, this specifies max. amount
129 ///< of memory can be allocated by process. If memory usage will be
130 ///< higher limit, the child is killed and this call returns. If zero
131 ///< - no memory limit.
132 std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a
133 ///< string instance in which error messages will be returned. If the
134 ///< string is non-empty upon return an error occurred while invoking the
135 ///< program.
136 bool *ExecutionFailed = nullptr,
137 std::optional<ProcessStatistics> *ProcStat = nullptr, ///< If non-zero,
138 /// provides a pointer to a structure in which process execution
139 /// statistics will be stored.
140 BitVector *AffinityMask = nullptr ///< CPUs or processors the new
141 /// program shall run on.
142 );
143
144 /// Similar to \ref ExecuteAndWait, but returns immediately.
145 /// \returns The \ref ProcessInfo of the newly launched process.
146 /// \note On Microsoft Windows systems, users will need to either call
147 /// \ref Wait until the process has finished executing or win32's CloseHandle
148 /// API on ProcessInfo.ProcessHandle to avoid memory leaks.
150 StringRef Program, ArrayRef<StringRef> Args,
151 std::optional<ArrayRef<StringRef>> Env,
152 ArrayRef<std::optional<StringRef>> Redirects = {},
153 unsigned MemoryLimit = 0, std::string *ErrMsg = nullptr,
154 bool *ExecutionFailed = nullptr, BitVector *AffinityMask = nullptr,
155 /// If true the executed program detatches from the controlling
156 /// terminal. I/O streams such as llvm::outs, llvm::errs, and stdin will
157 /// be closed until redirected to another output location
158 bool DetachProcess = false);
159
160 /// Return true if the given arguments fit within system-specific
161 /// argument length limits.
164
165 /// Return true if the given arguments fit within system-specific
166 /// argument length limits.
169
170 /// File encoding options when writing contents that a non-UTF8 tool will
171 /// read (on Windows systems). For UNIX, we always use UTF-8.
173 /// UTF-8 is the LLVM native encoding, being the same as "do not perform
174 /// encoding conversion".
178 };
179
180 /// Saves the UTF8-encoded \p contents string into the file \p FileName
181 /// using a specific encoding.
182 ///
183 /// This write file function adds the possibility to choose which encoding
184 /// to use when writing a text file. On Windows, this is important when
185 /// writing files with internationalization support with an encoding that is
186 /// different from the one used in LLVM (UTF-8). We use this when writing
187 /// response files, since GCC tools on MinGW only understand legacy code
188 /// pages, and VisualStudio tools only understand UTF-16.
189 /// For UNIX, using different encodings is silently ignored, since all tools
190 /// work well with UTF-8.
191 /// This function assumes that you only use UTF-8 *text* data and will convert
192 /// it to your desired encoding before writing to the file.
193 ///
194 /// FIXME: We use EM_CurrentCodePage to write response files for GNU tools in
195 /// a MinGW/MinGW-w64 environment, which has serious flaws but currently is
196 /// our best shot to make gcc/ld understand international characters. This
197 /// should be changed as soon as binutils fix this to support UTF16 on mingw.
198 ///
199 /// \returns non-zero error_code if failed
200 std::error_code
203
204 /// This function waits for the process specified by \p PI to finish.
205 /// \returns A \see ProcessInfo struct with Pid set to:
206 /// \li The process id of the child process if the child process has changed
207 /// state.
208 /// \li 0 if the child process has not changed state.
209 /// \note Users of this function should always check the ReturnCode member of
210 /// the \see ProcessInfo returned from this function.
212 Wait(const ProcessInfo &PI, ///< The child process that should be waited on.
213 std::optional<unsigned> SecondsToWait, ///< If std::nullopt, waits until
214 ///< child has terminated.
215 ///< If a value, this specifies the amount of time to wait for the child
216 ///< process. If the time expires, and \p Polling is false, the child is
217 ///< killed and this < function returns. If the time expires and \p
218 ///< Polling is true, the child is resumed.
219 ///<
220 ///< If zero, this function will perform a non-blocking
221 ///< wait on the child process.
222 std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a
223 ///< string instance in which error messages will be returned. If the
224 ///< string is non-empty upon return an error occurred while invoking the
225 ///< program.
226 std::optional<ProcessStatistics> *ProcStat =
227 nullptr, ///< If non-zero, provides
228 /// a pointer to a structure in which process execution statistics will
229 /// be stored.
230
231 bool Polling = false ///< If true, do not kill the process on timeout.
232 );
233
234 /// Print a command argument, and optionally quote it.
235 void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote);
236
237#if defined(_WIN32)
238 /// Given a list of command line arguments, quote and escape them as necessary
239 /// to build a single flat command line appropriate for calling CreateProcess
240 /// on
241 /// Windows.
242 ErrorOr<std::wstring> flattenWindowsCommandLine(ArrayRef<StringRef> Args);
243#endif
244 }
245}
246
247#endif
std::string Name
Provides ErrorOr<T> smart pointer.
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Represents either an error or a value T.
Definition: ErrorOr.h:56
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
::pid_t procid_t
Definition: Program.h:41
procid_t process_t
Definition: Program.h:42
std::error_code ChangeStdinMode(fs::OpenFlags Flags)
void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote)
Print a command argument, and optionally quote it.
Definition: Program.cpp:84
std::error_code ChangeStdinToBinary()
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
const char EnvPathSeparator
This is the OS-specific separator for PATH like environment variables:
Definition: Program.h:32
bool commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef< StringRef > Args)
Return true if the given arguments fit within system-specific argument length limits.
std::error_code ChangeStdoutMode(fs::OpenFlags Flags)
WindowsEncodingMethod
File encoding options when writing contents that a non-UTF8 tool will read (on Windows systems).
Definition: Program.h:172
@ WEM_CurrentCodePage
Definition: Program.h:176
@ WEM_UTF16
Definition: Program.h:177
@ WEM_UTF8
UTF-8 is the LLVM native encoding, being the same as "do not perform encoding conversion".
Definition: Program.h:175
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
std::error_code ChangeStdoutToBinary()
std::error_code writeFileWithEncoding(StringRef FileName, StringRef Contents, WindowsEncodingMethod Encoding=WEM_UTF8)
Saves the UTF8-encoded contents string into the file FileName using a specific encoding.
ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths={})
Find the first executable file Name in Paths.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Wait
Definition: Threading.h:61
This struct encapsulates information about a process.
Definition: Program.h:46
process_t Process
The process identifier.
Definition: Program.h:50
int ReturnCode
Platform-dependent process object.
Definition: Program.h:53
This struct encapsulates information about a process execution.
Definition: Program.h:59
uint64_t PeakMemory
Maximum resident set size in KiB.
Definition: Program.h:62
std::chrono::microseconds TotalTime
Definition: Program.h:60
std::chrono::microseconds UserTime
Definition: Program.h:61