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" |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 16 | #include <system_error> |
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 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 25 | static bool Execute(ProcessInfo &PI, StringRef Program, const char **args, |
Rafael Espindola | 7c1023a | 2013-06-13 20:25:38 +0000 | [diff] [blame] | 26 | const char **env, const StringRef **Redirects, |
Rafael Espindola | b0a5c96 | 2013-06-14 19:38:45 +0000 | [diff] [blame] | 27 | unsigned memoryLimit, std::string *ErrMsg); |
Rafael Espindola | 6828fe4 | 2013-06-13 20:06:28 +0000 | [diff] [blame] | 28 | |
Rafael Espindola | 7c1023a | 2013-06-13 20:25:38 +0000 | [diff] [blame] | 29 | int sys::ExecuteAndWait(StringRef Program, const char **args, const char **envp, |
| 30 | const StringRef **redirects, unsigned secondsToWait, |
Rafael Espindola | cb2eca0 | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 31 | unsigned memoryLimit, std::string *ErrMsg, |
Chad Rosier | 654190a | 2013-03-26 23:35:00 +0000 | [diff] [blame] | 32 | bool *ExecutionFailed) { |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 33 | ProcessInfo PI; |
| 34 | if (Execute(PI, Program, args, envp, redirects, memoryLimit, ErrMsg)) { |
| 35 | if (ExecutionFailed) |
| 36 | *ExecutionFailed = false; |
Peter Collingbourne | ec1aaca | 2014-05-31 01:36:02 +0000 | [diff] [blame] | 37 | ProcessInfo Result = Wait( |
| 38 | PI, secondsToWait, /*WaitUntilTerminates=*/secondsToWait == 0, ErrMsg); |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 39 | return Result.ReturnCode; |
Chad Rosier | 654190a | 2013-03-26 23:35:00 +0000 | [diff] [blame] | 40 | } |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 41 | |
| 42 | if (ExecutionFailed) |
| 43 | *ExecutionFailed = true; |
| 44 | |
Chad Rosier | 654190a | 2013-03-26 23:35:00 +0000 | [diff] [blame] | 45 | return -1; |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 48 | ProcessInfo sys::ExecuteNoWait(StringRef Program, const char **args, |
| 49 | const char **envp, const StringRef **redirects, |
| 50 | unsigned memoryLimit, std::string *ErrMsg, |
| 51 | bool *ExecutionFailed) { |
| 52 | ProcessInfo PI; |
| 53 | if (ExecutionFailed) |
| 54 | *ExecutionFailed = false; |
| 55 | if (!Execute(PI, Program, args, envp, redirects, memoryLimit, ErrMsg)) |
| 56 | if (ExecutionFailed) |
| 57 | *ExecutionFailed = true; |
| 58 | |
| 59 | return PI; |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 62 | // Include the platform-specific parts of this class. |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 63 | #ifdef LLVM_ON_UNIX |
Reid Spencer | c892a0d | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 64 | #include "Unix/Program.inc" |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 65 | #endif |
| 66 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 67 | #include "Windows/Program.inc" |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 68 | #endif |