cmProcess.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmProcess_h
  4. #define cmProcess_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cmsys/Process.h"
  7. #include <chrono>
  8. #include <string>
  9. #include <vector>
  10. /** \class cmProcess
  11. * \brief run a process with c++
  12. *
  13. * cmProcess wraps the kwsys process stuff in a c++ class.
  14. */
  15. class cmProcess
  16. {
  17. public:
  18. cmProcess();
  19. ~cmProcess();
  20. const char* GetCommand() { return this->Command.c_str(); }
  21. void SetCommand(const char* command);
  22. void SetCommandArguments(std::vector<std::string> const& arg);
  23. void SetWorkingDirectory(const char* dir) { this->WorkingDirectory = dir; }
  24. void SetTimeout(std::chrono::duration<double> t) { this->Timeout = t; }
  25. void ChangeTimeout(std::chrono::duration<double> t);
  26. void ResetStartTime();
  27. // Return true if the process starts
  28. bool StartProcess();
  29. // return the process status
  30. int GetProcessStatus();
  31. // Report the status of the program
  32. int ReportStatus();
  33. int GetId() { return this->Id; }
  34. void SetId(int id) { this->Id = id; }
  35. int GetExitValue() { return this->ExitValue; }
  36. std::chrono::duration<double> GetTotalTime() { return this->TotalTime; }
  37. int GetExitException();
  38. std::string GetExitExceptionString();
  39. /**
  40. * Read one line of output but block for no more than timeout.
  41. * Returns:
  42. * cmsysProcess_Pipe_None = Process terminated and all output read
  43. * cmsysProcess_Pipe_STDOUT = Line came from stdout or stderr
  44. * cmsysProcess_Pipe_Timeout = Timeout expired while waiting
  45. */
  46. int GetNextOutputLine(std::string& line,
  47. std::chrono::duration<double> timeout);
  48. private:
  49. std::chrono::duration<double> Timeout;
  50. std::chrono::steady_clock::time_point StartTime;
  51. std::chrono::duration<double> TotalTime;
  52. cmsysProcess* Process;
  53. class Buffer : public std::vector<char>
  54. {
  55. // Half-open index range of partial line already scanned.
  56. size_type First;
  57. size_type Last;
  58. public:
  59. Buffer()
  60. : First(0)
  61. , Last(0)
  62. {
  63. }
  64. bool GetLine(std::string& line);
  65. bool GetLast(std::string& line);
  66. };
  67. Buffer Output;
  68. std::string Command;
  69. std::string WorkingDirectory;
  70. std::vector<std::string> Arguments;
  71. std::vector<const char*> ProcessArgs;
  72. int Id;
  73. int ExitValue;
  74. };
  75. #endif