cmInstallExportGenerator.cxx 7.6 KB

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