LLVM  14.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/BitVector.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/Support/ErrorOr.h"
23 #include <chrono>
24 #include <system_error>
25 
26 namespace llvm {
27 namespace 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 
55  ProcessInfo();
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  Optional<ArrayRef<StringRef>> Env = None, ///< An optional vector of
111  ///< strings to use for the program's environment. If not provided, the
112  ///< current program's environment will be used. If specified, the
113  ///< vector should **not** be terminated by an empty StringRef.
114  ArrayRef<Optional<StringRef>> Redirects = {}, ///<
115  ///< An array of optional paths. Should have a size of zero or three.
116  ///< If the array is empty, no redirections are performed.
117  ///< Otherwise, the inferior process's stdin(0), stdout(1), and stderr(2)
118  ///< will be redirected to the corresponding paths, if the optional path
119  ///< is present (not \c llvm::None).
120  ///< When an empty path is passed in, the corresponding file descriptor
121  ///< will be disconnected (ie, /dev/null'd) in a portable way.
122  unsigned SecondsToWait = 0, ///< If non-zero, this specifies the amount
123  ///< of time to wait for the child process to exit. If the time
124  ///< expires, the child is killed and this call returns. If zero,
125  ///< this function will wait until the child finishes or forever if
126  ///< it doesn't.
127  unsigned MemoryLimit = 0, ///< If non-zero, this specifies max. amount
128  ///< of memory can be allocated by process. If memory usage will be
129  ///< higher limit, the child is killed and this call returns. If zero
130  ///< - no memory limit.
131  std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a
132  ///< string instance in which error messages will be returned. If the
133  ///< string is non-empty upon return an error occurred while invoking the
134  ///< program.
135  bool *ExecutionFailed = nullptr,
136  Optional<ProcessStatistics> *ProcStat = nullptr, ///< If non-zero,
137  /// provides a pointer to a structure in which process execution
138  /// statistics will be stored.
139  BitVector *AffinityMask = nullptr ///< CPUs or processors the new
140  /// program shall run on.
141  );
142 
143  /// Similar to ExecuteAndWait, but returns immediately.
144  /// @returns The \see ProcessInfo of the newly launched process.
145  /// \note On Microsoft Windows systems, users will need to either call
146  /// \see Wait until the process finished execution or win32 CloseHandle() API
147  /// on ProcessInfo.ProcessHandle to avoid memory leaks.
148  ProcessInfo ExecuteNoWait(StringRef Program, ArrayRef<StringRef> Args,
149  Optional<ArrayRef<StringRef>> Env,
150  ArrayRef<Optional<StringRef>> Redirects = {},
151  unsigned MemoryLimit = 0,
152  std::string *ErrMsg = nullptr,
153  bool *ExecutionFailed = nullptr,
154  BitVector *AffinityMask = nullptr);
155 
156  /// Return true if the given arguments fit within system-specific
157  /// argument length limits.
158  bool commandLineFitsWithinSystemLimits(StringRef Program,
159  ArrayRef<StringRef> Args);
160 
161  /// Return true if the given arguments fit within system-specific
162  /// argument length limits.
163  bool commandLineFitsWithinSystemLimits(StringRef Program,
164  ArrayRef<const char *> Args);
165 
166  /// File encoding options when writing contents that a non-UTF8 tool will
167  /// read (on Windows systems). For UNIX, we always use UTF-8.
169  /// UTF-8 is the LLVM native encoding, being the same as "do not perform
170  /// encoding conversion".
174  };
175 
176  /// Saves the UTF8-encoded \p contents string into the file \p FileName
177  /// using a specific encoding.
178  ///
179  /// This write file function adds the possibility to choose which encoding
180  /// to use when writing a text file. On Windows, this is important when
181  /// writing files with internationalization support with an encoding that is
182  /// different from the one used in LLVM (UTF-8). We use this when writing
183  /// response files, since GCC tools on MinGW only understand legacy code
184  /// pages, and VisualStudio tools only understand UTF-16.
185  /// For UNIX, using different encodings is silently ignored, since all tools
186  /// work well with UTF-8.
187  /// This function assumes that you only use UTF-8 *text* data and will convert
188  /// it to your desired encoding before writing to the file.
189  ///
190  /// FIXME: We use EM_CurrentCodePage to write response files for GNU tools in
191  /// a MinGW/MinGW-w64 environment, which has serious flaws but currently is
192  /// our best shot to make gcc/ld understand international characters. This
193  /// should be changed as soon as binutils fix this to support UTF16 on mingw.
194  ///
195  /// \returns non-zero error_code if failed
196  std::error_code
197  writeFileWithEncoding(StringRef FileName, StringRef Contents,
198  WindowsEncodingMethod Encoding = WEM_UTF8);
199 
200  /// This function waits for the process specified by \p PI to finish.
201  /// \returns A \see ProcessInfo struct with Pid set to:
202  /// \li The process id of the child process if the child process has changed
203  /// state.
204  /// \li 0 if the child process has not changed state.
205  /// \note Users of this function should always check the ReturnCode member of
206  /// the \see ProcessInfo returned from this function.
208  const ProcessInfo &PI, ///< The child process that should be waited on.
209  unsigned SecondsToWait, ///< If non-zero, this specifies the amount of
210  ///< time to wait for the child process to exit. If the time expires, the
211  ///< child is killed and this function returns. If zero, this function
212  ///< will perform a non-blocking wait on the child process.
213  bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits
214  ///< until child has terminated.
215  std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a
216  ///< string instance in which error messages will be returned. If the
217  ///< string is non-empty upon return an error occurred while invoking the
218  ///< program.
219  Optional<ProcessStatistics> *ProcStat = nullptr ///< If non-zero, provides
220  /// a pointer to a structure in which process execution statistics will be
221  /// stored.
222  );
223 
224  /// Print a command argument, and optionally quote it.
225  void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote);
226 
227 #if defined(_WIN32)
228  /// Given a list of command line arguments, quote and escape them as necessary
229  /// to build a single flat command line appropriate for calling CreateProcess
230  /// on
231  /// Windows.
232  ErrorOr<std::wstring> flattenWindowsCommandLine(ArrayRef<StringRef> Args);
233 #endif
234  }
235 }
236 
237 #endif
llvm::sys::findProgramByName
ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths={})
Find the first executable file Name in Paths.
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::sys::ExecuteNoWait
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, BitVector *AffinityMask=nullptr)
Similar to ExecuteAndWait, but returns immediately.
Definition: Program.cpp:57
Optional.h
FileSystem.h
llvm::sys::ChangeStdoutMode
std::error_code ChangeStdoutMode(fs::OpenFlags Flags)
StringRef.h
llvm::sys::writeFileWithEncoding
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.
llvm::Optional
Definition: APInt.h:33
llvm::sys::WEM_UTF8
@ WEM_UTF8
UTF-8 is the LLVM native encoding, being the same as "do not perform encoding conversion".
Definition: Program.h:171
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::sys::EnvPathSeparator
const char EnvPathSeparator
This is the OS-specific separator for PATH like environment variables:
Definition: Program.h:32
llvm::sys::ProcessInfo::InvalidPid
@ InvalidPid
Definition: Program.h:47
llvm::sys::process_t
procid_t process_t
Definition: Program.h:42
llvm::sys::ProcessInfo::Process
process_t Process
The process identifier.
Definition: Program.h:50
llvm::sys::ProcessInfo::ProcessInfo
ProcessInfo()
llvm::sys::ChangeStdoutToBinary
std::error_code ChangeStdoutToBinary()
llvm::sys::ProcessStatistics::UserTime
std::chrono::microseconds UserTime
Definition: Program.h:61
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
BitVector.h
llvm::sys::ProcessStatistics
This struct encapsulates information about a process execution.
Definition: Program.h:59
llvm::sys::ChangeStdinToBinary
std::error_code ChangeStdinToBinary()
uint64_t
llvm::sys::ProcessInfo::ReturnCode
int ReturnCode
Platform-dependent process object.
Definition: Program.h:53
ErrorOr.h
llvm::sys::WindowsEncodingMethod
WindowsEncodingMethod
File encoding options when writing contents that a non-UTF8 tool will read (on Windows systems).
Definition: Program.h:168
ArrayRef.h
llvm::sys::WEM_CurrentCodePage
@ WEM_CurrentCodePage
Definition: Program.h:172
llvm::sys::procid_t
::pid_t procid_t
Definition: Program.h:41
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::cl::Optional
@ Optional
Definition: CommandLine.h:119
llvm::sys::ProcessInfo
This struct encapsulates information about a process.
Definition: Program.h:46
llvm::sys::WEM_UTF16
@ WEM_UTF16
Definition: Program.h:173
llvm::sys::ChangeStdinMode
std::error_code ChangeStdinMode(fs::OpenFlags Flags)
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::sys::fs::OpenFlags
OpenFlags
Definition: FileSystem.h:756
llvm::sys::ProcessStatistics::PeakMemory
uint64_t PeakMemory
Maximum resident set size in KiB.
Definition: Program.h:62
llvm::sys::ExecuteAndWait
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, BitVector *AffinityMask=nullptr)
This function executes the program using the arguments provided.
Definition: Program.cpp:32
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::sys::Wait
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.
llvm::sys::ProcessInfo::Pid
procid_t Pid
Definition: Program.h:49
llvm::sys::printArg
void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote)
Print a command argument, and optionally quote it.
Definition: Program.cpp:83
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::sys::commandLineFitsWithinSystemLimits
bool commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef< StringRef > Args)
Return true if the given arguments fit within system-specific argument length limits.
llvm::sys::ProcessStatistics::TotalTime
std::chrono::microseconds TotalTime
Definition: Program.h:60