Program.cpp revision a29ece1693ff16ff991e184f6e073c9fb30fcb4b
1//===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This header file implements the operating system Program concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Program.h"
15#include "llvm/Config/config.h"
16#include "llvm/Support/system_error.h"
17using namespace llvm;
18using namespace sys;
19
20//===----------------------------------------------------------------------===//
21//=== WARNING: Implementation here must contain only TRULY operating system
22//===          independent code.
23//===----------------------------------------------------------------------===//
24
25static bool Execute(void **Data, const Path &path, const char **args,
26                    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
32int 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
55int sys::ExecuteAndWait(const Path &path, const char **args, const char **envp,
56                        const Path **redirects, unsigned secondsToWait,
57                        unsigned memoryLimit, std::string *ErrMsg,
58                        bool *ExecutionFailed) {
59  void *Data = 0;
60  if (Execute(&Data, path, args, envp, redirects, memoryLimit, ErrMsg)) {
61    if (ExecutionFailed) *ExecutionFailed = false;
62    return Wait(Data, path, secondsToWait, ErrMsg);
63  }
64  if (ExecutionFailed) *ExecutionFailed = true;
65  return -1;
66}
67
68void sys::ExecuteNoWait(const Path &path, const char **args, const char **envp,
69                        const Path **redirects, unsigned memoryLimit,
70                        std::string *ErrMsg) {
71  Execute(/*Data*/ 0, path, args, envp, redirects, memoryLimit, ErrMsg);
72}
73
74// Include the platform-specific parts of this class.
75#ifdef LLVM_ON_UNIX
76#include "Unix/Program.inc"
77#endif
78#ifdef LLVM_ON_WIN32
79#include "Windows/Program.inc"
80#endif
81