cmInstallExportGenerator.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. const char* filename, const char* name_space,
  29. cmMakefile* mf)
  30. :cmInstallGenerator(destination, configurations, component)
  31. ,ExportSet(exportSet)
  32. ,FilePermissions(file_permissions)
  33. ,FileName(filename)
  34. ,Namespace(name_space)
  35. ,Makefile(mf)
  36. {
  37. this->EFGen = new cmExportInstallFileGenerator(this);
  38. exportSet->AddInstallation(this);
  39. }
  40. //----------------------------------------------------------------------------
  41. cmInstallExportGenerator::~cmInstallExportGenerator()
  42. {
  43. delete this->EFGen;
  44. }
  45. //----------------------------------------------------------------------------
  46. void cmInstallExportGenerator::ComputeTempDir()
  47. {
  48. // Choose a temporary directory in which to generate the import
  49. // files to be installed.
  50. this->TempDir = this->Makefile->GetCurrentOutputDirectory();
  51. this->TempDir += cmake::GetCMakeFilesDirectory();
  52. this->TempDir += "/Export";
  53. if(this->Destination.empty())
  54. {
  55. return;
  56. }
  57. else
  58. {
  59. this->TempDir += "/";
  60. }
  61. // Enforce a maximum length.
  62. bool useMD5 = false;
  63. #if defined(_WIN32) || defined(__CYGWIN__)
  64. std::string::size_type const max_total_len = 250;
  65. #else
  66. std::string::size_type const max_total_len = 1000;
  67. #endif
  68. if(this->TempDir.size() < max_total_len)
  69. {
  70. // Keep the total path length below the limit.
  71. std::string::size_type max_len = max_total_len - this->TempDir.size();
  72. if(this->Destination.size() > max_len)
  73. {
  74. useMD5 = true;
  75. }
  76. }
  77. else
  78. {
  79. useMD5 = true;
  80. }
  81. if(useMD5)
  82. {
  83. // Replace the destination path with a hash to keep it short.
  84. this->TempDir +=
  85. cmSystemTools::ComputeStringMD5(this->Destination.c_str());
  86. }
  87. else
  88. {
  89. std::string dest = this->Destination;
  90. // Avoid unix full paths.
  91. if(dest[0] == '/')
  92. {
  93. dest[0] = '_';
  94. }
  95. // Avoid windows full paths by removing colons.
  96. cmSystemTools::ReplaceString(dest, ":", "_");
  97. // Avoid relative paths that go up the tree.
  98. cmSystemTools::ReplaceString(dest, "../", "__/");
  99. // Avoid spaces.
  100. cmSystemTools::ReplaceString(dest, " ", "_");
  101. this->TempDir += dest;
  102. }
  103. }
  104. //----------------------------------------------------------------------------
  105. void cmInstallExportGenerator::GenerateScript(std::ostream& os)
  106. {
  107. // Skip empty sets.
  108. if(ExportSet->GetTargetExports()->empty())
  109. {
  110. cmOStringStream e;
  111. e << "INSTALL(EXPORT) given unknown export \""
  112. << ExportSet->GetName() << "\"";
  113. cmSystemTools::Error(e.str().c_str());
  114. return;
  115. }
  116. // Create the temporary directory in which to store the files.
  117. this->ComputeTempDir();
  118. cmSystemTools::MakeDirectory(this->TempDir.c_str());
  119. // Construct a temporary location for the file.
  120. this->MainImportFile = this->TempDir;
  121. this->MainImportFile += "/";
  122. this->MainImportFile += this->FileName;
  123. // Generate the import file for this export set.
  124. this->EFGen->SetExportFile(this->MainImportFile.c_str());
  125. this->EFGen->SetNamespace(this->Namespace.c_str());
  126. if(this->ConfigurationTypes->empty())
  127. {
  128. if(this->ConfigurationName && *this->ConfigurationName)
  129. {
  130. this->EFGen->AddConfiguration(this->ConfigurationName);
  131. }
  132. else
  133. {
  134. this->EFGen->AddConfiguration("");
  135. }
  136. }
  137. else
  138. {
  139. for(std::vector<std::string>::const_iterator
  140. ci = this->ConfigurationTypes->begin();
  141. ci != this->ConfigurationTypes->end(); ++ci)
  142. {
  143. this->EFGen->AddConfiguration(ci->c_str());
  144. }
  145. }
  146. this->EFGen->GenerateImportFile();
  147. // Perform the main install script generation.
  148. this->cmInstallGenerator::GenerateScript(os);
  149. }
  150. //----------------------------------------------------------------------------
  151. void
  152. 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<cmStdString, cmStdString>::const_iterator
  161. i = this->EFGen->GetConfigImportFiles().begin();
  162. i != this->EFGen->GetConfigImportFiles().end(); ++i)
  163. {
  164. files.push_back(i->second);
  165. std::string config_test = this->CreateConfigTest(i->first.c_str());
  166. os << indent << "IF(" << config_test << ")\n";
  167. this->AddInstallRule(os, cmInstallType_FILES, files, false,
  168. this->FilePermissions.c_str(), 0, 0, 0,
  169. indent.Next());
  170. os << indent << "ENDIF(" << config_test << ")\n";
  171. files.clear();
  172. }
  173. }
  174. //----------------------------------------------------------------------------
  175. void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
  176. Indent const& indent)
  177. {
  178. // Remove old per-configuration export files if the main changes.
  179. std::string installedDir = "$ENV{DESTDIR}";
  180. installedDir += this->GetInstallDestination();
  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. os << indentN << "FILE(DIFFERENT EXPORT_FILE_CHANGED FILES\n"
  189. << indentN << " \"" << installedFile << "\"\n"
  190. << indentN << " \"" << this->MainImportFile << "\")\n";
  191. os << indentN << "IF(EXPORT_FILE_CHANGED)\n";
  192. os << indentNN << "FILE(GLOB OLD_CONFIG_FILES \"" << installedDir
  193. << this->EFGen->GetConfigImportFileGlob() << "\")\n";
  194. os << indentNN << "IF(OLD_CONFIG_FILES)\n";
  195. os << indentNNN << "MESSAGE(STATUS \"Old export file \\\"" << installedFile
  196. << "\\\" will be replaced. Removing files [${OLD_CONFIG_FILES}].\")\n";
  197. os << indentNNN << "FILE(REMOVE ${OLD_CONFIG_FILES})\n";
  198. os << indentNN << "ENDIF(OLD_CONFIG_FILES)\n";
  199. os << indentN << "ENDIF(EXPORT_FILE_CHANGED)\n";
  200. os << indent << "ENDIF()\n";
  201. // Install the main export file.
  202. std::vector<std::string> files;
  203. files.push_back(this->MainImportFile);
  204. this->AddInstallRule(os, cmInstallType_FILES, files, false,
  205. this->FilePermissions.c_str(), 0, 0, 0, indent);
  206. }