1 //===- llvm/System/Program.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the llvm::sys::Program class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SYSTEM_PROGRAM_H
15 #define LLVM_SYSTEM_PROGRAM_H
17 #include "llvm/System/Path.h"
22 // TODO: Add operations to communicate with the process, redirect its I/O,
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.
30 /// @brief An abstraction for finding and executing programs.
32 /// Opaque handle for target specific data.
38 Program(const Program& other);
39 Program& operator=(const Program& other);
48 /// Return process ID of this program.
49 unsigned GetPid() { return Pid_; }
51 /// This function executes the program using the \p arguments provided. The
52 /// invoked program will inherit the stdin, stdout, and stderr file
53 /// descriptors, the environment and other configuration settings of the
54 /// invoking program. If Path::executable() does not return true when this
55 /// function is called then a std::string is thrown.
56 /// @returns false in case of error, true otherwise.
57 /// @see FindProgramByName
58 /// @brief Executes the program with the given set of \p args.
60 ( const Path& path, ///< sys::Path object providing the path of the
61 ///< program to be executed. It is presumed this is the result of
62 ///< the FindProgramByName method.
63 const char** args, ///< A vector of strings that are passed to the
64 ///< program. The first element should be the name of the program.
65 ///< The list *must* be terminated by a null char* entry.
66 const char ** env = 0, ///< An optional vector of strings to use for
67 ///< the program's environment. If not provided, the current program's
68 ///< environment will be used.
69 const sys::Path** redirects = 0, ///< An optional array of pointers to
70 ///< Paths. If the array is null, no redirection is done. The array
71 ///< should have a size of at least three. If the pointer in the array
72 ///< are not null, then the inferior process's stdin(0), stdout(1),
73 ///< and stderr(2) will be redirected to the corresponding Paths.
74 ///< When an empty Path is passed in, the corresponding file
75 ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
77 unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
78 ///< of memory can be allocated by process. If memory usage will be
79 ///< higher limit, the child is killed and this call returns. If zero
80 ///< - no memory limit.
81 std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
82 ///< instance in which error messages will be returned. If the string
83 ///< is non-empty upon return an error occurred while invoking the
87 /// This function waits for the program to exit. This function will block
88 /// the current program until the invoked program exits.
89 /// @returns an integer result code indicating the status of the program.
90 /// A zero or positive value indicates the result code of the program. A
91 /// negative value is the signal number on which it terminated.
93 /// @brief Waits for the program to exit.
95 ( unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
96 ///< of time to wait for the child process to exit. If the time
97 ///< expires, the child is killed and this call returns. If zero,
98 ///< this function will wait until the child finishes or forever if
100 std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
101 ///< instance in which error messages will be returned. If the string
102 ///< is non-empty upon return an error occurred while invoking the
106 /// This function terminates the program.
107 /// @returns true if an error occured.
109 /// @brief Terminates the program.
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
117 /// This static constructor (factory) will attempt to locate a program in
118 /// the operating system's file system using some pre-determined set of
119 /// locations to search (e.g. the PATH on Unix).
120 /// @returns A Path object initialized to the path of the program or a
121 /// Path object that is empty (invalid) if the program could not be found.
123 /// @brief Construct a Program by finding it by name.
124 static Path FindProgramByName(const std::string& name);
126 // These methods change the specified standard stream (stdin or stdout) to
127 // binary mode. They return true if an error occurred
128 static bool ChangeStdinToBinary();
129 static bool ChangeStdoutToBinary();
131 /// A convenience function equivalent to Program prg; prg.Execute(..);
134 /// @see Execute, Wait
135 static int ExecuteAndWait(const Path& path,
137 const char ** env = 0,
138 const sys::Path** redirects = 0,
139 unsigned secondsToWait = 0,
140 unsigned memoryLimit = 0,
141 std::string* ErrMsg = 0);
143 /// A convenience function equivalent to Program prg; prg.Execute(..);
146 static void ExecuteNoWait(const Path& path,
148 const char ** env = 0,
149 const sys::Path** redirects = 0,
150 unsigned memoryLimit = 0,
151 std::string* ErrMsg = 0);