2004-08-30 03:20:41 +08:00
|
|
|
//===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===//
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2004-08-30 03:20:41 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2004-08-30 03:20:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-06-20 09:36:00 +08:00
|
|
|
// This file implements the operating system Program concept.
|
2004-08-30 03:20:41 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2015-03-24 02:07:13 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-04-30 22:59:11 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2010-04-18 11:33:55 +08:00
|
|
|
using namespace llvm;
|
2004-08-30 03:20:41 +08:00
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only TRULY operating system
|
2005-04-22 06:55:34 +08:00
|
|
|
//=== independent code.
|
2004-08-30 03:20:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-09-07 00:28:33 +08:00
|
|
|
static bool Execute(ProcessInfo &PI, StringRef Program, const char **Args,
|
2017-09-14 01:03:37 +08:00
|
|
|
const char **Env, ArrayRef<Optional<StringRef>> Redirects,
|
2017-09-07 00:28:33 +08:00
|
|
|
unsigned MemoryLimit, std::string *ErrMsg);
|
2013-06-14 04:06:28 +08:00
|
|
|
|
2017-09-07 00:28:33 +08:00
|
|
|
int sys::ExecuteAndWait(StringRef Program, const char **Args, const char **Envp,
|
2017-09-14 01:03:37 +08:00
|
|
|
ArrayRef<Optional<StringRef>> Redirects,
|
|
|
|
unsigned SecondsToWait, unsigned MemoryLimit,
|
|
|
|
std::string *ErrMsg, bool *ExecutionFailed) {
|
|
|
|
assert(Redirects.empty() || Redirects.size() == 3);
|
2013-10-01 22:28:18 +08:00
|
|
|
ProcessInfo PI;
|
2017-09-07 00:28:33 +08:00
|
|
|
if (Execute(PI, Program, Args, Envp, Redirects, MemoryLimit, ErrMsg)) {
|
2013-10-01 22:28:18 +08:00
|
|
|
if (ExecutionFailed)
|
|
|
|
*ExecutionFailed = false;
|
2014-05-31 09:36:02 +08:00
|
|
|
ProcessInfo Result = Wait(
|
2017-09-07 00:28:33 +08:00
|
|
|
PI, SecondsToWait, /*WaitUntilTerminates=*/SecondsToWait == 0, ErrMsg);
|
2013-10-01 22:28:18 +08:00
|
|
|
return Result.ReturnCode;
|
2013-03-27 07:35:00 +08:00
|
|
|
}
|
2013-10-01 22:28:18 +08:00
|
|
|
|
|
|
|
if (ExecutionFailed)
|
|
|
|
*ExecutionFailed = true;
|
|
|
|
|
2013-03-27 07:35:00 +08:00
|
|
|
return -1;
|
2009-07-19 05:43:12 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 00:28:33 +08:00
|
|
|
ProcessInfo sys::ExecuteNoWait(StringRef Program, const char **Args,
|
2017-09-14 01:03:37 +08:00
|
|
|
const char **Envp,
|
|
|
|
ArrayRef<Optional<StringRef>> Redirects,
|
2017-09-07 00:28:33 +08:00
|
|
|
unsigned MemoryLimit, std::string *ErrMsg,
|
2013-10-01 22:28:18 +08:00
|
|
|
bool *ExecutionFailed) {
|
2017-09-14 01:03:37 +08:00
|
|
|
assert(Redirects.empty() || Redirects.size() == 3);
|
2013-10-01 22:28:18 +08:00
|
|
|
ProcessInfo PI;
|
|
|
|
if (ExecutionFailed)
|
|
|
|
*ExecutionFailed = false;
|
2017-09-07 00:28:33 +08:00
|
|
|
if (!Execute(PI, Program, Args, Envp, Redirects, MemoryLimit, ErrMsg))
|
2013-10-01 22:28:18 +08:00
|
|
|
if (ExecutionFailed)
|
|
|
|
*ExecutionFailed = true;
|
|
|
|
|
|
|
|
return PI;
|
2009-07-19 05:43:12 +08:00
|
|
|
}
|
|
|
|
|
2004-08-30 03:20:41 +08:00
|
|
|
// Include the platform-specific parts of this class.
|
2004-12-27 14:16:25 +08:00
|
|
|
#ifdef LLVM_ON_UNIX
|
2005-01-10 07:29:00 +08:00
|
|
|
#include "Unix/Program.inc"
|
2004-12-27 14:16:25 +08:00
|
|
|
#endif
|
2018-04-29 08:45:03 +08:00
|
|
|
#ifdef _WIN32
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "Windows/Program.inc"
|
2004-12-27 14:16:25 +08:00
|
|
|
#endif
|