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 | // |
Hans Wennborg | cfe341f | 2014-06-20 01:36:00 +0000 | [diff] [blame] | 10 | // This file implements the operating system Program concept. |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Program.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringRef.h" |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 17 | #include <system_error> |
Chris Lattner | d9e22a8 | 2010-04-18 03:33:55 +0000 | [diff] [blame] | 18 | using namespace llvm; |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 19 | using namespace sys; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 23 | //=== independent code. |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Alexander Kornienko | 3ad84ee | 2017-09-06 16:28:33 +0000 | [diff] [blame] | 26 | static bool Execute(ProcessInfo &PI, StringRef Program, const char **Args, |
Alexander Kornienko | 208eecd | 2017-09-13 17:03:37 +0000 | [diff] [blame] | 27 | const char **Env, ArrayRef<Optional<StringRef>> Redirects, |
Alexander Kornienko | 3ad84ee | 2017-09-06 16:28:33 +0000 | [diff] [blame] | 28 | unsigned MemoryLimit, std::string *ErrMsg); |
Rafael Espindola | 6828fe4 | 2013-06-13 20:06:28 +0000 | [diff] [blame] | 29 | |
Alexander Kornienko | 3ad84ee | 2017-09-06 16:28:33 +0000 | [diff] [blame] | 30 | int sys::ExecuteAndWait(StringRef Program, const char **Args, const char **Envp, |
Alexander Kornienko | 208eecd | 2017-09-13 17:03:37 +0000 | [diff] [blame] | 31 | ArrayRef<Optional<StringRef>> Redirects, |
| 32 | unsigned SecondsToWait, unsigned MemoryLimit, |
| 33 | std::string *ErrMsg, bool *ExecutionFailed) { |
| 34 | assert(Redirects.empty() || Redirects.size() == 3); |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 35 | ProcessInfo PI; |
Alexander Kornienko | 3ad84ee | 2017-09-06 16:28:33 +0000 | [diff] [blame] | 36 | if (Execute(PI, Program, Args, Envp, Redirects, MemoryLimit, ErrMsg)) { |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 37 | if (ExecutionFailed) |
| 38 | *ExecutionFailed = false; |
Peter Collingbourne | ec1aaca | 2014-05-31 01:36:02 +0000 | [diff] [blame] | 39 | ProcessInfo Result = Wait( |
Alexander Kornienko | 3ad84ee | 2017-09-06 16:28:33 +0000 | [diff] [blame] | 40 | PI, SecondsToWait, /*WaitUntilTerminates=*/SecondsToWait == 0, ErrMsg); |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 41 | return Result.ReturnCode; |
Chad Rosier | 654190a | 2013-03-26 23:35:00 +0000 | [diff] [blame] | 42 | } |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 43 | |
| 44 | if (ExecutionFailed) |
| 45 | *ExecutionFailed = true; |
| 46 | |
Chad Rosier | 654190a | 2013-03-26 23:35:00 +0000 | [diff] [blame] | 47 | return -1; |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Alexander Kornienko | 3ad84ee | 2017-09-06 16:28:33 +0000 | [diff] [blame] | 50 | ProcessInfo sys::ExecuteNoWait(StringRef Program, const char **Args, |
Alexander Kornienko | 208eecd | 2017-09-13 17:03:37 +0000 | [diff] [blame] | 51 | const char **Envp, |
| 52 | ArrayRef<Optional<StringRef>> Redirects, |
Alexander Kornienko | 3ad84ee | 2017-09-06 16:28:33 +0000 | [diff] [blame] | 53 | unsigned MemoryLimit, std::string *ErrMsg, |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 54 | bool *ExecutionFailed) { |
Alexander Kornienko | 208eecd | 2017-09-13 17:03:37 +0000 | [diff] [blame] | 55 | assert(Redirects.empty() || Redirects.size() == 3); |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 56 | ProcessInfo PI; |
| 57 | if (ExecutionFailed) |
| 58 | *ExecutionFailed = false; |
Alexander Kornienko | 3ad84ee | 2017-09-06 16:28:33 +0000 | [diff] [blame] | 59 | if (!Execute(PI, Program, Args, Envp, Redirects, MemoryLimit, ErrMsg)) |
Tareq A. Siraj | d88b983 | 2013-10-01 14:28:18 +0000 | [diff] [blame] | 60 | if (ExecutionFailed) |
| 61 | *ExecutionFailed = true; |
| 62 | |
| 63 | return PI; |
Mikhail Glushenkov | 36cb832 | 2009-07-18 21:43:12 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Reid Spencer | 76b83a1 | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 66 | // Include the platform-specific parts of this class. |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 67 | #ifdef LLVM_ON_UNIX |
Reid Spencer | c892a0d | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 68 | #include "Unix/Program.inc" |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 69 | #endif |
| 70 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 71 | #include "Windows/Program.inc" |
Reid Spencer | 51ea06a | 2004-12-27 06:16:25 +0000 | [diff] [blame] | 72 | #endif |