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" 16 #include "llvm/Support/ErrorHandling.h" 17 #include "llvm/Support/FileSystem.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 /// \brief A helper function to compute the elapsed wall-time since the program 30 /// started. 31 /// 32 /// Note that this routine actually computes the elapsed wall time since the 33 /// first time it was called. However, we arrange to have it called during the 34 /// startup of the process to get approximately correct results. 35 static TimeValue getElapsedWallTime() { 36 static TimeValue &StartTime = *new TimeValue(TimeValue::now()); 37 return TimeValue::now() - StartTime; 38 } 39 40 /// \brief A special global variable to ensure we call \c getElapsedWallTime 41 /// during global initialization of the program. 42 /// 43 /// Note that this variable is never referenced elsewhere. Doing so could 44 /// create race conditions during program startup or shutdown. 45 static volatile TimeValue DummyTimeValue = getElapsedWallTime(); 46 47 Optional<std::string> Process::FindInEnvPath(const std::string& EnvName, 48 const std::string& FileName) 49 { 50 Optional<std::string> FoundPath; 51 Optional<std::string> OptPath = Process::GetEnv(EnvName); 52 if (!OptPath.hasValue()) 53 return FoundPath; 54 55 const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'}; 56 SmallVector<StringRef, 8> Dirs; 57 SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr); 58 59 for (const auto &Dir : Dirs) { 60 if (Dir.empty()) 61 continue; 62 63 SmallString<128> FilePath(Dir); 64 path::append(FilePath, FileName); 65 if (fs::exists(Twine(FilePath))) { 66 FoundPath = FilePath.str(); 67 break; 68 } 69 } 70 71 return FoundPath; 72 } 73 74 75 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m" 76 77 #define ALLCOLORS(FGBG,BOLD) {\ 78 COLOR(FGBG, "0", BOLD),\ 79 COLOR(FGBG, "1", BOLD),\ 80 COLOR(FGBG, "2", BOLD),\ 81 COLOR(FGBG, "3", BOLD),\ 82 COLOR(FGBG, "4", BOLD),\ 83 COLOR(FGBG, "5", BOLD),\ 84 COLOR(FGBG, "6", BOLD),\ 85 COLOR(FGBG, "7", BOLD)\ 86 } 87 88 static const char colorcodes[2][2][8][10] = { 89 { ALLCOLORS("3",""), ALLCOLORS("3","1;") }, 90 { ALLCOLORS("4",""), ALLCOLORS("4","1;") } 91 }; 92 93 // Include the platform-specific parts of this class. 94 #ifdef LLVM_ON_UNIX 95 #include "Unix/Process.inc" 96 #endif 97 #ifdef LLVM_ON_WIN32 98 #include "Windows/Process.inc" 99 #endif 100