cmInstallScriptHandler.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "cmInstallScriptHandler.h"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include <cm/memory>
  11. #include <cm3p/json/reader.h>
  12. #include <cm3p/json/value.h>
  13. #include <cm3p/uv.h>
  14. #include "cmJSONState.h"
  15. #include "cmProcessOutput.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. #include "cmUVHandlePtr.h"
  19. #include "cmUVProcessChain.h"
  20. #include "cmUVStream.h"
  21. using InstallScript = cmInstallScriptHandler::InstallScript;
  22. cmInstallScriptHandler::cmInstallScriptHandler(const std::string& binary_dir,
  23. std::vector<std::string>& args)
  24. {
  25. const std::string& file =
  26. cmStrCat(binary_dir, "/CMakeFiles/InstallScripts.json");
  27. if (cmSystemTools::FileExists(file)) {
  28. int compare;
  29. cmSystemTools::FileTimeCompare(
  30. cmStrCat(binary_dir, "/CMakeFiles/cmake.check_cache"), file, &compare);
  31. if (compare < 1) {
  32. args.insert(args.end() - 1, "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  33. Json::CharReaderBuilder rbuilder;
  34. auto JsonReader =
  35. std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
  36. std::vector<char> content;
  37. Json::Value value;
  38. cmJSONState state(file, &value);
  39. for (auto const& script : value["InstallScripts"]) {
  40. this->commands.push_back(args);
  41. this->commands.back().emplace_back(script.asCString());
  42. }
  43. }
  44. }
  45. }
  46. bool cmInstallScriptHandler::isParallel()
  47. {
  48. return !this->commands.empty();
  49. }
  50. int cmInstallScriptHandler::install(unsigned int j)
  51. {
  52. cm::uv_loop_ptr loop;
  53. loop.init();
  54. std::vector<InstallScript> scripts;
  55. for (auto const& cmd : this->commands) {
  56. scripts.push_back(InstallScript(cmd));
  57. }
  58. std::size_t working = 0;
  59. std::size_t installed = 0;
  60. std::size_t i = 0;
  61. while (installed < scripts.size()) {
  62. for (auto queue = std::min(j - working, scripts.size() - i); queue > 0;
  63. --queue) {
  64. scripts[i].start(loop, [&scripts, &working, &installed, i]() {
  65. scripts[i].printResult(++installed, scripts.size());
  66. --working;
  67. });
  68. ++i;
  69. }
  70. uv_run(loop, UV_RUN_DEFAULT);
  71. }
  72. return 0;
  73. }
  74. InstallScript::InstallScript(const std::vector<std::string>& cmd)
  75. {
  76. this->name = cmSystemTools::RelativePath(
  77. cmSystemTools::GetCurrentWorkingDirectory(), cmd.back());
  78. this->command = cmd;
  79. }
  80. void InstallScript::start(cm::uv_loop_ptr& loop,
  81. std::function<void()> callback)
  82. {
  83. cmUVProcessChainBuilder builder;
  84. builder.AddCommand(this->command)
  85. .SetExternalLoop(*loop)
  86. .SetMergedBuiltinStreams();
  87. this->chain = cm::make_unique<cmUVProcessChain>(builder.Start());
  88. this->pipe.init(this->chain->GetLoop(), 0);
  89. uv_pipe_open(this->pipe, this->chain->OutputStream());
  90. this->streamHandler = cmUVStreamRead(
  91. this->pipe,
  92. [this](std::vector<char> data) {
  93. std::string strdata;
  94. cmProcessOutput(cmProcessOutput::Auto)
  95. .DecodeText(data.data(), data.size(), strdata);
  96. this->output.push_back(strdata);
  97. },
  98. std::move(callback));
  99. }
  100. void InstallScript::printResult(std::size_t n, std::size_t total)
  101. {
  102. cmSystemTools::Stdout(cmStrCat("[", n, "/", total, "] ", this->name, "\n"));
  103. for (auto const& line : this->output) {
  104. cmSystemTools::Stdout(line);
  105. }
  106. }