blob: 165972610d8f09e25de578d28fc0726ac46dc0c7 [file] [log] [blame]
Reid Spencer76b83a12004-08-29 19:20:41 +00001//===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer76b83a12004-08-29 19:20:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer76b83a12004-08-29 19:20:41 +00008//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system Program concept.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "llvm/Support/Program.h"
Reid Spencer51ea06a2004-12-27 06:16:25 +000015#include "llvm/Config/config.h"
Michael J. Spencera2755f82011-12-13 23:16:49 +000016#include "llvm/Support/system_error.h"
Chris Lattnerd9e22a82010-04-18 03:33:55 +000017using namespace llvm;
Reid Spencer76b83a12004-08-29 19:20:41 +000018using namespace sys;
19
20//===----------------------------------------------------------------------===//
21//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukman10468d82005-04-21 22:55:34 +000022//=== independent code.
Reid Spencer76b83a12004-08-29 19:20:41 +000023//===----------------------------------------------------------------------===//
24
Reid Kleckner6e6a0f52013-06-13 15:27:17 +000025static bool Execute(void **Data, const Path &path, const char **args,
Rafael Espindolacb2eca02013-06-12 20:58:35 +000026 const char **env, const sys::Path **redirects,
27 unsigned memoryLimit, std::string *ErrMsg);
28
29static int Wait(void *&Data, const Path &path, unsigned secondsToWait,
30 std::string *ErrMsg);
31
Rafael Espindola6828fe42013-06-13 20:06:28 +000032int 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 Espindolacb2eca02013-06-12 20:58:35 +000055int sys::ExecuteAndWait(const Path &path, const char **args, const char **envp,
56 const Path **redirects, unsigned secondsToWait,
57 unsigned memoryLimit, std::string *ErrMsg,
Chad Rosier654190a2013-03-26 23:35:00 +000058 bool *ExecutionFailed) {
Aaron Ballman54e9bd22013-06-13 14:39:07 +000059 void *Data = 0;
Reid Kleckner6e6a0f52013-06-13 15:27:17 +000060 if (Execute(&Data, path, args, envp, redirects, memoryLimit, ErrMsg)) {
Chad Rosier654190a2013-03-26 23:35:00 +000061 if (ExecutionFailed) *ExecutionFailed = false;
Rafael Espindolacb2eca02013-06-12 20:58:35 +000062 return Wait(Data, path, secondsToWait, ErrMsg);
Chad Rosier654190a2013-03-26 23:35:00 +000063 }
64 if (ExecutionFailed) *ExecutionFailed = true;
65 return -1;
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +000066}
67
Rafael Espindolacb2eca02013-06-12 20:58:35 +000068void sys::ExecuteNoWait(const Path &path, const char **args, const char **envp,
69 const Path **redirects, unsigned memoryLimit,
70 std::string *ErrMsg) {
Reid Kleckner6e6a0f52013-06-13 15:27:17 +000071 Execute(/*Data*/ 0, path, args, envp, redirects, memoryLimit, ErrMsg);
Mikhail Glushenkov36cb8322009-07-18 21:43:12 +000072}
73
Reid Spencer76b83a12004-08-29 19:20:41 +000074// Include the platform-specific parts of this class.
Reid Spencer51ea06a2004-12-27 06:16:25 +000075#ifdef LLVM_ON_UNIX
Reid Spencerc892a0d2005-01-09 23:29:00 +000076#include "Unix/Program.inc"
Reid Spencer51ea06a2004-12-27 06:16:25 +000077#endif
78#ifdef LLVM_ON_WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +000079#include "Windows/Program.inc"
Reid Spencer51ea06a2004-12-27 06:16:25 +000080#endif