cmInstallExportGenerator.cxx 7.7 KB

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