Remove system_error.h.
[oota-llvm.git] / include / llvm / Support / Program.h
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/Support/Path.h"
19 #include <system_error>
20
21 namespace llvm {
22 using std::error_code;
23 namespace sys {
24
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 = ';';
31 #endif
32
33 /// @brief This struct encapsulates information about a process.
34 struct ProcessInfo {
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).
41   HANDLE ProcessHandle;
42 #else
43 #error "ProcessInfo is not defined for this platform!"
44 #endif
45
46   /// The process identifier.
47   ProcessId Pid;
48
49   /// The return code, set after execution.
50   int ReturnCode;
51
52   ProcessInfo();
53 };
54
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.
58   ///
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.
62   ///
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);
66
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();
72
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
76   /// invoking program.
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
84   int ExecuteAndWait(
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 = nullptr, ///< 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 = nullptr, ///< An optional array of pointers
94       ///< to 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
100       ///< way.
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
105       ///< it doesn't.
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 = nullptr, ///< If non-zero, provides a pointer to a
111       ///< string instance in which error messages will be returned. If the
112       ///< string is non-empty upon return an error occurred while invoking the
113       ///< program.
114       bool *ExecutionFailed = nullptr);
115
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.
121   ProcessInfo
122   ExecuteNoWait(StringRef Program, const char **args, const char **env = nullptr,
123                 const StringRef **redirects = nullptr, unsigned memoryLimit = 0,
124                 std::string *ErrMsg = nullptr, bool *ExecutionFailed = nullptr);
125
126   /// Return true if the given arguments fit within system-specific
127   /// argument length limits.
128   bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
129
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
133   /// state.
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.
137   ProcessInfo Wait(
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 = nullptr ///< If non-zero, provides a pointer to a
146       ///< string instance in which error messages will be returned. If the
147       ///< string is non-empty upon return an error occurred while invoking the
148       ///< program.
149       );
150   }
151 }
152
153 #endif