LLVM API Documentation
00001 //===- llvm/Support/Program.h ------------------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares the llvm::sys::Program class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_SUPPORT_PROGRAM_H 00015 #define LLVM_SUPPORT_PROGRAM_H 00016 00017 #include "llvm/ADT/ArrayRef.h" 00018 #include "llvm/Support/Path.h" 00019 #include <system_error> 00020 00021 namespace llvm { 00022 namespace sys { 00023 00024 /// This is the OS-specific separator for PATH like environment variables: 00025 // a colon on Unix or a semicolon on Windows. 00026 #if defined(LLVM_ON_UNIX) 00027 const char EnvPathSeparator = ':'; 00028 #elif defined (LLVM_ON_WIN32) 00029 const char EnvPathSeparator = ';'; 00030 #endif 00031 00032 /// @brief This struct encapsulates information about a process. 00033 struct ProcessInfo { 00034 #if defined(LLVM_ON_UNIX) 00035 typedef pid_t ProcessId; 00036 #elif defined(LLVM_ON_WIN32) 00037 typedef unsigned long ProcessId; // Must match the type of DWORD on Windows. 00038 typedef void * HANDLE; // Must match the type of HANDLE on Windows. 00039 /// The handle to the process (available on Windows only). 00040 HANDLE ProcessHandle; 00041 #else 00042 #error "ProcessInfo is not defined for this platform!" 00043 #endif 00044 00045 /// The process identifier. 00046 ProcessId Pid; 00047 00048 /// The return code, set after execution. 00049 int ReturnCode; 00050 00051 ProcessInfo(); 00052 }; 00053 00054 /// This function attempts to locate a program in the operating 00055 /// system's file system using some pre-determined set of locations to search 00056 /// (e.g. the PATH on Unix). Paths with slashes are returned unmodified. 00057 /// 00058 /// It does not perform hashing as a shell would but instead stats each PATH 00059 /// entry individually so should generally be avoided. Core LLVM library 00060 /// functions and options should instead require fully specified paths. 00061 /// 00062 /// @returns A string containing the path of the program or an empty string if 00063 /// the program could not be found. 00064 std::string FindProgramByName(const std::string& name); 00065 00066 // These functions change the specified standard stream (stdin or stdout) to 00067 // binary mode. They return errc::success if the specified stream 00068 // was changed. Otherwise a platform dependent error is returned. 00069 std::error_code ChangeStdinToBinary(); 00070 std::error_code ChangeStdoutToBinary(); 00071 00072 /// This function executes the program using the arguments provided. The 00073 /// invoked program will inherit the stdin, stdout, and stderr file 00074 /// descriptors, the environment and other configuration settings of the 00075 /// invoking program. 00076 /// This function waits for the program to finish, so should be avoided in 00077 /// library functions that aren't expected to block. Consider using 00078 /// ExecuteNoWait() instead. 00079 /// @returns an integer result code indicating the status of the program. 00080 /// A zero or positive value indicates the result code of the program. 00081 /// -1 indicates failure to execute 00082 /// -2 indicates a crash during execution or timeout 00083 int ExecuteAndWait( 00084 StringRef Program, ///< Path of the program to be executed. It is 00085 /// presumed this is the result of the FindProgramByName method. 00086 const char **args, ///< A vector of strings that are passed to the 00087 ///< program. The first element should be the name of the program. 00088 ///< The list *must* be terminated by a null char* entry. 00089 const char **env = nullptr, ///< An optional vector of strings to use for 00090 ///< the program's environment. If not provided, the current program's 00091 ///< environment will be used. 00092 const StringRef **redirects = nullptr, ///< An optional array of pointers 00093 ///< to paths. If the array is null, no redirection is done. The array 00094 ///< should have a size of at least three. The inferior process's 00095 ///< stdin(0), stdout(1), and stderr(2) will be redirected to the 00096 ///< corresponding paths. 00097 ///< When an empty path is passed in, the corresponding file 00098 ///< descriptor will be disconnected (ie, /dev/null'd) in a portable 00099 ///< way. 00100 unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount 00101 ///< of time to wait for the child process to exit. If the time 00102 ///< expires, the child is killed and this call returns. If zero, 00103 ///< this function will wait until the child finishes or forever if 00104 ///< it doesn't. 00105 unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount 00106 ///< of memory can be allocated by process. If memory usage will be 00107 ///< higher limit, the child is killed and this call returns. If zero 00108 ///< - no memory limit. 00109 std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a 00110 ///< string instance in which error messages will be returned. If the 00111 ///< string is non-empty upon return an error occurred while invoking the 00112 ///< program. 00113 bool *ExecutionFailed = nullptr); 00114 00115 /// Similar to ExecuteAndWait, but returns immediately. 00116 /// @returns The \see ProcessInfo of the newly launced process. 00117 /// \note On Microsoft Windows systems, users will need to either call \see 00118 /// Wait until the process finished execution or win32 CloseHandle() API on 00119 /// ProcessInfo.ProcessHandle to avoid memory leaks. 00120 ProcessInfo 00121 ExecuteNoWait(StringRef Program, const char **args, const char **env = nullptr, 00122 const StringRef **redirects = nullptr, unsigned memoryLimit = 0, 00123 std::string *ErrMsg = nullptr, bool *ExecutionFailed = nullptr); 00124 00125 /// Return true if the given arguments fit within system-specific 00126 /// argument length limits. 00127 bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args); 00128 00129 /// File encoding options when writing contents that a non-UTF8 tool will 00130 /// read (on Windows systems). For UNIX, we always use UTF-8. 00131 enum WindowsEncodingMethod { 00132 /// UTF-8 is the LLVM native encoding, being the same as "do not perform 00133 /// encoding conversion". 00134 WEM_UTF8, 00135 WEM_CurrentCodePage, 00136 WEM_UTF16 00137 }; 00138 00139 /// Saves the UTF8-encoded \p contents string into the file \p FileName 00140 /// using a specific encoding. 00141 /// 00142 /// This write file function adds the possibility to choose which encoding 00143 /// to use when writing a text file. On Windows, this is important when 00144 /// writing files with internationalization support with an encoding that is 00145 /// different from the one used in LLVM (UTF-8). We use this when writing 00146 /// response files, since GCC tools on MinGW only understand legacy code 00147 /// pages, and VisualStudio tools only understand UTF-16. 00148 /// For UNIX, using different encodings is silently ignored, since all tools 00149 /// work well with UTF-8. 00150 /// This function assumes that you only use UTF-8 *text* data and will convert 00151 /// it to your desired encoding before writing to the file. 00152 /// 00153 /// FIXME: We use EM_CurrentCodePage to write response files for GNU tools in 00154 /// a MinGW/MinGW-w64 environment, which has serious flaws but currently is 00155 /// our best shot to make gcc/ld understand international characters. This 00156 /// should be changed as soon as binutils fix this to support UTF16 on mingw. 00157 /// 00158 /// \returns non-zero error_code if failed 00159 std::error_code 00160 writeFileWithEncoding(StringRef FileName, StringRef Contents, 00161 WindowsEncodingMethod Encoding = WEM_UTF8); 00162 00163 /// This function waits for the process specified by \p PI to finish. 00164 /// \returns A \see ProcessInfo struct with Pid set to: 00165 /// \li The process id of the child process if the child process has changed 00166 /// state. 00167 /// \li 0 if the child process has not changed state. 00168 /// \note Users of this function should always check the ReturnCode member of 00169 /// the \see ProcessInfo returned from this function. 00170 ProcessInfo Wait( 00171 const ProcessInfo &PI, ///< The child process that should be waited on. 00172 unsigned SecondsToWait, ///< If non-zero, this specifies the amount of 00173 ///< time to wait for the child process to exit. If the time expires, the 00174 ///< child is killed and this function returns. If zero, this function 00175 ///< will perform a non-blocking wait on the child process. 00176 bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits 00177 ///< until child has terminated. 00178 std::string *ErrMsg = nullptr ///< If non-zero, provides a pointer to a 00179 ///< string instance in which error messages will be returned. If the 00180 ///< string is non-empty upon return an error occurred while invoking the 00181 ///< program. 00182 ); 00183 } 00184 } 00185 00186 #endif