cmInstallExportGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "cmInstallExportGenerator.h"
  4. #include <map>
  5. #include <sstream>
  6. #include <utility>
  7. #include <cm/memory>
  8. #include "cmCryptoHash.h"
  9. #ifndef CMAKE_BOOTSTRAP
  10. # include "cmExportInstallAndroidMKGenerator.h"
  11. #endif
  12. #include "cmExportInstallFileGenerator.h"
  13. #include "cmExportSet.h"
  14. #include "cmInstallType.h"
  15. #include "cmListFileCache.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmScriptGenerator.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmSystemTools.h"
  20. cmInstallExportGenerator::cmInstallExportGenerator(
  21. cmExportSet* exportSet, std::string const& destination,
  22. std::string file_permissions, std::vector<std::string> const& configurations,
  23. std::string const& component, MessageLevel message, bool exclude_from_all,
  24. std::string filename, std::string name_space,
  25. std::string cxx_modules_directory, bool exportOld, bool android,
  26. cmListFileBacktrace backtrace)
  27. : cmInstallGenerator(destination, configurations, component, message,
  28. exclude_from_all, false, std::move(backtrace))
  29. , ExportSet(exportSet)
  30. , FilePermissions(std::move(file_permissions))
  31. , FileName(std::move(filename))
  32. , Namespace(std::move(name_space))
  33. , CxxModulesDirectory(std::move(cxx_modules_directory))
  34. , ExportOld(exportOld)
  35. {
  36. if (android) {
  37. #ifndef CMAKE_BOOTSTRAP
  38. this->EFGen = cm::make_unique<cmExportInstallAndroidMKGenerator>(this);
  39. #endif
  40. } else {
  41. this->EFGen = cm::make_unique<cmExportInstallFileGenerator>(this);
  42. }
  43. exportSet->AddInstallation(this);
  44. }
  45. cmInstallExportGenerator::~cmInstallExportGenerator() = default;
  46. bool cmInstallExportGenerator::Compute(cmLocalGenerator* lg)
  47. {
  48. this->LocalGenerator = lg;
  49. return this->ExportSet->Compute(lg);
  50. }
  51. std::string cmInstallExportGenerator::TempDirCalculate() const
  52. {
  53. // Choose a temporary directory in which to generate the import
  54. // files to be installed.
  55. std::string path = cmStrCat(
  56. this->LocalGenerator->GetCurrentBinaryDirectory(), "/CMakeFiles/Export");
  57. if (this->Destination.empty()) {
  58. return path;
  59. }
  60. cmCryptoHash hasher(cmCryptoHash::AlgoMD5);
  61. path += '/';
  62. // Replace the destination path with a hash to keep it short.
  63. path += hasher.HashString(this->Destination);
  64. return path;
  65. }
  66. void cmInstallExportGenerator::ComputeTempDir()
  67. {
  68. this->TempDir = this->TempDirCalculate();
  69. }
  70. std::string cmInstallExportGenerator::GetTempDir() const
  71. {
  72. if (this->TempDir.empty()) {
  73. return this->TempDirCalculate();
  74. }
  75. return this->TempDir;
  76. }
  77. void cmInstallExportGenerator::GenerateScript(std::ostream& os)
  78. {
  79. // Skip empty sets.
  80. if (this->ExportSet->GetTargetExports().empty()) {
  81. std::ostringstream e;
  82. e << "INSTALL(EXPORT) given unknown export \""
  83. << this->ExportSet->GetName() << "\"";
  84. cmSystemTools::Error(e.str());
  85. return;
  86. }
  87. // Create the temporary directory in which to store the files.
  88. this->ComputeTempDir();
  89. cmSystemTools::MakeDirectory(this->TempDir);
  90. // Construct a temporary location for the file.
  91. this->MainImportFile = cmStrCat(this->TempDir, '/', this->FileName);
  92. // Generate the import file for this export set.
  93. this->EFGen->SetExportFile(this->MainImportFile.c_str());
  94. this->EFGen->SetNamespace(this->Namespace);
  95. this->EFGen->SetExportOld(this->ExportOld);
  96. if (this->ConfigurationTypes->empty()) {
  97. if (!this->ConfigurationName.empty()) {
  98. this->EFGen->AddConfiguration(this->ConfigurationName);
  99. } else {
  100. this->EFGen->AddConfiguration("");
  101. }
  102. } else {
  103. for (std::string const& c : *this->ConfigurationTypes) {
  104. this->EFGen->AddConfiguration(c);
  105. }
  106. }
  107. this->EFGen->GenerateImportFile();
  108. // Perform the main install script generation.
  109. this->cmInstallGenerator::GenerateScript(os);
  110. }
  111. void cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
  112. Indent indent)
  113. {
  114. // Create the main install rules first.
  115. this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
  116. // Now create a configuration-specific install rule for the import
  117. // file of each configuration.
  118. std::vector<std::string> files;
  119. for (auto const& i : this->EFGen->GetConfigImportFiles()) {
  120. files.push_back(i.second);
  121. std::string config_test = this->CreateConfigTest(i.first);
  122. os << indent << "if(" << config_test << ")\n";
  123. this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
  124. false, this->FilePermissions.c_str(), nullptr,
  125. nullptr, nullptr, indent.Next());
  126. os << indent << "endif()\n";
  127. files.clear();
  128. }
  129. // Now create a configuration-specific install rule for the C++ module import
  130. // property file of each configuration.
  131. auto cxx_module_dest =
  132. cmStrCat(this->Destination, '/', this->CxxModulesDirectory);
  133. std::string config_file_example;
  134. for (auto const& i : this->EFGen->GetConfigCxxModuleFiles()) {
  135. config_file_example = i.second;
  136. break;
  137. }
  138. if (!config_file_example.empty()) {
  139. // Remove old per-configuration export files if the main changes.
  140. std::string installedDir = cmStrCat(
  141. "$ENV{DESTDIR}", ConvertToAbsoluteDestination(cxx_module_dest), '/');
  142. std::string installedFile = cmStrCat(installedDir, "/cxx-modules-",
  143. this->ExportSet->GetName(), ".cmake");
  144. std::string toInstallFile =
  145. cmStrCat(cmSystemTools::GetFilenamePath(config_file_example),
  146. "/cxx-modules-", this->ExportSet->GetName(), ".cmake");
  147. os << indent << "if(EXISTS \"" << installedFile << "\")\n";
  148. Indent indentN = indent.Next();
  149. Indent indentNN = indentN.Next();
  150. Indent indentNNN = indentNN.Next();
  151. /* clang-format off */
  152. os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
  153. << indentN << " \"" << installedFile << "\"\n"
  154. << indentN << " \"" << toInstallFile << "\")\n";
  155. os << indentN << "if(_cmake_export_file_changed)\n";
  156. os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
  157. << this->EFGen->GetConfigImportFileGlob() << "\")\n";
  158. os << indentNN << "if(_cmake_old_config_files)\n";
  159. os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
  160. os << indentNNN << R"(message(STATUS "Old C++ module export file \")" << installedFile
  161. << "\\\" will be replaced. Removing files [${_cmake_old_config_files_text}].\")\n";
  162. os << indentNNN << "unset(_cmake_old_config_files_text)\n";
  163. os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
  164. os << indentNN << "endif()\n";
  165. os << indentNN << "unset(_cmake_old_config_files)\n";
  166. os << indentN << "endif()\n";
  167. os << indentN << "unset(_cmake_export_file_changed)\n";
  168. os << indent << "endif()\n";
  169. /* clang-format on */
  170. // All of these files are siblings; get its location to know where the
  171. // "anchor" file is.
  172. files.push_back(toInstallFile);
  173. this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, files,
  174. false, this->FilePermissions.c_str(), nullptr,
  175. nullptr, nullptr, indent);
  176. files.clear();
  177. }
  178. for (auto const& i : this->EFGen->GetConfigCxxModuleFiles()) {
  179. files.push_back(i.second);
  180. std::string config_test = this->CreateConfigTest(i.first);
  181. os << indent << "if(" << config_test << ")\n";
  182. this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, files,
  183. false, this->FilePermissions.c_str(), nullptr,
  184. nullptr, nullptr, indent.Next());
  185. os << indent << "endif()\n";
  186. files.clear();
  187. }
  188. for (auto const& i : this->EFGen->GetConfigCxxModuleTargetFiles()) {
  189. std::string config_test = this->CreateConfigTest(i.first);
  190. os << indent << "if(" << config_test << ")\n";
  191. this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, i.second,
  192. false, this->FilePermissions.c_str(), nullptr,
  193. nullptr, nullptr, indent.Next());
  194. os << indent << "endif()\n";
  195. files.clear();
  196. }
  197. }
  198. void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
  199. Indent indent)
  200. {
  201. // Remove old per-configuration export files if the main changes.
  202. std::string installedDir = cmStrCat(
  203. "$ENV{DESTDIR}", ConvertToAbsoluteDestination(this->Destination), '/');
  204. std::string installedFile = cmStrCat(installedDir, this->FileName);
  205. os << indent << "if(EXISTS \"" << installedFile << "\")\n";
  206. Indent indentN = indent.Next();
  207. Indent indentNN = indentN.Next();
  208. Indent indentNNN = indentNN.Next();
  209. /* clang-format off */
  210. os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
  211. << indentN << " \"" << installedFile << "\"\n"
  212. << indentN << " \"" << this->MainImportFile << "\")\n";
  213. os << indentN << "if(_cmake_export_file_changed)\n";
  214. os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
  215. << this->EFGen->GetConfigImportFileGlob() << "\")\n";
  216. os << indentNN << "if(_cmake_old_config_files)\n";
  217. os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
  218. os << indentNNN << R"(message(STATUS "Old export file \")" << installedFile
  219. << "\\\" will be replaced. Removing files [${_cmake_old_config_files_text}].\")\n";
  220. os << indentNNN << "unset(_cmake_old_config_files_text)\n";
  221. os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
  222. os << indentNN << "endif()\n";
  223. os << indentNN << "unset(_cmake_old_config_files)\n";
  224. os << indentN << "endif()\n";
  225. os << indentN << "unset(_cmake_export_file_changed)\n";
  226. os << indent << "endif()\n";
  227. /* clang-format on */
  228. // Install the main export file.
  229. std::vector<std::string> files;
  230. files.push_back(this->MainImportFile);
  231. this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
  232. false, this->FilePermissions.c_str(), nullptr, nullptr,
  233. nullptr, indent);
  234. }
  235. std::string cmInstallExportGenerator::GetDestinationFile() const
  236. {
  237. return this->Destination + '/' + this->FileName;
  238. }