BumpPtrAllocator: do the size check without moving any pointers
[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 namespace sys {
23
24   /// This is the OS-specific separator for PATH like environment variables:
25   // a colon on Unix or a semicolon on Windows.
26 #if defined(LLVM_ON_UNIX)
27   const char EnvPathSeparator = ':';
28 #elif defined (LLVM_ON_WIN32)
29   const char EnvPathSeparator = ';';
30 #endif
31
32 /// @brief This struct encapsulates information about a process.
33 struct ProcessInfo {
34 #if defined(LLVM_ON_UNIX)
35   typedef pid_t ProcessId;
36 #elif defined(LLVM_ON_WIN32)
37   typedef unsigned long ProcessId; // Must match the type of DWORD on Windows.
38   typedef void * HANDLE; // Must match the type of HANDLE on Windows.
39   /// The handle to the process (available on Windows only).
40   HANDLE ProcessHandle;
41 #else
42 #error "ProcessInfo is not defined for this platform!"
43 #endif
44
45   /// The process identifier.
46   ProcessId Pid;
47
48   /// The return code, set after execution.
49   int ReturnCode;
50
51   ProcessInfo();
52 };
53
54   /// This function attempts to locate a program in the operating
55   /// system's file system using some pre-determined set of locations to search
56   /// (e.g. the PATH on Unix). Paths with slashes are returned unmodified.
57   ///
58   /// It 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   /// @returns A string containing the path of the program or an empty string if
63   /// the program could not be found.
64   std::string FindProgramByName(const std::string& name);
65
66   // These functions change the specified standard stream (stdin or stdout) to
67   // binary mode. They return errc::success if the specified stream
68   // was changed. Otherwise a platform dependent error is returned.
69   std::error_code ChangeStdinToBinary();
70   std::error_code ChangeStdoutToBinary();
71
72   /// This function executes the program using the arguments provided.  The
73   /// invoked program will inherit the stdin, stdout, and stderr file
74   /// descriptors, the environment and other configuration settings of the
75   /// invoking program.
76   /// This function waits for the program to finish, so should be avoided in
77   /// library functions that aren't expected to block. Consider using
78   /// ExecuteNoWait() instead.
79   /// @returns an integer result code indicating the status of the program.
80   /// A zero or positive value indicates the result code of the program.
81   /// -1 indicates failure to execute
82   /// -2 indicates a crash during execution or timeout
83   int ExecuteAndWait(
84       StringRef Program, ///< Path of the program to be executed. It is
85       /// presumed this is the result of the FindProgramByName method.
86       const char **args, ///< A vector of strings that are passed to the
87       ///< program.  The first element should be the name of the program.
88       ///< The list *must* be terminated by a null char* entry.
89       const char **env = nullptr, ///< An optional vector of strings to use for
90       ///< the program's environment. If not provided, the current program's
91       ///< environment will be used.
92       const StringRef **redirects = nullptr, ///< An optional array of pointers
93       ///< to paths. If the array is null, no redirection is done. The array
94       ///< should have a size of at least three. The inferior process's
95       ///< stdin(0), stdout(1), and stderr(2) will be redirected to the
96       ///< corresponding paths.
97       ///< When an empty path is passed in, the corresponding file
98       ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
99       ///< way.
100       unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
101       ///< of time to wait for the child process to exit. If the time
102       ///< expires, the child is killed and this call returns. If zero,
103       ///< this function will wait until the child finishes or forever if
104       ///< it doesn't.
105       unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
106       ///< of memory can be allocated by process. If memory usage will be
107       ///< higher limit, the child is killed and this call returns. If zero
108       ///< - no memory limit.
109       std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a
110       ///< string instance in which error messages will be returned. If the
111       ///< string is non-empty upon return an error occurred while invoking the
112       ///< program.
113       bool *ExecutionFailed = nullptr);
114
115   /// Similar to ExecuteAndWait, but returns immediately.
116   /// @returns The \see ProcessInfo of the newly launced process.
117   /// \note On Microsoft Windows systems, users will need to either call \see
118   /// Wait until the process finished execution or win32 CloseHandle() API on
119   /// ProcessInfo.ProcessHandle to avoid memory leaks.
120   ProcessInfo
121   ExecuteNoWait(StringRef Program, const char **args, const char **env = nullptr,
122                 const StringRef **redirects = nullptr, unsigned memoryLimit = 0,
123                 std::string *ErrMsg = nullptr, bool *ExecutionFailed = nullptr);
124
125   /// Return true if the given arguments fit within system-specific
126   /// argument length limits.
127   bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
128
129   /// File encoding options when writing contents that a non-UTF8 tool will
130   /// read (on Windows systems). For UNIX, we always use UTF-8.
131   enum WindowsEncodingMethod {
132     /// UTF-8 is the LLVM native encoding, being the same as "do not perform
133     /// encoding conversion".
134     WEM_UTF8,
135     WEM_CurrentCodePage,
136     WEM_UTF16
137   };
138
139   /// Saves the UTF8-encoded \p contents string into the file \p FileName
140   /// using a specific encoding.
141   ///
142   /// This write file function adds the possibility to choose which encoding
143   /// to use when writing a text file. On Windows, this is important when
144   /// writing files with internationalization support with an encoding that is
145   /// different from the one used in LLVM (UTF-8). We use this when writing
146   /// response files, since GCC tools on MinGW only understand legacy code
147   /// pages, and VisualStudio tools only understand UTF-16.
148   /// For UNIX, using different encodings is silently ignored, since all tools
149   /// work well with UTF-8.
150   /// This function assumes that you only use UTF-8 *text* data and will convert
151   /// it to your desired encoding before writing to the file.
152   ///
153   /// FIXME: We use EM_CurrentCodePage to write response files for GNU tools in
154   /// a MinGW/MinGW-w64 environment, which has serious flaws but currently is
155   /// our best shot to make gcc/ld understand international characters. This
156   /// should be changed as soon as binutils fix this to support UTF16 on mingw.
157   ///
158   /// \returns non-zero error_code if failed
159   std::error_code
160   writeFileWithEncoding(StringRef FileName, StringRef Contents,
161                         WindowsEncodingMethod Encoding = WEM_UTF8);
162
163   /// This function waits for the process specified by \p PI to finish.
164   /// \returns A \see ProcessInfo struct with Pid set to:
165   /// \li The process id of the child process if the child process has changed
166   /// state.
167   /// \li 0 if the child process has not changed state.
168   /// \note Users of this function should always check the ReturnCode member of
169   /// the \see ProcessInfo returned from this function.
170   ProcessInfo Wait(
171       const ProcessInfo &PI, ///< The child process that should be waited on.
172       unsigned SecondsToWait, ///< If non-zero, this specifies the amount of
173       ///< time to wait for the child process to exit. If the time expires, the
174       ///< child is killed and this function returns. If zero, this function
175       ///< will perform a non-blocking wait on the child process.
176       bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits
177       ///< until child has terminated.
178       std::string *ErrMsg = nullptr ///< If non-zero, provides a pointer to a
179       ///< string instance in which error messages will be returned. If the
180       ///< string is non-empty upon return an error occurred while invoking the
181       ///< program.
182       );
183   }
184 }
185
186 #endif