cmProcess.cxx 7.2 KB

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