blob: 3571cd3132e5ebb34fa0c506a43882677c1ebe20 [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
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000014#include "llvm/ADT/StringExtras.h"
Reid Spencer97d4a172004-12-27 06:15:29 +000015#include "llvm/Config/config.h"
Ehsan Akhgari33d1ae52014-06-30 19:54:20 +000016#include "llvm/Support/FileSystem.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000017#include "llvm/Support/Path.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000018#include "llvm/Support/Process.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{
32 Optional<std::string> FoundPath;
33 Optional<std::string> OptPath = Process::GetEnv(EnvName);
34 if (!OptPath.hasValue())
35 return FoundPath;
36
37 const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'};
38 SmallVector<StringRef, 8> Dirs;
39 SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
40
41 for (const auto &Dir : Dirs) {
42 if (Dir.empty())
43 continue;
44
45 SmallString<128> FilePath(Dir);
46 path::append(FilePath, FileName);
47 if (fs::exists(Twine(FilePath))) {
48 FoundPath = FilePath.str();
49 break;
50 }
51 }
52
53 return FoundPath;
54}
55
Chandler Carruthef7f9682013-01-04 23:19:55 +000056
Nico Rieck92d649a2013-09-11 00:36:48 +000057#define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
58
59#define ALLCOLORS(FGBG,BOLD) {\
60 COLOR(FGBG, "0", BOLD),\
61 COLOR(FGBG, "1", BOLD),\
62 COLOR(FGBG, "2", BOLD),\
63 COLOR(FGBG, "3", BOLD),\
64 COLOR(FGBG, "4", BOLD),\
65 COLOR(FGBG, "5", BOLD),\
66 COLOR(FGBG, "6", BOLD),\
67 COLOR(FGBG, "7", BOLD)\
68 }
69
70static const char colorcodes[2][2][8][10] = {
71 { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
72 { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
73};
74
Reid Spencer566ac282004-09-11 04:59:30 +000075// Include the platform-specific parts of this class.
Reid Spencer97d4a172004-12-27 06:15:29 +000076#ifdef LLVM_ON_UNIX
Reid Spencerc892a0d2005-01-09 23:29:00 +000077#include "Unix/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000078#endif
79#ifdef LLVM_ON_WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +000080#include "Windows/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000081#endif