cmExecuteProcessCommand.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. cmSystemTools::MaybePrependCmdExe(cmd);
  153. }
  154. // Parse the timeout string.
  155. double timeout = -1;
  156. if (!arguments.Timeout.empty()) {
  157. if (sscanf(arguments.Timeout.c_str(), "%lg", &timeout) != 1) {
  158. status.SetError(" called with TIMEOUT value that could not be parsed.");
  159. return false;
  160. }
  161. }
  162. std::string commandErrorIsFatal = arguments.CommandErrorIsFatal;
  163. if (commandErrorIsFatal.empty() && arguments.ResultVariable.empty() &&
  164. arguments.ResultsVariable.empty()) {
  165. commandErrorIsFatal = status.GetMakefile().GetSafeDefinition(
  166. "CMAKE_EXECUTE_PROCESS_COMMAND_ERROR_IS_FATAL");
  167. }
  168. if (!commandErrorIsFatal.empty() && commandErrorIsFatal != "ANY"_s &&
  169. commandErrorIsFatal != "LAST"_s && commandErrorIsFatal != "NONE"_s) {
  170. if (!arguments.CommandErrorIsFatal.empty()) {
  171. status.SetError(
  172. "COMMAND_ERROR_IS_FATAL option can be ANY, LAST or NONE");
  173. return false;
  174. }
  175. status.SetError(cmStrCat(
  176. "Using CMAKE_EXECUTE_PROCESS_COMMAND_ERROR_IS_FATAL with invalid value "
  177. "\"",
  178. commandErrorIsFatal, "\". This variable can be ANY, LAST or NONE"));
  179. return false;
  180. }
  181. // Create a process instance.
  182. cmUVProcessChainBuilder builder;
  183. // Set the command sequence.
  184. for (std::vector<std::string> const& cmd : arguments.Commands) {
  185. builder.AddCommand(cmd);
  186. }
  187. // Set the process working directory.
  188. if (!arguments.WorkingDirectory.empty()) {
  189. builder.SetWorkingDirectory(arguments.WorkingDirectory);
  190. }
  191. // Check the output variables.
  192. std::unique_ptr<FILE, int (*)(FILE*)> inputFile(nullptr, fclose);
  193. if (!inputFilename.empty()) {
  194. inputFile.reset(FopenCLOEXEC(inputFilename, "rb"));
  195. if (inputFile) {
  196. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT,
  197. inputFile.get());
  198. }
  199. } else {
  200. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, stdin);
  201. }
  202. std::unique_ptr<FILE, int (*)(FILE*)> outputFile(nullptr, fclose);
  203. if (!outputFilename.empty()) {
  204. outputFile.reset(FopenCLOEXEC(outputFilename, "wb"));
  205. if (outputFile) {
  206. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT,
  207. outputFile.get());
  208. }
  209. } else {
  210. if (arguments.OutputVariable == arguments.ErrorVariable &&
  211. !arguments.ErrorVariable.empty()) {
  212. builder.SetMergedBuiltinStreams();
  213. } else {
  214. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT);
  215. }
  216. }
  217. std::unique_ptr<FILE, int (*)(FILE*)> errorFile(nullptr, fclose);
  218. if (!errorFilename.empty()) {
  219. if (errorFilename == outputFilename) {
  220. if (outputFile) {
  221. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR,
  222. outputFile.get());
  223. }
  224. } else {
  225. errorFile.reset(FopenCLOEXEC(errorFilename, "wb"));
  226. if (errorFile) {
  227. builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR,
  228. errorFile.get());
  229. }
  230. }
  231. } else if (arguments.ErrorVariable.empty() ||
  232. (!arguments.ErrorVariable.empty() &&
  233. arguments.OutputVariable != arguments.ErrorVariable)) {
  234. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
  235. }
  236. // Set the timeout if any.
  237. int64_t timeoutMillis = static_cast<int64_t>(timeout * 1000.0);
  238. bool echo_stdout = false;
  239. bool echo_stderr = false;
  240. bool echo_output_from_variable = true;
  241. std::string echo_output = status.GetMakefile().GetSafeDefinition(
  242. "CMAKE_EXECUTE_PROCESS_COMMAND_ECHO");
  243. if (!arguments.CommandEcho.empty()) {
  244. echo_output_from_variable = false;
  245. echo_output = arguments.CommandEcho;
  246. }
  247. if (!echo_output.empty()) {
  248. if (echo_output == "STDERR") {
  249. echo_stderr = true;
  250. } else if (echo_output == "STDOUT") {
  251. echo_stdout = true;
  252. } else if (echo_output != "NONE") {
  253. std::string error;
  254. if (echo_output_from_variable) {
  255. error = "CMAKE_EXECUTE_PROCESS_COMMAND_ECHO set to '";
  256. } else {
  257. error = " called with '";
  258. }
  259. error += echo_output;
  260. error += "' expected STDERR|STDOUT|NONE";
  261. if (!echo_output_from_variable) {
  262. error += " for COMMAND_ECHO.";
  263. }
  264. status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR, error);
  265. return true;
  266. }
  267. }
  268. if (echo_stdout || echo_stderr) {
  269. std::string command;
  270. for (auto const& cmd : arguments.Commands) {
  271. command += "'";
  272. command += cmJoin(cmd, "' '");
  273. command += "'";
  274. command += "\n";
  275. }
  276. if (echo_stdout) {
  277. std::cout << command;
  278. } else if (echo_stderr) {
  279. std::cerr << command;
  280. }
  281. }
  282. // Start the process.
  283. auto chain = builder.Start();
  284. bool timedOut = false;
  285. cm::uv_timer_ptr timer;
  286. if (timeoutMillis >= 0) {
  287. timer.init(chain.GetLoop(), &timedOut);
  288. timer.start(
  289. [](uv_timer_t* handle) {
  290. auto* timeoutPtr = static_cast<bool*>(handle->data);
  291. *timeoutPtr = true;
  292. },
  293. timeoutMillis, 0);
  294. }
  295. // Read the process output.
  296. struct ReadData
  297. {
  298. bool Finished = false;
  299. std::vector<char> Output;
  300. cm::uv_pipe_ptr Stream;
  301. };
  302. ReadData outputData;
  303. ReadData errorData;
  304. cmPolicies::PolicyStatus const cmp0176 =
  305. status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0176);
  306. cmProcessOutput::Encoding encoding =
  307. cmp0176 == cmPolicies::OLD || cmp0176 == cmPolicies::WARN
  308. ? cmProcessOutput::Auto
  309. : cmProcessOutput::UTF8;
  310. if (arguments.Encoding) {
  311. if (cm::optional<cmProcessOutput::Encoding> maybeEncoding =
  312. cmProcessOutput::FindEncoding(*arguments.Encoding)) {
  313. encoding = *maybeEncoding;
  314. } else {
  315. status.GetMakefile().IssueMessage(
  316. MessageType::AUTHOR_WARNING,
  317. cmStrCat("ENCODING option given unknown value \"", *arguments.Encoding,
  318. "\". Ignoring."));
  319. }
  320. }
  321. cmProcessOutput processOutput(encoding);
  322. std::string strdata;
  323. std::unique_ptr<cmUVStreamReadHandle> outputHandle;
  324. if (chain.OutputStream() >= 0) {
  325. outputData.Stream.init(chain.GetLoop(), 0);
  326. uv_pipe_open(outputData.Stream, chain.OutputStream());
  327. outputHandle = cmUVStreamRead(
  328. outputData.Stream,
  329. [&arguments, &processOutput, &outputData,
  330. &strdata](std::vector<char> data) {
  331. if (!arguments.OutputQuiet) {
  332. if (arguments.OutputVariable.empty() ||
  333. arguments.EchoOutputVariable) {
  334. processOutput.DecodeText(data.data(), data.size(), strdata, 1);
  335. cmSystemTools::Stdout(strdata);
  336. }
  337. if (!arguments.OutputVariable.empty()) {
  338. cmExecuteProcessCommandAppend(outputData.Output, data.data(),
  339. data.size());
  340. }
  341. }
  342. },
  343. [&outputData]() { outputData.Finished = true; });
  344. } else {
  345. outputData.Finished = true;
  346. }
  347. std::unique_ptr<cmUVStreamReadHandle> errorHandle;
  348. if (chain.ErrorStream() >= 0 &&
  349. chain.ErrorStream() != chain.OutputStream()) {
  350. errorData.Stream.init(chain.GetLoop(), 0);
  351. uv_pipe_open(errorData.Stream, chain.ErrorStream());
  352. errorHandle = cmUVStreamRead(
  353. errorData.Stream,
  354. [&arguments, &processOutput, &errorData,
  355. &strdata](std::vector<char> data) {
  356. if (!arguments.ErrorQuiet) {
  357. if (arguments.ErrorVariable.empty() || arguments.EchoErrorVariable) {
  358. processOutput.DecodeText(data.data(), data.size(), strdata, 2);
  359. cmSystemTools::Stderr(strdata);
  360. }
  361. if (!arguments.ErrorVariable.empty()) {
  362. cmExecuteProcessCommandAppend(errorData.Output, data.data(),
  363. data.size());
  364. }
  365. }
  366. },
  367. [&errorData]() { errorData.Finished = true; });
  368. } else {
  369. errorData.Finished = true;
  370. }
  371. while (chain.Valid() && !timedOut &&
  372. !(chain.Finished() && outputData.Finished && errorData.Finished)) {
  373. uv_run(&chain.GetLoop(), UV_RUN_ONCE);
  374. }
  375. if (!arguments.OutputQuiet &&
  376. (arguments.OutputVariable.empty() || arguments.EchoOutputVariable)) {
  377. processOutput.DecodeText(std::string(), strdata, 1);
  378. if (!strdata.empty()) {
  379. cmSystemTools::Stdout(strdata);
  380. }
  381. }
  382. if (!arguments.ErrorQuiet &&
  383. (arguments.ErrorVariable.empty() || arguments.EchoErrorVariable)) {
  384. processOutput.DecodeText(std::string(), strdata, 2);
  385. if (!strdata.empty()) {
  386. cmSystemTools::Stderr(strdata);
  387. }
  388. }
  389. // All output has been read.
  390. processOutput.DecodeText(outputData.Output, outputData.Output);
  391. processOutput.DecodeText(errorData.Output, errorData.Output);
  392. // Fix the text in the output strings.
  393. cmExecuteProcessCommandFixText(outputData.Output,
  394. arguments.OutputStripTrailingWhitespace);
  395. cmExecuteProcessCommandFixText(errorData.Output,
  396. arguments.ErrorStripTrailingWhitespace);
  397. // Store the output obtained.
  398. if (!arguments.OutputVariable.empty() && !outputData.Output.empty()) {
  399. status.GetMakefile().AddDefinition(arguments.OutputVariable,
  400. outputData.Output.data());
  401. }
  402. if (arguments.ErrorVariable != arguments.OutputVariable &&
  403. !arguments.ErrorVariable.empty() && !errorData.Output.empty()) {
  404. status.GetMakefile().AddDefinition(arguments.ErrorVariable,
  405. errorData.Output.data());
  406. }
  407. // Store the result of running the process.
  408. if (!arguments.ResultVariable.empty()) {
  409. if (timedOut) {
  410. status.GetMakefile().AddDefinition(arguments.ResultVariable,
  411. "Process terminated due to timeout");
  412. } else {
  413. auto const* lastStatus = chain.GetStatus().back();
  414. auto exception = lastStatus->GetException();
  415. if (exception.first == cmUVProcessChain::ExceptionCode::None) {
  416. status.GetMakefile().AddDefinition(
  417. arguments.ResultVariable,
  418. std::to_string(static_cast<int>(lastStatus->ExitStatus)));
  419. } else {
  420. status.GetMakefile().AddDefinition(arguments.ResultVariable,
  421. exception.second);
  422. }
  423. }
  424. }
  425. // Store the result of running the processes.
  426. if (!arguments.ResultsVariable.empty()) {
  427. if (timedOut) {
  428. status.GetMakefile().AddDefinition(arguments.ResultsVariable,
  429. "Process terminated due to timeout");
  430. } else {
  431. std::vector<std::string> res;
  432. for (auto const* processStatus : chain.GetStatus()) {
  433. auto exception = processStatus->GetException();
  434. if (exception.first == cmUVProcessChain::ExceptionCode::None) {
  435. res.emplace_back(
  436. std::to_string(static_cast<int>(processStatus->ExitStatus)));
  437. } else {
  438. res.emplace_back(exception.second);
  439. }
  440. }
  441. status.GetMakefile().AddDefinition(arguments.ResultsVariable,
  442. cmList::to_string(res));
  443. }
  444. }
  445. auto queryProcessStatusByIndex = [&chain](std::size_t index) -> std::string {
  446. auto const& processStatus = chain.GetStatus(index);
  447. auto exception = processStatus.GetException();
  448. if (exception.first == cmUVProcessChain::ExceptionCode::None) {
  449. if (processStatus.ExitStatus) {
  450. return cmStrCat("Child return code: ", processStatus.ExitStatus);
  451. }
  452. return "";
  453. }
  454. return cmStrCat("Abnormal exit with child return code: ",
  455. exception.second);
  456. };
  457. if (commandErrorIsFatal == "ANY"_s) {
  458. bool ret = true;
  459. if (timedOut) {
  460. status.SetError("Process terminated due to timeout");
  461. ret = false;
  462. } else {
  463. std::map<std::size_t, std::string> failureIndices;
  464. auto statuses = chain.GetStatus();
  465. for (std::size_t i = 0; i < statuses.size(); ++i) {
  466. std::string processStatus = queryProcessStatusByIndex(i);
  467. if (!processStatus.empty()) {
  468. failureIndices[i] = processStatus;
  469. }
  470. }
  471. if (!failureIndices.empty()) {
  472. std::ostringstream oss;
  473. oss << "failed command indexes:\n";
  474. for (auto const& e : failureIndices) {
  475. oss << " " << e.first + 1 << ": \"" << e.second << "\"\n";
  476. }
  477. status.SetError(oss.str());
  478. ret = false;
  479. }
  480. }
  481. if (!ret) {
  482. cmSystemTools::SetFatalErrorOccurred();
  483. return false;
  484. }
  485. }
  486. if (commandErrorIsFatal == "LAST"_s) {
  487. bool ret = true;
  488. if (timedOut) {
  489. status.SetError("Process terminated due to timeout");
  490. ret = false;
  491. } else {
  492. auto const& lastStatus = chain.GetStatus(arguments.Commands.size() - 1);
  493. auto exception = lastStatus.GetException();
  494. if (exception.first != cmUVProcessChain::ExceptionCode::None) {
  495. status.SetError(cmStrCat("Abnormal exit: ", exception.second));
  496. ret = false;
  497. } else {
  498. int lastIndex = static_cast<int>(arguments.Commands.size() - 1);
  499. std::string const processStatus = queryProcessStatusByIndex(lastIndex);
  500. if (!processStatus.empty()) {
  501. status.SetError("last command failed");
  502. ret = false;
  503. }
  504. }
  505. }
  506. if (!ret) {
  507. cmSystemTools::SetFatalErrorOccurred();
  508. return false;
  509. }
  510. }
  511. return true;
  512. }
  513. namespace {
  514. void cmExecuteProcessCommandFixText(std::vector<char>& output,
  515. bool strip_trailing_whitespace)
  516. {
  517. // Remove \0 characters and the \r part of \r\n pairs.
  518. unsigned int in_index = 0;
  519. unsigned int out_index = 0;
  520. while (in_index < output.size()) {
  521. char c = output[in_index++];
  522. if ((c != '\r' ||
  523. !(in_index < output.size() && output[in_index] == '\n')) &&
  524. c != '\0') {
  525. output[out_index++] = c;
  526. }
  527. }
  528. // Remove trailing whitespace if requested.
  529. if (strip_trailing_whitespace) {
  530. while (out_index > 0 &&
  531. cmExecuteProcessCommandIsWhitespace(output[out_index - 1])) {
  532. --out_index;
  533. }
  534. }
  535. // Shrink the vector to the size needed.
  536. output.resize(out_index);
  537. // Put a terminator on the text string.
  538. output.push_back('\0');
  539. }
  540. void cmExecuteProcessCommandAppend(std::vector<char>& output, char const* data,
  541. std::size_t length)
  542. {
  543. #if defined(__APPLE__)
  544. // HACK on Apple to work around bug with inserting at the
  545. // end of an empty vector. This resulted in random failures
  546. // that were hard to reproduce.
  547. if (output.empty() && length > 0) {
  548. output.push_back(data[0]);
  549. ++data;
  550. --length;
  551. }
  552. #endif
  553. cm::append(output, data, data + length);
  554. }
  555. }