cmProcess.cxx 7.1 KB

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