cmInstallExportGenerator.cxx 9.8 KB

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