2
0

Process.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include "llvm/ADT/StringExtras.h"
  14. #include "llvm/Config/config.h"
  15. #include "llvm/Support/FileSystem.h"
  16. #include "llvm/Support/Path.h"
  17. #include "llvm/Support/Process.h"
  18. #include "llvm/Support/Program.h"
  19. using namespace llvm;
  20. using namespace sys;
  21. //===----------------------------------------------------------------------===//
  22. //=== WARNING: Implementation here must contain only TRULY operating system
  23. //=== independent code.
  24. //===----------------------------------------------------------------------===//
  25. Optional<std::string> Process::FindInEnvPath(const std::string& EnvName,
  26. const std::string& FileName)
  27. {
  28. assert(!path::is_absolute(FileName));
  29. Optional<std::string> FoundPath;
  30. Optional<std::string> OptPath = Process::GetEnv(EnvName);
  31. if (!OptPath.hasValue())
  32. return FoundPath;
  33. const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'};
  34. SmallVector<StringRef, 8> Dirs;
  35. SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
  36. for (const auto &Dir : Dirs) {
  37. if (Dir.empty())
  38. continue;
  39. SmallString<128> FilePath(Dir);
  40. path::append(FilePath, FileName);
  41. if (fs::exists(Twine(FilePath))) {
  42. FoundPath = FilePath.str();
  43. break;
  44. }
  45. }
  46. return FoundPath;
  47. }
  48. #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
  49. #define ALLCOLORS(FGBG,BOLD) {\
  50. COLOR(FGBG, "0", BOLD),\
  51. COLOR(FGBG, "1", BOLD),\
  52. COLOR(FGBG, "2", BOLD),\
  53. COLOR(FGBG, "3", BOLD),\
  54. COLOR(FGBG, "4", BOLD),\
  55. COLOR(FGBG, "5", BOLD),\
  56. COLOR(FGBG, "6", BOLD),\
  57. COLOR(FGBG, "7", BOLD)\
  58. }
  59. static const char colorcodes[2][2][8][10] = {
  60. { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
  61. { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
  62. };
  63. // Include the platform-specific parts of this class.
  64. #ifdef LLVM_ON_UNIX
  65. #include "Unix/Process.inc"
  66. #endif
  67. #ifdef LLVM_ON_WIN32
  68. #include "Windows/Process.inc"
  69. #endif