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