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"
19 #include "llvm/Support/ErrorOr.h"
20 #include "llvm/Support/FileSystem.h"
21 #include <chrono>
22 #include <optional>
23 #include <system_error>
24 
25 namespace llvm {
26 class BitVector;
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;
42   typedef procid_t process_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.
53     int ReturnCode;
54 
55     ProcessInfo();
56   };
57 
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.
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.
78   ErrorOr<std::string>
79   findProgramByName(StringRef Name, ArrayRef<StringRef> Paths = {});
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 ExecuteAndWait, but returns immediately.
145   /// @returns The \see ProcessInfo of the newly launched process.
146   /// \note On Microsoft Windows systems, users will need to either call
147   /// \see Wait until the process finished execution or win32 CloseHandle() API
148   /// on ProcessInfo.ProcessHandle to avoid memory leaks.
149   ProcessInfo ExecuteNoWait(StringRef Program, ArrayRef<StringRef> Args,
150                             std::optional<ArrayRef<StringRef>> Env,
151                             ArrayRef<std::optional<StringRef>> Redirects = {},
152                             unsigned MemoryLimit = 0,
153                             std::string *ErrMsg = nullptr,
154                             bool *ExecutionFailed = nullptr,
155                             BitVector *AffinityMask = nullptr);
156 
157   /// Return true if the given arguments fit within system-specific
158   /// argument length limits.
159   bool commandLineFitsWithinSystemLimits(StringRef Program,
160                                          ArrayRef<StringRef> Args);
161 
162   /// Return true if the given arguments fit within system-specific
163   /// argument length limits.
164   bool commandLineFitsWithinSystemLimits(StringRef Program,
165                                          ArrayRef<const char *> Args);
166 
167   /// File encoding options when writing contents that a non-UTF8 tool will
168   /// read (on Windows systems). For UNIX, we always use UTF-8.
169   enum WindowsEncodingMethod {
170     /// UTF-8 is the LLVM native encoding, being the same as "do not perform
171     /// encoding conversion".
172     WEM_UTF8,
173     WEM_CurrentCodePage,
174     WEM_UTF16
175   };
176 
177   /// Saves the UTF8-encoded \p contents string into the file \p FileName
178   /// using a specific encoding.
179   ///
180   /// This write file function adds the possibility to choose which encoding
181   /// to use when writing a text file. On Windows, this is important when
182   /// writing files with internationalization support with an encoding that is
183   /// different from the one used in LLVM (UTF-8). We use this when writing
184   /// response files, since GCC tools on MinGW only understand legacy code
185   /// pages, and VisualStudio tools only understand UTF-16.
186   /// For UNIX, using different encodings is silently ignored, since all tools
187   /// work well with UTF-8.
188   /// This function assumes that you only use UTF-8 *text* data and will convert
189   /// it to your desired encoding before writing to the file.
190   ///
191   /// FIXME: We use EM_CurrentCodePage to write response files for GNU tools in
192   /// a MinGW/MinGW-w64 environment, which has serious flaws but currently is
193   /// our best shot to make gcc/ld understand international characters. This
194   /// should be changed as soon as binutils fix this to support UTF16 on mingw.
195   ///
196   /// \returns non-zero error_code if failed
197   std::error_code
198   writeFileWithEncoding(StringRef FileName, StringRef Contents,
199                         WindowsEncodingMethod Encoding = WEM_UTF8);
200 
201   /// This function waits for the process specified by \p PI to finish.
202   /// \returns A \see ProcessInfo struct with Pid set to:
203   /// \li The process id of the child process if the child process has changed
204   /// state.
205   /// \li 0 if the child process has not changed state.
206   /// \note Users of this function should always check the ReturnCode member of
207   /// the \see ProcessInfo returned from this function.
208   ProcessInfo
209   Wait(const ProcessInfo &PI, ///< The child process that should be waited on.
210        std::optional<unsigned> SecondsToWait, ///< If std::nullopt, waits until
211        ///< child has terminated.
212        ///< If a value, this specifies the amount of time to wait for the child
213        ///< process. If the time expires, and \p Polling is false, the child is
214        ///< killed and this < function returns. If the time expires and \p
215        ///< Polling is true, the child is resumed.
216        ///<
217        ///< If zero, this function will perform a non-blocking
218        ///< wait on the child process.
219        std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a
220        ///< string instance in which error messages will be returned. If the
221        ///< string is non-empty upon return an error occurred while invoking the
222        ///< program.
223        std::optional<ProcessStatistics> *ProcStat =
224            nullptr, ///< If non-zero, provides
225        /// a pointer to a structure in which process execution statistics will
226        /// be stored.
227 
228        bool Polling = false ///< If true, do not kill the process on timeout.
229   );
230 
231   /// Print a command argument, and optionally quote it.
232   void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote);
233 
234 #if defined(_WIN32)
235   /// Given a list of command line arguments, quote and escape them as necessary
236   /// to build a single flat command line appropriate for calling CreateProcess
237   /// on
238   /// Windows.
239   ErrorOr<std::wstring> flattenWindowsCommandLine(ArrayRef<StringRef> Args);
240 #endif
241   }
242 }
243 
244 #endif
245