cmProcess.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /*
  11. * A wrapper function for cmsysProcess_SetTimeout that takes an
  12. * std::chrono::duration. For convenience only.
  13. */
  14. void cmsysProcess_SetTimeout(cmsysProcess* process,
  15. std::chrono::duration<double> timeout);
  16. /** \class cmProcess
  17. * \brief run a process with c++
  18. *
  19. * cmProcess wraps the kwsys process stuff in a c++ class.
  20. */
  21. class cmProcess
  22. {
  23. public:
  24. cmProcess();
  25. ~cmProcess();
  26. const char* GetCommand() { return this->Command.c_str(); }
  27. void SetCommand(const char* command);
  28. void SetCommandArguments(std::vector<std::string> const& arg);
  29. void SetWorkingDirectory(const char* dir) { this->WorkingDirectory = dir; }
  30. void SetTimeout(std::chrono::duration<double> t) { this->Timeout = t; }
  31. void ChangeTimeout(std::chrono::duration<double> t);
  32. void ResetStartTime();
  33. // Return true if the process starts
  34. bool StartProcess();
  35. // return the process status
  36. int GetProcessStatus();
  37. // Report the status of the program
  38. int ReportStatus();
  39. int GetId() { return this->Id; }
  40. void SetId(int id) { this->Id = id; }
  41. int GetExitValue() { return this->ExitValue; }
  42. std::chrono::duration<double> GetTotalTime() { return this->TotalTime; }
  43. int GetExitException();
  44. std::string GetExitExceptionString();
  45. /**
  46. * Read one line of output but block for no more than timeout.
  47. * Returns:
  48. * cmsysProcess_Pipe_None = Process terminated and all output read
  49. * cmsysProcess_Pipe_STDOUT = Line came from stdout or stderr
  50. * cmsysProcess_Pipe_Timeout = Timeout expired while waiting
  51. */
  52. int GetNextOutputLine(std::string& line,
  53. std::chrono::duration<double> timeout);
  54. private:
  55. std::chrono::duration<double> Timeout;
  56. std::chrono::steady_clock::time_point StartTime;
  57. std::chrono::duration<double> TotalTime;
  58. cmsysProcess* Process;
  59. class Buffer : public std::vector<char>
  60. {
  61. // Half-open index range of partial line already scanned.
  62. size_type First;
  63. size_type Last;
  64. public:
  65. Buffer()
  66. : First(0)
  67. , Last(0)
  68. {
  69. }
  70. bool GetLine(std::string& line);
  71. bool GetLast(std::string& line);
  72. };
  73. Buffer Output;
  74. std::string Command;
  75. std::string WorkingDirectory;
  76. std::vector<std::string> Arguments;
  77. std::vector<const char*> ProcessArgs;
  78. int Id;
  79. int ExitValue;
  80. };
  81. #endif