LLVM  3.7.0
Program.cpp
Go to the documentation of this file.
1 //===-- Program.cpp - Implement OS Program Concept --------------*- 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 implements the operating system Program concept.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/Program.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Config/config.h"
17 #include <system_error>
18 using namespace llvm;
19 using namespace sys;
20 
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //=== independent code.
24 //===----------------------------------------------------------------------===//
25 
26 static bool Execute(ProcessInfo &PI, StringRef Program, const char **args,
27  const char **env, const StringRef **Redirects,
28  unsigned memoryLimit, std::string *ErrMsg);
29 
30 int sys::ExecuteAndWait(StringRef Program, const char **args, const char **envp,
31  const StringRef **redirects, unsigned secondsToWait,
32  unsigned memoryLimit, std::string *ErrMsg,
33  bool *ExecutionFailed) {
34  ProcessInfo PI;
35  if (Execute(PI, Program, args, envp, redirects, memoryLimit, ErrMsg)) {
36  if (ExecutionFailed)
37  *ExecutionFailed = false;
38  ProcessInfo Result = Wait(
39  PI, secondsToWait, /*WaitUntilTerminates=*/secondsToWait == 0, ErrMsg);
40  return Result.ReturnCode;
41  }
42 
43  if (ExecutionFailed)
44  *ExecutionFailed = true;
45 
46  return -1;
47 }
48 
49 ProcessInfo sys::ExecuteNoWait(StringRef Program, const char **args,
50  const char **envp, const StringRef **redirects,
51  unsigned memoryLimit, std::string *ErrMsg,
52  bool *ExecutionFailed) {
53  ProcessInfo PI;
54  if (ExecutionFailed)
55  *ExecutionFailed = false;
56  if (!Execute(PI, Program, args, envp, redirects, memoryLimit, ErrMsg))
57  if (ExecutionFailed)
58  *ExecutionFailed = true;
59 
60  return PI;
61 }
62 
63 // Include the platform-specific parts of this class.
64 #ifdef LLVM_ON_UNIX
65 #include "Unix/Program.inc"
66 #endif
67 #ifdef LLVM_ON_WIN32
68 #include "Windows/Program.inc"
69 #endif
ProcessInfo ExecuteNoWait(StringRef Program, const char **args, const char **env=nullptr, const StringRef **redirects=nullptr, unsigned memoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
Similar to ExecuteAndWait, but returns immediately.
Definition: Program.cpp:49
int ExecuteAndWait(StringRef Program, const char **args, const char **env=nullptr, const StringRef **redirects=nullptr, unsigned secondsToWait=0, unsigned memoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
This function executes the program using the arguments provided.
Definition: Program.cpp:30
static bool Execute(ProcessInfo &PI, StringRef Program, const char **args, const char **env, const StringRef **Redirects, unsigned memoryLimit, std::string *ErrMsg)
int ReturnCode
The return code, set after execution.
Definition: Program.h:51
This is the OS-specific separator for PATH like environment variables:
Definition: Program.h:35
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
ProcessInfo Wait(const ProcessInfo &PI, unsigned SecondsToWait, bool WaitUntilTerminates, std::string *ErrMsg=nullptr)
This function waits for the process specified by PI to finish.