12754fe60SDimitry Andric //===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
22754fe60SDimitry Andric //
32754fe60SDimitry Andric //                     The LLVM Compiler Infrastructure
42754fe60SDimitry Andric //
52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source
62754fe60SDimitry Andric // License. See LICENSE.TXT for details.
72754fe60SDimitry Andric //
82754fe60SDimitry Andric //===----------------------------------------------------------------------===//
92754fe60SDimitry Andric //
1091bc56edSDimitry Andric //  This file implements the operating system Process concept.
112754fe60SDimitry Andric //
122754fe60SDimitry Andric //===----------------------------------------------------------------------===//
132754fe60SDimitry Andric 
14db17bf38SDimitry Andric #include "llvm/Support/Process.h"
152cab237bSDimitry Andric #include "llvm/ADT/STLExtras.h"
1691bc56edSDimitry Andric #include "llvm/ADT/StringExtras.h"
174ba319b5SDimitry Andric #include "llvm/Config/llvm-config.h"
18*b5893f02SDimitry Andric #include "llvm/Config/config.h"
1991bc56edSDimitry Andric #include "llvm/Support/FileSystem.h"
20ff0cc061SDimitry Andric #include "llvm/Support/Path.h"
2191bc56edSDimitry Andric #include "llvm/Support/Program.h"
222754fe60SDimitry Andric 
23139f7f9bSDimitry Andric using namespace llvm;
242754fe60SDimitry Andric using namespace sys;
252754fe60SDimitry Andric 
262754fe60SDimitry Andric //===----------------------------------------------------------------------===//
272754fe60SDimitry Andric //=== WARNING: Implementation here must contain only TRULY operating system
282754fe60SDimitry Andric //===          independent code.
292754fe60SDimitry Andric //===----------------------------------------------------------------------===//
302754fe60SDimitry Andric 
FindInEnvPath(StringRef EnvName,StringRef FileName)312cab237bSDimitry Andric Optional<std::string> Process::FindInEnvPath(StringRef EnvName,
322cab237bSDimitry Andric                                              StringRef FileName) {
332cab237bSDimitry Andric   return FindInEnvPath(EnvName, FileName, {});
342cab237bSDimitry Andric }
352cab237bSDimitry Andric 
FindInEnvPath(StringRef EnvName,StringRef FileName,ArrayRef<std::string> IgnoreList)362cab237bSDimitry Andric Optional<std::string> Process::FindInEnvPath(StringRef EnvName,
372cab237bSDimitry Andric                                              StringRef FileName,
382cab237bSDimitry Andric                                              ArrayRef<std::string> IgnoreList) {
39ff0cc061SDimitry Andric   assert(!path::is_absolute(FileName));
4091bc56edSDimitry Andric   Optional<std::string> FoundPath;
4191bc56edSDimitry Andric   Optional<std::string> OptPath = Process::GetEnv(EnvName);
4291bc56edSDimitry Andric   if (!OptPath.hasValue())
4391bc56edSDimitry Andric     return FoundPath;
44139f7f9bSDimitry Andric 
4591bc56edSDimitry Andric   const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'};
4691bc56edSDimitry Andric   SmallVector<StringRef, 8> Dirs;
4791bc56edSDimitry Andric   SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
4891bc56edSDimitry Andric 
492cab237bSDimitry Andric   for (StringRef Dir : Dirs) {
5091bc56edSDimitry Andric     if (Dir.empty())
5191bc56edSDimitry Andric       continue;
5291bc56edSDimitry Andric 
532cab237bSDimitry Andric     if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); }))
542cab237bSDimitry Andric       continue;
552cab237bSDimitry Andric 
5691bc56edSDimitry Andric     SmallString<128> FilePath(Dir);
5791bc56edSDimitry Andric     path::append(FilePath, FileName);
5891bc56edSDimitry Andric     if (fs::exists(Twine(FilePath))) {
5991bc56edSDimitry Andric       FoundPath = FilePath.str();
6091bc56edSDimitry Andric       break;
6191bc56edSDimitry Andric     }
6291bc56edSDimitry Andric   }
6391bc56edSDimitry Andric 
6491bc56edSDimitry Andric   return FoundPath;
6591bc56edSDimitry Andric }
66139f7f9bSDimitry Andric 
67139f7f9bSDimitry Andric 
68f785676fSDimitry Andric #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
69f785676fSDimitry Andric 
70f785676fSDimitry Andric #define ALLCOLORS(FGBG,BOLD) {\
71f785676fSDimitry Andric     COLOR(FGBG, "0", BOLD),\
72f785676fSDimitry Andric     COLOR(FGBG, "1", BOLD),\
73f785676fSDimitry Andric     COLOR(FGBG, "2", BOLD),\
74f785676fSDimitry Andric     COLOR(FGBG, "3", BOLD),\
75f785676fSDimitry Andric     COLOR(FGBG, "4", BOLD),\
76f785676fSDimitry Andric     COLOR(FGBG, "5", BOLD),\
77f785676fSDimitry Andric     COLOR(FGBG, "6", BOLD),\
78f785676fSDimitry Andric     COLOR(FGBG, "7", BOLD)\
79f785676fSDimitry Andric   }
80f785676fSDimitry Andric 
81f785676fSDimitry Andric static const char colorcodes[2][2][8][10] = {
82f785676fSDimitry Andric  { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
83f785676fSDimitry Andric  { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
84f785676fSDimitry Andric };
85f785676fSDimitry Andric 
86*b5893f02SDimitry Andric // A CMake option controls wheter we emit core dumps by default. An application
87*b5893f02SDimitry Andric // may disable core dumps by calling Process::PreventCoreFiles().
88*b5893f02SDimitry Andric static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
893ca95b02SDimitry Andric 
AreCoreFilesPrevented()90*b5893f02SDimitry Andric bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; }
913ca95b02SDimitry Andric 
922754fe60SDimitry Andric // Include the platform-specific parts of this class.
932754fe60SDimitry Andric #ifdef LLVM_ON_UNIX
942754fe60SDimitry Andric #include "Unix/Process.inc"
952754fe60SDimitry Andric #endif
964ba319b5SDimitry Andric #ifdef _WIN32
972754fe60SDimitry Andric #include "Windows/Process.inc"
982754fe60SDimitry Andric #endif
99