cmExecuteProcessCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 "cmExecuteProcessCommand.h"
  11. #include "cmSystemTools.h"
  12. #include <cmsys/Process.h>
  13. #include <ctype.h> /* isspace */
  14. static bool cmExecuteProcessCommandIsWhitespace(char c)
  15. {
  16. return (isspace((int)c) || c == '\n' || c == '\r');
  17. }
  18. void cmExecuteProcessCommandFixText(std::vector<char>& output,
  19. bool strip_trailing_whitespace);
  20. void cmExecuteProcessCommandAppend(std::vector<char>& output,
  21. const char* data, int length);
  22. // cmExecuteProcessCommand
  23. bool cmExecuteProcessCommand
  24. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  25. {
  26. if(args.size() < 1 )
  27. {
  28. this->SetError("called with incorrect number of arguments");
  29. return false;
  30. }
  31. std::vector< std::vector<const char*> > cmds;
  32. std::string arguments;
  33. bool doing_command = false;
  34. size_t command_index = 0;
  35. bool output_quiet = false;
  36. bool error_quiet = false;
  37. bool output_strip_trailing_whitespace = false;
  38. bool error_strip_trailing_whitespace = false;
  39. std::string timeout_string;
  40. std::string input_file;
  41. std::string output_file;
  42. std::string error_file;
  43. std::string output_variable;
  44. std::string error_variable;
  45. std::string result_variable;
  46. std::string working_directory;
  47. for(size_t i=0; i < args.size(); ++i)
  48. {
  49. if(args[i] == "COMMAND")
  50. {
  51. doing_command = true;
  52. command_index = cmds.size();
  53. cmds.push_back(std::vector<const char*>());
  54. }
  55. else if(args[i] == "OUTPUT_VARIABLE")
  56. {
  57. doing_command = false;
  58. if(++i < args.size())
  59. {
  60. output_variable = args[i];
  61. }
  62. else
  63. {
  64. this->SetError(" called with no value for OUTPUT_VARIABLE.");
  65. return false;
  66. }
  67. }
  68. else if(args[i] == "ERROR_VARIABLE")
  69. {
  70. doing_command = false;
  71. if(++i < args.size())
  72. {
  73. error_variable = args[i];
  74. }
  75. else
  76. {
  77. this->SetError(" called with no value for ERROR_VARIABLE.");
  78. return false;
  79. }
  80. }
  81. else if(args[i] == "RESULT_VARIABLE")
  82. {
  83. doing_command = false;
  84. if(++i < args.size())
  85. {
  86. result_variable = args[i];
  87. }
  88. else
  89. {
  90. this->SetError(" called with no value for RESULT_VARIABLE.");
  91. return false;
  92. }
  93. }
  94. else if(args[i] == "WORKING_DIRECTORY")
  95. {
  96. doing_command = false;
  97. if(++i < args.size())
  98. {
  99. working_directory = args[i];
  100. }
  101. else
  102. {
  103. this->SetError(" called with no value for WORKING_DIRECTORY.");
  104. return false;
  105. }
  106. }
  107. else if(args[i] == "INPUT_FILE")
  108. {
  109. doing_command = false;
  110. if(++i < args.size())
  111. {
  112. input_file = args[i];
  113. }
  114. else
  115. {
  116. this->SetError(" called with no value for INPUT_FILE.");
  117. return false;
  118. }
  119. }
  120. else if(args[i] == "OUTPUT_FILE")
  121. {
  122. doing_command = false;
  123. if(++i < args.size())
  124. {
  125. output_file = args[i];
  126. }
  127. else
  128. {
  129. this->SetError(" called with no value for OUTPUT_FILE.");
  130. return false;
  131. }
  132. }
  133. else if(args[i] == "ERROR_FILE")
  134. {
  135. doing_command = false;
  136. if(++i < args.size())
  137. {
  138. error_file = args[i];
  139. }
  140. else
  141. {
  142. this->SetError(" called with no value for ERROR_FILE.");
  143. return false;
  144. }
  145. }
  146. else if(args[i] == "TIMEOUT")
  147. {
  148. doing_command = false;
  149. if(++i < args.size())
  150. {
  151. timeout_string = args[i];
  152. }
  153. else
  154. {
  155. this->SetError(" called with no value for TIMEOUT.");
  156. return false;
  157. }
  158. }
  159. else if(args[i] == "OUTPUT_QUIET")
  160. {
  161. doing_command = false;
  162. output_quiet = true;
  163. }
  164. else if(args[i] == "ERROR_QUIET")
  165. {
  166. doing_command = false;
  167. error_quiet = true;
  168. }
  169. else if(args[i] == "OUTPUT_STRIP_TRAILING_WHITESPACE")
  170. {
  171. doing_command = false;
  172. output_strip_trailing_whitespace = true;
  173. }
  174. else if(args[i] == "ERROR_STRIP_TRAILING_WHITESPACE")
  175. {
  176. doing_command = false;
  177. error_strip_trailing_whitespace = true;
  178. }
  179. else if(doing_command)
  180. {
  181. cmds[command_index].push_back(args[i].c_str());
  182. }
  183. else
  184. {
  185. cmOStringStream e;
  186. e << " given unknown argument \"" << args[i] << "\".";
  187. this->SetError(e.str());
  188. return false;
  189. }
  190. }
  191. if ( !this->Makefile->CanIWriteThisFile(output_file.c_str()) )
  192. {
  193. std::string e = "attempted to output into a file: " + output_file
  194. + " into a source directory.";
  195. this->SetError(e);
  196. cmSystemTools::SetFatalErrorOccured();
  197. return false;
  198. }
  199. // Check for commands given.
  200. if(cmds.empty())
  201. {
  202. this->SetError(" called with no COMMAND argument.");
  203. return false;
  204. }
  205. for(unsigned int i=0; i < cmds.size(); ++i)
  206. {
  207. if(cmds[i].empty())
  208. {
  209. this->SetError(" given COMMAND argument with no value.");
  210. return false;
  211. }
  212. else
  213. {
  214. // Add the null terminating pointer to the command argument list.
  215. cmds[i].push_back(0);
  216. }
  217. }
  218. // Parse the timeout string.
  219. double timeout = -1;
  220. if(!timeout_string.empty())
  221. {
  222. if(sscanf(timeout_string.c_str(), "%lg", &timeout) != 1)
  223. {
  224. this->SetError(" called with TIMEOUT value that could not be parsed.");
  225. return false;
  226. }
  227. }
  228. // Create a process instance.
  229. cmsysProcess* cp = cmsysProcess_New();
  230. // Set the command sequence.
  231. for(unsigned int i=0; i < cmds.size(); ++i)
  232. {
  233. cmsysProcess_AddCommand(cp, &*cmds[i].begin());
  234. }
  235. // Set the process working directory.
  236. if(!working_directory.empty())
  237. {
  238. cmsysProcess_SetWorkingDirectory(cp, working_directory.c_str());
  239. }
  240. // Always hide the process window.
  241. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  242. // Check the output variables.
  243. bool merge_output = (output_variable == error_variable);
  244. if(error_variable.empty() && !error_quiet)
  245. {
  246. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
  247. }
  248. if(!input_file.empty())
  249. {
  250. cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDIN, input_file.c_str());
  251. }
  252. if(!output_file.empty())
  253. {
  254. cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDOUT,
  255. output_file.c_str());
  256. }
  257. if(!error_file.empty())
  258. {
  259. cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDERR,
  260. error_file.c_str());
  261. }
  262. // Set the timeout if any.
  263. if(timeout >= 0)
  264. {
  265. cmsysProcess_SetTimeout(cp, timeout);
  266. }
  267. // Start the process.
  268. cmsysProcess_Execute(cp);
  269. // Read the process output.
  270. std::vector<char> tempOutput;
  271. std::vector<char> tempError;
  272. int length;
  273. char* data;
  274. int p;
  275. while((p = cmsysProcess_WaitForData(cp, &data, &length, 0), p))
  276. {
  277. // Put the output in the right place.
  278. if((p == cmsysProcess_Pipe_STDOUT && !output_quiet) ||
  279. (p == cmsysProcess_Pipe_STDERR && !error_quiet && merge_output))
  280. {
  281. if(output_variable.empty())
  282. {
  283. cmSystemTools::Stdout(data, length);
  284. }
  285. else
  286. {
  287. cmExecuteProcessCommandAppend(tempOutput, data, length);
  288. }
  289. }
  290. else if(p == cmsysProcess_Pipe_STDERR && !error_quiet)
  291. {
  292. if(!error_variable.empty())
  293. {
  294. cmExecuteProcessCommandAppend(tempError, data, length);
  295. }
  296. }
  297. }
  298. // All output has been read. Wait for the process to exit.
  299. cmsysProcess_WaitForExit(cp, 0);
  300. // Fix the text in the output strings.
  301. cmExecuteProcessCommandFixText(tempOutput,
  302. output_strip_trailing_whitespace);
  303. cmExecuteProcessCommandFixText(tempError,
  304. error_strip_trailing_whitespace);
  305. // Store the output obtained.
  306. if(!output_variable.empty() && tempOutput.size())
  307. {
  308. this->Makefile->AddDefinition(output_variable,
  309. &*tempOutput.begin());
  310. }
  311. if(!merge_output && !error_variable.empty() && tempError.size())
  312. {
  313. this->Makefile->AddDefinition(error_variable,
  314. &*tempError.begin());
  315. }
  316. // Store the result of running the process.
  317. if(!result_variable.empty())
  318. {
  319. switch(cmsysProcess_GetState(cp))
  320. {
  321. case cmsysProcess_State_Exited:
  322. {
  323. int v = cmsysProcess_GetExitValue(cp);
  324. char buf[100];
  325. sprintf(buf, "%d", v);
  326. this->Makefile->AddDefinition(result_variable, buf);
  327. }
  328. break;
  329. case cmsysProcess_State_Exception:
  330. this->Makefile->AddDefinition(result_variable,
  331. cmsysProcess_GetExceptionString(cp));
  332. break;
  333. case cmsysProcess_State_Error:
  334. this->Makefile->AddDefinition(result_variable,
  335. cmsysProcess_GetErrorString(cp));
  336. break;
  337. case cmsysProcess_State_Expired:
  338. this->Makefile->AddDefinition(result_variable,
  339. "Process terminated due to timeout");
  340. break;
  341. }
  342. }
  343. // Delete the process instance.
  344. cmsysProcess_Delete(cp);
  345. return true;
  346. }
  347. //----------------------------------------------------------------------------
  348. void cmExecuteProcessCommandFixText(std::vector<char>& output,
  349. bool strip_trailing_whitespace)
  350. {
  351. // Remove \0 characters and the \r part of \r\n pairs.
  352. unsigned int in_index = 0;
  353. unsigned int out_index = 0;
  354. while(in_index < output.size())
  355. {
  356. char c = output[in_index++];
  357. if((c != '\r' || !(in_index < output.size() && output[in_index] == '\n'))
  358. && c != '\0')
  359. {
  360. output[out_index++] = c;
  361. }
  362. }
  363. // Remove trailing whitespace if requested.
  364. if(strip_trailing_whitespace)
  365. {
  366. while(out_index > 0 &&
  367. cmExecuteProcessCommandIsWhitespace(output[out_index-1]))
  368. {
  369. --out_index;
  370. }
  371. }
  372. // Shrink the vector to the size needed.
  373. output.resize(out_index);
  374. // Put a terminator on the text string.
  375. output.push_back('\0');
  376. }
  377. //----------------------------------------------------------------------------
  378. void cmExecuteProcessCommandAppend(std::vector<char>& output,
  379. const char* data, int length)
  380. {
  381. #if defined(__APPLE__)
  382. // HACK on Apple to work around bug with inserting at the
  383. // end of an empty vector. This resulted in random failures
  384. // that were hard to reproduce.
  385. if(output.empty() && length > 0)
  386. {
  387. output.push_back(data[0]);
  388. ++data;
  389. --length;
  390. }
  391. #endif
  392. output.insert(output.end(), data, data+length);
  393. }