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