LLVM  3.7.0
Process.cpp
Go to the documentation of this file.
1 //===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the operating system Process concept.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/Config/config.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/Process.h"
19 #include "llvm/Support/Program.h"
20 
21 using namespace llvm;
22 using namespace sys;
23 
24 //===----------------------------------------------------------------------===//
25 //=== WARNING: Implementation here must contain only TRULY operating system
26 //=== independent code.
27 //===----------------------------------------------------------------------===//
28 
29 Optional<std::string> Process::FindInEnvPath(const std::string& EnvName,
30  const std::string& FileName)
31 {
32  assert(!path::is_absolute(FileName));
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'};
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 
57 
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 
76 // Include the platform-specific parts of this class.
77 #ifdef LLVM_ON_UNIX
78 #include "Unix/Process.inc"
79 #endif
80 #ifdef LLVM_ON_WIN32
81 #include "Windows/Process.inc"
82 #endif
bool hasValue() const
Definition: Optional.h:125
static Optional< std::string > FindInEnvPath(const std::string &EnvName, const std::string &FileName)
This function searches for an existing file in the list of directories in a PATH like environment var...
Definition: Process.cpp:29
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:443
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
bool is_absolute(const Twine &path)
Is path absolute?
Definition: Path.cpp:650
static const char colorcodes[2][2][8][10]
Definition: Process.cpp:71
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:121
void SplitString(StringRef Source, SmallVectorImpl< StringRef > &OutFragments, StringRef Delimiters=" \t\n\v\f\r")
SplitString - Split up the specified string according to the specified delimiters, appending the result fragments to the output list.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
Provides a library for accessing information about this process and other processes on the operating ...
#define ALLCOLORS(FGBG, BOLD)
Definition: Process.cpp:60
bool exists(file_status status)
Does file exist?
Definition: Path.cpp:845
static Optional< std::string > GetEnv(StringRef name)