cmProcess.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <cmSystemTools.h>
  6. #include <iostream>
  7. cmProcess::cmProcess()
  8. {
  9. this->Process = CM_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(CM_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. for (;;) {
  97. // Look for lines already buffered.
  98. if (this->Output.GetLine(line)) {
  99. return cmsysProcess_Pipe_STDOUT;
  100. }
  101. // Check for more data from the process.
  102. char* data;
  103. int length;
  104. int p = cmsysProcess_WaitForData(this->Process, &data, &length, &timeout);
  105. if (p == cmsysProcess_Pipe_Timeout) {
  106. return cmsysProcess_Pipe_Timeout;
  107. }
  108. if (p == cmsysProcess_Pipe_STDOUT) {
  109. this->Output.insert(this->Output.end(), data, data + length);
  110. } else { // p == cmsysProcess_Pipe_None
  111. // The process will provide no more data.
  112. break;
  113. }
  114. }
  115. // Look for partial last lines.
  116. if (this->Output.GetLast(line)) {
  117. return cmsysProcess_Pipe_STDOUT;
  118. }
  119. // No more data. Wait for process exit.
  120. if (!cmsysProcess_WaitForExit(this->Process, &timeout)) {
  121. return cmsysProcess_Pipe_Timeout;
  122. }
  123. // Record exit information.
  124. this->ExitValue = cmsysProcess_GetExitValue(this->Process);
  125. this->TotalTime = cmSystemTools::GetTime() - this->StartTime;
  126. // Because of a processor clock scew the runtime may become slightly
  127. // negative. If someone changed the system clock while the process was
  128. // running this may be even more. Make sure not to report a negative
  129. // duration here.
  130. if (this->TotalTime <= 0.0) {
  131. this->TotalTime = 0.0;
  132. }
  133. // std::cerr << "Time to run: " << this->TotalTime << "\n";
  134. return cmsysProcess_Pipe_None;
  135. }
  136. // return the process status
  137. int cmProcess::GetProcessStatus()
  138. {
  139. if (!this->Process) {
  140. return cmsysProcess_State_Exited;
  141. }
  142. return cmsysProcess_GetState(this->Process);
  143. }
  144. int cmProcess::ReportStatus()
  145. {
  146. int result = 1;
  147. switch (cmsysProcess_GetState(this->Process)) {
  148. case cmsysProcess_State_Starting: {
  149. std::cerr << "cmProcess: Never started " << this->Command
  150. << " process.\n";
  151. } break;
  152. case cmsysProcess_State_Error: {
  153. std::cerr << "cmProcess: Error executing " << this->Command
  154. << " process: " << cmsysProcess_GetErrorString(this->Process)
  155. << "\n";
  156. } break;
  157. case cmsysProcess_State_Exception: {
  158. std::cerr << "cmProcess: " << this->Command
  159. << " process exited with an exception: ";
  160. switch (cmsysProcess_GetExitException(this->Process)) {
  161. case cmsysProcess_Exception_None: {
  162. std::cerr << "None";
  163. } break;
  164. case cmsysProcess_Exception_Fault: {
  165. std::cerr << "Segmentation fault";
  166. } break;
  167. case cmsysProcess_Exception_Illegal: {
  168. std::cerr << "Illegal instruction";
  169. } break;
  170. case cmsysProcess_Exception_Interrupt: {
  171. std::cerr << "Interrupted by user";
  172. } break;
  173. case cmsysProcess_Exception_Numerical: {
  174. std::cerr << "Numerical exception";
  175. } break;
  176. case cmsysProcess_Exception_Other: {
  177. std::cerr << "Unknown";
  178. } break;
  179. }
  180. std::cerr << "\n";
  181. } break;
  182. case cmsysProcess_State_Executing: {
  183. std::cerr << "cmProcess: Never terminated " << this->Command
  184. << " process.\n";
  185. } break;
  186. case cmsysProcess_State_Exited: {
  187. result = cmsysProcess_GetExitValue(this->Process);
  188. std::cerr << "cmProcess: " << this->Command
  189. << " process exited with code " << result << "\n";
  190. } break;
  191. case cmsysProcess_State_Expired: {
  192. std::cerr << "cmProcess: killed " << this->Command
  193. << " process due to timeout.\n";
  194. } break;
  195. case cmsysProcess_State_Killed: {
  196. std::cerr << "cmProcess: killed " << this->Command << " process.\n";
  197. } break;
  198. }
  199. return result;
  200. }
  201. void cmProcess::ChangeTimeout(double t)
  202. {
  203. this->Timeout = t;
  204. cmsysProcess_SetTimeout(this->Process, this->Timeout);
  205. }
  206. void cmProcess::ResetStartTime()
  207. {
  208. cmsysProcess_ResetStartTime(this->Process);
  209. this->StartTime = cmSystemTools::GetTime();
  210. }
  211. int cmProcess::GetExitException()
  212. {
  213. return cmsysProcess_GetExitException(this->Process);
  214. }