cmExecuteProcessCommand.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 <cstdint>
  5. #include <cstdio>
  6. #include <iostream>
  7. #include <map>
  8. #include <memory>
  9. #include <sstream>
  10. #include <utility>
  11. #include <vector>
  12. #include <cm/optional>
  13. #include <cm/string_view>
  14. #include <cmext/algorithm>
  15. #include <cmext/string_view>
  16. #include <cm3p/uv.h>
  17. #ifndef _WIN32
  18. # include <fcntl.h>
  19. # include "cm_fileno.hxx"
  20. #endif
  21. #include "cmArgumentParser.h"
  22. #include "cmExecutionStatus.h"
  23. #include "cmList.h"
  24. #include "cmMakefile.h"
  25. #include "cmMessageType.h"
  26. #include "cmPolicies.h"
  27. #include "cmProcessOutput.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmUVHandlePtr.h"
  31. #include "cmUVProcessChain.h"
  32. #include "cmUVStream.h"
  33. namespace {
  34. bool cmExecuteProcessCommandIsWhitespace(char c)
  35. {
  36. return (cmIsSpace(c) || c == '\n' || c == '\r');
  37. }
  38. FILE* FopenCLOEXEC(std::string const& path, char const* mode)
  39. {
  40. FILE* f = cmsys::SystemTools::Fopen(path, mode);
  41. #ifndef _WIN32
  42. if (f) {
  43. if (fcntl(cm_fileno(f), F_SETFD, FD_CLOEXEC) < 0) {
  44. fclose(f);
  45. f = nullptr;
  46. }
  47. }
  48. #endif
  49. return f;
  50. }
  51. void cmExecuteProcessCommandFixText(std::vector<char>& output,
  52. bool strip_trailing_whitespace);
  53. void cmExecuteProcessCommandAppend(std::vector<char>& output, char const* data,
  54. std::size_t length);
  55. }
  56. // cmExecuteProcessCommand
  57. bool cmExecuteProcessCommand(std::vector<std::string> const& args,
  58. cmExecutionStatus& status)
  59. {
  60. if (args.empty()) {
  61. status.SetError("called with incorrect number of arguments");
  62. return false;
  63. }
  64. struct Arguments : public ArgumentParser::ParseResult
  65. {
  66. std::vector<std::vector<std::string>> Commands;
  67. std::string OutputVariable;
  68. std::string ErrorVariable;
  69. std::string ResultVariable;
  70. std::string ResultsVariable;
  71. std::string WorkingDirectory;
  72. std::string InputFile;
  73. std::string OutputFile;
  74. std::string ErrorFile;
  75. std::string Timeout;
  76. std::string CommandEcho;
  77. bool OutputQuiet = false;
  78. bool ErrorQuiet = false;
  79. bool OutputStripTrailingWhitespace = false;
  80. bool ErrorStripTrailingWhitespace = false;
  81. bool EchoOutputVariable = false;
  82. bool EchoErrorVariable = false;
  83. cm::optional<std::string> Encoding;
  84. std::string CommandErrorIsFatal;
  85. };
  86. static auto const parser =
  87. cmArgumentParser<Arguments>{}
  88. .Bind("COMMAND"_s, &Arguments::Commands)
  89. .Bind("COMMAND_ECHO"_s, &Arguments::CommandEcho)
  90. .Bind("OUTPUT_VARIABLE"_s, &Arguments::OutputVariable)
  91. .Bind("ERROR_VARIABLE"_s, &Arguments::ErrorVariable)
  92. .Bind("RESULT_VARIABLE"_s, &Arguments::ResultVariable)
  93. .Bind("RESULTS_VARIABLE"_s, &Arguments::ResultsVariable)
  94. .Bind("WORKING_DIRECTORY"_s, &Arguments::WorkingDirectory)
  95. .Bind("INPUT_FILE"_s, &Arguments::InputFile)
  96. .Bind("OUTPUT_FILE"_s, &Arguments::OutputFile)
  97. .Bind("ERROR_FILE"_s, &Arguments::ErrorFile)
  98. .Bind("TIMEOUT"_s, &Arguments::Timeout)
  99. .Bind("OUTPUT_QUIET"_s, &Arguments::OutputQuiet)
  100. .Bind("ERROR_QUIET"_s, &Arguments::ErrorQuiet)
  101. .Bind("OUTPUT_STRIP_TRAILING_WHITESPACE"_s,
  102. &Arguments::OutputStripTrailingWhitespace)
  103. .Bind("ERROR_STRIP_TRAILING_WHITESPACE"_s,
  104. &Arguments::ErrorStripTrailingWhitespace)
  105. .Bind("ENCODING"_s, &Arguments::Encoding)
  106. .Bind("ECHO_OUTPUT_VARIABLE"_s, &Arguments::EchoOutputVariable)
  107. .Bind("ECHO_ERROR_VARIABLE"_s, &Arguments::EchoErrorVariable)
  108. .Bind("COMMAND_ERROR_IS_FATAL"_s, &Arguments::CommandErrorIsFatal);
  109. std::vector<std::string> unparsedArguments;
  110. Arguments arguments = parser.Parse(args, &unparsedArguments);
  111. if (arguments.MaybeReportError(status.GetMakefile())) {
  112. return true;
  113. }
  114. if (!unparsedArguments.empty()) {
  115. status.SetError(" given unknown argument \"" + unparsedArguments.front() +
  116. "\".");
  117. return false;
  118. }
  119. std::string inputFilename = arguments.InputFile;
  120. std::string outputFilename = arguments.OutputFile;
  121. std::string errorFilename = arguments.ErrorFile;
  122. if (!arguments.WorkingDirectory.empty()) {
  123. if (!inputFilename.empty()) {
  124. inputFilename = cmSystemTools::CollapseFullPath(
  125. inputFilename, arguments.WorkingDirectory);
  126. }
  127. if (!outputFilename.empty()) {
  128. outputFilename = cmSystemTools::CollapseFullPath(
  129. outputFilename, arguments.WorkingDirectory);
  130. }
  131. if (!errorFilename.empty()) {
  132. errorFilename = cmSystemTools::CollapseFullPath(
  133. errorFilename, arguments.WorkingDirectory);
  134. }
  135. }
  136. if (!status.GetMakefile().CanIWriteThisFile(outputFilename)) {
  137. status.SetError("attempted to output into a file: " + outputFilename +
  138. " into a source directory.");
  139. cmSystemTools::SetFatalErrorOccurred();
  140. return false;
  141. }
  142. // Check for commands given.
  143. if (arguments.Commands.empty()) {
  144. status.SetError(" called with no COMMAND argument.");
  145. return false;
  146. }
  147. for (std::vector<std::string>& cmd : arguments.Commands) {
  148. if (cmd.empty()) {
  149. status.SetError(" given COMMAND argument with no value.");
  150. return false;
  151. }
  152. #ifdef _WIN32
  153. cmsys::Status shortPathRes = cmSystemTools::MaybePrependCmdExe(cmd);
  154. if (!shortPathRes) {
  155. status.GetMakefile().IssueMessage(
  156. MessageType::WARNING,
  157. cmStrCat("Conversion of COMMAND:\n ", cmd[2], '\n',
  158. "to a short path without spaces failed:\n ",
  159. shortPathRes.GetString()));
  160. }
  161. #endif
  162. }
  163. // Parse the timeout string.
  164. double timeout = -1;
  165. if (!arguments.Timeout.empty()) {
  166. if (sscanf(arguments.Timeout.c_str(), "%lg", &timeout) != 1) {
  167. status.SetError(" called with TIMEOUT value that could not be parsed.");
  168. return false;
  169. }
  170. }
  171. std::string commandErrorIsFatal = arguments.CommandErrorIsFatal;
  172. if (commandErrorIsFatal.empty() && arguments.ResultVariable.empty() &&
  173. arguments.ResultsVariable.empty()) {
  174. commandErrorIsFatal = status.GetMakefile().GetSafeDefinition(
  175. "CMAKE_EXECUTE_PROCESS_COMMAND_ERROR_IS_FATAL");
  176. }
  177. if (!commandErrorIsFatal.empty() && commandErrorIsFatal != "ANY"_s &&
  178. commandErrorIsFatal != "LAST"_s && commandErrorIsFatal != "NONE"_s) {
  179. if (!arguments.CommandErrorIsFatal.empty()) {
  180. status.SetError(
  181. "COMMAND_ERROR_IS_FATAL option can be ANY, LAST or NONE");
  182. return false;
  183. }
  184. status.SetError(cmStrCat(
  185. "Using CMAKE_EXECUTE_PROCESS_COMMAND_ERROR_IS_FATAL with invalid value "
  186. "\"",
  187. commandErrorIsFatal, "\". This variable can be ANY, LAST or NONE"));
  188. return false;
  189. }
  190. // Create a process instance.
  191. cmUVProcessChainBuilder builder;
  192. // Set the command sequence.
  193. for (std::vector<std::string> const& cmd : arguments.Commands) {
  194. builder.AddCommand(cmd);
  195. }
  196. // Set the process working directory.
  197. if (!arguments.WorkingDirectory.empty()) {
  198. builder.SetWorkingDirectory(arguments.WorkingDirectory);
  199. }
  200. // Check the output variables.
  201. std::unique_ptr<FILE, int (*)(FILE*)> inputFile(nullptr, fclose);
  202. if (!inputFilename.empty()) {
  203. inputFile.reset(FopenCLOEXEC(inputFilename, "rb"));
  204. if (inputFile) {
  205. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT,
  206. inputFile.get());
  207. }
  208. } else {
  209. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, stdin);
  210. }
  211. std::unique_ptr<FILE, int (*)(FILE*)> outputFile(nullptr, fclose);
  212. if (!outputFilename.empty()) {
  213. outputFile.reset(FopenCLOEXEC(outputFilename, "wb"));
  214. if (outputFile) {
  215. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT,
  216. outputFile.get());
  217. }
  218. } else {
  219. if (arguments.OutputVariable == arguments.ErrorVariable &&
  220. !arguments.ErrorVariable.empty()) {
  221. builder.SetMergedBuiltinStreams();
  222. } else {
  223. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT);
  224. }
  225. }
  226. std::unique_ptr<FILE, int (*)(FILE*)> errorFile(nullptr, fclose);
  227. if (!errorFilename.empty()) {
  228. if (errorFilename == outputFilename) {
  229. if (outputFile) {
  230. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR,
  231. outputFile.get());
  232. }
  233. } else {
  234. errorFile.reset(FopenCLOEXEC(errorFilename, "wb"));
  235. if (errorFile) {
  236. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR,
  237. errorFile.get());
  238. }
  239. }
  240. } else if (arguments.ErrorVariable.empty() ||
  241. (!arguments.ErrorVariable.empty() &&
  242. arguments.OutputVariable != arguments.ErrorVariable)) {
  243. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
  244. }
  245. // Set the timeout if any.
  246. int64_t timeoutMillis = static_cast<int64_t>(timeout * 1000.0);
  247. bool echo_stdout = false;
  248. bool echo_stderr = false;
  249. bool echo_output_from_variable = true;
  250. std::string echo_output = status.GetMakefile().GetSafeDefinition(
  251. "CMAKE_EXECUTE_PROCESS_COMMAND_ECHO");
  252. if (!arguments.CommandEcho.empty()) {
  253. echo_output_from_variable = false;
  254. echo_output = arguments.CommandEcho;
  255. }
  256. if (!echo_output.empty()) {
  257. if (echo_output == "STDERR") {
  258. echo_stderr = true;
  259. } else if (echo_output == "STDOUT") {
  260. echo_stdout = true;
  261. } else if (echo_output != "NONE") {
  262. std::string error;
  263. if (echo_output_from_variable) {
  264. error = "CMAKE_EXECUTE_PROCESS_COMMAND_ECHO set to '";
  265. } else {
  266. error = " called with '";
  267. }
  268. error += echo_output;
  269. error += "' expected STDERR|STDOUT|NONE";
  270. if (!echo_output_from_variable) {
  271. error += " for COMMAND_ECHO.";
  272. }
  273. status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR, error);
  274. return true;
  275. }
  276. }
  277. if (echo_stdout || echo_stderr) {
  278. std::string command;
  279. for (auto const& cmd : arguments.Commands) {
  280. command += "'";
  281. command += cmJoin(cmd, "' '");
  282. command += "'";
  283. command += "\n";
  284. }
  285. if (echo_stdout) {
  286. std::cout << command;
  287. } else if (echo_stderr) {
  288. std::cerr << command;
  289. }
  290. }
  291. // Start the process.
  292. auto chain = builder.Start();
  293. bool timedOut = false;
  294. cm::uv_timer_ptr timer;
  295. if (timeoutMillis >= 0) {
  296. timer.init(chain.GetLoop(), &timedOut);
  297. timer.start(
  298. [](uv_timer_t* handle) {
  299. auto* timeoutPtr = static_cast<bool*>(handle->data);
  300. *timeoutPtr = true;
  301. },
  302. timeoutMillis, 0);
  303. }
  304. // Read the process output.
  305. struct ReadData
  306. {
  307. bool Finished = false;
  308. std::vector<char> Output;
  309. cm::uv_pipe_ptr Stream;
  310. };
  311. ReadData outputData;
  312. ReadData errorData;
  313. cmPolicies::PolicyStatus const cmp0176 =
  314. status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0176);
  315. cmProcessOutput::Encoding encoding =
  316. cmp0176 == cmPolicies::OLD || cmp0176 == cmPolicies::WARN
  317. ? cmProcessOutput::Auto
  318. : cmProcessOutput::UTF8;
  319. if (arguments.Encoding) {
  320. if (cm::optional<cmProcessOutput::Encoding> maybeEncoding =
  321. cmProcessOutput::FindEncoding(*arguments.Encoding)) {
  322. encoding = *maybeEncoding;
  323. } else {
  324. status.GetMakefile().IssueMessage(
  325. MessageType::AUTHOR_WARNING,
  326. cmStrCat("ENCODING option given unknown value \"", *arguments.Encoding,
  327. "\". Ignoring."));
  328. }
  329. }
  330. cmProcessOutput processOutput(encoding);
  331. std::string strdata;
  332. std::unique_ptr<cmUVStreamReadHandle> outputHandle;
  333. if (chain.OutputStream() >= 0) {
  334. outputData.Stream.init(chain.GetLoop(), 0);
  335. uv_pipe_open(outputData.Stream, chain.OutputStream());
  336. outputHandle = cmUVStreamRead(
  337. outputData.Stream,
  338. [&arguments, &processOutput, &outputData,
  339. &strdata](std::vector<char> data) {
  340. if (!arguments.OutputQuiet) {
  341. if (arguments.OutputVariable.empty() ||
  342. arguments.EchoOutputVariable) {
  343. processOutput.DecodeText(data.data(), data.size(), strdata, 1);
  344. cmSystemTools::Stdout(strdata);
  345. }
  346. if (!arguments.OutputVariable.empty()) {
  347. cmExecuteProcessCommandAppend(outputData.Output, data.data(),
  348. data.size());
  349. }
  350. }
  351. },
  352. [&outputData]() { outputData.Finished = true; });
  353. } else {
  354. outputData.Finished = true;
  355. }
  356. std::unique_ptr<cmUVStreamReadHandle> errorHandle;
  357. if (chain.ErrorStream() >= 0 &&
  358. chain.ErrorStream() != chain.OutputStream()) {
  359. errorData.Stream.init(chain.GetLoop(), 0);
  360. uv_pipe_open(errorData.Stream, chain.ErrorStream());
  361. errorHandle = cmUVStreamRead(
  362. errorData.Stream,
  363. [&arguments, &processOutput, &errorData,
  364. &strdata](std::vector<char> data) {
  365. if (!arguments.ErrorQuiet) {
  366. if (arguments.ErrorVariable.empty() || arguments.EchoErrorVariable) {
  367. processOutput.DecodeText(data.data(), data.size(), strdata, 2);
  368. cmSystemTools::Stderr(strdata);
  369. }
  370. if (!arguments.ErrorVariable.empty()) {
  371. cmExecuteProcessCommandAppend(errorData.Output, data.data(),
  372. data.size());
  373. }
  374. }
  375. },
  376. [&errorData]() { errorData.Finished = true; });
  377. } else {
  378. errorData.Finished = true;
  379. }
  380. while (chain.Valid() && !timedOut &&
  381. !(chain.Finished() && outputData.Finished && errorData.Finished)) {
  382. uv_run(&chain.GetLoop(), UV_RUN_ONCE);
  383. }
  384. if (!arguments.OutputQuiet &&
  385. (arguments.OutputVariable.empty() || arguments.EchoOutputVariable)) {
  386. processOutput.DecodeText(std::string(), strdata, 1);
  387. if (!strdata.empty()) {
  388. cmSystemTools::Stdout(strdata);
  389. }
  390. }
  391. if (!arguments.ErrorQuiet &&
  392. (arguments.ErrorVariable.empty() || arguments.EchoErrorVariable)) {
  393. processOutput.DecodeText(std::string(), strdata, 2);
  394. if (!strdata.empty()) {
  395. cmSystemTools::Stderr(strdata);
  396. }
  397. }
  398. // All output has been read.
  399. processOutput.DecodeText(outputData.Output, outputData.Output);
  400. processOutput.DecodeText(errorData.Output, errorData.Output);
  401. // Fix the text in the output strings.
  402. cmExecuteProcessCommandFixText(outputData.Output,
  403. arguments.OutputStripTrailingWhitespace);
  404. cmExecuteProcessCommandFixText(errorData.Output,
  405. arguments.ErrorStripTrailingWhitespace);
  406. // Store the output obtained.
  407. if (!arguments.OutputVariable.empty() && !outputData.Output.empty()) {
  408. status.GetMakefile().AddDefinition(arguments.OutputVariable,
  409. outputData.Output.data());
  410. }
  411. if (arguments.ErrorVariable != arguments.OutputVariable &&
  412. !arguments.ErrorVariable.empty() && !errorData.Output.empty()) {
  413. status.GetMakefile().AddDefinition(arguments.ErrorVariable,
  414. errorData.Output.data());
  415. }
  416. // Store the result of running the process.
  417. if (!arguments.ResultVariable.empty()) {
  418. if (timedOut) {
  419. status.GetMakefile().AddDefinition(arguments.ResultVariable,
  420. "Process terminated due to timeout");
  421. } else {
  422. auto const* lastStatus = chain.GetStatus().back();
  423. auto exception = lastStatus->GetException();
  424. if (exception.first == cmUVProcessChain::ExceptionCode::None) {
  425. status.GetMakefile().AddDefinition(
  426. arguments.ResultVariable,
  427. std::to_string(static_cast<int>(lastStatus->ExitStatus)));
  428. } else {
  429. status.GetMakefile().AddDefinition(arguments.ResultVariable,
  430. exception.second);
  431. }
  432. }
  433. }
  434. // Store the result of running the processes.
  435. if (!arguments.ResultsVariable.empty()) {
  436. if (timedOut) {
  437. status.GetMakefile().AddDefinition(arguments.ResultsVariable,
  438. "Process terminated due to timeout");
  439. } else {
  440. std::vector<std::string> res;
  441. for (auto const* processStatus : chain.GetStatus()) {
  442. auto exception = processStatus->GetException();
  443. if (exception.first == cmUVProcessChain::ExceptionCode::None) {
  444. res.emplace_back(
  445. std::to_string(static_cast<int>(processStatus->ExitStatus)));
  446. } else {
  447. res.emplace_back(exception.second);
  448. }
  449. }
  450. status.GetMakefile().AddDefinition(arguments.ResultsVariable,
  451. cmList::to_string(res));
  452. }
  453. }
  454. auto queryProcessStatusByIndex = [&chain](std::size_t index) -> std::string {
  455. auto const& processStatus = chain.GetStatus(index);
  456. auto exception = processStatus.GetException();
  457. if (exception.first == cmUVProcessChain::ExceptionCode::None) {
  458. if (processStatus.ExitStatus) {
  459. return cmStrCat("Child return code: ", processStatus.ExitStatus);
  460. }
  461. return "";
  462. }
  463. return cmStrCat("Abnormal exit with child return code: ",
  464. exception.second);
  465. };
  466. if (commandErrorIsFatal == "ANY"_s) {
  467. bool ret = true;
  468. if (timedOut) {
  469. status.SetError("Process terminated due to timeout");
  470. ret = false;
  471. } else {
  472. std::map<std::size_t, std::string> failureIndices;
  473. auto statuses = chain.GetStatus();
  474. for (std::size_t i = 0; i < statuses.size(); ++i) {
  475. std::string processStatus = queryProcessStatusByIndex(i);
  476. if (!processStatus.empty()) {
  477. failureIndices[i] = processStatus;
  478. }
  479. }
  480. if (!failureIndices.empty()) {
  481. std::ostringstream oss;
  482. oss << "failed command indexes:\n";
  483. for (auto const& e : failureIndices) {
  484. oss << " " << e.first + 1 << ": \"" << e.second << "\"\n";
  485. }
  486. status.SetError(oss.str());
  487. ret = false;
  488. }
  489. }
  490. if (!ret) {
  491. cmSystemTools::SetFatalErrorOccurred();
  492. return false;
  493. }
  494. }
  495. if (commandErrorIsFatal == "LAST"_s) {
  496. bool ret = true;
  497. if (timedOut) {
  498. status.SetError("Process terminated due to timeout");
  499. ret = false;
  500. } else {
  501. auto const& lastStatus = chain.GetStatus(arguments.Commands.size() - 1);
  502. auto exception = lastStatus.GetException();
  503. if (exception.first != cmUVProcessChain::ExceptionCode::None) {
  504. status.SetError(cmStrCat("Abnormal exit: ", exception.second));
  505. ret = false;
  506. } else {
  507. int lastIndex = static_cast<int>(arguments.Commands.size() - 1);
  508. std::string const processStatus = queryProcessStatusByIndex(lastIndex);
  509. if (!processStatus.empty()) {
  510. status.SetError("last command failed");
  511. ret = false;
  512. }
  513. }
  514. }
  515. if (!ret) {
  516. cmSystemTools::SetFatalErrorOccurred();
  517. return false;
  518. }
  519. }
  520. return true;
  521. }
  522. namespace {
  523. void cmExecuteProcessCommandFixText(std::vector<char>& output,
  524. bool strip_trailing_whitespace)
  525. {
  526. // Remove \0 characters and the \r part of \r\n pairs.
  527. unsigned int in_index = 0;
  528. unsigned int out_index = 0;
  529. while (in_index < output.size()) {
  530. char c = output[in_index++];
  531. if ((c != '\r' ||
  532. !(in_index < output.size() && output[in_index] == '\n')) &&
  533. c != '\0') {
  534. output[out_index++] = c;
  535. }
  536. }
  537. // Remove trailing whitespace if requested.
  538. if (strip_trailing_whitespace) {
  539. while (out_index > 0 &&
  540. cmExecuteProcessCommandIsWhitespace(output[out_index - 1])) {
  541. --out_index;
  542. }
  543. }
  544. // Shrink the vector to the size needed.
  545. output.resize(out_index);
  546. // Put a terminator on the text string.
  547. output.push_back('\0');
  548. }
  549. void cmExecuteProcessCommandAppend(std::vector<char>& output, char const* data,
  550. std::size_t length)
  551. {
  552. #if defined(__APPLE__)
  553. // HACK on Apple to work around bug with inserting at the
  554. // end of an empty vector. This resulted in random failures
  555. // that were hard to reproduce.
  556. if (output.empty() && length > 0) {
  557. output.push_back(data[0]);
  558. ++data;
  559. --length;
  560. }
  561. #endif
  562. cm::append(output, data, data + length);
  563. }
  564. }