1 //===- llvm/Support/Program.h ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares the llvm::sys::Program class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_SUPPORT_PROGRAM_H
14 #define LLVM_SUPPORT_PROGRAM_H
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/FileSystem.h"
23 #include <system_error>
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 = ';';
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.
41 typedef ::pid_t procid_t;
42 typedef procid_t process_t;
45 /// This struct encapsulates information about a process.
47 enum : procid_t { InvalidPid = 0 };
49 procid_t Pid; /// The process identifier.
50 process_t Process; /// Platform-dependent process object.
52 /// The return code, set after execution.
58 /// This struct encapsulates information about a process execution.
59 struct ProcessStatistics {
60 std::chrono::microseconds TotalTime;
61 std::chrono::microseconds UserTime;
62 uint64_t PeakMemory = 0; ///< Maximum resident set size in KiB.
65 /// Find the first executable file \p Name in \p Paths.
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.
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.
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.
79 findProgramByName(StringRef Name, ArrayRef<StringRef> Paths = {});
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);
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();
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
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
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
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
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.
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);
156 /// Return true if the given arguments fit within system-specific
157 /// argument length limits.
158 bool commandLineFitsWithinSystemLimits(StringRef Program,
159 ArrayRef<StringRef> Args);
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);
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.
168 enum WindowsEncodingMethod {
169 /// UTF-8 is the LLVM native encoding, being the same as "do not perform
170 /// encoding conversion".
176 /// Saves the UTF8-encoded \p contents string into the file \p FileName
177 /// using a specific encoding.
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.
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.
195 /// \returns non-zero error_code if failed
197 writeFileWithEncoding(StringRef FileName, StringRef Contents,
198 WindowsEncodingMethod Encoding = WEM_UTF8);
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
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
219 Optional<ProcessStatistics> *ProcStat = nullptr ///< If non-zero, provides
220 /// a pointer to a structure in which process execution statistics will be
224 /// Print a command argument, and optionally quote it.
225 void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote);
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
232 ErrorOr<std::wstring> flattenWindowsCommandLine(ArrayRef<StringRef> Args);