Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Support/Program.h ------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the llvm::sys::Program class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_SUPPORT_PROGRAM_H |
| 15 | #define LLVM_SUPPORT_PROGRAM_H |
| 16 | |
| 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/Optional.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 20 | #include "llvm/Config/llvm-config.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 21 | #include "llvm/Support/ErrorOr.h" |
| 22 | #include <system_error> |
| 23 | |
| 24 | namespace llvm { |
| 25 | namespace sys { |
| 26 | |
| 27 | /// This is the OS-specific separator for PATH like environment variables: |
| 28 | // a colon on Unix or a semicolon on Windows. |
| 29 | #if defined(LLVM_ON_UNIX) |
| 30 | const char EnvPathSeparator = ':'; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 31 | #elif defined (_WIN32) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 32 | const char EnvPathSeparator = ';'; |
| 33 | #endif |
| 34 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 35 | #if defined(_WIN32) |
| 36 | typedef unsigned long procid_t; // Must match the type of DWORD on Windows. |
| 37 | typedef void *process_t; // Must match the type of HANDLE on Windows. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 38 | #else |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 39 | typedef pid_t procid_t; |
| 40 | typedef procid_t process_t; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | #endif |
| 42 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 43 | /// This struct encapsulates information about a process. |
| 44 | struct ProcessInfo { |
| 45 | enum : procid_t { InvalidPid = 0 }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 47 | procid_t Pid; /// The process identifier. |
| 48 | process_t Process; /// Platform-dependent process object. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 50 | /// The return code, set after execution. |
| 51 | int ReturnCode; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 53 | ProcessInfo(); |
| 54 | }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 55 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 56 | /// Find the first executable file \p Name in \p Paths. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 57 | /// |
| 58 | /// This does not perform hashing as a shell would but instead stats each PATH |
| 59 | /// entry individually so should generally be avoided. Core LLVM library |
| 60 | /// functions and options should instead require fully specified paths. |
| 61 | /// |
| 62 | /// \param Name name of the executable to find. If it contains any system |
| 63 | /// slashes, it will be returned as is. |
| 64 | /// \param Paths optional list of paths to search for \p Name. If empty it |
| 65 | /// will use the system PATH environment instead. |
| 66 | /// |
| 67 | /// \returns The fully qualified path to the first \p Name in \p Paths if it |
| 68 | /// exists. \p Name if \p Name has slashes in it. Otherwise an error. |
| 69 | ErrorOr<std::string> |
| 70 | findProgramByName(StringRef Name, ArrayRef<StringRef> Paths = {}); |
| 71 | |
| 72 | // These functions change the specified standard stream (stdin or stdout) to |
| 73 | // binary mode. They return errc::success if the specified stream |
| 74 | // was changed. Otherwise a platform dependent error is returned. |
| 75 | std::error_code ChangeStdinToBinary(); |
| 76 | std::error_code ChangeStdoutToBinary(); |
| 77 | |
| 78 | /// This function executes the program using the arguments provided. The |
| 79 | /// invoked program will inherit the stdin, stdout, and stderr file |
| 80 | /// descriptors, the environment and other configuration settings of the |
| 81 | /// invoking program. |
| 82 | /// This function waits for the program to finish, so should be avoided in |
| 83 | /// library functions that aren't expected to block. Consider using |
| 84 | /// ExecuteNoWait() instead. |
| 85 | /// \returns an integer result code indicating the status of the program. |
| 86 | /// A zero or positive value indicates the result code of the program. |
| 87 | /// -1 indicates failure to execute |
| 88 | /// -2 indicates a crash during execution or timeout |
| 89 | int ExecuteAndWait( |
| 90 | StringRef Program, ///< Path of the program to be executed. It is |
| 91 | ///< presumed this is the result of the findProgramByName method. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 92 | ArrayRef<StringRef> Args, ///< An array of strings that are passed to the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 93 | ///< program. The first element should be the name of the program. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 94 | ///< The array should **not** be terminated by an empty StringRef. |
| 95 | Optional<ArrayRef<StringRef>> Env = None, ///< An optional vector of |
| 96 | ///< strings to use for the program's environment. If not provided, the |
| 97 | ///< current program's environment will be used. If specified, the |
| 98 | ///< vector should **not** be terminated by an empty StringRef. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 99 | ArrayRef<Optional<StringRef>> Redirects = {}, ///< |
| 100 | ///< An array of optional paths. Should have a size of zero or three. |
| 101 | ///< If the array is empty, no redirections are performed. |
| 102 | ///< Otherwise, the inferior process's stdin(0), stdout(1), and stderr(2) |
| 103 | ///< will be redirected to the corresponding paths, if the optional path |
| 104 | ///< is present (not \c llvm::None). |
| 105 | ///< When an empty path is passed in, the corresponding file descriptor |
| 106 | ///< will be disconnected (ie, /dev/null'd) in a portable way. |
| 107 | unsigned SecondsToWait = 0, ///< If non-zero, this specifies the amount |
| 108 | ///< of time to wait for the child process to exit. If the time |
| 109 | ///< expires, the child is killed and this call returns. If zero, |
| 110 | ///< this function will wait until the child finishes or forever if |
| 111 | ///< it doesn't. |
| 112 | unsigned MemoryLimit = 0, ///< If non-zero, this specifies max. amount |
| 113 | ///< of memory can be allocated by process. If memory usage will be |
| 114 | ///< higher limit, the child is killed and this call returns. If zero |
| 115 | ///< - no memory limit. |
| 116 | std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a |
| 117 | ///< string instance in which error messages will be returned. If the |
| 118 | ///< string is non-empty upon return an error occurred while invoking the |
| 119 | ///< program. |
| 120 | bool *ExecutionFailed = nullptr); |
| 121 | |
| 122 | /// Similar to ExecuteAndWait, but returns immediately. |
| 123 | /// @returns The \see ProcessInfo of the newly launced process. |
| 124 | /// \note On Microsoft Windows systems, users will need to either call |
| 125 | /// \see Wait until the process finished execution or win32 CloseHandle() API |
| 126 | /// on ProcessInfo.ProcessHandle to avoid memory leaks. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 127 | ProcessInfo ExecuteNoWait(StringRef Program, ArrayRef<StringRef> Args, |
| 128 | Optional<ArrayRef<StringRef>> Env, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 129 | ArrayRef<Optional<StringRef>> Redirects = {}, |
| 130 | unsigned MemoryLimit = 0, |
| 131 | std::string *ErrMsg = nullptr, |
| 132 | bool *ExecutionFailed = nullptr); |
| 133 | |
| 134 | /// Return true if the given arguments fit within system-specific |
| 135 | /// argument length limits. |
| 136 | bool commandLineFitsWithinSystemLimits(StringRef Program, |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 137 | ArrayRef<StringRef> Args); |
| 138 | |
| 139 | /// Return true if the given arguments fit within system-specific |
| 140 | /// argument length limits. |
| 141 | bool commandLineFitsWithinSystemLimits(StringRef Program, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 142 | ArrayRef<const char *> Args); |
| 143 | |
| 144 | /// File encoding options when writing contents that a non-UTF8 tool will |
| 145 | /// read (on Windows systems). For UNIX, we always use UTF-8. |
| 146 | enum WindowsEncodingMethod { |
| 147 | /// UTF-8 is the LLVM native encoding, being the same as "do not perform |
| 148 | /// encoding conversion". |
| 149 | WEM_UTF8, |
| 150 | WEM_CurrentCodePage, |
| 151 | WEM_UTF16 |
| 152 | }; |
| 153 | |
| 154 | /// Saves the UTF8-encoded \p contents string into the file \p FileName |
| 155 | /// using a specific encoding. |
| 156 | /// |
| 157 | /// This write file function adds the possibility to choose which encoding |
| 158 | /// to use when writing a text file. On Windows, this is important when |
| 159 | /// writing files with internationalization support with an encoding that is |
| 160 | /// different from the one used in LLVM (UTF-8). We use this when writing |
| 161 | /// response files, since GCC tools on MinGW only understand legacy code |
| 162 | /// pages, and VisualStudio tools only understand UTF-16. |
| 163 | /// For UNIX, using different encodings is silently ignored, since all tools |
| 164 | /// work well with UTF-8. |
| 165 | /// This function assumes that you only use UTF-8 *text* data and will convert |
| 166 | /// it to your desired encoding before writing to the file. |
| 167 | /// |
| 168 | /// FIXME: We use EM_CurrentCodePage to write response files for GNU tools in |
| 169 | /// a MinGW/MinGW-w64 environment, which has serious flaws but currently is |
| 170 | /// our best shot to make gcc/ld understand international characters. This |
| 171 | /// should be changed as soon as binutils fix this to support UTF16 on mingw. |
| 172 | /// |
| 173 | /// \returns non-zero error_code if failed |
| 174 | std::error_code |
| 175 | writeFileWithEncoding(StringRef FileName, StringRef Contents, |
| 176 | WindowsEncodingMethod Encoding = WEM_UTF8); |
| 177 | |
| 178 | /// This function waits for the process specified by \p PI to finish. |
| 179 | /// \returns A \see ProcessInfo struct with Pid set to: |
| 180 | /// \li The process id of the child process if the child process has changed |
| 181 | /// state. |
| 182 | /// \li 0 if the child process has not changed state. |
| 183 | /// \note Users of this function should always check the ReturnCode member of |
| 184 | /// the \see ProcessInfo returned from this function. |
| 185 | ProcessInfo Wait( |
| 186 | const ProcessInfo &PI, ///< The child process that should be waited on. |
| 187 | unsigned SecondsToWait, ///< If non-zero, this specifies the amount of |
| 188 | ///< time to wait for the child process to exit. If the time expires, the |
| 189 | ///< child is killed and this function returns. If zero, this function |
| 190 | ///< will perform a non-blocking wait on the child process. |
| 191 | bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits |
| 192 | ///< until child has terminated. |
| 193 | std::string *ErrMsg = nullptr ///< If non-zero, provides a pointer to a |
| 194 | ///< string instance in which error messages will be returned. If the |
| 195 | ///< string is non-empty upon return an error occurred while invoking the |
| 196 | ///< program. |
| 197 | ); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 198 | |
| 199 | #if defined(_WIN32) |
| 200 | /// Given a list of command line arguments, quote and escape them as necessary |
| 201 | /// to build a single flat command line appropriate for calling CreateProcess |
| 202 | /// on |
| 203 | /// Windows. |
| 204 | std::string flattenWindowsCommandLine(ArrayRef<StringRef> Args); |
| 205 | #endif |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | #endif |