Add PrefixPrinter arguments to the dump routines for MachineFunction and
[oota-llvm.git] / include / llvm / System / Program.h
1 //===- llvm/System/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_SYSTEM_PROGRAM_H
15 #define LLVM_SYSTEM_PROGRAM_H
16
17 #include "llvm/System/Path.h"
18
19 namespace llvm {
20 namespace sys {
21
22   // TODO: Add operations to communicate with the process, redirect its I/O,
23   // etc.
24
25   /// This class provides an abstraction for programs that are executable by the
26   /// operating system. It provides a platform generic way to find executable
27   /// programs from the path and to execute them in various ways. The sys::Path
28   /// class is used to specify the location of the Program.
29   /// @since 1.4
30   /// @brief An abstraction for finding and executing programs.
31   class Program {
32     unsigned Pid_;
33
34     // Noncopyable.
35     Program(const Program& other);
36     Program& operator=(const Program& other);
37
38     /// @name Methods
39     /// @{
40   public:
41
42     Program() : Pid_(0)
43     {}
44
45     /// Return process ID of this program.
46     unsigned GetPid() { return Pid_; }
47
48     /// This function executes the program using the \p arguments provided.  The
49     /// invoked program will inherit the stdin, stdout, and stderr file
50     /// descriptors, the environment and other configuration settings of the
51     /// invoking program. If Path::executable() does not return true when this
52     /// function is called then a std::string is thrown.
53     /// @returns false in case of error, true otherwise.
54     /// @see FindProgramByName
55     /// @brief Executes the program with the given set of \p args.
56     bool Execute
57     ( const Path& path,  ///< sys::Path object providing the path of the
58       ///< program to be executed. It is presumed this is the result of
59       ///< the FindProgramByName method.
60       const char** args, ///< A vector of strings that are passed to the
61       ///< program.  The first element should be the name of the program.
62       ///< The list *must* be terminated by a null char* entry.
63       const char ** env = 0, ///< An optional vector of strings to use for
64       ///< the program's environment. If not provided, the current program's
65       ///< environment will be used.
66       const sys::Path** redirects = 0, ///< An optional array of pointers to
67       ///< Paths. If the array is null, no redirection is done. The array
68       ///< should have a size of at least three. If the pointer in the array
69       ///< are not null, then the inferior process's stdin(0), stdout(1),
70       ///< and stderr(2) will be redirected to the corresponding Paths.
71       ///< When an empty Path is passed in, the corresponding file
72       ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
73       ///< way.
74       unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
75       ///< of memory can be allocated by process. If memory usage will be
76       ///< higher limit, the child is killed and this call returns. If zero
77       ///< - no memory limit.
78       std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
79       ///< instance in which error messages will be returned. If the string
80       ///< is non-empty upon return an error occurred while invoking the
81       ///< program.
82       );
83
84     /// This function waits for the program to exit. This function will block
85     /// the current program until the invoked program exits.
86     /// @returns an integer result code indicating the status of the program.
87     /// A zero or positive value indicates the result code of the program. A
88     /// negative value is the signal number on which it terminated.
89     /// @see Execute
90     /// @brief Waits for the program to exit.
91     int Wait
92     ( unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
93       ///< of time to wait for the child process to exit. If the time
94       ///< expires, the child is killed and this call returns. If zero,
95       ///< this function will wait until the child finishes or forever if
96       ///< it doesn't.
97       std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
98       ///< instance in which error messages will be returned. If the string
99       ///< is non-empty upon return an error occurred while invoking the
100       ///< program.
101       );
102
103     /// This static constructor (factory) will attempt to locate a program in
104     /// the operating system's file system using some pre-determined set of
105     /// locations to search (e.g. the PATH on Unix).
106     /// @returns A Path object initialized to the path of the program or a
107     /// Path object that is empty (invalid) if the program could not be found.
108     /// @throws nothing
109     /// @brief Construct a Program by finding it by name.
110     static Path FindProgramByName(const std::string& name);
111
112     // These methods change the specified standard stream (stdin or stdout) to
113     // binary mode. They return true if an error occurred
114     static bool ChangeStdinToBinary();
115     static bool ChangeStdoutToBinary();
116
117     /// A convenience function equivalent to Program prg; prg.Execute(..);
118     /// prg.Wait(..);
119     /// @throws nothing
120     /// @see Execute, Wait
121     static int ExecuteAndWait(const Path& path,
122                               const char** args,
123                               const char ** env = 0,
124                               const sys::Path** redirects = 0,
125                               unsigned secondsToWait = 0,
126                               unsigned memoryLimit = 0,
127                               std::string* ErrMsg = 0);
128
129     /// A convenience function equivalent to Program prg; prg.Execute(..);
130     /// @throws nothing
131     /// @see Execute
132     static void ExecuteNoWait(const Path& path,
133                               const char** args,
134                               const char ** env = 0,
135                               const sys::Path** redirects = 0,
136                               unsigned memoryLimit = 0,
137                               std::string* ErrMsg = 0);
138
139     /// @}
140
141   };
142 }
143 }
144
145 #endif