cmProcess.cxx 7.6 KB

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