Include PathV1.h in files that use it.
[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/PathV1.h"
20
21 namespace llvm {
22 class error_code;
23 namespace sys {
24
25   // TODO: Add operations to communicate with the process, redirect its I/O,
26   // etc.
27
28   /// This class provides an abstraction for programs that are executable by the
29   /// operating system. It provides a platform generic way to find executable
30   /// programs from the path and to execute them in various ways. The sys::Path
31   /// class is used to specify the location of the Program.
32   /// @since 1.4
33   /// @brief An abstraction for finding and executing programs.
34   class Program {
35     /// Opaque handle for target specific data.
36     void *Data_;
37
38     // Noncopyable.
39     Program(const Program& other) LLVM_DELETED_FUNCTION;
40     Program& operator=(const Program& other) LLVM_DELETED_FUNCTION;
41
42     /// @name Methods
43     /// @{
44
45     Program();
46     ~Program();
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.
88     /// -1 indicates failure to execute
89     /// -2 indicates a crash during execution or timeout
90     /// @see Execute
91     /// @brief Waits for the program to exit.
92     int Wait
93     ( const Path& path, ///< The path to the child process executable.
94       unsigned secondsToWait, ///< If non-zero, this specifies the amount
95       ///< of time to wait for the child process to exit. If the time
96       ///< expires, the child is killed and this call returns. If zero,
97       ///< this function will wait until the child finishes or forever if
98       ///< it doesn't.
99       std::string* ErrMsg ///< If non-zero, provides a pointer to a string
100       ///< instance in which error messages will be returned. If the string
101       ///< is non-empty upon return an error occurred while waiting.
102       );
103
104   public:
105     /// This static constructor (factory) will attempt to locate a program in
106     /// the operating system's file system using some pre-determined set of
107     /// locations to search (e.g. the PATH on Unix). Paths with slashes are
108     /// returned unmodified.
109     /// @returns A Path object initialized to the path of the program or a
110     /// Path object that is empty (invalid) if the program could not be found.
111     /// @brief Construct a Program by finding it by name.
112     static Path FindProgramByName(const std::string& name);
113
114     // These methods change the specified standard stream (stdin, stdout, or
115     // stderr) to binary mode. They return errc::success if the specified stream
116     // was changed. Otherwise a platform dependent error is returned.
117     static error_code ChangeStdinToBinary();
118     static error_code ChangeStdoutToBinary();
119     static error_code ChangeStderrToBinary();
120
121     /// A convenience function equivalent to Program prg; prg.Execute(..);
122     /// prg.Wait(..);
123     /// @see Execute, Wait
124     static int ExecuteAndWait(const Path& path,
125                               const char** args,
126                               const char ** env = 0,
127                               const sys::Path** redirects = 0,
128                               unsigned secondsToWait = 0,
129                               unsigned memoryLimit = 0,
130                               std::string* ErrMsg = 0,
131                               bool *ExecutionFailed = 0);
132
133     /// A convenience function equivalent to Program prg; prg.Execute(..);
134     /// @see Execute
135     static void ExecuteNoWait(const Path& path,
136                               const char** args,
137                               const char ** env = 0,
138                               const sys::Path** redirects = 0,
139                               unsigned memoryLimit = 0,
140                               std::string* ErrMsg = 0);
141
142     /// @}
143
144   };
145
146   // Return true if the given arguments fit within system-specific
147   // argument length limits.
148   bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
149 }
150 }
151
152 #endif