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/system_error.h"
20
21namespace llvm {
22class error_code;
23namespace 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  /// This static constructor (factory) will attempt to locate a program in
34  /// the operating system's file system using some pre-determined set of
35  /// locations to search (e.g. the PATH on Unix). Paths with slashes are
36  /// returned unmodified.
37  /// @returns A Path object initialized to the path of the program or a
38  /// Path object that is empty (invalid) if the program could not be found.
39  /// @brief Construct a Program by finding it by name.
40  std::string FindProgramByName(const std::string& name);
41
42  // These functions change the specified standard stream (stdin, stdout, or
43  // stderr) to binary mode. They return errc::success if the specified stream
44  // was changed. Otherwise a platform dependent error is returned.
45  error_code ChangeStdinToBinary();
46  error_code ChangeStdoutToBinary();
47  error_code ChangeStderrToBinary();
48
49  /// This function executes the program using the 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.
53  /// This function waits the program to finish.
54  /// @returns an integer result code indicating the status of the program.
55  /// A zero or positive value indicates the result code of the program.
56  /// -1 indicates failure to execute
57  /// -2 indicates a crash during execution or timeout
58  int ExecuteAndWait(
59      StringRef Program, ///< Path of the program to be executed. It is
60      /// presumed this is the result of 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 StringRef **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. The inferior process's
70      ///< stdin(0), stdout(1), and stderr(2) will be redirected to the
71      ///< 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
74      ///< way.
75      unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
76      ///< of time to wait for the child process to exit. If the time
77      ///< expires, the child is killed and this call returns. If zero,
78      ///< this function will wait until the child finishes or forever if
79      ///< it doesn't.
80      unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
81      ///< of memory can be allocated by process. If memory usage will be
82      ///< higher limit, the child is killed and this call returns. If zero
83      ///< - no memory limit.
84      std::string *ErrMsg = 0, ///< If non-zero, provides a pointer to a string
85      ///< instance in which error messages will be returned. If the string
86      ///< is non-empty upon return an error occurred while invoking the
87      ///< program.
88      bool *ExecutionFailed = 0);
89
90  /// Similar to ExecuteAndWait, but return immediately.
91  void ExecuteNoWait(StringRef Program, const char **args, const char **env = 0,
92                     const StringRef **redirects = 0, unsigned memoryLimit = 0,
93                     std::string *ErrMsg = 0);
94
95  // Return true if the given arguments fit within system-specific
96  // argument length limits.
97  bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
98}
99}
100
101#endif
102