cmInstallExportGenerator.cxx 7.6 KB

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