cmExecuteProcessCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.size() < 1) {
  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. } else {
  153. // Add the null terminating pointer to the command argument list.
  154. cmds[i].push_back(CM_NULLPTR);
  155. }
  156. }
  157. // Parse the timeout string.
  158. double timeout = -1;
  159. if (!timeout_string.empty()) {
  160. if (sscanf(timeout_string.c_str(), "%lg", &timeout) != 1) {
  161. this->SetError(" called with TIMEOUT value that could not be parsed.");
  162. return false;
  163. }
  164. }
  165. // Create a process instance.
  166. cmsysProcess* cp = cmsysProcess_New();
  167. // Set the command sequence.
  168. for (unsigned int i = 0; i < cmds.size(); ++i) {
  169. cmsysProcess_AddCommand(cp, &*cmds[i].begin());
  170. }
  171. // Set the process working directory.
  172. if (!working_directory.empty()) {
  173. cmsysProcess_SetWorkingDirectory(cp, working_directory.c_str());
  174. }
  175. // Always hide the process window.
  176. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  177. // Check the output variables.
  178. bool merge_output = false;
  179. if (!input_file.empty()) {
  180. cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDIN, input_file.c_str());
  181. }
  182. if (!output_file.empty()) {
  183. cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDOUT,
  184. output_file.c_str());
  185. }
  186. if (!error_file.empty()) {
  187. if (error_file == output_file) {
  188. merge_output = true;
  189. } else {
  190. cmsysProcess_SetPipeFile(cp, cmsysProcess_Pipe_STDERR,
  191. error_file.c_str());
  192. }
  193. }
  194. if (!output_variable.empty() && output_variable == error_variable) {
  195. merge_output = true;
  196. }
  197. if (merge_output) {
  198. cmsysProcess_SetOption(cp, cmsysProcess_Option_MergeOutput, 1);
  199. }
  200. // Set the timeout if any.
  201. if (timeout >= 0) {
  202. cmsysProcess_SetTimeout(cp, timeout);
  203. }
  204. // Start the process.
  205. cmsysProcess_Execute(cp);
  206. // Read the process output.
  207. std::vector<char> tempOutput;
  208. std::vector<char> tempError;
  209. int length;
  210. char* data;
  211. int p;
  212. while ((p = cmsysProcess_WaitForData(cp, &data, &length, CM_NULLPTR), p)) {
  213. // Put the output in the right place.
  214. if (p == cmsysProcess_Pipe_STDOUT && !output_quiet) {
  215. if (output_variable.empty()) {
  216. cmSystemTools::Stdout(data, length);
  217. } else {
  218. cmExecuteProcessCommandAppend(tempOutput, data, length);
  219. }
  220. } else if (p == cmsysProcess_Pipe_STDERR && !error_quiet) {
  221. if (error_variable.empty()) {
  222. cmSystemTools::Stderr(data, length);
  223. } else {
  224. cmExecuteProcessCommandAppend(tempError, data, length);
  225. }
  226. }
  227. }
  228. // All output has been read. Wait for the process to exit.
  229. cmsysProcess_WaitForExit(cp, CM_NULLPTR);
  230. // Fix the text in the output strings.
  231. cmExecuteProcessCommandFixText(tempOutput, output_strip_trailing_whitespace);
  232. cmExecuteProcessCommandFixText(tempError, error_strip_trailing_whitespace);
  233. // Store the output obtained.
  234. if (!output_variable.empty() && !tempOutput.empty()) {
  235. this->Makefile->AddDefinition(output_variable, &*tempOutput.begin());
  236. }
  237. if (!merge_output && !error_variable.empty() && !tempError.empty()) {
  238. this->Makefile->AddDefinition(error_variable, &*tempError.begin());
  239. }
  240. // Store the result of running the process.
  241. if (!result_variable.empty()) {
  242. switch (cmsysProcess_GetState(cp)) {
  243. case cmsysProcess_State_Exited: {
  244. int v = cmsysProcess_GetExitValue(cp);
  245. char buf[100];
  246. sprintf(buf, "%d", v);
  247. this->Makefile->AddDefinition(result_variable, buf);
  248. } break;
  249. case cmsysProcess_State_Exception:
  250. this->Makefile->AddDefinition(result_variable,
  251. cmsysProcess_GetExceptionString(cp));
  252. break;
  253. case cmsysProcess_State_Error:
  254. this->Makefile->AddDefinition(result_variable,
  255. cmsysProcess_GetErrorString(cp));
  256. break;
  257. case cmsysProcess_State_Expired:
  258. this->Makefile->AddDefinition(result_variable,
  259. "Process terminated due to timeout");
  260. break;
  261. }
  262. }
  263. // Delete the process instance.
  264. cmsysProcess_Delete(cp);
  265. return true;
  266. }
  267. void cmExecuteProcessCommandFixText(std::vector<char>& output,
  268. bool strip_trailing_whitespace)
  269. {
  270. // Remove \0 characters and the \r part of \r\n pairs.
  271. unsigned int in_index = 0;
  272. unsigned int out_index = 0;
  273. while (in_index < output.size()) {
  274. char c = output[in_index++];
  275. if ((c != '\r' ||
  276. !(in_index < output.size() && output[in_index] == '\n')) &&
  277. c != '\0') {
  278. output[out_index++] = c;
  279. }
  280. }
  281. // Remove trailing whitespace if requested.
  282. if (strip_trailing_whitespace) {
  283. while (out_index > 0 &&
  284. cmExecuteProcessCommandIsWhitespace(output[out_index - 1])) {
  285. --out_index;
  286. }
  287. }
  288. // Shrink the vector to the size needed.
  289. output.resize(out_index);
  290. // Put a terminator on the text string.
  291. output.push_back('\0');
  292. }
  293. void cmExecuteProcessCommandAppend(std::vector<char>& output, const char* data,
  294. int length)
  295. {
  296. #if defined(__APPLE__)
  297. // HACK on Apple to work around bug with inserting at the
  298. // end of an empty vector. This resulted in random failures
  299. // that were hard to reproduce.
  300. if (output.empty() && length > 0) {
  301. output.push_back(data[0]);
  302. ++data;
  303. --length;
  304. }
  305. #endif
  306. output.insert(output.end(), data, data + length);
  307. }