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/system_error.h"
24 /// This static constructor (factory) will attempt to locate a program in
25 /// the operating system's file system using some pre-determined set of
26 /// locations to search (e.g. the PATH on Unix). Paths with slashes are
27 /// returned unmodified.
28 /// @returns A Path object initialized to the path of the program or a
29 /// Path object that is empty (invalid) if the program could not be found.
30 /// @brief Construct a Program by finding it by name.
31 std::string FindProgramByName(const std::string& name);
33 // These functions change the specified standard stream (stdin, stdout, or
34 // stderr) to binary mode. They return errc::success if the specified stream
35 // was changed. Otherwise a platform dependent error is returned.
36 error_code ChangeStdinToBinary();
37 error_code ChangeStdoutToBinary();
38 error_code ChangeStderrToBinary();
40 /// This function executes the program using the arguments provided. The
41 /// invoked program will inherit the stdin, stdout, and stderr file
42 /// descriptors, the environment and other configuration settings of the
44 /// This function waits the program to finish.
45 /// @returns an integer result code indicating the status of the program.
46 /// A zero or positive value indicates the result code of the program.
47 /// -1 indicates failure to execute
48 /// -2 indicates a crash during execution or timeout
50 StringRef Program, ///< Path of the program to be executed. It is
51 /// presumed this is the result of the FindProgramByName method.
52 const char **args, ///< A vector of strings that are passed to the
53 ///< program. The first element should be the name of the program.
54 ///< The list *must* be terminated by a null char* entry.
55 const char **env = 0, ///< An optional vector of strings to use for
56 ///< the program's environment. If not provided, the current program's
57 ///< environment will be used.
58 const StringRef **redirects = 0, ///< An optional array of pointers to
59 ///< paths. If the array is null, no redirection is done. The array
60 ///< should have a size of at least three. The inferior process's
61 ///< stdin(0), stdout(1), and stderr(2) will be redirected to the
62 ///< corresponding paths.
63 ///< When an empty path is passed in, the corresponding file
64 ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
66 unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
67 ///< of time to wait for the child process to exit. If the time
68 ///< expires, the child is killed and this call returns. If zero,
69 ///< this function will wait until the child finishes or forever if
71 unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
72 ///< of memory can be allocated by process. If memory usage will be
73 ///< higher limit, the child is killed and this call returns. If zero
74 ///< - no memory limit.
75 std::string *ErrMsg = 0, ///< If non-zero, provides a pointer to a string
76 ///< instance in which error messages will be returned. If the string
77 ///< is non-empty upon return an error occurred while invoking the
79 bool *ExecutionFailed = 0);
81 /// Similar to ExecuteAndWait, but return immediately.
82 void ExecuteNoWait(StringRef Program, const char **args, const char **env = 0,
83 const StringRef **redirects = 0, unsigned memoryLimit = 0,
84 std::string *ErrMsg = 0);
86 // Return true if the given arguments fit within system-specific
87 // argument length limits.
88 bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);