1 //===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the operating system Program concept.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Program.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Config/llvm-config.h"
16 #include <system_error>
20 //===----------------------------------------------------------------------===//
21 //=== WARNING: Implementation here must contain only TRULY operating system
22 //=== independent code.
23 //===----------------------------------------------------------------------===//
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
);
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);
37 if (Execute(PI
, Program
, Args
, Env
, Redirects
, MemoryLimit
, ErrMsg
)) {
39 *ExecutionFailed
= false;
40 ProcessInfo Result
= Wait(
41 PI
, SecondsToWait
, /*WaitUntilTerminates=*/SecondsToWait
== 0, ErrMsg
);
42 return Result
.ReturnCode
;
46 *ExecutionFailed
= true;
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);
59 *ExecutionFailed
= false;
60 if (!Execute(PI
, Program
, Args
, Env
, Redirects
, MemoryLimit
, ErrMsg
))
62 *ExecutionFailed
= true;
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
);
76 // Include the platform-specific parts of this class.
78 #include "Unix/Program.inc"
81 #include "Windows/Program.inc"