1 //===- llvm/Support/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_SUPPORT_PROGRAM_H
15 #define LLVM_SUPPORT_PROGRAM_H
17 #include "llvm/Support/Path.h"
23 // TODO: Add operations to communicate with the process, redirect its I/O,
26 /// This class provides an abstraction for programs that are executable by the
27 /// operating system. It provides a platform generic way to find executable
28 /// programs from the path and to execute them in various ways. The sys::Path
29 /// class is used to specify the location of the Program.
31 /// @brief An abstraction for finding and executing programs.
33 /// Opaque handle for target specific data.
37 Program(const Program& other) LLVM_DELETED_FUNCTION;
38 Program& operator=(const Program& other) LLVM_DELETED_FUNCTION;
46 /// This function executes the program using the \p arguments provided. The
47 /// invoked program will inherit the stdin, stdout, and stderr file
48 /// descriptors, the environment and other configuration settings of the
49 /// invoking program. If Path::executable() does not return true when this
50 /// function is called then a std::string is thrown.
51 /// @returns false in case of error, true otherwise.
52 /// @see FindProgramByName
53 /// @brief Executes the program with the given set of \p args.
55 ( const Path& path, ///< sys::Path object providing the path of the
56 ///< program to be executed. It is presumed this is the result of
57 ///< the FindProgramByName method.
58 const char** args, ///< A vector of strings that are passed to the
59 ///< program. The first element should be the name of the program.
60 ///< The list *must* be terminated by a null char* entry.
61 const char ** env = 0, ///< An optional vector of strings to use for
62 ///< the program's environment. If not provided, the current program's
63 ///< environment will be used.
64 const sys::Path** redirects = 0, ///< An optional array of pointers to
65 ///< Paths. If the array is null, no redirection is done. The array
66 ///< should have a size of at least three. If the pointer in the array
67 ///< are not null, then the inferior process's stdin(0), stdout(1),
68 ///< and stderr(2) will be redirected to the corresponding Paths.
69 ///< When an empty Path is passed in, the corresponding file
70 ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
72 unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
73 ///< of memory can be allocated by process. If memory usage will be
74 ///< higher limit, the child is killed and this call returns. If zero
75 ///< - no memory limit.
76 std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
77 ///< instance in which error messages will be returned. If the string
78 ///< is non-empty upon return an error occurred while invoking the
82 /// This function waits for the program to exit. This function will block
83 /// the current program until the invoked program exits.
84 /// @returns an integer result code indicating the status of the program.
85 /// A zero or positive value indicates the result code of the program.
86 /// -1 indicates failure to execute
87 /// -2 indicates a crash during execution or timeout
89 /// @brief Waits for the program to exit.
91 ( const Path& path, ///< The path to the child process executable.
92 unsigned secondsToWait, ///< 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
97 std::string* ErrMsg ///< 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 waiting.
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). Paths with slashes are
106 /// returned unmodified.
107 /// @returns A Path object initialized to the path of the program or a
108 /// Path object that is empty (invalid) if the program could not be found.
109 /// @brief Construct a Program by finding it by name.
110 static Path FindProgramByName(const std::string& name);
112 // These methods change the specified standard stream (stdin, stdout, or
113 // stderr) to binary mode. They return errc::success if the specified stream
114 // was changed. Otherwise a platform dependent error is returned.
115 static error_code ChangeStdinToBinary();
116 static error_code ChangeStdoutToBinary();
117 static error_code ChangeStderrToBinary();
119 /// A convenience function equivalent to Program prg; prg.Execute(..);
121 /// @see Execute, Wait
122 static int ExecuteAndWait(const Path& path,
124 const char ** env = 0,
125 const sys::Path** redirects = 0,
126 unsigned secondsToWait = 0,
127 unsigned memoryLimit = 0,
128 std::string* ErrMsg = 0);
130 /// A convenience function equivalent to Program prg; prg.Execute(..);
132 static void ExecuteNoWait(const Path& path,
134 const char ** env = 0,
135 const sys::Path** redirects = 0,
136 unsigned memoryLimit = 0,
137 std::string* ErrMsg = 0);