Process.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the operating system Process concept.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/Process.h"
  13. #include "llvm/ADT/STLExtras.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/Config/config.h"
  16. #include "llvm/Config/llvm-config.h"
  17. #include "llvm/Support/CrashRecoveryContext.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/Path.h"
  20. #include "llvm/Support/Program.h"
  21. #include <optional>
  22. #include <stdlib.h> // for _Exit
  23. using namespace llvm;
  24. using namespace sys;
  25. //===----------------------------------------------------------------------===//
  26. //=== WARNING: Implementation here must contain only TRULY operating system
  27. //=== independent code.
  28. //===----------------------------------------------------------------------===//
  29. std::optional<std::string>
  30. Process::FindInEnvPath(StringRef EnvName, StringRef FileName, char Separator) {
  31. return FindInEnvPath(EnvName, FileName, {}, Separator);
  32. }
  33. std::optional<std::string>
  34. Process::FindInEnvPath(StringRef EnvName, StringRef FileName,
  35. ArrayRef<std::string> IgnoreList, char Separator) {
  36. assert(!path::is_absolute(FileName));
  37. std::optional<std::string> FoundPath;
  38. std::optional<std::string> OptPath = Process::GetEnv(EnvName);
  39. if (!OptPath)
  40. return FoundPath;
  41. const char EnvPathSeparatorStr[] = {Separator, '\0'};
  42. SmallVector<StringRef, 8> Dirs;
  43. SplitString(*OptPath, Dirs, EnvPathSeparatorStr);
  44. for (StringRef Dir : Dirs) {
  45. if (Dir.empty())
  46. continue;
  47. if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); }))
  48. continue;
  49. SmallString<128> FilePath(Dir);
  50. path::append(FilePath, FileName);
  51. if (fs::exists(Twine(FilePath))) {
  52. FoundPath = std::string(FilePath.str());
  53. break;
  54. }
  55. }
  56. return FoundPath;
  57. }
  58. #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
  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. static const char colorcodes[2][2][8][10] = {
  70. { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
  71. { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
  72. };
  73. // A CMake option controls wheter we emit core dumps by default. An application
  74. // may disable core dumps by calling Process::PreventCoreFiles().
  75. static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
  76. bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; }
  77. [[noreturn]] void Process::Exit(int RetCode, bool NoCleanup) {
  78. if (CrashRecoveryContext *CRC = CrashRecoveryContext::GetCurrent())
  79. CRC->HandleExit(RetCode);
  80. if (NoCleanup)
  81. ExitNoCleanup(RetCode);
  82. else
  83. ::exit(RetCode);
  84. }
  85. // Include the platform-specific parts of this class.
  86. #ifdef LLVM_ON_UNIX
  87. #include "Unix/Process.inc"
  88. #endif
  89. #ifdef _WIN32
  90. #include "Windows/Process.inc"
  91. #endif