cmInstallScriptHandler.cxx 6.5 KB

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