cmInstallExportGenerator.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <algorithm>
  5. #include <map>
  6. #include <sstream>
  7. #include <utility>
  8. #include <cm/memory>
  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 "cmStringAlgorithms.h"
  18. #include "cmSystemTools.h"
  19. cmInstallExportGenerator::cmInstallExportGenerator(
  20. cmExportSet* exportSet, std::string const& destination,
  21. std::string file_permissions, std::vector<std::string> const& configurations,
  22. std::string const& component, MessageLevel message, bool exclude_from_all,
  23. std::string filename, std::string name_space, 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. , ExportOld(exportOld)
  32. , LocalGenerator(nullptr)
  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. this->ExportSet->Compute(lg);
  48. return true;
  49. }
  50. void cmInstallExportGenerator::ComputeTempDir()
  51. {
  52. // Choose a temporary directory in which to generate the import
  53. // files to be installed.
  54. this->TempDir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  55. "/CMakeFiles/Export");
  56. if (this->Destination.empty()) {
  57. return;
  58. }
  59. this->TempDir += "/";
  60. // Enforce a maximum length.
  61. bool useMD5 = false;
  62. #if defined(_WIN32) || defined(__CYGWIN__)
  63. std::string::size_type const max_total_len = 250;
  64. #else
  65. std::string::size_type const max_total_len = 1000;
  66. #endif
  67. // Will generate files of the form "<temp-dir>/<base>-<config>.<ext>".
  68. std::string::size_type const len = this->TempDir.size() + 1 +
  69. this->FileName.size() + 1 + this->GetMaxConfigLength();
  70. if (len < max_total_len) {
  71. // Keep the total path length below the limit.
  72. std::string::size_type const max_len = max_total_len - len;
  73. if (this->Destination.size() > max_len) {
  74. useMD5 = true;
  75. }
  76. } else {
  77. useMD5 = true;
  78. }
  79. if (useMD5) {
  80. // Replace the destination path with a hash to keep it short.
  81. #ifndef CMAKE_BOOTSTRAP
  82. this->TempDir += cmSystemTools::ComputeStringMD5(this->Destination);
  83. #endif
  84. } else {
  85. std::string dest = this->Destination;
  86. // Avoid unix full paths.
  87. if (dest[0] == '/') {
  88. dest[0] = '_';
  89. }
  90. // Avoid windows full paths by removing colons.
  91. std::replace(dest.begin(), dest.end(), ':', '_');
  92. // Avoid relative paths that go up the tree.
  93. cmSystemTools::ReplaceString(dest, "../", "__/");
  94. // Avoid spaces.
  95. std::replace(dest.begin(), dest.end(), ' ', '_');
  96. this->TempDir += dest;
  97. }
  98. }
  99. size_t cmInstallExportGenerator::GetMaxConfigLength() const
  100. {
  101. // Always use at least 8 for "noconfig".
  102. size_t len = 8;
  103. if (this->ConfigurationTypes->empty()) {
  104. if (this->ConfigurationName.size() > 8) {
  105. len = this->ConfigurationName.size();
  106. }
  107. } else {
  108. for (std::string const& c : *this->ConfigurationTypes) {
  109. if (c.size() > len) {
  110. len = c.size();
  111. }
  112. }
  113. }
  114. return len;
  115. }
  116. void cmInstallExportGenerator::GenerateScript(std::ostream& os)
  117. {
  118. // Skip empty sets.
  119. if (this->ExportSet->GetTargetExports().empty()) {
  120. std::ostringstream e;
  121. e << "INSTALL(EXPORT) given unknown export \""
  122. << this->ExportSet->GetName() << "\"";
  123. cmSystemTools::Error(e.str());
  124. return;
  125. }
  126. // Create the temporary directory in which to store the files.
  127. this->ComputeTempDir();
  128. cmSystemTools::MakeDirectory(this->TempDir);
  129. // Construct a temporary location for the file.
  130. this->MainImportFile = cmStrCat(this->TempDir, '/', this->FileName);
  131. // Generate the import file for this export set.
  132. this->EFGen->SetExportFile(this->MainImportFile.c_str());
  133. this->EFGen->SetNamespace(this->Namespace);
  134. this->EFGen->SetExportOld(this->ExportOld);
  135. if (this->ConfigurationTypes->empty()) {
  136. if (!this->ConfigurationName.empty()) {
  137. this->EFGen->AddConfiguration(this->ConfigurationName);
  138. } else {
  139. this->EFGen->AddConfiguration("");
  140. }
  141. } else {
  142. for (std::string const& c : *this->ConfigurationTypes) {
  143. this->EFGen->AddConfiguration(c);
  144. }
  145. }
  146. this->EFGen->GenerateImportFile();
  147. // Perform the main install script generation.
  148. this->cmInstallGenerator::GenerateScript(os);
  149. }
  150. void cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
  151. Indent indent)
  152. {
  153. // Create the main install rules first.
  154. this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
  155. // Now create a configuration-specific install rule for the import
  156. // file of each configuration.
  157. std::vector<std::string> files;
  158. for (auto const& i : this->EFGen->GetConfigImportFiles()) {
  159. files.push_back(i.second);
  160. std::string config_test = this->CreateConfigTest(i.first);
  161. os << indent << "if(" << config_test << ")\n";
  162. this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
  163. false, this->FilePermissions.c_str(), nullptr,
  164. nullptr, nullptr, indent.Next());
  165. os << indent << "endif()\n";
  166. files.clear();
  167. }
  168. }
  169. void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
  170. Indent indent)
  171. {
  172. // Remove old per-configuration export files if the main changes.
  173. std::string installedDir = cmStrCat(
  174. "$ENV{DESTDIR}", ConvertToAbsoluteDestination(this->Destination), '/');
  175. std::string installedFile = cmStrCat(installedDir, this->FileName);
  176. os << indent << "if(EXISTS \"" << installedFile << "\")\n";
  177. Indent indentN = indent.Next();
  178. Indent indentNN = indentN.Next();
  179. Indent indentNNN = indentNN.Next();
  180. /* clang-format off */
  181. os << indentN << "file(DIFFERENT EXPORT_FILE_CHANGED FILES\n"
  182. << indentN << " \"" << installedFile << "\"\n"
  183. << indentN << " \"" << this->MainImportFile << "\")\n";
  184. os << indentN << "if(EXPORT_FILE_CHANGED)\n";
  185. os << indentNN << "file(GLOB OLD_CONFIG_FILES \"" << installedDir
  186. << this->EFGen->GetConfigImportFileGlob() << "\")\n";
  187. os << indentNN << "if(OLD_CONFIG_FILES)\n";
  188. os << indentNNN << R"(message(STATUS "Old export file \")" << installedFile
  189. << "\\\" will be replaced. Removing files [${OLD_CONFIG_FILES}].\")\n";
  190. os << indentNNN << "file(REMOVE ${OLD_CONFIG_FILES})\n";
  191. os << indentNN << "endif()\n";
  192. os << indentN << "endif()\n";
  193. os << indent << "endif()\n";
  194. /* clang-format on */
  195. // Install the main export file.
  196. std::vector<std::string> files;
  197. files.push_back(this->MainImportFile);
  198. this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
  199. false, this->FilePermissions.c_str(), nullptr, nullptr,
  200. nullptr, indent);
  201. }
  202. std::string cmInstallExportGenerator::GetDestinationFile() const
  203. {
  204. return this->Destination + '/' + this->FileName;
  205. }