1f4a2713aSLionel Sambuc //===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc //  This file implements the operating system Program concept.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "llvm/Support/Program.h"
15f4a2713aSLionel Sambuc #include "llvm/Config/config.h"
16*0a6a1f1dSLionel Sambuc #include <system_error>
17f4a2713aSLionel Sambuc using namespace llvm;
18f4a2713aSLionel Sambuc using namespace sys;
19f4a2713aSLionel Sambuc 
20f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
21f4a2713aSLionel Sambuc //=== WARNING: Implementation here must contain only TRULY operating system
22f4a2713aSLionel Sambuc //===          independent code.
23f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc static bool Execute(ProcessInfo &PI, StringRef Program, const char **args,
26f4a2713aSLionel Sambuc                     const char **env, const StringRef **Redirects,
27f4a2713aSLionel Sambuc                     unsigned memoryLimit, std::string *ErrMsg);
28f4a2713aSLionel Sambuc 
ExecuteAndWait(StringRef Program,const char ** args,const char ** envp,const StringRef ** redirects,unsigned secondsToWait,unsigned memoryLimit,std::string * ErrMsg,bool * ExecutionFailed)29f4a2713aSLionel Sambuc int sys::ExecuteAndWait(StringRef Program, const char **args, const char **envp,
30f4a2713aSLionel Sambuc                         const StringRef **redirects, unsigned secondsToWait,
31f4a2713aSLionel Sambuc                         unsigned memoryLimit, std::string *ErrMsg,
32f4a2713aSLionel Sambuc                         bool *ExecutionFailed) {
33f4a2713aSLionel Sambuc   ProcessInfo PI;
34f4a2713aSLionel Sambuc   if (Execute(PI, Program, args, envp, redirects, memoryLimit, ErrMsg)) {
35f4a2713aSLionel Sambuc     if (ExecutionFailed)
36f4a2713aSLionel Sambuc       *ExecutionFailed = false;
37*0a6a1f1dSLionel Sambuc     ProcessInfo Result = Wait(
38*0a6a1f1dSLionel Sambuc         PI, secondsToWait, /*WaitUntilTerminates=*/secondsToWait == 0, ErrMsg);
39f4a2713aSLionel Sambuc     return Result.ReturnCode;
40f4a2713aSLionel Sambuc   }
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc   if (ExecutionFailed)
43f4a2713aSLionel Sambuc     *ExecutionFailed = true;
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc   return -1;
46f4a2713aSLionel Sambuc }
47f4a2713aSLionel Sambuc 
ExecuteNoWait(StringRef Program,const char ** args,const char ** envp,const StringRef ** redirects,unsigned memoryLimit,std::string * ErrMsg,bool * ExecutionFailed)48f4a2713aSLionel Sambuc ProcessInfo sys::ExecuteNoWait(StringRef Program, const char **args,
49f4a2713aSLionel Sambuc                                const char **envp, const StringRef **redirects,
50f4a2713aSLionel Sambuc                                unsigned memoryLimit, std::string *ErrMsg,
51f4a2713aSLionel Sambuc                                bool *ExecutionFailed) {
52f4a2713aSLionel Sambuc   ProcessInfo PI;
53f4a2713aSLionel Sambuc   if (ExecutionFailed)
54f4a2713aSLionel Sambuc     *ExecutionFailed = false;
55f4a2713aSLionel Sambuc   if (!Execute(PI, Program, args, envp, redirects, memoryLimit, ErrMsg))
56f4a2713aSLionel Sambuc     if (ExecutionFailed)
57f4a2713aSLionel Sambuc       *ExecutionFailed = true;
58f4a2713aSLionel Sambuc 
59f4a2713aSLionel Sambuc   return PI;
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc // Include the platform-specific parts of this class.
63f4a2713aSLionel Sambuc #ifdef LLVM_ON_UNIX
64f4a2713aSLionel Sambuc #include "Unix/Program.inc"
65f4a2713aSLionel Sambuc #endif
66f4a2713aSLionel Sambuc #ifdef LLVM_ON_WIN32
67f4a2713aSLionel Sambuc #include "Windows/Program.inc"
68f4a2713aSLionel Sambuc #endif
69