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/ADT/ArrayRef.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/PathV1.h"
20 #include "llvm/Support/system_error.h"
26 // TODO: Add operations to communicate with the process, redirect its I/O,
29 /// This class provides an abstraction for programs that are executable by the
30 /// operating system. It provides a platform generic way to find executable
31 /// programs from the path and to execute them in various ways. The sys::Path
32 /// class is used to specify the location of the Program.
34 /// @brief An abstraction for finding and executing programs.
36 /// Opaque handle for target specific data.
40 Program(const Program& other) LLVM_DELETED_FUNCTION;
41 Program& operator=(const Program& other) LLVM_DELETED_FUNCTION;
49 /// This function executes the program using the \p arguments provided. The
50 /// invoked program will inherit the stdin, stdout, and stderr file
51 /// descriptors, the environment and other configuration settings of the
52 /// invoking program. If Path::executable() does not return true when this
53 /// function is called then a std::string is thrown.
54 /// @returns false in case of error, true otherwise.
55 /// @see FindProgramByName
56 /// @brief Executes the program with the given set of \p args.
58 ( const Path& path, ///< sys::Path object providing the path of the
59 ///< program to be executed. It is presumed this is the result of
60 ///< the FindProgramByName method.
61 const char** args, ///< A vector of strings that are passed to the
62 ///< program. The first element should be the name of the program.
63 ///< The list *must* be terminated by a null char* entry.
64 const char ** env = 0, ///< An optional vector of strings to use for
65 ///< the program's environment. If not provided, the current program's
66 ///< environment will be used.
67 const sys::Path** redirects = 0, ///< An optional array of pointers to
68 ///< Paths. If the array is null, no redirection is done. The array
69 ///< should have a size of at least three. If the pointer in the array
70 ///< are not null, then the inferior process's stdin(0), stdout(1),
71 ///< and stderr(2) will be redirected to the corresponding Paths.
72 ///< When an empty Path is passed in, the corresponding file
73 ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
75 unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
76 ///< of memory can be allocated by process. If memory usage will be
77 ///< higher limit, the child is killed and this call returns. If zero
78 ///< - no memory limit.
79 std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
80 ///< instance in which error messages will be returned. If the string
81 ///< is non-empty upon return an error occurred while invoking the
85 /// This function waits for the program to exit. This function will block
86 /// the current program until the invoked program exits.
87 /// @returns an integer result code indicating the status of the program.
88 /// A zero or positive value indicates the result code of the program.
89 /// -1 indicates failure to execute
90 /// -2 indicates a crash during execution or timeout
92 /// @brief Waits for the program to exit.
94 ( const Path& path, ///< The path to the child process executable.
95 unsigned secondsToWait, ///< 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 ///< 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 waiting.
106 /// This static constructor (factory) will attempt to locate a program in
107 /// the operating system's file system using some pre-determined set of
108 /// locations to search (e.g. the PATH on Unix). Paths with slashes are
109 /// returned unmodified.
110 /// @returns A Path object initialized to the path of the program or a
111 /// Path object that is empty (invalid) if the program could not be found.
112 /// @brief Construct a Program by finding it by name.
113 static Path FindProgramByName(const std::string& name);
115 // These methods change the specified standard stream (stdin, stdout, or
116 // stderr) to binary mode. They return errc::success if the specified stream
117 // was changed. Otherwise a platform dependent error is returned.
118 static error_code ChangeStdinToBinary();
119 static error_code ChangeStdoutToBinary();
120 static error_code ChangeStderrToBinary();
122 /// A convenience function equivalent to Program prg; prg.Execute(..);
124 /// @see Execute, Wait
125 static int ExecuteAndWait(const Path& path,
127 const char ** env = 0,
128 const sys::Path** redirects = 0,
129 unsigned secondsToWait = 0,
130 unsigned memoryLimit = 0,
131 std::string* ErrMsg = 0,
132 bool *ExecutionFailed = 0);
134 /// A convenience function equivalent to Program prg; prg.Execute(..);
136 static void ExecuteNoWait(const Path& path,
138 const char ** env = 0,
139 const sys::Path** redirects = 0,
140 unsigned memoryLimit = 0,
141 std::string* ErrMsg = 0);
147 inline int ExecuteAndWait(const Path &path, const char **args,
148 const char **env = 0,
149 const sys::Path **redirects = 0,
150 unsigned secondsToWait = 0,
151 unsigned memoryLimit = 0, std::string *ErrMsg = 0,
152 bool *ExecutionFailed = 0) {
153 return Program::ExecuteAndWait(path, args, env, redirects, secondsToWait,
154 memoryLimit, ErrMsg, ExecutionFailed);
157 inline Path FindProgramByName(const std::string& name) {
158 return Program::FindProgramByName(name);
161 inline error_code ChangeStdoutToBinary() {
162 return Program::ChangeStdoutToBinary();
165 // Return true if the given arguments fit within system-specific
166 // argument length limits.
167 bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);