cmProcess.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmProcess.h"
  4. #include "cmProcessOutput.h"
  5. cmProcess::cmProcess()
  6. {
  7. this->Process = nullptr;
  8. this->Timeout = std::chrono::duration<double>::zero();
  9. this->TotalTime = std::chrono::duration<double>::zero();
  10. this->ExitValue = 0;
  11. this->Id = 0;
  12. this->StartTime = std::chrono::steady_clock::time_point();
  13. }
  14. cmProcess::~cmProcess()
  15. {
  16. cmsysProcess_Delete(this->Process);
  17. }
  18. void cmProcess::SetCommand(const char* command)
  19. {
  20. this->Command = command;
  21. }
  22. void cmProcess::SetCommandArguments(std::vector<std::string> const& args)
  23. {
  24. this->Arguments = args;
  25. }
  26. bool cmProcess::StartProcess()
  27. {
  28. if (this->Command.empty()) {
  29. return false;
  30. }
  31. this->StartTime = std::chrono::steady_clock::now();
  32. this->ProcessArgs.clear();
  33. // put the command as arg0
  34. this->ProcessArgs.push_back(this->Command.c_str());
  35. // now put the command arguments in
  36. for (std::string const& arg : this->Arguments) {
  37. this->ProcessArgs.push_back(arg.c_str());
  38. }
  39. this->ProcessArgs.push_back(nullptr); // null terminate the list
  40. this->Process = cmsysProcess_New();
  41. cmsysProcess_SetCommand(this->Process, &*this->ProcessArgs.begin());
  42. if (!this->WorkingDirectory.empty()) {
  43. cmsysProcess_SetWorkingDirectory(this->Process,
  44. this->WorkingDirectory.c_str());
  45. }
  46. cmsysProcess_SetTimeout(this->Process, this->Timeout.count());
  47. cmsysProcess_SetOption(this->Process, cmsysProcess_Option_MergeOutput, 1);
  48. cmsysProcess_Execute(this->Process);
  49. return (cmsysProcess_GetState(this->Process) ==
  50. cmsysProcess_State_Executing);
  51. }
  52. bool cmProcess::Buffer::GetLine(std::string& line)
  53. {
  54. // Scan for the next newline.
  55. for (size_type sz = this->size(); this->Last != sz; ++this->Last) {
  56. if ((*this)[this->Last] == '\n' || (*this)[this->Last] == '\0') {
  57. // Extract the range first..last as a line.
  58. const char* text = &*this->begin() + this->First;
  59. size_type length = this->Last - this->First;
  60. while (length && text[length - 1] == '\r') {
  61. length--;
  62. }
  63. line.assign(text, length);
  64. // Start a new range for the next line.
  65. ++this->Last;
  66. this->First = Last;
  67. // Return the line extracted.
  68. return true;
  69. }
  70. }
  71. // Available data have been exhausted without a newline.
  72. if (this->First != 0) {
  73. // Move the partial line to the beginning of the buffer.
  74. this->erase(this->begin(), this->begin() + this->First);
  75. this->First = 0;
  76. this->Last = this->size();
  77. }
  78. return false;
  79. }
  80. bool cmProcess::Buffer::GetLast(std::string& line)
  81. {
  82. // Return the partial last line, if any.
  83. if (!this->empty()) {
  84. line.assign(&*this->begin(), this->size());
  85. this->First = this->Last = 0;
  86. this->clear();
  87. return true;
  88. }
  89. return false;
  90. }
  91. int cmProcess::GetNextOutputLine(std::string& line,
  92. std::chrono::duration<double> timeout)
  93. {
  94. cmProcessOutput processOutput(cmProcessOutput::UTF8);
  95. std::string strdata;
  96. double waitTimeout = timeout.count();
  97. for (;;) {
  98. // Look for lines already buffered.
  99. if (this->Output.GetLine(line)) {
  100. return cmsysProcess_Pipe_STDOUT;
  101. }
  102. // Check for more data from the process.
  103. char* data;
  104. int length;
  105. int p =
  106. cmsysProcess_WaitForData(this->Process, &data, &length, &waitTimeout);
  107. if (p == cmsysProcess_Pipe_Timeout) {
  108. return cmsysProcess_Pipe_Timeout;
  109. }
  110. if (p == cmsysProcess_Pipe_STDOUT) {
  111. processOutput.DecodeText(data, length, strdata);
  112. this->Output.insert(this->Output.end(), strdata.begin(), strdata.end());
  113. } else { // p == cmsysProcess_Pipe_None
  114. // The process will provide no more data.
  115. break;
  116. }
  117. }
  118. processOutput.DecodeText(std::string(), strdata);
  119. if (!strdata.empty()) {
  120. this->Output.insert(this->Output.end(), strdata.begin(), strdata.end());
  121. }
  122. // Look for partial last lines.
  123. if (this->Output.GetLast(line)) {
  124. return cmsysProcess_Pipe_STDOUT;
  125. }
  126. // No more data. Wait for process exit.
  127. if (!cmsysProcess_WaitForExit(this->Process, &waitTimeout)) {
  128. return cmsysProcess_Pipe_Timeout;
  129. }
  130. // Record exit information.
  131. this->ExitValue = cmsysProcess_GetExitValue(this->Process);
  132. this->TotalTime = std::chrono::steady_clock::now() - this->StartTime;
  133. // Because of a processor clock scew the runtime may become slightly
  134. // negative. If someone changed the system clock while the process was
  135. // running this may be even more. Make sure not to report a negative
  136. // duration here.
  137. if (this->TotalTime <= std::chrono::duration<double>::zero()) {
  138. this->TotalTime = std::chrono::duration<double>::zero();
  139. }
  140. // std::cerr << "Time to run: " << this->TotalTime << "\n";
  141. return cmsysProcess_Pipe_None;
  142. }
  143. cmProcess::State cmProcess::GetProcessStatus()
  144. {
  145. if (this->Process) {
  146. switch (cmsysProcess_GetState(this->Process)) {
  147. case cmsysProcess_State_Starting:
  148. return State::Starting;
  149. case cmsysProcess_State_Error:
  150. return State::Error;
  151. case cmsysProcess_State_Exception:
  152. return State::Exception;
  153. case cmsysProcess_State_Executing:
  154. return State::Executing;
  155. case cmsysProcess_State_Expired:
  156. return State::Expired;
  157. case cmsysProcess_State_Killed:
  158. return State::Killed;
  159. case cmsysProcess_State_Disowned:
  160. return State::Disowned;
  161. default: // case cmsysProcess_State_Exited:
  162. break;
  163. }
  164. }
  165. return State::Exited;
  166. }
  167. void cmProcess::ChangeTimeout(std::chrono::duration<double> t)
  168. {
  169. this->Timeout = t;
  170. cmsysProcess_SetTimeout(this->Process, this->Timeout.count());
  171. }
  172. void cmProcess::ResetStartTime()
  173. {
  174. cmsysProcess_ResetStartTime(this->Process);
  175. this->StartTime = std::chrono::steady_clock::now();
  176. }
  177. cmProcess::Exception cmProcess::GetExitException()
  178. {
  179. switch (cmsysProcess_GetExitException(this->Process)) {
  180. case cmsysProcess_Exception_None:
  181. return Exception::None;
  182. case cmsysProcess_Exception_Fault:
  183. return Exception::Fault;
  184. case cmsysProcess_Exception_Illegal:
  185. return Exception::Illegal;
  186. case cmsysProcess_Exception_Interrupt:
  187. return Exception::Interrupt;
  188. case cmsysProcess_Exception_Numerical:
  189. return Exception::Numerical;
  190. default: // case cmsysProcess_Exception_Other:
  191. break;
  192. }
  193. return Exception::Other;
  194. }
  195. std::string cmProcess::GetExitExceptionString()
  196. {
  197. return cmsysProcess_GetExceptionString(this->Process);
  198. }