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/Path.h"
19 #include "llvm/Support/system_error.h"
25 /// This is the OS-specific separator for PATH like environment variables:
26 // a colon on Unix or a semicolon on Windows.
27 #if defined(LLVM_ON_UNIX)
28 const char EnvPathSeparator = ':';
29 #elif defined (LLVM_ON_WIN32)
30 const char EnvPathSeparator = ';';
33 /// @brief This struct encapsulates information about a process.
35 #if defined(LLVM_ON_UNIX)
36 typedef pid_t ProcessId;
37 #elif defined(LLVM_ON_WIN32)
38 typedef unsigned long ProcessId; // Must match the type of DWORD on Windows.
39 typedef void * HANDLE; // Must match the type of HANDLE on Windows.
40 /// The handle to the process (available on Windows only).
43 #error "ProcessInfo is not defined for this platform!"
46 /// The process identifier.
49 /// The return code, set after execution.
55 /// This function attempts to locate a program in the operating
56 /// system's file system using some pre-determined set of locations to search
57 /// (e.g. the PATH on Unix). Paths with slashes are returned unmodified.
59 /// It does not perform hashing as a shell would but instead stats each PATH
60 /// entry individually so should generally be avoided. Core LLVM library
61 /// functions and options should instead require fully specified paths.
63 /// @returns A string containing the path of the program or an empty string if
64 /// the program could not be found.
65 std::string FindProgramByName(const std::string& name);
67 // These functions change the specified standard stream (stdin or stdout) to
68 // binary mode. They return errc::success if the specified stream
69 // was changed. Otherwise a platform dependent error is returned.
70 error_code ChangeStdinToBinary();
71 error_code ChangeStdoutToBinary();
73 /// This function executes the program using the arguments provided. The
74 /// invoked program will inherit the stdin, stdout, and stderr file
75 /// descriptors, the environment and other configuration settings of the
77 /// This function waits for the program to finish, so should be avoided in
78 /// library functions that aren't expected to block. Consider using
79 /// ExecuteNoWait() instead.
80 /// @returns an integer result code indicating the status of the program.
81 /// A zero or positive value indicates the result code of the program.
82 /// -1 indicates failure to execute
83 /// -2 indicates a crash during execution or timeout
85 StringRef Program, ///< Path of the program to be executed. It is
86 /// presumed this is the result of the FindProgramByName method.
87 const char **args, ///< A vector of strings that are passed to the
88 ///< program. The first element should be the name of the program.
89 ///< The list *must* be terminated by a null char* entry.
90 const char **env = 0, ///< An optional vector of strings to use for
91 ///< the program's environment. If not provided, the current program's
92 ///< environment will be used.
93 const StringRef **redirects = 0, ///< An optional array of pointers to
94 ///< paths. If the array is null, no redirection is done. The array
95 ///< should have a size of at least three. The inferior process's
96 ///< stdin(0), stdout(1), and stderr(2) will be redirected to the
97 ///< corresponding paths.
98 ///< When an empty path is passed in, the corresponding file
99 ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
101 unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
102 ///< of time to wait for the child process to exit. If the time
103 ///< expires, the child is killed and this call returns. If zero,
104 ///< this function will wait until the child finishes or forever if
106 unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
107 ///< of memory can be allocated by process. If memory usage will be
108 ///< higher limit, the child is killed and this call returns. If zero
109 ///< - no memory limit.
110 std::string *ErrMsg = 0, ///< If non-zero, provides a pointer to a string
111 ///< instance in which error messages will be returned. If the string
112 ///< is non-empty upon return an error occurred while invoking the
114 bool *ExecutionFailed = 0);
116 /// Similar to ExecuteAndWait, but returns immediately.
117 /// @returns The \see ProcessInfo of the newly launced process.
118 /// \note On Microsoft Windows systems, users will need to either call \see
119 /// Wait until the process finished execution or win32 CloseHandle() API on
120 /// ProcessInfo.ProcessHandle to avoid memory leaks.
122 ExecuteNoWait(StringRef Program, const char **args, const char **env = 0,
123 const StringRef **redirects = 0, unsigned memoryLimit = 0,
124 std::string *ErrMsg = 0, bool *ExecutionFailed = 0);
126 /// Return true if the given arguments fit within system-specific
127 /// argument length limits.
128 bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
130 /// This function waits for the process specified by \p PI to finish.
131 /// \returns A \see ProcessInfo struct with Pid set to:
132 /// \li The process id of the child process if the child process has changed
134 /// \li 0 if the child process has not changed state.
135 /// \note Users of this function should always check the ReturnCode member of
136 /// the \see ProcessInfo returned from this function.
138 const ProcessInfo &PI, ///< The child process that should be waited on.
139 unsigned SecondsToWait, ///< If non-zero, this specifies the amount of
140 ///< time to wait for the child process to exit. If the time expires, the
141 ///< child is killed and this function returns. If zero, this function
142 ///< will perform a non-blocking wait on the child process.
143 bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits
144 ///< until child has terminated.
145 std::string *ErrMsg = 0 ///< If non-zero, provides a pointer to a string
146 ///< instance in which error messages will be returned. If the string
147 ///< is non-empty upon return an error occurred while invoking the