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" 19cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines#include <system_error> 2067d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer 2167d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencernamespace llvm { 2267d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencernamespace sys { 23593fcb56b37857d64e58ac3be5277d3cfccb9492Rafael Espindola 24593fcb56b37857d64e58ac3be5277d3cfccb9492Rafael Espindola /// This is the OS-specific separator for PATH like environment variables: 25593fcb56b37857d64e58ac3be5277d3cfccb9492Rafael Espindola // a colon on Unix or a semicolon on Windows. 26593fcb56b37857d64e58ac3be5277d3cfccb9492Rafael Espindola#if defined(LLVM_ON_UNIX) 27593fcb56b37857d64e58ac3be5277d3cfccb9492Rafael Espindola const char EnvPathSeparator = ':'; 28593fcb56b37857d64e58ac3be5277d3cfccb9492Rafael Espindola#elif defined (LLVM_ON_WIN32) 29593fcb56b37857d64e58ac3be5277d3cfccb9492Rafael Espindola const char EnvPathSeparator = ';'; 30593fcb56b37857d64e58ac3be5277d3cfccb9492Rafael Espindola#endif 31593fcb56b37857d64e58ac3be5277d3cfccb9492Rafael Espindola 32c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj/// @brief This struct encapsulates information about a process. 33c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Sirajstruct ProcessInfo { 34c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj#if defined(LLVM_ON_UNIX) 35c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj typedef pid_t ProcessId; 36c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj#elif defined(LLVM_ON_WIN32) 37c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj typedef unsigned long ProcessId; // Must match the type of DWORD on Windows. 38c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj typedef void * HANDLE; // Must match the type of HANDLE on Windows. 39c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// The handle to the process (available on Windows only). 40c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj HANDLE ProcessHandle; 41c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj#else 42c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj#error "ProcessInfo is not defined for this platform!" 43c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj#endif 44c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj 45c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// The process identifier. 46c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ProcessId Pid; 47c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj 48c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// The return code, set after execution. 49c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj int ReturnCode; 50c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj 51c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ProcessInfo(); 52c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj}; 53c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj 5436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// This function attempts to locate a program in the operating 5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// system's file system using some pre-determined set of locations to search 5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// (e.g. the PATH on Unix). Paths with slashes are returned unmodified. 5736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// 5836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// It does not perform hashing as a shell would but instead stats each PATH 5936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// entry individually so should generally be avoided. Core LLVM library 6036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// functions and options should instead require fully specified paths. 6136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// 6236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// @returns A string containing the path of the program or an empty string if 6336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// the program could not be found. 646585b388cb7bfc623adb9e4dd910423f838e5d96Rafael Espindola std::string FindProgramByName(const std::string& name); 659f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola 6636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // These functions change the specified standard stream (stdin or stdout) to 6736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // binary mode. They return errc::success if the specified stream 689f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola // was changed. Otherwise a platform dependent error is returned. 69cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines std::error_code ChangeStdinToBinary(); 70cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines std::error_code ChangeStdoutToBinary(); 719f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola 729f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola /// This function executes the program using the arguments provided. The 739f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola /// invoked program will inherit the stdin, stdout, and stderr file 749f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola /// descriptors, the environment and other configuration settings of the 759f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola /// invoking program. 7636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// This function waits for the program to finish, so should be avoided in 7736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// library functions that aren't expected to block. Consider using 7836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines /// ExecuteNoWait() instead. 799f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola /// @returns an integer result code indicating the status of the program. 809f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola /// A zero or positive value indicates the result code of the program. 819f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola /// -1 indicates failure to execute 829f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola /// -2 indicates a crash during execution or timeout 839f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola int ExecuteAndWait( 84675e0ac0bfd6fb78423d9fbee9f50c1dec62c111Rafael Espindola StringRef Program, ///< Path of the program to be executed. It is 85675e0ac0bfd6fb78423d9fbee9f50c1dec62c111Rafael Espindola /// presumed this is the result of the FindProgramByName method. 869f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola const char **args, ///< A vector of strings that are passed to the 873140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< program. The first element should be the name of the program. 883140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< The list *must* be terminated by a null char* entry. 89dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines const char **env = nullptr, ///< An optional vector of strings to use for 903140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< the program's environment. If not provided, the current program's 913140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< environment will be used. 92dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines const StringRef **redirects = nullptr, ///< An optional array of pointers 93dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines ///< to paths. If the array is null, no redirection is done. The array 94675e0ac0bfd6fb78423d9fbee9f50c1dec62c111Rafael Espindola ///< should have a size of at least three. The inferior process's 95675e0ac0bfd6fb78423d9fbee9f50c1dec62c111Rafael Espindola ///< stdin(0), stdout(1), and stderr(2) will be redirected to the 96675e0ac0bfd6fb78423d9fbee9f50c1dec62c111Rafael Espindola ///< corresponding paths. 97675e0ac0bfd6fb78423d9fbee9f50c1dec62c111Rafael Espindola ///< When an empty path is passed in, the corresponding file 983140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< descriptor will be disconnected (ie, /dev/null'd) in a portable 993140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< way. 1009f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount 1019f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola ///< of time to wait for the child process to exit. If the time 1029f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola ///< expires, the child is killed and this call returns. If zero, 1039f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola ///< this function will wait until the child finishes or forever if 1049f1d9fd1964d82f3e801efb71518144492cdf290Rafael Espindola ///< it doesn't. 1053140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount 1063140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< of memory can be allocated by process. If memory usage will be 1073140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< higher limit, the child is killed and this call returns. If zero 1083140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< - no memory limit. 109dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a 110dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines ///< string instance in which error messages will be returned. If the 111dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines ///< string is non-empty upon return an error occurred while invoking the 1123140619c63d205d79b8eac1d88afa918981fa258Mikhail Glushenkov ///< program. 113dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines bool *ExecutionFailed = nullptr); 114609baa376a6f3e0c3930555c5055e6da193fd8a8Rafael Espindola 115c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// Similar to ExecuteAndWait, but returns immediately. 116c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// @returns The \see ProcessInfo of the newly launced process. 1170d1785a2e64648584d8230172125d171d5f1ca02NAKAMURA Takumi /// \note On Microsoft Windows systems, users will need to either call \see 118c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// Wait until the process finished execution or win32 CloseHandle() API on 119c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// ProcessInfo.ProcessHandle to avoid memory leaks. 120c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ProcessInfo 121dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines ExecuteNoWait(StringRef Program, const char **args, const char **env = nullptr, 122dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines const StringRef **redirects = nullptr, unsigned memoryLimit = 0, 123dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines std::string *ErrMsg = nullptr, bool *ExecutionFailed = nullptr); 124609baa376a6f3e0c3930555c5055e6da193fd8a8Rafael Espindola 125c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// Return true if the given arguments fit within system-specific 126c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// argument length limits. 127845a932af74ecbd2a20af5751dd61fa8cf2246f5Rafael Espindola bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args); 128c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj 129c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// This function waits for the process specified by \p PI to finish. 130c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// \returns A \see ProcessInfo struct with Pid set to: 131c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// \li The process id of the child process if the child process has changed 132c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// state. 133c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// \li 0 if the child process has not changed state. 1340d1785a2e64648584d8230172125d171d5f1ca02NAKAMURA Takumi /// \note Users of this function should always check the ReturnCode member of 135c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj /// the \see ProcessInfo returned from this function. 136c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ProcessInfo Wait( 137c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj const ProcessInfo &PI, ///< The child process that should be waited on. 138c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj unsigned SecondsToWait, ///< If non-zero, this specifies the amount of 139c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ///< time to wait for the child process to exit. If the time expires, the 140c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ///< child is killed and this function returns. If zero, this function 141c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ///< will perform a non-blocking wait on the child process. 142c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits 143c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ///< until child has terminated. 144dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines std::string *ErrMsg = nullptr ///< If non-zero, provides a pointer to a 145dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines ///< string instance in which error messages will be returned. If the 146dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines ///< string is non-empty upon return an error occurred while invoking the 147c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ///< program. 148c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj ); 149c269c4f505a2dd7c3a88d12706257410ed6c7068Tareq A. Siraj } 15067d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer} 15167d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer 15267d556567f3fa759bfe5a38f56695c717743f4c4Reid Spencer#endif 153