cmInstallExportGenerator.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #ifndef CMAKE_BOOTSTRAP
  9. # include "cmExportInstallAndroidMKGenerator.h"
  10. #endif
  11. #include "cmExportInstallFileGenerator.h"
  12. #include "cmExportSet.h"
  13. #include "cmInstallType.h"
  14. #include "cmListFileCache.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. cmInstallExportGenerator::cmInstallExportGenerator(
  19. cmExportSet* exportSet, std::string const& destination,
  20. std::string file_permissions, std::vector<std::string> const& configurations,
  21. std::string const& component, MessageLevel message, bool exclude_from_all,
  22. std::string filename, std::string name_space,
  23. std::string cxx_modules_directory, bool exportOld, bool android,
  24. cmListFileBacktrace backtrace)
  25. : cmInstallGenerator(destination, configurations, component, message,
  26. exclude_from_all, false, std::move(backtrace))
  27. , ExportSet(exportSet)
  28. , FilePermissions(std::move(file_permissions))
  29. , FileName(std::move(filename))
  30. , Namespace(std::move(name_space))
  31. , CxxModulesDirectory(std::move(cxx_modules_directory))
  32. , ExportOld(exportOld)
  33. {
  34. if (android) {
  35. #ifndef CMAKE_BOOTSTRAP
  36. this->EFGen = cm::make_unique<cmExportInstallAndroidMKGenerator>(this);
  37. #endif
  38. } else {
  39. this->EFGen = cm::make_unique<cmExportInstallFileGenerator>(this);
  40. }
  41. exportSet->AddInstallation(this);
  42. }
  43. cmInstallExportGenerator::~cmInstallExportGenerator() = default;
  44. bool cmInstallExportGenerator::Compute(cmLocalGenerator* lg)
  45. {
  46. this->LocalGenerator = lg;
  47. return this->ExportSet->Compute(lg);
  48. }
  49. std::string cmInstallExportGenerator::TempDirCalculate() const
  50. {
  51. // Choose a temporary directory in which to generate the import
  52. // files to be installed.
  53. std::string path = cmStrCat(
  54. this->LocalGenerator->GetCurrentBinaryDirectory(), "/CMakeFiles/Export");
  55. if (this->Destination.empty()) {
  56. return path;
  57. }
  58. #ifndef CMAKE_BOOTSTRAP
  59. path += '/';
  60. // Replace the destination path with a hash to keep it short.
  61. path += cmSystemTools::ComputeStringMD5(this->Destination);
  62. #endif
  63. return path;
  64. }
  65. void cmInstallExportGenerator::ComputeTempDir()
  66. {
  67. this->TempDir = this->TempDirCalculate();
  68. }
  69. std::string cmInstallExportGenerator::GetTempDir() const
  70. {
  71. if (this->TempDir.empty()) {
  72. return this->TempDirCalculate();
  73. }
  74. return this->TempDir;
  75. }
  76. void cmInstallExportGenerator::GenerateScript(std::ostream& os)
  77. {
  78. // Skip empty sets.
  79. if (this->ExportSet->GetTargetExports().empty()) {
  80. std::ostringstream e;
  81. e << "INSTALL(EXPORT) given unknown export \""
  82. << this->ExportSet->GetName() << "\"";
  83. cmSystemTools::Error(e.str());
  84. return;
  85. }
  86. // Create the temporary directory in which to store the files.
  87. this->ComputeTempDir();
  88. cmSystemTools::MakeDirectory(this->TempDir);
  89. // Construct a temporary location for the file.
  90. this->MainImportFile = cmStrCat(this->TempDir, '/', this->FileName);
  91. // Generate the import file for this export set.
  92. this->EFGen->SetExportFile(this->MainImportFile.c_str());
  93. this->EFGen->SetNamespace(this->Namespace);
  94. this->EFGen->SetExportOld(this->ExportOld);
  95. if (this->ConfigurationTypes->empty()) {
  96. if (!this->ConfigurationName.empty()) {
  97. this->EFGen->AddConfiguration(this->ConfigurationName);
  98. } else {
  99. this->EFGen->AddConfiguration("");
  100. }
  101. } else {
  102. for (std::string const& c : *this->ConfigurationTypes) {
  103. this->EFGen->AddConfiguration(c);
  104. }
  105. }
  106. this->EFGen->GenerateImportFile();
  107. // Perform the main install script generation.
  108. this->cmInstallGenerator::GenerateScript(os);
  109. }
  110. void cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
  111. Indent indent)
  112. {
  113. // Create the main install rules first.
  114. this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
  115. // Now create a configuration-specific install rule for the import
  116. // file of each configuration.
  117. std::vector<std::string> files;
  118. for (auto const& i : this->EFGen->GetConfigImportFiles()) {
  119. files.push_back(i.second);
  120. std::string config_test = this->CreateConfigTest(i.first);
  121. os << indent << "if(" << config_test << ")\n";
  122. this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
  123. false, this->FilePermissions.c_str(), nullptr,
  124. nullptr, nullptr, indent.Next());
  125. os << indent << "endif()\n";
  126. files.clear();
  127. }
  128. }
  129. void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
  130. Indent indent)
  131. {
  132. // Remove old per-configuration export files if the main changes.
  133. std::string installedDir = cmStrCat(
  134. "$ENV{DESTDIR}", ConvertToAbsoluteDestination(this->Destination), '/');
  135. std::string installedFile = cmStrCat(installedDir, this->FileName);
  136. os << indent << "if(EXISTS \"" << installedFile << "\")\n";
  137. Indent indentN = indent.Next();
  138. Indent indentNN = indentN.Next();
  139. Indent indentNNN = indentNN.Next();
  140. /* clang-format off */
  141. os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
  142. << indentN << " \"" << installedFile << "\"\n"
  143. << indentN << " \"" << this->MainImportFile << "\")\n";
  144. os << indentN << "if(_cmake_export_file_changed)\n";
  145. os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
  146. << this->EFGen->GetConfigImportFileGlob() << "\")\n";
  147. os << indentNN << "if(_cmake_old_config_files)\n";
  148. os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
  149. os << indentNNN << R"(message(STATUS "Old export file \")" << installedFile
  150. << "\\\" will be replaced. Removing files [${_cmake_old_config_files_text}].\")\n";
  151. os << indentNNN << "unset(_cmake_old_config_files_text)\n";
  152. os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
  153. os << indentNN << "endif()\n";
  154. os << indentNN << "unset(_cmake_old_config_files)\n";
  155. os << indentN << "endif()\n";
  156. os << indentN << "unset(_cmake_export_file_changed)\n";
  157. os << indent << "endif()\n";
  158. /* clang-format on */
  159. // Install the main export file.
  160. std::vector<std::string> files;
  161. files.push_back(this->MainImportFile);
  162. this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
  163. false, this->FilePermissions.c_str(), nullptr, nullptr,
  164. nullptr, indent);
  165. }
  166. std::string cmInstallExportGenerator::GetDestinationFile() const
  167. {
  168. return this->Destination + '/' + this->FileName;
  169. }