cmExecProgramCommand.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "cmExecProgramCommand.h"
  11. #include "cmSystemTools.h"
  12. #include <cmsys/Process.h>
  13. // cmExecProgramCommand
  14. bool cmExecProgramCommand
  15. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  16. {
  17. if(args.size() < 1 )
  18. {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. std::string arguments;
  23. bool doingargs = false;
  24. int count = 0;
  25. std::string output_variable;
  26. bool haveoutput_variable = false;
  27. std::string return_variable;
  28. bool havereturn_variable = false;
  29. for(size_t i=0; i < args.size(); ++i)
  30. {
  31. if(args[i] == "OUTPUT_VARIABLE")
  32. {
  33. count++;
  34. doingargs = false;
  35. havereturn_variable = false;
  36. haveoutput_variable = true;
  37. }
  38. else if ( haveoutput_variable )
  39. {
  40. if (!output_variable.empty())
  41. {
  42. this->SetError("called with incorrect number of arguments");
  43. return false;
  44. }
  45. output_variable = args[i];
  46. haveoutput_variable = false;
  47. count ++;
  48. }
  49. else if(args[i] == "RETURN_VALUE")
  50. {
  51. count++;
  52. doingargs = false;
  53. haveoutput_variable = false;
  54. havereturn_variable = true;
  55. }
  56. else if ( havereturn_variable )
  57. {
  58. if (!return_variable.empty())
  59. {
  60. this->SetError("called with incorrect number of arguments");
  61. return false;
  62. }
  63. return_variable = args[i];
  64. havereturn_variable = false;
  65. count ++;
  66. }
  67. else if(args[i] == "ARGS")
  68. {
  69. count++;
  70. havereturn_variable = false;
  71. haveoutput_variable = false;
  72. doingargs = true;
  73. }
  74. else if(doingargs)
  75. {
  76. arguments += args[i];
  77. arguments += " ";
  78. count++;
  79. }
  80. }
  81. std::string command;
  82. if(arguments.size())
  83. {
  84. command = cmSystemTools::ConvertToRunCommandPath(args[0].c_str());
  85. command += " ";
  86. command += arguments;
  87. }
  88. else
  89. {
  90. command = args[0];
  91. }
  92. bool verbose = true;
  93. if(!output_variable.empty())
  94. {
  95. verbose = false;
  96. }
  97. int retVal = 0;
  98. std::string output;
  99. bool result = true;
  100. if(args.size() - count == 2)
  101. {
  102. cmSystemTools::MakeDirectory(args[1].c_str());
  103. result = cmExecProgramCommand::RunCommand(command.c_str(), output, retVal,
  104. args[1].c_str(), verbose);
  105. }
  106. else
  107. {
  108. result = cmExecProgramCommand::RunCommand(command.c_str(), output,
  109. retVal, 0, verbose);
  110. }
  111. if(!result)
  112. {
  113. retVal = -1;
  114. }
  115. if (!output_variable.empty())
  116. {
  117. std::string::size_type first = output.find_first_not_of(" \n\t\r");
  118. std::string::size_type last = output.find_last_not_of(" \n\t\r");
  119. if(first == std::string::npos)
  120. {
  121. first = 0;
  122. }
  123. if(last == std::string::npos)
  124. {
  125. last = output.size()-1;
  126. }
  127. std::string coutput = std::string(output, first, last-first+1);
  128. this->Makefile->AddDefinition(output_variable, coutput.c_str());
  129. }
  130. if (!return_variable.empty())
  131. {
  132. char buffer[100];
  133. sprintf(buffer, "%d", retVal);
  134. this->Makefile->AddDefinition(return_variable, buffer);
  135. }
  136. return true;
  137. }
  138. bool cmExecProgramCommand::RunCommand(const char* command,
  139. std::string& output,
  140. int &retVal,
  141. const char* dir,
  142. bool verbose)
  143. {
  144. if(cmSystemTools::GetRunCommandOutput())
  145. {
  146. verbose = false;
  147. }
  148. #if defined(WIN32) && !defined(__CYGWIN__)
  149. // if the command does not start with a quote, then
  150. // try to find the program, and if the program can not be
  151. // found use system to run the command as it must be a built in
  152. // shell command like echo or dir
  153. int count = 0;
  154. std::string shortCmd;
  155. if(command[0] == '\"')
  156. {
  157. // count the number of quotes
  158. for(const char* s = command; *s != 0; ++s)
  159. {
  160. if(*s == '\"')
  161. {
  162. count++;
  163. if(count > 2)
  164. {
  165. break;
  166. }
  167. }
  168. }
  169. // if there are more than two double quotes use
  170. // GetShortPathName, the cmd.exe program in windows which
  171. // is used by system fails to execute if there are more than
  172. // one set of quotes in the arguments
  173. if(count > 2)
  174. {
  175. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  176. if(quoted.find(command))
  177. {
  178. std::string cmd = quoted.match(1);
  179. std::string args = quoted.match(2);
  180. if(! cmSystemTools::FileExists(cmd.c_str()) )
  181. {
  182. shortCmd = cmd;
  183. }
  184. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  185. {
  186. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  187. return false;
  188. }
  189. shortCmd += " ";
  190. shortCmd += args;
  191. command = shortCmd.c_str();
  192. }
  193. else
  194. {
  195. cmSystemTools::Error("Could not parse command line with quotes ",
  196. command);
  197. }
  198. }
  199. }
  200. #endif
  201. // Allocate a process instance.
  202. cmsysProcess* cp = cmsysProcess_New();
  203. if(!cp)
  204. {
  205. cmSystemTools::Error("Error allocating process instance.");
  206. return false;
  207. }
  208. #if defined(WIN32) && !defined(__CYGWIN__)
  209. if(dir)
  210. {
  211. cmsysProcess_SetWorkingDirectory(cp, dir);
  212. }
  213. if(cmSystemTools::GetRunCommandHideConsole())
  214. {
  215. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  216. }
  217. cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
  218. const char* cmd[] = {command, 0};
  219. cmsysProcess_SetCommand(cp, cmd);
  220. #else
  221. std::string commandInDir;
  222. if(dir)
  223. {
  224. commandInDir = "cd \"";
  225. commandInDir += dir;
  226. commandInDir += "\" && ";
  227. commandInDir += command;
  228. }
  229. else
  230. {
  231. commandInDir = command;
  232. }
  233. #ifndef __VMS
  234. commandInDir += " 2>&1";
  235. #endif
  236. command = commandInDir.c_str();
  237. if(verbose)
  238. {
  239. cmSystemTools::Stdout("running ");
  240. cmSystemTools::Stdout(command);
  241. cmSystemTools::Stdout("\n");
  242. }
  243. fflush(stdout);
  244. fflush(stderr);
  245. const char* cmd[] = {"/bin/sh", "-c", command, 0};
  246. cmsysProcess_SetCommand(cp, cmd);
  247. #endif
  248. cmsysProcess_Execute(cp);
  249. // Read the process output.
  250. int length;
  251. char* data;
  252. int p;
  253. while((p = cmsysProcess_WaitForData(cp, &data, &length, 0), p))
  254. {
  255. if(p == cmsysProcess_Pipe_STDOUT || p == cmsysProcess_Pipe_STDERR)
  256. {
  257. if(verbose)
  258. {
  259. cmSystemTools::Stdout(data, length);
  260. }
  261. output.append(data, length);
  262. }
  263. }
  264. // All output has been read. Wait for the process to exit.
  265. cmsysProcess_WaitForExit(cp, 0);
  266. // Check the result of running the process.
  267. std::string msg;
  268. switch(cmsysProcess_GetState(cp))
  269. {
  270. case cmsysProcess_State_Exited:
  271. retVal = cmsysProcess_GetExitValue(cp);
  272. break;
  273. case cmsysProcess_State_Exception:
  274. retVal = -1;
  275. msg += "\nProcess terminated due to: ";
  276. msg += cmsysProcess_GetExceptionString(cp);
  277. break;
  278. case cmsysProcess_State_Error:
  279. retVal = -1;
  280. msg += "\nProcess failed because: ";
  281. msg += cmsysProcess_GetErrorString(cp);
  282. break;
  283. case cmsysProcess_State_Expired:
  284. retVal = -1;
  285. msg += "\nProcess terminated due to timeout.";
  286. break;
  287. }
  288. if(!msg.empty())
  289. {
  290. #if defined(WIN32) && !defined(__CYGWIN__)
  291. // Old Windows process execution printed this info.
  292. msg += "\n\nfor command: ";
  293. msg += command;
  294. if(dir)
  295. {
  296. msg += "\nin dir: ";
  297. msg += dir;
  298. }
  299. msg += "\n";
  300. if(verbose)
  301. {
  302. cmSystemTools::Stdout(msg.c_str());
  303. }
  304. output += msg;
  305. #else
  306. // Old UNIX process execution only put message in output.
  307. output += msg;
  308. #endif
  309. }
  310. // Delete the process instance.
  311. cmsysProcess_Delete(cp);
  312. return true;
  313. }