1 //===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements the operating system Program concept.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/Program.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Config/llvm-config.h"
16 #include <system_error>
17 using namespace llvm;
18 using namespace sys;
19 
20 //===----------------------------------------------------------------------===//
21 //=== WARNING: Implementation here must contain only TRULY operating system
22 //===          independent code.
23 //===----------------------------------------------------------------------===//
24 
25 static bool Execute(ProcessInfo &PI, StringRef Program,
26                     ArrayRef<StringRef> Args, Optional<ArrayRef<StringRef>> Env,
27                     ArrayRef<Optional<StringRef>> Redirects,
28                     unsigned MemoryLimit, std::string *ErrMsg);
29 
ExecuteAndWait(StringRef Program,ArrayRef<StringRef> Args,Optional<ArrayRef<StringRef>> Env,ArrayRef<Optional<StringRef>> Redirects,unsigned SecondsToWait,unsigned MemoryLimit,std::string * ErrMsg,bool * ExecutionFailed)30 int sys::ExecuteAndWait(StringRef Program, ArrayRef<StringRef> Args,
31                         Optional<ArrayRef<StringRef>> Env,
32                         ArrayRef<Optional<StringRef>> Redirects,
33                         unsigned SecondsToWait, unsigned MemoryLimit,
34                         std::string *ErrMsg, bool *ExecutionFailed) {
35   assert(Redirects.empty() || Redirects.size() == 3);
36   ProcessInfo PI;
37   if (Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg)) {
38     if (ExecutionFailed)
39       *ExecutionFailed = false;
40     ProcessInfo Result = Wait(
41         PI, SecondsToWait, /*WaitUntilTerminates=*/SecondsToWait == 0, ErrMsg);
42     return Result.ReturnCode;
43   }
44 
45   if (ExecutionFailed)
46     *ExecutionFailed = true;
47 
48   return -1;
49 }
50 
ExecuteNoWait(StringRef Program,ArrayRef<StringRef> Args,Optional<ArrayRef<StringRef>> Env,ArrayRef<Optional<StringRef>> Redirects,unsigned MemoryLimit,std::string * ErrMsg,bool * ExecutionFailed)51 ProcessInfo sys::ExecuteNoWait(StringRef Program, ArrayRef<StringRef> Args,
52                                Optional<ArrayRef<StringRef>> Env,
53                                ArrayRef<Optional<StringRef>> Redirects,
54                                unsigned MemoryLimit, std::string *ErrMsg,
55                                bool *ExecutionFailed) {
56   assert(Redirects.empty() || Redirects.size() == 3);
57   ProcessInfo PI;
58   if (ExecutionFailed)
59     *ExecutionFailed = false;
60   if (!Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg))
61     if (ExecutionFailed)
62       *ExecutionFailed = true;
63 
64   return PI;
65 }
66 
commandLineFitsWithinSystemLimits(StringRef Program,ArrayRef<const char * > Args)67 bool sys::commandLineFitsWithinSystemLimits(StringRef Program,
68                                             ArrayRef<const char *> Args) {
69   SmallVector<StringRef, 8> StringRefArgs;
70   StringRefArgs.reserve(Args.size());
71   for (const char *A : Args)
72     StringRefArgs.emplace_back(A);
73   return commandLineFitsWithinSystemLimits(Program, StringRefArgs);
74 }
75 
76 // Include the platform-specific parts of this class.
77 #ifdef LLVM_ON_UNIX
78 #include "Unix/Program.inc"
79 #endif
80 #ifdef _WIN32
81 #include "Windows/Program.inc"
82 #endif
83