~pali/+junk/llvm-toolchain-3.7

« back to all changes in this revision

Viewing changes to include/llvm/Support/Program.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2015-07-15 17:51:08 UTC
  • Revision ID: package-import@ubuntu.com-20150715175108-l8mynwovkx4zx697
Tags: upstream-3.7~+rc2
ImportĀ upstreamĀ versionĀ 3.7~+rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/ErrorOr.h"
 
19
#include <system_error>
 
20
 
 
21
namespace llvm {
 
22
class StringRef;
 
23
 
 
24
namespace sys {
 
25
 
 
26
  /// This is the OS-specific separator for PATH like environment variables:
 
27
  // a colon on Unix or a semicolon on Windows.
 
28
#if defined(LLVM_ON_UNIX)
 
29
  const char EnvPathSeparator = ':';
 
30
#elif defined (LLVM_ON_WIN32)
 
31
  const char EnvPathSeparator = ';';
 
32
#endif
 
33
 
 
34
/// @brief This struct encapsulates information about a process.
 
35
struct ProcessInfo {
 
36
#if defined(LLVM_ON_UNIX)
 
37
  typedef pid_t ProcessId;
 
38
#elif defined(LLVM_ON_WIN32)
 
39
  typedef unsigned long ProcessId; // Must match the type of DWORD on Windows.
 
40
  typedef void * HANDLE; // Must match the type of HANDLE on Windows.
 
41
  /// The handle to the process (available on Windows only).
 
42
  HANDLE ProcessHandle;
 
43
#else
 
44
#error "ProcessInfo is not defined for this platform!"
 
45
#endif
 
46
 
 
47
  /// The process identifier.
 
48
  ProcessId Pid;
 
49
 
 
50
  /// The return code, set after execution.
 
51
  int ReturnCode;
 
52
 
 
53
  ProcessInfo();
 
54
};
 
55
 
 
56
  /// \brief Find the first executable file \p Name in \p Paths.
 
57
  ///
 
58
  /// This does not perform hashing as a shell would but instead stats each PATH
 
59
  /// entry individually so should generally be avoided. Core LLVM library
 
60
  /// functions and options should instead require fully specified paths.
 
61
  ///
 
62
  /// \param Name name of the executable to find. If it contains any system
 
63
  ///   slashes, it will be returned as is.
 
64
  /// \param Paths optional list of paths to search for \p Name. If empty it
 
65
  ///   will use the system PATH environment instead.
 
66
  ///
 
67
  /// \returns The fully qualified path to the first \p Name in \p Paths if it
 
68
  ///   exists. \p Name if \p Name has slashes in it. Otherwise an error.
 
69
  ErrorOr<std::string>
 
70
  findProgramByName(StringRef Name,
 
71
                    ArrayRef<StringRef> Paths = ArrayRef<StringRef>());
 
72
 
 
73
  // These functions change the specified standard stream (stdin or stdout) to
 
74
  // binary mode. They return errc::success if the specified stream
 
75
  // was changed. Otherwise a platform dependent error is returned.
 
76
  std::error_code ChangeStdinToBinary();
 
77
  std::error_code ChangeStdoutToBinary();
 
78
 
 
79
  /// This function executes the program using the arguments provided.  The
 
80
  /// invoked program will inherit the stdin, stdout, and stderr file
 
81
  /// descriptors, the environment and other configuration settings of the
 
82
  /// invoking program.
 
83
  /// This function waits for the program to finish, so should be avoided in
 
84
  /// library functions that aren't expected to block. Consider using
 
85
  /// ExecuteNoWait() instead.
 
86
  /// @returns an integer result code indicating the status of the program.
 
87
  /// A zero or positive value indicates the result code of the program.
 
88
  /// -1 indicates failure to execute
 
89
  /// -2 indicates a crash during execution or timeout
 
90
  int ExecuteAndWait(
 
91
      StringRef Program, ///< Path of the program to be executed. It is
 
92
      /// presumed this is the result of the findProgramByName method.
 
93
      const char **args, ///< A vector of strings that are passed to the
 
94
      ///< program.  The first element should be the name of the program.
 
95
      ///< The list *must* be terminated by a null char* entry.
 
96
      const char **env = nullptr, ///< An optional vector of strings to use for
 
97
      ///< the program's environment. If not provided, the current program's
 
98
      ///< environment will be used.
 
99
      const StringRef **redirects = nullptr, ///< An optional array of pointers
 
100
      ///< to paths. If the array is null, no redirection is done. The array
 
101
      ///< should have a size of at least three. The inferior process's
 
102
      ///< stdin(0), stdout(1), and stderr(2) will be redirected to the
 
103
      ///< corresponding paths.
 
104
      ///< When an empty path is passed in, the corresponding file
 
105
      ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
 
106
      ///< way.
 
107
      unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
 
108
      ///< of time to wait for the child process to exit. If the time
 
109
      ///< expires, the child is killed and this call returns. If zero,
 
110
      ///< this function will wait until the child finishes or forever if
 
111
      ///< it doesn't.
 
112
      unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
 
113
      ///< of memory can be allocated by process. If memory usage will be
 
114
      ///< higher limit, the child is killed and this call returns. If zero
 
115
      ///< - no memory limit.
 
116
      std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a
 
117
      ///< string instance in which error messages will be returned. If the
 
118
      ///< string is non-empty upon return an error occurred while invoking the
 
119
      ///< program.
 
120
      bool *ExecutionFailed = nullptr);
 
121
 
 
122
  /// Similar to ExecuteAndWait, but returns immediately.
 
123
  /// @returns The \see ProcessInfo of the newly launced process.
 
124
  /// \note On Microsoft Windows systems, users will need to either call \see
 
125
  /// Wait until the process finished execution or win32 CloseHandle() API on
 
126
  /// ProcessInfo.ProcessHandle to avoid memory leaks.
 
127
  ProcessInfo
 
128
  ExecuteNoWait(StringRef Program, const char **args, const char **env = nullptr,
 
129
                const StringRef **redirects = nullptr, unsigned memoryLimit = 0,
 
130
                std::string *ErrMsg = nullptr, bool *ExecutionFailed = nullptr);
 
131
 
 
132
  /// Return true if the given arguments fit within system-specific
 
133
  /// argument length limits.
 
134
  bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
 
135
 
 
136
  /// File encoding options when writing contents that a non-UTF8 tool will
 
137
  /// read (on Windows systems). For UNIX, we always use UTF-8.
 
138
  enum WindowsEncodingMethod {
 
139
    /// UTF-8 is the LLVM native encoding, being the same as "do not perform
 
140
    /// encoding conversion".
 
141
    WEM_UTF8,
 
142
    WEM_CurrentCodePage,
 
143
    WEM_UTF16
 
144
  };
 
145
 
 
146
  /// Saves the UTF8-encoded \p contents string into the file \p FileName
 
147
  /// using a specific encoding.
 
148
  ///
 
149
  /// This write file function adds the possibility to choose which encoding
 
150
  /// to use when writing a text file. On Windows, this is important when
 
151
  /// writing files with internationalization support with an encoding that is
 
152
  /// different from the one used in LLVM (UTF-8). We use this when writing
 
153
  /// response files, since GCC tools on MinGW only understand legacy code
 
154
  /// pages, and VisualStudio tools only understand UTF-16.
 
155
  /// For UNIX, using different encodings is silently ignored, since all tools
 
156
  /// work well with UTF-8.
 
157
  /// This function assumes that you only use UTF-8 *text* data and will convert
 
158
  /// it to your desired encoding before writing to the file.
 
159
  ///
 
160
  /// FIXME: We use EM_CurrentCodePage to write response files for GNU tools in
 
161
  /// a MinGW/MinGW-w64 environment, which has serious flaws but currently is
 
162
  /// our best shot to make gcc/ld understand international characters. This
 
163
  /// should be changed as soon as binutils fix this to support UTF16 on mingw.
 
164
  ///
 
165
  /// \returns non-zero error_code if failed
 
166
  std::error_code
 
167
  writeFileWithEncoding(StringRef FileName, StringRef Contents,
 
168
                        WindowsEncodingMethod Encoding = WEM_UTF8);
 
169
 
 
170
  /// This function waits for the process specified by \p PI to finish.
 
171
  /// \returns A \see ProcessInfo struct with Pid set to:
 
172
  /// \li The process id of the child process if the child process has changed
 
173
  /// state.
 
174
  /// \li 0 if the child process has not changed state.
 
175
  /// \note Users of this function should always check the ReturnCode member of
 
176
  /// the \see ProcessInfo returned from this function.
 
177
  ProcessInfo Wait(
 
178
      const ProcessInfo &PI, ///< The child process that should be waited on.
 
179
      unsigned SecondsToWait, ///< If non-zero, this specifies the amount of
 
180
      ///< time to wait for the child process to exit. If the time expires, the
 
181
      ///< child is killed and this function returns. If zero, this function
 
182
      ///< will perform a non-blocking wait on the child process.
 
183
      bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits
 
184
      ///< until child has terminated.
 
185
      std::string *ErrMsg = nullptr ///< If non-zero, provides a pointer to a
 
186
      ///< string instance in which error messages will be returned. If the
 
187
      ///< string is non-empty upon return an error occurred while invoking the
 
188
      ///< program.
 
189
      );
 
190
  }
 
191
}
 
192
 
 
193
#endif