cmExecuteProcessCommand.cxx 19 KB

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