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 header file implements the operating system Program concept. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Support/Program.h" 15 #include "llvm/Support/PathV1.h" 16 #include "llvm/Config/config.h" 17 #include "llvm/Support/system_error.h" 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(void **Data, const Path &path, const char **args, 27 const char **env, const sys::Path **redirects, 28 unsigned memoryLimit, std::string *ErrMsg); 29 30 static int Wait(void *&Data, const Path &path, unsigned secondsToWait, 31 std::string *ErrMsg); 32 33 34 static bool Execute(void **Data, StringRef Program, const char **args, 35 const char **env, const StringRef **Redirects, 36 unsigned memoryLimit, std::string *ErrMsg) { 37 Path P(Program); 38 if (!Redirects) 39 return Execute(Data, P, args, env, 0, memoryLimit, ErrMsg); 40 Path IO[3]; 41 const Path *IOP[3]; 42 for (int I = 0; I < 3; ++I) { 43 if (Redirects[I]) { 44 IO[I] = *Redirects[I]; 45 IOP[I] = &IO[I]; 46 } else { 47 IOP[I] = 0; 48 } 49 } 50 51 return Execute(Data, P, args, env, IOP, memoryLimit, ErrMsg); 52 } 53 54 static int Wait(void *&Data, StringRef Program, unsigned secondsToWait, 55 std::string *ErrMsg) { 56 Path P(Program); 57 return Wait(Data, P, secondsToWait, ErrMsg); 58 } 59 60 int sys::ExecuteAndWait(StringRef Program, const char **args, const char **envp, 61 const StringRef **redirects, unsigned secondsToWait, 62 unsigned memoryLimit, std::string *ErrMsg, 63 bool *ExecutionFailed) { 64 void *Data = 0; 65 if (Execute(&Data, Program, args, envp, redirects, memoryLimit, ErrMsg)) { 66 if (ExecutionFailed) *ExecutionFailed = false; 67 return Wait(Data, Program, secondsToWait, ErrMsg); 68 } 69 if (ExecutionFailed) *ExecutionFailed = true; 70 return -1; 71 } 72 73 void sys::ExecuteNoWait(StringRef Program, const char **args, const char **envp, 74 const StringRef **redirects, unsigned memoryLimit, 75 std::string *ErrMsg) { 76 Execute(/*Data*/ 0, Program, args, envp, redirects, memoryLimit, ErrMsg); 77 } 78 79 // Include the platform-specific parts of this class. 80 #ifdef LLVM_ON_UNIX 81 #include "Unix/Program.inc" 82 #endif 83 #ifdef LLVM_ON_WIN32 84 #include "Windows/Program.inc" 85 #endif 86