blob: 5b647100815976f4fc09add93bb48a557c762b76 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Brukman10468d82005-04-21 22:55:34 +00006//
Reid Spencer566ac282004-09-11 04:59:30 +00007//===----------------------------------------------------------------------===//
8//
Hans Wennborgcfe341f2014-06-20 01:36:00 +00009// This file implements the operating system Process concept.
Reid Spencer566ac282004-09-11 04:59:30 +000010//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth6bda14b2017-06-06 11:49:48 +000013#include "llvm/Support/Process.h"
Zachary Turnerfa0ca6c2017-10-11 20:12:09 +000014#include "llvm/ADT/STLExtras.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000015#include "llvm/ADT/StringExtras.h"
Nico Weber432a3882018-04-30 14:59:11 +000016#include "llvm/Config/llvm-config.h"
Reid Kleckner53131932018-08-20 16:49:54 +000017#include "llvm/Config/config.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000018#include "llvm/Support/FileSystem.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000019#include "llvm/Support/Path.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000020#include "llvm/Support/Program.h"
Reid Spencer566ac282004-09-11 04:59:30 +000021
Chandler Carruth5473dfb2012-12-31 11:45:20 +000022using namespace llvm;
Reid Spencer566ac282004-09-11 04:59:30 +000023using namespace sys;
24
25//===----------------------------------------------------------------------===//
26//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukman10468d82005-04-21 22:55:34 +000027//=== independent code.
Reid Spencer566ac282004-09-11 04:59:30 +000028//===----------------------------------------------------------------------===//
29
Zachary Turnerfa0ca6c2017-10-11 20:12:09 +000030Optional<std::string> Process::FindInEnvPath(StringRef EnvName,
31 StringRef FileName) {
32 return FindInEnvPath(EnvName, FileName, {});
33}
34
35Optional<std::string> Process::FindInEnvPath(StringRef EnvName,
36 StringRef FileName,
37 ArrayRef<std::string> IgnoreList) {
Nico Weber7d831a52015-04-24 22:18:46 +000038 assert(!path::is_absolute(FileName));
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000039 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 Turnerfa0ca6c2017-10-11 20:12:09 +000048 for (StringRef Dir : Dirs) {
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000049 if (Dir.empty())
50 continue;
51
Zachary Turnerfa0ca6c2017-10-11 20:12:09 +000052 if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); }))
53 continue;
54
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000055 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 Carruthef7f9682013-01-04 23:19:55 +000066
Nico Rieck92d649a2013-09-11 00:36:48 +000067#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
80static const char colorcodes[2][2][8][10] = {
81 { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
82 { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
83};
84
Reid Kleckner7a973fb2018-08-23 22:58:56 +000085// A CMake option controls wheter we emit core dumps by default. An application
86// may disable core dumps by calling Process::PreventCoreFiles().
87static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
Leny Kholodov1b73e662016-05-04 16:56:51 +000088
Reid Kleckner7a973fb2018-08-23 22:58:56 +000089bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; }
Leny Kholodov1b73e662016-05-04 16:56:51 +000090
Reid Spencer566ac282004-09-11 04:59:30 +000091// Include the platform-specific parts of this class.
Reid Spencer97d4a172004-12-27 06:15:29 +000092#ifdef LLVM_ON_UNIX
Reid Spencerc892a0d2005-01-09 23:29:00 +000093#include "Unix/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000094#endif
Nico Weber712e8d22018-04-29 00:45:03 +000095#ifdef _WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +000096#include "Windows/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000097#endif