cmInstallExportGenerator.cxx 10 KB

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