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 | // |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Hans Wennborg | cfe341f | 2014-06-20 01:36:00 +0000 | [diff] [blame] | 10 | // This file implements the operating system Process concept. |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame^] | 14 | #include "llvm/Support/Process.h" |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 97d4a17 | 2004-12-27 06:15:29 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Program.h" |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 20 | |
Chandler Carruth | 5473dfb | 2012-12-31 11:45:20 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 22 | using namespace sys; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 26 | //=== independent code. |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 29 | Optional<std::string> Process::FindInEnvPath(const std::string& EnvName, |
| 30 | const std::string& FileName) |
| 31 | { |
Nico Weber | 7d831a5 | 2015-04-24 22:18:46 +0000 | [diff] [blame] | 32 | assert(!path::is_absolute(FileName)); |
Ehsan Akhgari | 33d1ae5 | 2014-06-30 19:54:20 +0000 | [diff] [blame] | 33 | Optional<std::string> FoundPath; |
| 34 | Optional<std::string> OptPath = Process::GetEnv(EnvName); |
| 35 | if (!OptPath.hasValue()) |
| 36 | return FoundPath; |
| 37 | |
| 38 | const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'}; |
| 39 | SmallVector<StringRef, 8> Dirs; |
| 40 | SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr); |
| 41 | |
| 42 | for (const auto &Dir : Dirs) { |
| 43 | if (Dir.empty()) |
| 44 | continue; |
| 45 | |
| 46 | SmallString<128> FilePath(Dir); |
| 47 | path::append(FilePath, FileName); |
| 48 | if (fs::exists(Twine(FilePath))) { |
| 49 | FoundPath = FilePath.str(); |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return FoundPath; |
| 55 | } |
| 56 | |
Chandler Carruth | ef7f968 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 57 | |
Nico Rieck | 92d649a | 2013-09-11 00:36:48 +0000 | [diff] [blame] | 58 | #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m" |
| 59 | |
| 60 | #define ALLCOLORS(FGBG,BOLD) {\ |
| 61 | COLOR(FGBG, "0", BOLD),\ |
| 62 | COLOR(FGBG, "1", BOLD),\ |
| 63 | COLOR(FGBG, "2", BOLD),\ |
| 64 | COLOR(FGBG, "3", BOLD),\ |
| 65 | COLOR(FGBG, "4", BOLD),\ |
| 66 | COLOR(FGBG, "5", BOLD),\ |
| 67 | COLOR(FGBG, "6", BOLD),\ |
| 68 | COLOR(FGBG, "7", BOLD)\ |
| 69 | } |
| 70 | |
| 71 | static const char colorcodes[2][2][8][10] = { |
| 72 | { ALLCOLORS("3",""), ALLCOLORS("3","1;") }, |
| 73 | { ALLCOLORS("4",""), ALLCOLORS("4","1;") } |
| 74 | }; |
| 75 | |
Leny Kholodov | 1b73e66 | 2016-05-04 16:56:51 +0000 | [diff] [blame] | 76 | // This is set to true when Process::PreventCoreFiles() is called. |
| 77 | static bool coreFilesPrevented = false; |
| 78 | |
| 79 | bool Process::AreCoreFilesPrevented() { |
| 80 | return coreFilesPrevented; |
| 81 | } |
| 82 | |
Reid Spencer | 566ac28 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 83 | // Include the platform-specific parts of this class. |
Reid Spencer | 97d4a17 | 2004-12-27 06:15:29 +0000 | [diff] [blame] | 84 | #ifdef LLVM_ON_UNIX |
Reid Spencer | c892a0d | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 85 | #include "Unix/Process.inc" |
Reid Spencer | 97d4a17 | 2004-12-27 06:15:29 +0000 | [diff] [blame] | 86 | #endif |
| 87 | #ifdef LLVM_ON_WIN32 |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 88 | #include "Windows/Process.inc" |
Reid Spencer | 97d4a17 | 2004-12-27 06:15:29 +0000 | [diff] [blame] | 89 | #endif |