cmProcess.cxx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include <iostream>
  6. void cmsysProcess_SetTimeout(cmsysProcess* process,
  7. std::chrono::duration<double> timeout)
  8. {
  9. cmsysProcess_SetTimeout(
  10. process,
  11. double(
  12. std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()) /
  13. 1000.0);
  14. };
  15. cmProcess::cmProcess()
  16. {
  17. this->Process = nullptr;
  18. this->Timeout = std::chrono::duration<double>::zero();
  19. this->TotalTime = std::chrono::duration<double>::zero();
  20. this->ExitValue = 0;
  21. this->Id = 0;
  22. this->StartTime = std::chrono::steady_clock::time_point();
  23. }
  24. cmProcess::~cmProcess()
  25. {
  26. cmsysProcess_Delete(this->Process);
  27. }
  28. void cmProcess::SetCommand(const char* command)
  29. {
  30. this->Command = command;
  31. }
  32. void cmProcess::SetCommandArguments(std::vector<std::string> const& args)
  33. {
  34. this->Arguments = args;
  35. }
  36. bool cmProcess::StartProcess()
  37. {
  38. if (this->Command.empty()) {
  39. return false;
  40. }
  41. this->StartTime = std::chrono::steady_clock::now();
  42. this->ProcessArgs.clear();
  43. // put the command as arg0
  44. this->ProcessArgs.push_back(this->Command.c_str());
  45. // now put the command arguments in
  46. for (std::string const& arg : this->Arguments) {
  47. this->ProcessArgs.push_back(arg.c_str());
  48. }
  49. this->ProcessArgs.push_back(nullptr); // null terminate the list
  50. this->Process = cmsysProcess_New();
  51. cmsysProcess_SetCommand(this->Process, &*this->ProcessArgs.begin());
  52. if (!this->WorkingDirectory.empty()) {
  53. cmsysProcess_SetWorkingDirectory(this->Process,
  54. this->WorkingDirectory.c_str());
  55. }
  56. cmsysProcess_SetTimeout(this->Process, this->Timeout);
  57. cmsysProcess_SetOption(this->Process, cmsysProcess_Option_MergeOutput, 1);
  58. cmsysProcess_Execute(this->Process);
  59. return (cmsysProcess_GetState(this->Process) ==
  60. cmsysProcess_State_Executing);
  61. }
  62. bool cmProcess::Buffer::GetLine(std::string& line)
  63. {
  64. // Scan for the next newline.
  65. for (size_type sz = this->size(); this->Last != sz; ++this->Last) {
  66. if ((*this)[this->Last] == '\n' || (*this)[this->Last] == '\0') {
  67. // Extract the range first..last as a line.
  68. const char* text = &*this->begin() + this->First;
  69. size_type length = this->Last - this->First;
  70. while (length && text[length - 1] == '\r') {
  71. length--;
  72. }
  73. line.assign(text, length);
  74. // Start a new range for the next line.
  75. ++this->Last;
  76. this->First = Last;
  77. // Return the line extracted.
  78. return true;
  79. }
  80. }
  81. // Available data have been exhausted without a newline.
  82. if (this->First != 0) {
  83. // Move the partial line to the beginning of the buffer.
  84. this->erase(this->begin(), this->begin() + this->First);
  85. this->First = 0;
  86. this->Last = this->size();
  87. }
  88. return false;
  89. }
  90. bool cmProcess::Buffer::GetLast(std::string& line)
  91. {
  92. // Return the partial last line, if any.
  93. if (!this->empty()) {
  94. line.assign(&*this->begin(), this->size());
  95. this->First = this->Last = 0;
  96. this->clear();
  97. return true;
  98. }
  99. return false;
  100. }
  101. int cmProcess::GetNextOutputLine(std::string& line,
  102. std::chrono::duration<double> timeout)
  103. {
  104. cmProcessOutput processOutput(cmProcessOutput::UTF8);
  105. std::string strdata;
  106. double waitTimeout =
  107. double(
  108. std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()) /
  109. 1000.0;
  110. for (;;) {
  111. // Look for lines already buffered.
  112. if (this->Output.GetLine(line)) {
  113. return cmsysProcess_Pipe_STDOUT;
  114. }
  115. // Check for more data from the process.
  116. char* data;
  117. int length;
  118. int p =
  119. cmsysProcess_WaitForData(this->Process, &data, &length, &waitTimeout);
  120. if (p == cmsysProcess_Pipe_Timeout) {
  121. return cmsysProcess_Pipe_Timeout;
  122. }
  123. if (p == cmsysProcess_Pipe_STDOUT) {
  124. processOutput.DecodeText(data, length, strdata);
  125. this->Output.insert(this->Output.end(), strdata.begin(), strdata.end());
  126. } else { // p == cmsysProcess_Pipe_None
  127. // The process will provide no more data.
  128. break;
  129. }
  130. }
  131. processOutput.DecodeText(std::string(), strdata);
  132. if (!strdata.empty()) {
  133. this->Output.insert(this->Output.end(), strdata.begin(), strdata.end());
  134. }
  135. // Look for partial last lines.
  136. if (this->Output.GetLast(line)) {
  137. return cmsysProcess_Pipe_STDOUT;
  138. }
  139. // No more data. Wait for process exit.
  140. if (!cmsysProcess_WaitForExit(this->Process, &waitTimeout)) {
  141. return cmsysProcess_Pipe_Timeout;
  142. }
  143. // Record exit information.
  144. this->ExitValue = cmsysProcess_GetExitValue(this->Process);
  145. this->TotalTime = std::chrono::steady_clock::now() - this->StartTime;
  146. // Because of a processor clock scew the runtime may become slightly
  147. // negative. If someone changed the system clock while the process was
  148. // running this may be even more. Make sure not to report a negative
  149. // duration here.
  150. if (this->TotalTime <= std::chrono::duration<double>::zero()) {
  151. this->TotalTime = std::chrono::duration<double>::zero();
  152. }
  153. // std::cerr << "Time to run: " << this->TotalTime << "\n";
  154. return cmsysProcess_Pipe_None;
  155. }
  156. // return the process status
  157. int cmProcess::GetProcessStatus()
  158. {
  159. if (!this->Process) {
  160. return cmsysProcess_State_Exited;
  161. }
  162. return cmsysProcess_GetState(this->Process);
  163. }
  164. int cmProcess::ReportStatus()
  165. {
  166. int result = 1;
  167. switch (cmsysProcess_GetState(this->Process)) {
  168. case cmsysProcess_State_Starting: {
  169. std::cerr << "cmProcess: Never started " << this->Command
  170. << " process.\n";
  171. } break;
  172. case cmsysProcess_State_Error: {
  173. std::cerr << "cmProcess: Error executing " << this->Command
  174. << " process: " << cmsysProcess_GetErrorString(this->Process)
  175. << "\n";
  176. } break;
  177. case cmsysProcess_State_Exception: {
  178. std::cerr << "cmProcess: " << this->Command
  179. << " process exited with an exception: ";
  180. switch (cmsysProcess_GetExitException(this->Process)) {
  181. case cmsysProcess_Exception_None: {
  182. std::cerr << "None";
  183. } break;
  184. case cmsysProcess_Exception_Fault: {
  185. std::cerr << "Segmentation fault";
  186. } break;
  187. case cmsysProcess_Exception_Illegal: {
  188. std::cerr << "Illegal instruction";
  189. } break;
  190. case cmsysProcess_Exception_Interrupt: {
  191. std::cerr << "Interrupted by user";
  192. } break;
  193. case cmsysProcess_Exception_Numerical: {
  194. std::cerr << "Numerical exception";
  195. } break;
  196. case cmsysProcess_Exception_Other: {
  197. std::cerr << "Unknown";
  198. } break;
  199. }
  200. std::cerr << "\n";
  201. } break;
  202. case cmsysProcess_State_Executing: {
  203. std::cerr << "cmProcess: Never terminated " << this->Command
  204. << " process.\n";
  205. } break;
  206. case cmsysProcess_State_Exited: {
  207. result = cmsysProcess_GetExitValue(this->Process);
  208. std::cerr << "cmProcess: " << this->Command
  209. << " process exited with code " << result << "\n";
  210. } break;
  211. case cmsysProcess_State_Expired: {
  212. std::cerr << "cmProcess: killed " << this->Command
  213. << " process due to timeout.\n";
  214. } break;
  215. case cmsysProcess_State_Killed: {
  216. std::cerr << "cmProcess: killed " << this->Command << " process.\n";
  217. } break;
  218. }
  219. return result;
  220. }
  221. void cmProcess::ChangeTimeout(std::chrono::duration<double> t)
  222. {
  223. this->Timeout = t;
  224. cmsysProcess_SetTimeout(this->Process, this->Timeout);
  225. }
  226. void cmProcess::ResetStartTime()
  227. {
  228. cmsysProcess_ResetStartTime(this->Process);
  229. this->StartTime = std::chrono::steady_clock::now();
  230. }
  231. int cmProcess::GetExitException()
  232. {
  233. return cmsysProcess_GetExitException(this->Process);
  234. }
  235. std::string cmProcess::GetExitExceptionString()
  236. {
  237. return cmsysProcess_GetExceptionString(this->Process);
  238. }