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 
ExecuteAndWait(StringRef Program,const char ** args,const char ** envp,const StringRef ** redirects,unsigned secondsToWait,unsigned memoryLimit,std::string * ErrMsg,bool * ExecutionFailed)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 
ExecuteNoWait(StringRef Program,const char ** args,const char ** envp,const StringRef ** redirects,unsigned memoryLimit,std::string * ErrMsg,bool * ExecutionFailed)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
70