Program.h revision 6585b388cb7bfc623adb9e4dd910423f838e5d96
153ca1f3190680f3e86aebe0f72f7918d63f71e0dCharles Davis//===- llvm/Support/Program.h ------------------------------------*- C++ -*-===//
263b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
367d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer//                     The LLVM Compiler Infrastructure
467d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
763b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
867d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer//===----------------------------------------------------------------------===//
967d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer//
1067d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer// This file declares the llvm::sys::Program class.
1167d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer//
1267d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer//===----------------------------------------------------------------------===//
1367d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer
14674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_SUPPORT_PROGRAM_H
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_SUPPORT_PROGRAM_H
1667d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer
17845a932af74ecbd2a20af5751dd61fa8cf2246f5Rafael Espindola#include "llvm/ADT/ArrayRef.h"
181f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Path.h"
19f3e397eb17327423b3f8fff5eac8547c85efddb1Rafael Espindola#include "llvm/Support/PathV1.h"
20609baa376a6f3e0c3930555c5055e6da193fd8a8Rafael Espindola#include "llvm/Support/system_error.h"
2167d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer
2267d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencernamespace llvm {
23b92cb30cf547bf9a8590a981a49040f480c8eb82Michael J. Spencerclass error_code;
2467d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencernamespace sys {
259f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// This static constructor (factory) will attempt to locate a program in
269f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// the operating system's file system using some pre-determined set of
279f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// locations to search (e.g. the PATH on Unix). Paths with slashes are
289f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// returned unmodified.
299f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// @returns A Path object initialized to the path of the program or a
309f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// Path object that is empty (invalid) if the program could not be found.
319f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// @brief Construct a Program by finding it by name.
326585b388cb7bfc623adb9e4dd910423f838e5d96Rafael Espindola  std::string FindProgramByName(const std::string& name);
339f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola
349f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  // These functions change the specified standard stream (stdin, stdout, or
359f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  // stderr) to binary mode. They return errc::success if the specified stream
369f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  // was changed. Otherwise a platform dependent error is returned.
379f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  error_code ChangeStdinToBinary();
389f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  error_code ChangeStdoutToBinary();
399f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  error_code ChangeStderrToBinary();
409f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola
419f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// This function executes the program using the arguments provided.  The
429f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// invoked program will inherit the stdin, stdout, and stderr file
439f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// descriptors, the environment and other configuration settings of the
449f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// invoking program.
459f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// This function waits the program to finish.
469f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// @returns an integer result code indicating the status of the program.
479f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// A zero or positive value indicates the result code of the program.
489f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// -1 indicates failure to execute
499f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// -2 indicates a crash during execution or timeout
509f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  int ExecuteAndWait(
519f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      const Path &path, ///< sys::Path object providing the path of the
523140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< program to be executed. It is presumed this is the result of
533140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< the FindProgramByName method.
549f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      const char **args, ///< A vector of strings that are passed to the
553140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< program.  The first element should be the name of the program.
563140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< The list *must* be terminated by a null char* entry.
579f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      const char **env = 0, ///< An optional vector of strings to use for
583140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< the program's environment. If not provided, the current program's
593140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< environment will be used.
609f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      const sys::Path **redirects = 0, ///< An optional array of pointers to
613140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< Paths. If the array is null, no redirection is done. The array
623140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< should have a size of at least three. If the pointer in the array
633140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< are not null, then the inferior process's stdin(0), stdout(1),
643140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< and stderr(2) will be redirected to the corresponding Paths.
653140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< When an empty Path is passed in, the corresponding file
663140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
673140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< way.
689f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
699f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      ///< of time to wait for the child process to exit. If the time
709f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      ///< expires, the child is killed and this call returns. If zero,
719f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      ///< this function will wait until the child finishes or forever if
729f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      ///< it doesn't.
733140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
743140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< of memory can be allocated by process. If memory usage will be
753140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< higher limit, the child is killed and this call returns. If zero
763140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< - no memory limit.
779f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      std::string *ErrMsg = 0, ///< If non-zero, provides a pointer to a string
783140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< instance in which error messages will be returned. If the string
793140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< is non-empty upon return an error occurred while invoking the
803140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov      ///< program.
819f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola      bool *ExecutionFailed = 0);
82609baa376a6f3e0c3930555c5055e6da193fd8a8Rafael Espindola
839f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  /// Similar to ExecuteAndWait, but return immediately.
849f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola  void ExecuteNoWait(const Path &path, const char **args, const char **env = 0,
859f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola                     const sys::Path **redirects = 0, unsigned memoryLimit = 0,
869f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola                     std::string *ErrMsg = 0);
87609baa376a6f3e0c3930555c5055e6da193fd8a8Rafael Espindola
88845a932af74ecbd2a20af5751dd61fa8cf2246f5Rafael Espindola  // Return true if the given arguments fit within system-specific
89845a932af74ecbd2a20af5751dd61fa8cf2246f5Rafael Espindola  // argument length limits.
90845a932af74ecbd2a20af5751dd61fa8cf2246f5Rafael Espindola  bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
9167d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer}
9267d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer}
9367d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer
9467d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer#endif
95