cmProcess.cxx 7.2 KB

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