cmInstallExportGenerator.cxx 7.9 KB

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