cmInstallScriptHandler.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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::string _config,
  29. std::vector<std::string>& args)
  30. : binaryDir(std::move(_binaryDir))
  31. , component(std::move(_component))
  32. {
  33. const std::string& file =
  34. cmStrCat(this->binaryDir, "/CMakeFiles/InstallScripts.json");
  35. this->parallel = false;
  36. auto addScript = [this, &args](std::string script,
  37. std::string config) -> void {
  38. this->commands.push_back(args);
  39. if (!config.empty()) {
  40. this->commands.back().insert(
  41. this->commands.back().end() - 1,
  42. cmStrCat("-DCMAKE_INSTALL_CONFIG_NAME=", config));
  43. }
  44. this->commands.back().emplace_back(script);
  45. this->directories.push_back(cmSystemTools::GetFilenamePath(script));
  46. };
  47. int compare = 1;
  48. if (cmSystemTools::FileExists(file)) {
  49. cmSystemTools::FileTimeCompare(
  50. cmStrCat(this->binaryDir, "/CMakeFiles/cmake.check_cache"), file,
  51. &compare);
  52. }
  53. if (compare < 1) {
  54. Json::CharReaderBuilder rbuilder;
  55. auto JsonReader =
  56. std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
  57. std::vector<char> content;
  58. Json::Value value;
  59. cmJSONState state(file, &value);
  60. this->parallel = value["Parallel"].asBool();
  61. if (this->parallel) {
  62. args.insert(args.end() - 1, "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  63. }
  64. if (_config.empty() && value.isMember("Configs")) {
  65. for (auto const& config : value["Configs"]) {
  66. this->configs.push_back(config.asCString());
  67. }
  68. } else {
  69. this->configs.push_back(_config);
  70. }
  71. for (auto const& script : value["InstallScripts"]) {
  72. for (auto const& config : configs) {
  73. addScript(script.asCString(), config);
  74. }
  75. if (!this->parallel) {
  76. break;
  77. }
  78. }
  79. } else {
  80. addScript(cmStrCat(this->binaryDir, "/cmake_install.cmake"), _config);
  81. }
  82. }
  83. bool cmInstallScriptHandler::IsParallel()
  84. {
  85. return this->parallel;
  86. }
  87. std::vector<std::vector<std::string>> cmInstallScriptHandler::GetCommands()
  88. const
  89. {
  90. return this->commands;
  91. }
  92. int cmInstallScriptHandler::Install(unsigned int j)
  93. {
  94. cm::uv_loop_ptr loop;
  95. loop.init();
  96. std::vector<InstallScript> scripts;
  97. scripts.reserve(this->commands.size());
  98. for (auto const& cmd : this->commands) {
  99. scripts.emplace_back(cmd);
  100. }
  101. std::size_t working = 0;
  102. std::size_t installed = 0;
  103. std::size_t i = 0;
  104. std::function<void()> queueScripts;
  105. queueScripts = [&scripts, &working, &installed, &i, &loop, j,
  106. &queueScripts]() {
  107. for (auto queue = std::min(j - working, scripts.size() - i); queue > 0;
  108. --queue) {
  109. ++working;
  110. scripts[i].start(loop,
  111. [&scripts, &working, &installed, i, &queueScripts]() {
  112. scripts[i].printResult(++installed, scripts.size());
  113. --working;
  114. queueScripts();
  115. });
  116. ++i;
  117. }
  118. };
  119. queueScripts();
  120. uv_run(loop, UV_RUN_DEFAULT);
  121. // Write install manifest
  122. std::string install_manifest;
  123. if (this->component.empty()) {
  124. install_manifest = "install_manifest.txt";
  125. } else {
  126. cmsys::RegularExpression regEntry;
  127. if (regEntry.compile("^[a-zA-Z0-9_.+-]+$") &&
  128. regEntry.find(this->component)) {
  129. install_manifest =
  130. cmStrCat("install_manifest_", this->component, ".txt");
  131. } else {
  132. cmCryptoHash md5(cmCryptoHash::AlgoMD5);
  133. md5.Initialize();
  134. install_manifest =
  135. cmStrCat("install_manifest_", md5.HashString(this->component), ".txt");
  136. }
  137. }
  138. cmGeneratedFileStream fout(cmStrCat(this->binaryDir, "/", install_manifest));
  139. fout.SetCopyIfDifferent(true);
  140. for (auto const& dir : this->directories) {
  141. auto local_manifest = cmStrCat(dir, "/install_local_manifest.txt");
  142. if (cmSystemTools::FileExists(local_manifest)) {
  143. cmsys::ifstream fin(local_manifest.c_str());
  144. std::string line;
  145. while (std::getline(fin, line)) {
  146. fout << line << "\n";
  147. }
  148. }
  149. }
  150. return 0;
  151. }
  152. InstallScript::InstallScript(const std::vector<std::string>& cmd)
  153. {
  154. this->name = cmSystemTools::RelativePath(
  155. cmSystemTools::GetLogicalWorkingDirectory(), cmd.back());
  156. this->command = cmd;
  157. }
  158. void InstallScript::start(cm::uv_loop_ptr& loop,
  159. std::function<void()> callback)
  160. {
  161. cmUVProcessChainBuilder builder;
  162. builder.AddCommand(this->command)
  163. .SetExternalLoop(*loop)
  164. .SetMergedBuiltinStreams();
  165. this->chain = cm::make_unique<cmUVProcessChain>(builder.Start());
  166. this->pipe.init(this->chain->GetLoop(), 0);
  167. uv_pipe_open(this->pipe, this->chain->OutputStream());
  168. this->streamHandler = cmUVStreamRead(
  169. this->pipe,
  170. [this](std::vector<char> data) {
  171. std::string strdata;
  172. cmProcessOutput(cmProcessOutput::Auto)
  173. .DecodeText(data.data(), data.size(), strdata);
  174. this->output.push_back(strdata);
  175. },
  176. std::move(callback));
  177. }
  178. void InstallScript::printResult(std::size_t n, std::size_t total)
  179. {
  180. cmSystemTools::Stdout(cmStrCat('[', n, '/', total, "] ", this->name, '\n'));
  181. for (auto const& line : this->output) {
  182. cmSystemTools::Stdout(line);
  183. }
  184. }