Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 1 | //===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===// |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 6 | // |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Hans Wennborg | cfe341f | 2014-06-20 01:36:00 +0000 | [diff] [blame] | 9 | // This file implements the operating system Process concept. |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Process.h" |
Zachary Turner | fa0ca6c | 2017-10-11 20:12:09 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 16 | #include "llvm/Config/llvm-config.h" |
Reid Kleckner | 5313193 | 2018-08-20 16:49:54 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Program.h" |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 21 | |
Chandler Carruth | 5473dfb | 2012-12-31 11:45:20 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 23 | using namespace sys; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 27 | //=== independent code. |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Zachary Turner | fa0ca6c | 2017-10-11 20:12:09 +0000 | [diff] [blame] | 30 | Optional<std::string> Process::FindInEnvPath(StringRef EnvName, |
| 31 | StringRef FileName) { |
| 32 | return FindInEnvPath(EnvName, FileName, {}); |
| 33 | } |
| 34 | |
| 35 | Optional<std::string> Process::FindInEnvPath(StringRef EnvName, |
| 36 | StringRef FileName, |
| 37 | ArrayRef<std::string> IgnoreList) { |
Nico Weber | 7d831a5 | 2015-04-24 22:18:46 +0000 | [diff] [blame] | 38 | assert(!path::is_absolute(FileName)); |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 39 | Optional<std::string> FoundPath; |
| 40 | Optional<std::string> OptPath = Process::GetEnv(EnvName); |
| 41 | if (!OptPath.hasValue()) |
| 42 | return FoundPath; |
| 43 | |
| 44 | const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'}; |
| 45 | SmallVector<StringRef, 8> Dirs; |
| 46 | SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr); |
| 47 | |
Zachary Turner | fa0ca6c | 2017-10-11 20:12:09 +0000 | [diff] [blame] | 48 | for (StringRef Dir : Dirs) { |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 49 | if (Dir.empty()) |
| 50 | continue; |
| 51 | |
Zachary Turner | fa0ca6c | 2017-10-11 20:12:09 +0000 | [diff] [blame] | 52 | if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); })) |
| 53 | continue; |
| 54 | |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 55 | SmallString<128> FilePath(Dir); |
| 56 | path::append(FilePath, FileName); |
| 57 | if (fs::exists(Twine(FilePath))) { |
| 58 | FoundPath = FilePath.str(); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return FoundPath; |
| 64 | } |
| 65 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 66 | |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 67 | #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m" |
| 68 | |
| 69 | #define ALLCOLORS(FGBG,BOLD) {\ |
| 70 | COLOR(FGBG, "0", BOLD),\ |
| 71 | COLOR(FGBG, "1", BOLD),\ |
| 72 | COLOR(FGBG, "2", BOLD),\ |
| 73 | COLOR(FGBG, "3", BOLD),\ |
| 74 | COLOR(FGBG, "4", BOLD),\ |
| 75 | COLOR(FGBG, "5", BOLD),\ |
| 76 | COLOR(FGBG, "6", BOLD),\ |
| 77 | COLOR(FGBG, "7", BOLD)\ |
| 78 | } |
| 79 | |
| 80 | static const char colorcodes[2][2][8][10] = { |
| 81 | { ALLCOLORS("3",""), ALLCOLORS("3","1;") }, |
| 82 | { ALLCOLORS("4",""), ALLCOLORS("4","1;") } |
| 83 | }; |
| 84 | |
Reid Kleckner | 7a973fb | 2018-08-23 22:58:56 +0000 | [diff] [blame] | 85 | // A CMake option controls wheter we emit core dumps by default. An application |
| 86 | // may disable core dumps by calling Process::PreventCoreFiles(). |
| 87 | static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS; |
Leny Kholodov | 1b73e66 | 2016-05-04 16:56:51 +0000 | [diff] [blame] | 88 | |
Reid Kleckner | 7a973fb | 2018-08-23 22:58:56 +0000 | [diff] [blame] | 89 | bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; } |
Leny Kholodov | 1b73e66 | 2016-05-04 16:56:51 +0000 | [diff] [blame] | 90 | |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 91 | // Include the platform-specific parts of this class. |
Reid Spencer | 97d4a17 | 2004-12-27 06:15:29 +0000 | [diff] [blame] | 92 | #ifdef LLVM_ON_UNIX |
Reid Spencer | c892a0d | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 93 | #include "Unix/Process.inc" |
Reid Spencer | 97d4a17 | 2004-12-27 06:15:29 +0000 | [diff] [blame] | 94 | #endif |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 95 | #ifdef _WIN32 |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 96 | #include "Windows/Process.inc" |
Reid Spencer | 97d4a17 | 2004-12-27 06:15:29 +0000 | [diff] [blame] | 97 | #endif |