cmProcess.cxx 7.6 KB

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