cmExecuteProcessCommand.cxx 10 KB

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