cmInstallScriptHandler.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "cmsys/FStream.hxx"
  15. #include "cmsys/RegularExpression.hxx"
  16. #include "cmCryptoHash.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmJSONState.h"
  19. #include "cmProcessOutput.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmSystemTools.h"
  22. #include "cmUVHandlePtr.h"
  23. #include "cmUVProcessChain.h"
  24. #include "cmUVStream.h"
  25. using InstallScript = cmInstallScriptHandler::InstallScript;
  26. cmInstallScriptHandler::cmInstallScriptHandler(std::string _binaryDir,
  27. std::string _component,
  28. std::vector<std::string>& args)
  29. : binaryDir(std::move(_binaryDir))
  30. , component(std::move(_component))
  31. {
  32. const std::string& file =
  33. cmStrCat(this->binaryDir, "/CMakeFiles/InstallScripts.json");
  34. if (cmSystemTools::FileExists(file)) {
  35. int compare;
  36. cmSystemTools::FileTimeCompare(
  37. cmStrCat(this->binaryDir, "/CMakeFiles/cmake.check_cache"), file,
  38. &compare);
  39. if (compare < 1) {
  40. args.insert(args.end() - 1, "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  41. Json::CharReaderBuilder rbuilder;
  42. auto JsonReader =
  43. std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
  44. std::vector<char> content;
  45. Json::Value value;
  46. cmJSONState state(file, &value);
  47. for (auto const& script : value["InstallScripts"]) {
  48. this->commands.push_back(args);
  49. this->commands.back().emplace_back(script.asCString());
  50. this->directories.push_back(
  51. cmSystemTools::GetFilenamePath(script.asCString()));
  52. }
  53. }
  54. }
  55. }
  56. bool cmInstallScriptHandler::isParallel()
  57. {
  58. return !this->commands.empty();
  59. }
  60. int cmInstallScriptHandler::install(unsigned int j)
  61. {
  62. cm::uv_loop_ptr loop;
  63. loop.init();
  64. std::vector<InstallScript> scripts;
  65. for (auto const& cmd : this->commands) {
  66. scripts.push_back(InstallScript(cmd));
  67. }
  68. std::size_t working = 0;
  69. std::size_t installed = 0;
  70. std::size_t i = 0;
  71. while (installed < scripts.size()) {
  72. for (auto queue = std::min(j - working, scripts.size() - i); queue > 0;
  73. --queue) {
  74. scripts[i].start(loop, [&scripts, &working, &installed, i]() {
  75. scripts[i].printResult(++installed, scripts.size());
  76. --working;
  77. });
  78. ++i;
  79. }
  80. uv_run(loop, UV_RUN_DEFAULT);
  81. }
  82. // Write install manifest
  83. std::string install_manifest;
  84. if (this->component.empty()) {
  85. install_manifest = "install_manifest.txt";
  86. } else {
  87. cmsys::RegularExpression regEntry;
  88. if (regEntry.compile("^[a-zA-Z0-9_.+-]+$") &&
  89. regEntry.find(this->component)) {
  90. install_manifest =
  91. cmStrCat("install_manifest_", this->component, ".txt");
  92. } else {
  93. cmCryptoHash md5(cmCryptoHash::AlgoMD5);
  94. md5.Initialize();
  95. install_manifest =
  96. cmStrCat("install_manifest_", md5.HashString(this->component), ".txt");
  97. }
  98. }
  99. cmGeneratedFileStream fout(cmStrCat(this->binaryDir, "/", install_manifest));
  100. fout.SetCopyIfDifferent(true);
  101. for (auto const& dir : this->directories) {
  102. auto local_manifest = cmStrCat(dir, "/install_local_manifest.txt");
  103. if (cmSystemTools::FileExists(local_manifest)) {
  104. cmsys::ifstream fin(local_manifest.c_str());
  105. std::string line;
  106. while (std::getline(fin, line)) {
  107. fout << line << "\n";
  108. }
  109. }
  110. }
  111. return 0;
  112. }
  113. InstallScript::InstallScript(const std::vector<std::string>& cmd)
  114. {
  115. this->name = cmSystemTools::RelativePath(
  116. cmSystemTools::GetCurrentWorkingDirectory(), cmd.back());
  117. this->command = cmd;
  118. }
  119. void InstallScript::start(cm::uv_loop_ptr& loop,
  120. std::function<void()> callback)
  121. {
  122. cmUVProcessChainBuilder builder;
  123. builder.AddCommand(this->command)
  124. .SetExternalLoop(*loop)
  125. .SetMergedBuiltinStreams();
  126. this->chain = cm::make_unique<cmUVProcessChain>(builder.Start());
  127. this->pipe.init(this->chain->GetLoop(), 0);
  128. uv_pipe_open(this->pipe, this->chain->OutputStream());
  129. this->streamHandler = cmUVStreamRead(
  130. this->pipe,
  131. [this](std::vector<char> data) {
  132. std::string strdata;
  133. cmProcessOutput(cmProcessOutput::Auto)
  134. .DecodeText(data.data(), data.size(), strdata);
  135. this->output.push_back(strdata);
  136. },
  137. std::move(callback));
  138. }
  139. void InstallScript::printResult(std::size_t n, std::size_t total)
  140. {
  141. cmSystemTools::Stdout(cmStrCat('[', n, '/', total, "] ", this->name, '\n'));
  142. for (auto const& line : this->output) {
  143. cmSystemTools::Stdout(line);
  144. }
  145. }