LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Program.h
Go to the documentation of this file.
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"
20 
21 namespace llvm {
22 class error_code;
23 namespace sys {
24 
25  /// This is the OS-specific separator for PATH like environment variables:
26  // a colon on Unix or a semicolon on Windows.
27 #if defined(LLVM_ON_UNIX)
28  const char EnvPathSeparator = ':';
29 #elif defined (LLVM_ON_WIN32)
30  const char EnvPathSeparator = ';';
31 #endif
32 
33 /// @brief This struct encapsulates information about a process.
34 struct ProcessInfo {
35 #if defined(LLVM_ON_UNIX)
36  typedef pid_t ProcessId;
37 #elif defined(LLVM_ON_WIN32)
38  typedef unsigned long ProcessId; // Must match the type of DWORD on Windows.
39  typedef void * HANDLE; // Must match the type of HANDLE on Windows.
40  /// The handle to the process (available on Windows only).
41  HANDLE ProcessHandle;
42 #else
43 #error "ProcessInfo is not defined for this platform!"
44 #endif
45 
46  /// The process identifier.
47  ProcessId Pid;
48 
49  /// The return code, set after execution.
51 
52  ProcessInfo();
53 };
54 
55  /// This static constructor (factory) will attempt to locate a program in
56  /// the operating system's file system using some pre-determined set of
57  /// locations to search (e.g. the PATH on Unix). Paths with slashes are
58  /// returned unmodified.
59  /// @returns A Path object initialized to the path of the program or a
60  /// Path object that is empty (invalid) if the program could not be found.
61  /// @brief Construct a Program by finding it by name.
62  std::string FindProgramByName(const std::string& name);
63 
64  // These functions change the specified standard stream (stdin, stdout, or
65  // stderr) to binary mode. They return errc::success if the specified stream
66  // was changed. Otherwise a platform dependent error is returned.
70 
71  /// This function executes the program using the arguments provided. The
72  /// invoked program will inherit the stdin, stdout, and stderr file
73  /// descriptors, the environment and other configuration settings of the
74  /// invoking program.
75  /// This function waits the program to finish.
76  /// @returns an integer result code indicating the status of the program.
77  /// A zero or positive value indicates the result code of the program.
78  /// -1 indicates failure to execute
79  /// -2 indicates a crash during execution or timeout
80  int ExecuteAndWait(
81  StringRef Program, ///< Path of the program to be executed. It is
82  /// presumed this is the result of the FindProgramByName method.
83  const char **args, ///< A vector of strings that are passed to the
84  ///< program. The first element should be the name of the program.
85  ///< The list *must* be terminated by a null char* entry.
86  const char **env = 0, ///< An optional vector of strings to use for
87  ///< the program's environment. If not provided, the current program's
88  ///< environment will be used.
89  const StringRef **redirects = 0, ///< An optional array of pointers to
90  ///< paths. If the array is null, no redirection is done. The array
91  ///< should have a size of at least three. The inferior process's
92  ///< stdin(0), stdout(1), and stderr(2) will be redirected to the
93  ///< corresponding paths.
94  ///< When an empty path is passed in, the corresponding file
95  ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
96  ///< way.
97  unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
98  ///< of time to wait for the child process to exit. If the time
99  ///< expires, the child is killed and this call returns. If zero,
100  ///< this function will wait until the child finishes or forever if
101  ///< it doesn't.
102  unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
103  ///< of memory can be allocated by process. If memory usage will be
104  ///< higher limit, the child is killed and this call returns. If zero
105  ///< - no memory limit.
106  std::string *ErrMsg = 0, ///< If non-zero, provides a pointer to a string
107  ///< instance in which error messages will be returned. If the string
108  ///< is non-empty upon return an error occurred while invoking the
109  ///< program.
110  bool *ExecutionFailed = 0);
111 
112  /// Similar to ExecuteAndWait, but returns immediately.
113  /// @returns The \see ProcessInfo of the newly launced process.
114  /// \note On Microsoft Windows systems, users will need to either call \see
115  /// Wait until the process finished execution or win32 CloseHandle() API on
116  /// ProcessInfo.ProcessHandle to avoid memory leaks.
118  ExecuteNoWait(StringRef Program, const char **args, const char **env = 0,
119  const StringRef **redirects = 0, unsigned memoryLimit = 0,
120  std::string *ErrMsg = 0, bool *ExecutionFailed = 0);
121 
122  /// Return true if the given arguments fit within system-specific
123  /// argument length limits.
125 
126  /// This function waits for the process specified by \p PI to finish.
127  /// \returns A \see ProcessInfo struct with Pid set to:
128  /// \li The process id of the child process if the child process has changed
129  /// state.
130  /// \li 0 if the child process has not changed state.
131  /// \note Users of this function should always check the ReturnCode member of
132  /// the \see ProcessInfo returned from this function.
134  const ProcessInfo &PI, ///< The child process that should be waited on.
135  unsigned SecondsToWait, ///< If non-zero, this specifies the amount of
136  ///< time to wait for the child process to exit. If the time expires, the
137  ///< child is killed and this function returns. If zero, this function
138  ///< will perform a non-blocking wait on the child process.
139  bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits
140  ///< until child has terminated.
141  std::string *ErrMsg = 0 ///< If non-zero, provides a pointer to a string
142  ///< instance in which error messages will be returned. If the string
143  ///< is non-empty upon return an error occurred while invoking the
144  ///< program.
145  );
146  }
147 }
148 
149 #endif
error_code ChangeStdinToBinary()
bool argumentsFitWithinSystemLimits(ArrayRef< const char * > Args)
ProcessId Pid
The process identifier.
Definition: Program.h:47
int ExecuteAndWait(StringRef Program, const char **args, const char **env=0, const StringRef **redirects=0, unsigned secondsToWait=0, unsigned memoryLimit=0, std::string *ErrMsg=0, bool *ExecutionFailed=0)
Definition: Program.cpp:29
ProcessInfo ExecuteNoWait(StringRef Program, const char **args, const char **env=0, const StringRef **redirects=0, unsigned memoryLimit=0, std::string *ErrMsg=0, bool *ExecutionFailed=0)
Definition: Program.cpp:47
error_code ChangeStderrToBinary()
int ReturnCode
The return code, set after execution.
Definition: Program.h:50
This is the OS-specific separator for PATH like environment variables:
Definition: Program.h:34
std::string FindProgramByName(const std::string &name)
Construct a Program by finding it by name.
ProcessInfo Wait(const ProcessInfo &PI, unsigned SecondsToWait, bool WaitUntilTerminates, std::string *ErrMsg=0)
error_code ChangeStdoutToBinary()