Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 1 | //===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===// |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system Program concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Program.h" |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Michael J. Spencer | a2755f8 | 2011-12-13 23:16:49 +0000 | [diff] [blame] | 16 | #include "llvm/Support/system_error.h" |
Chris Lattner | d9e22a8 | 2010-04-18 03:33:55 +0000 | [diff] [blame] | 17 | using namespace llvm; |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 18 | using namespace sys; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 22 | //=== independent code. |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Reid Kleckner | 6e6a0f5 | 2013-06-13 15:27:17 +0000 | [diff] [blame] | 25 | static bool Execute(void **Data, const Path &path, const char **args, |
Rafael Espindola | cb2eca0 | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 26 | const char **env, const sys::Path **redirects, |
| 27 | unsigned memoryLimit, std::string *ErrMsg); |
| 28 | |
| 29 | static int Wait(void *&Data, const Path &path, unsigned secondsToWait, |
| 30 | std::string *ErrMsg); |
| 31 | |
Rafael Espindola | 6828fe4 | 2013-06-13 20:06:28 +0000 | [diff] [blame] | 32 | int sys::ExecuteAndWait(StringRef path, const char **args, const char **env, |
| 33 | const StringRef **redirects, unsigned secondsToWait, |
| 34 | unsigned memoryLimit, std::string *ErrMsg, |
| 35 | bool *ExecutionFailed) { |
| 36 | Path P(path); |
| 37 | if (!redirects) |
| 38 | return ExecuteAndWait(P, args, env, 0, secondsToWait, memoryLimit, ErrMsg, |
| 39 | ExecutionFailed); |
| 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 ExecuteAndWait(P, args, env, IOP, secondsToWait, memoryLimit, ErrMsg, |
| 52 | ExecutionFailed); |
| 53 | } |
| 54 | |
Rafael Espindola | cb2eca0 | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 55 | int sys::ExecuteAndWait(const Path &path, const char **args, const char **envp, |
| 56 | const Path **redirects, unsigned secondsToWait, |
| 57 | unsigned memoryLimit, std::string *ErrMsg, |
Chad Rosier | 654190a | 2013-03-26 23:35:00 +0000 | [diff] [blame] | 58 | bool *ExecutionFailed) { |
Aaron Ballman | 54e9bd2 | 2013-06-13 14:39:07 +0000 | [diff] [blame] | 59 | void *Data = 0; |
Reid Kleckner | 6e6a0f5 | 2013-06-13 15:27:17 +0000 | [diff] [blame] | 60 | if (Execute(&Data, path, args, envp, redirects, memoryLimit, ErrMsg)) { |
Chad Rosier | 654190a | 2013-03-26 23:35:00 +0000 | [diff] [blame] | 61 | if (ExecutionFailed) *ExecutionFailed = false; |
Rafael Espindola | cb2eca0 | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 62 | return Wait(Data, path, secondsToWait, ErrMsg); |
Chad Rosier | 654190a | 2013-03-26 23:35:00 +0000 | [diff] [blame] | 63 | } |
| 64 | if (ExecutionFailed) *ExecutionFailed = true; |
| 65 | return -1; |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Rafael Espindola | cb2eca0 | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 68 | void sys::ExecuteNoWait(const Path &path, const char **args, const char **envp, |
| 69 | const Path **redirects, unsigned memoryLimit, |
| 70 | std::string *ErrMsg) { |
Reid Kleckner | 6e6a0f5 | 2013-06-13 15:27:17 +0000 | [diff] [blame] | 71 | Execute(/*Data*/ 0, path, args, envp, redirects, memoryLimit, ErrMsg); |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 74 | // Include the platform-specific parts of this class. |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 75 | #ifdef LLVM_ON_UNIX |
Reid Spencer | c892a0d | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 76 | #include "Unix/Program.inc" |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 77 | #endif |
| 78 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 79 | #include "Windows/Program.inc" |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 80 | #endif |