Introduce llvm::sys::path::home_directory.
[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 "llvm/Support/system_error.h"
20
21 namespace llvm {
22 class 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, stdout, or
68   // stderr) to 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   error_code ChangeStderrToBinary();
73
74   /// This function executes the program using the arguments provided.  The
75   /// invoked program will inherit the stdin, stdout, and stderr file
76   /// descriptors, the environment and other configuration settings of the
77   /// invoking program.
78   /// This function waits for the program to finish, so should be avoided in
79   /// library functions that aren't expected to block. Consider using
80   /// ExecuteNoWait() instead.
81   /// @returns an integer result code indicating the status of the program.
82   /// A zero or positive value indicates the result code of the program.
83   /// -1 indicates failure to execute
84   /// -2 indicates a crash during execution or timeout
85   int ExecuteAndWait(
86       StringRef Program, ///< Path of the program to be executed. It is
87       /// presumed this is the result of the FindProgramByName method.
88       const char **args, ///< A vector of strings that are passed to the
89       ///< program.  The first element should be the name of the program.
90       ///< The list *must* be terminated by a null char* entry.
91       const char **env = 0, ///< An optional vector of strings to use for
92       ///< the program's environment. If not provided, the current program's
93       ///< environment will be used.
94       const StringRef **redirects = 0, ///< An optional array of pointers to
95       ///< paths. If the array is null, no redirection is done. The array
96       ///< should have a size of at least three. The inferior process's
97       ///< stdin(0), stdout(1), and stderr(2) will be redirected to the
98       ///< corresponding paths.
99       ///< When an empty path is passed in, the corresponding file
100       ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
101       ///< way.
102       unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
103       ///< of time to wait for the child process to exit. If the time
104       ///< expires, the child is killed and this call returns. If zero,
105       ///< this function will wait until the child finishes or forever if
106       ///< it doesn't.
107       unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
108       ///< of memory can be allocated by process. If memory usage will be
109       ///< higher limit, the child is killed and this call returns. If zero
110       ///< - no memory limit.
111       std::string *ErrMsg = 0, ///< If non-zero, provides a pointer to a string
112       ///< instance in which error messages will be returned. If the string
113       ///< is non-empty upon return an error occurred while invoking the
114       ///< program.
115       bool *ExecutionFailed = 0);
116
117   /// Similar to ExecuteAndWait, but returns immediately.
118   /// @returns The \see ProcessInfo of the newly launced process.
119   /// \note On Microsoft Windows systems, users will need to either call \see
120   /// Wait until the process finished execution or win32 CloseHandle() API on
121   /// ProcessInfo.ProcessHandle to avoid memory leaks.
122   ProcessInfo
123   ExecuteNoWait(StringRef Program, const char **args, const char **env = 0,
124                 const StringRef **redirects = 0, unsigned memoryLimit = 0,
125                 std::string *ErrMsg = 0, bool *ExecutionFailed = 0);
126
127   /// Return true if the given arguments fit within system-specific
128   /// argument length limits.
129   bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
130
131   /// This function waits for the process specified by \p PI to finish.
132   /// \returns A \see ProcessInfo struct with Pid set to:
133   /// \li The process id of the child process if the child process has changed
134   /// state.
135   /// \li 0 if the child process has not changed state.
136   /// \note Users of this function should always check the ReturnCode member of
137   /// the \see ProcessInfo returned from this function.
138   ProcessInfo Wait(
139       const ProcessInfo &PI, ///< The child process that should be waited on.
140       unsigned SecondsToWait, ///< If non-zero, this specifies the amount of
141       ///< time to wait for the child process to exit. If the time expires, the
142       ///< child is killed and this function returns. If zero, this function
143       ///< will perform a non-blocking wait on the child process.
144       bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits
145       ///< until child has terminated.
146       std::string *ErrMsg = 0 ///< If non-zero, provides a pointer to a string
147       ///< instance in which error messages will be returned. If the string
148       ///< is non-empty upon return an error occurred while invoking the
149       ///< program.
150       );
151   }
152 }
153
154 #endif