blob: caec993ee1653e5078d5636ced01febf46b68548 [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"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000015#include "llvm/ADT/StringExtras.h"
Reid Spencer97d4a172004-12-27 06:15:29 +000016#include "llvm/Config/config.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000017#include "llvm/Support/FileSystem.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000018#include "llvm/Support/Path.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000019#include "llvm/Support/Program.h"
Reid Spencer566ac282004-09-11 04:59:30 +000020
Chandler Carruth5473dfb2012-12-31 11:45:20 +000021using namespace llvm;
Reid Spencer566ac282004-09-11 04:59:30 +000022using namespace sys;
23
24//===----------------------------------------------------------------------===//
25//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukman10468d82005-04-21 22:55:34 +000026//=== independent code.
Reid Spencer566ac282004-09-11 04:59:30 +000027//===----------------------------------------------------------------------===//
28
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000029Optional<std::string> Process::FindInEnvPath(const std::string& EnvName,
30 const std::string& FileName)
31{
Nico Weber7d831a52015-04-24 22:18:46 +000032 assert(!path::is_absolute(FileName));
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000033 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 Carruthef7f9682013-01-04 23:19:55 +000057
Nico Rieck92d649a2013-09-11 00:36:48 +000058#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
71static const char colorcodes[2][2][8][10] = {
72 { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
73 { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
74};
75
Leny Kholodov1b73e662016-05-04 16:56:51 +000076// This is set to true when Process::PreventCoreFiles() is called.
77static bool coreFilesPrevented = false;
78
79bool Process::AreCoreFilesPrevented() {
80 return coreFilesPrevented;
81}
82
Reid Spencer566ac282004-09-11 04:59:30 +000083// Include the platform-specific parts of this class.
Reid Spencer97d4a172004-12-27 06:15:29 +000084#ifdef LLVM_ON_UNIX
Reid Spencerc892a0d2005-01-09 23:29:00 +000085#include "Unix/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000086#endif
87#ifdef LLVM_ON_WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +000088#include "Windows/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000089#endif