cmExecuteProcessCommand.cxx 11 KB

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