blob: f32355aefbb7585940322c1f638b3db8d180568f [file] [log] [blame]
Reid Spencer566ac282004-09-11 04:59:30 +00001//===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer566ac282004-09-11 04:59:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer566ac282004-09-11 04:59:30 +00008//===----------------------------------------------------------------------===//
9//
Hans Wennborgcfe341f2014-06-20 01:36:00 +000010// This file implements the operating system Process concept.
Reid Spencer566ac282004-09-11 04:59:30 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/Support/Process.h"
Zachary Turnerfa0ca6c2017-10-11 20:12:09 +000015#include "llvm/ADT/STLExtras.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000016#include "llvm/ADT/StringExtras.h"
Nico Weber432a3882018-04-30 14:59:11 +000017#include "llvm/Config/llvm-config.h"
Reid Kleckner53131932018-08-20 16:49:54 +000018#include "llvm/Config/config.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000019#include "llvm/Support/FileSystem.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000020#include "llvm/Support/Path.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000021#include "llvm/Support/Program.h"
Reid Spencer566ac282004-09-11 04:59:30 +000022
Chandler Carruth5473dfb2012-12-31 11:45:20 +000023using namespace llvm;
Reid Spencer566ac282004-09-11 04:59:30 +000024using namespace sys;
25
26//===----------------------------------------------------------------------===//
27//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukman10468d82005-04-21 22:55:34 +000028//=== independent code.
Reid Spencer566ac282004-09-11 04:59:30 +000029//===----------------------------------------------------------------------===//
30
Zachary Turnerfa0ca6c2017-10-11 20:12:09 +000031Optional<std::string> Process::FindInEnvPath(StringRef EnvName,
32 StringRef FileName) {
33 return FindInEnvPath(EnvName, FileName, {});
34}
35
36Optional<std::string> Process::FindInEnvPath(StringRef EnvName,
37 StringRef FileName,
38 ArrayRef<std::string> IgnoreList) {
Nico Weber7d831a52015-04-24 22:18:46 +000039 assert(!path::is_absolute(FileName));
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000040 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
Zachary Turnerfa0ca6c2017-10-11 20:12:09 +000049 for (StringRef Dir : Dirs) {
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000050 if (Dir.empty())
51 continue;
52
Zachary Turnerfa0ca6c2017-10-11 20:12:09 +000053 if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); }))
54 continue;
55
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000056 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
Chandler Carruthef7f9682013-01-04 23:19:55 +000067
Nico Rieck92d649a2013-09-11 00:36:48 +000068#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
81static const char colorcodes[2][2][8][10] = {
82 { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
83 { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
84};
85
Reid Kleckner7a973fb2018-08-23 22:58:56 +000086// A CMake option controls wheter we emit core dumps by default. An application
87// may disable core dumps by calling Process::PreventCoreFiles().
88static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
Leny Kholodov1b73e662016-05-04 16:56:51 +000089
Reid Kleckner7a973fb2018-08-23 22:58:56 +000090bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; }
Leny Kholodov1b73e662016-05-04 16:56:51 +000091
Reid Spencer566ac282004-09-11 04:59:30 +000092// Include the platform-specific parts of this class.
Reid Spencer97d4a172004-12-27 06:15:29 +000093#ifdef LLVM_ON_UNIX
Reid Spencerc892a0d2005-01-09 23:29:00 +000094#include "Unix/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000095#endif
Nico Weber712e8d22018-04-29 00:45:03 +000096#ifdef _WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +000097#include "Windows/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000098#endif