xref: /freebsd/contrib/llvm-project/llvm/lib/Support/Process.cpp (revision 77013d11e6483b970af25e13c9b892075742f7e5)
1 //===-- Process.cpp - Implement OS Process 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 Process concept.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/Process.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Config/llvm-config.h"
18 #include "llvm/Support/CrashRecoveryContext.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Program.h"
22 
23 #include <stdlib.h> // for _Exit
24 
25 using namespace llvm;
26 using namespace sys;
27 
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only TRULY operating system
30 //===          independent code.
31 //===----------------------------------------------------------------------===//
32 
33 Optional<std::string>
34 Process::FindInEnvPath(StringRef EnvName, StringRef FileName, char Separator) {
35   return FindInEnvPath(EnvName, FileName, {}, Separator);
36 }
37 
38 Optional<std::string> Process::FindInEnvPath(StringRef EnvName,
39                                              StringRef FileName,
40                                              ArrayRef<std::string> IgnoreList,
41                                              char Separator) {
42   assert(!path::is_absolute(FileName));
43   Optional<std::string> FoundPath;
44   Optional<std::string> OptPath = Process::GetEnv(EnvName);
45   if (!OptPath.hasValue())
46     return FoundPath;
47 
48   const char EnvPathSeparatorStr[] = {Separator, '\0'};
49   SmallVector<StringRef, 8> Dirs;
50   SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
51 
52   for (StringRef Dir : Dirs) {
53     if (Dir.empty())
54       continue;
55 
56     if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); }))
57       continue;
58 
59     SmallString<128> FilePath(Dir);
60     path::append(FilePath, FileName);
61     if (fs::exists(Twine(FilePath))) {
62       FoundPath = std::string(FilePath.str());
63       break;
64     }
65   }
66 
67   return FoundPath;
68 }
69 
70 
71 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
72 
73 #define ALLCOLORS(FGBG,BOLD) {\
74     COLOR(FGBG, "0", BOLD),\
75     COLOR(FGBG, "1", BOLD),\
76     COLOR(FGBG, "2", BOLD),\
77     COLOR(FGBG, "3", BOLD),\
78     COLOR(FGBG, "4", BOLD),\
79     COLOR(FGBG, "5", BOLD),\
80     COLOR(FGBG, "6", BOLD),\
81     COLOR(FGBG, "7", BOLD)\
82   }
83 
84 static const char colorcodes[2][2][8][10] = {
85  { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
86  { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
87 };
88 
89 // A CMake option controls wheter we emit core dumps by default. An application
90 // may disable core dumps by calling Process::PreventCoreFiles().
91 static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
92 
93 bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; }
94 
95 LLVM_ATTRIBUTE_NORETURN
96 void Process::Exit(int RetCode, bool NoCleanup) {
97   if (CrashRecoveryContext *CRC = CrashRecoveryContext::GetCurrent())
98     CRC->HandleExit(RetCode);
99 
100   if (NoCleanup)
101     _Exit(RetCode);
102   else
103     ::exit(RetCode);
104 }
105 
106 // Include the platform-specific parts of this class.
107 #ifdef LLVM_ON_UNIX
108 #include "Unix/Process.inc"
109 #endif
110 #ifdef _WIN32
111 #include "Windows/Process.inc"
112 #endif
113