cmExecuteProcessCommand.cxx 13 KB

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