cmInstallScriptHandler.cxx 6.9 KB

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