cmInstallExportGenerator.cxx 7.3 KB

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