1 //===- llvm/Support/Program.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the llvm::sys::Program class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_PROGRAM_H
15 #define LLVM_SUPPORT_PROGRAM_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include <system_error>
26 /// This is the OS-specific separator for PATH like environment variables:
27 // a colon on Unix or a semicolon on Windows.
28 #if defined(LLVM_ON_UNIX)
29 const char EnvPathSeparator = ':';
30 #elif defined (LLVM_ON_WIN32)
31 const char EnvPathSeparator = ';';
34 /// @brief This struct encapsulates information about a process.
36 #if defined(LLVM_ON_UNIX)
37 typedef pid_t ProcessId;
38 #elif defined(LLVM_ON_WIN32)
39 typedef unsigned long ProcessId; // Must match the type of DWORD on Windows.
40 typedef void * HANDLE; // Must match the type of HANDLE on Windows.
41 /// The handle to the process (available on Windows only).
44 #error "ProcessInfo is not defined for this platform!"
47 enum : ProcessId { InvalidPid = 0 };
49 /// The process identifier.
52 /// The return code, set after execution.
58 /// \brief Find the first executable file \p Name in \p Paths.
60 /// This does not perform hashing as a shell would but instead stats each PATH
61 /// entry individually so should generally be avoided. Core LLVM library
62 /// functions and options should instead require fully specified paths.
64 /// \param Name name of the executable to find. If it contains any system
65 /// slashes, it will be returned as is.
66 /// \param Paths optional list of paths to search for \p Name. If empty it
67 /// will use the system PATH environment instead.
69 /// \returns The fully qualified path to the first \p Name in \p Paths if it
70 /// exists. \p Name if \p Name has slashes in it. Otherwise an error.
72 findProgramByName(StringRef Name, ArrayRef<StringRef> Paths = None);
74 // These functions change the specified standard stream (stdin or stdout) to
75 // binary mode. They return errc::success if the specified stream
76 // was changed. Otherwise a platform dependent error is returned.
77 std::error_code ChangeStdinToBinary();
78 std::error_code ChangeStdoutToBinary();
80 /// This function executes the program using the arguments provided. The
81 /// invoked program will inherit the stdin, stdout, and stderr file
82 /// descriptors, the environment and other configuration settings of the
84 /// This function waits for the program to finish, so should be avoided in
85 /// library functions that aren't expected to block. Consider using
86 /// ExecuteNoWait() instead.
87 /// @returns an integer result code indicating the status of the program.
88 /// A zero or positive value indicates the result code of the program.
89 /// -1 indicates failure to execute
90 /// -2 indicates a crash during execution or timeout
92 StringRef Program, ///< Path of the program to be executed. It is
93 ///< presumed this is the result of the findProgramByName method.
94 const char **args, ///< A vector of strings that are passed to the
95 ///< program. The first element should be the name of the program.
96 ///< The list *must* be terminated by a null char* entry.
97 const char **env = nullptr, ///< An optional vector of strings to use for
98 ///< the program's environment. If not provided, the current program's
99 ///< environment will be used.
100 const StringRef **redirects = nullptr, ///< An optional array of pointers
101 ///< to paths. If the array is null, no redirection is done. The array
102 ///< should have a size of at least three. The inferior process's
103 ///< stdin(0), stdout(1), and stderr(2) will be redirected to the
104 ///< corresponding paths.
105 ///< When an empty path is passed in, the corresponding file
106 ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
108 unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
109 ///< of time to wait for the child process to exit. If the time
110 ///< expires, the child is killed and this call returns. If zero,
111 ///< this function will wait until the child finishes or forever if
113 unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
114 ///< of memory can be allocated by process. If memory usage will be
115 ///< higher limit, the child is killed and this call returns. If zero
116 ///< - no memory limit.
117 std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a
118 ///< string instance in which error messages will be returned. If the
119 ///< string is non-empty upon return an error occurred while invoking the
121 bool *ExecutionFailed = nullptr);
123 /// Similar to ExecuteAndWait, but returns immediately.
124 /// @returns The \see ProcessInfo of the newly launced process.
125 /// \note On Microsoft Windows systems, users will need to either call \see
126 /// Wait until the process finished execution or win32 CloseHandle() API on
127 /// ProcessInfo.ProcessHandle to avoid memory leaks.
129 ExecuteNoWait(StringRef Program, const char **args, const char **env = nullptr,
130 const StringRef **redirects = nullptr, unsigned memoryLimit = 0,
131 std::string *ErrMsg = nullptr, bool *ExecutionFailed = nullptr);
133 /// Return true if the given arguments fit within system-specific
134 /// argument length limits.
135 bool commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef<const char*> Args);
137 /// File encoding options when writing contents that a non-UTF8 tool will
138 /// read (on Windows systems). For UNIX, we always use UTF-8.
139 enum WindowsEncodingMethod {
140 /// UTF-8 is the LLVM native encoding, being the same as "do not perform
141 /// encoding conversion".
147 /// Saves the UTF8-encoded \p contents string into the file \p FileName
148 /// using a specific encoding.
150 /// This write file function adds the possibility to choose which encoding
151 /// to use when writing a text file. On Windows, this is important when
152 /// writing files with internationalization support with an encoding that is
153 /// different from the one used in LLVM (UTF-8). We use this when writing
154 /// response files, since GCC tools on MinGW only understand legacy code
155 /// pages, and VisualStudio tools only understand UTF-16.
156 /// For UNIX, using different encodings is silently ignored, since all tools
157 /// work well with UTF-8.
158 /// This function assumes that you only use UTF-8 *text* data and will convert
159 /// it to your desired encoding before writing to the file.
161 /// FIXME: We use EM_CurrentCodePage to write response files for GNU tools in
162 /// a MinGW/MinGW-w64 environment, which has serious flaws but currently is
163 /// our best shot to make gcc/ld understand international characters. This
164 /// should be changed as soon as binutils fix this to support UTF16 on mingw.
166 /// \returns non-zero error_code if failed
168 writeFileWithEncoding(StringRef FileName, StringRef Contents,
169 WindowsEncodingMethod Encoding = WEM_UTF8);
171 /// This function waits for the process specified by \p PI to finish.
172 /// \returns A \see ProcessInfo struct with Pid set to:
173 /// \li The process id of the child process if the child process has changed
175 /// \li 0 if the child process has not changed state.
176 /// \note Users of this function should always check the ReturnCode member of
177 /// the \see ProcessInfo returned from this function.
179 const ProcessInfo &PI, ///< The child process that should be waited on.
180 unsigned SecondsToWait, ///< If non-zero, this specifies the amount of
181 ///< time to wait for the child process to exit. If the time expires, the
182 ///< child is killed and this function returns. If zero, this function
183 ///< will perform a non-blocking wait on the child process.
184 bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits
185 ///< until child has terminated.
186 std::string *ErrMsg = nullptr ///< If non-zero, provides a pointer to a
187 ///< string instance in which error messages will be returned. If the
188 ///< string is non-empty upon return an error occurred while invoking the