cmProcess.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmProcess.h"
  11. #include <cmConfigure.h>
  12. #include <cmSystemTools.h>
  13. #include <iostream>
  14. cmProcess::cmProcess()
  15. {
  16. this->Process = CM_NULLPTR;
  17. this->Timeout = 0;
  18. this->TotalTime = 0;
  19. this->ExitValue = 0;
  20. this->Id = 0;
  21. this->StartTime = 0;
  22. }
  23. cmProcess::~cmProcess()
  24. {
  25. cmsysProcess_Delete(this->Process);
  26. }
  27. void cmProcess::SetCommand(const char* command)
  28. {
  29. this->Command = command;
  30. }
  31. void cmProcess::SetCommandArguments(std::vector<std::string> const& args)
  32. {
  33. this->Arguments = args;
  34. }
  35. bool cmProcess::StartProcess()
  36. {
  37. if (this->Command.empty()) {
  38. return false;
  39. }
  40. this->StartTime = cmSystemTools::GetTime();
  41. this->ProcessArgs.clear();
  42. // put the command as arg0
  43. this->ProcessArgs.push_back(this->Command.c_str());
  44. // now put the command arguments in
  45. for (std::vector<std::string>::iterator i = this->Arguments.begin();
  46. i != this->Arguments.end(); ++i) {
  47. this->ProcessArgs.push_back(i->c_str());
  48. }
  49. this->ProcessArgs.push_back(CM_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, double timeout)
  102. {
  103. for (;;) {
  104. // Look for lines already buffered.
  105. if (this->Output.GetLine(line)) {
  106. return cmsysProcess_Pipe_STDOUT;
  107. }
  108. // Check for more data from the process.
  109. char* data;
  110. int length;
  111. int p = cmsysProcess_WaitForData(this->Process, &data, &length, &timeout);
  112. if (p == cmsysProcess_Pipe_Timeout) {
  113. return cmsysProcess_Pipe_Timeout;
  114. }
  115. if (p == cmsysProcess_Pipe_STDOUT) {
  116. this->Output.insert(this->Output.end(), data, data + length);
  117. } else { // p == cmsysProcess_Pipe_None
  118. // The process will provide no more data.
  119. break;
  120. }
  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. }
  217. int cmProcess::GetExitException()
  218. {
  219. return cmsysProcess_GetExitException(this->Process);
  220. }