cmInstallExportGenerator.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. }
  39. //----------------------------------------------------------------------------
  40. cmInstallExportGenerator::~cmInstallExportGenerator()
  41. {
  42. delete this->EFGen;
  43. }
  44. //----------------------------------------------------------------------------
  45. void cmInstallExportGenerator::ComputeTempDir()
  46. {
  47. // Choose a temporary directory in which to generate the import
  48. // files to be installed.
  49. this->TempDir = this->Makefile->GetCurrentOutputDirectory();
  50. this->TempDir += cmake::GetCMakeFilesDirectory();
  51. this->TempDir += "/Export";
  52. if(this->Destination.empty())
  53. {
  54. return;
  55. }
  56. else
  57. {
  58. this->TempDir += "/";
  59. }
  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. if(this->TempDir.size() < max_total_len)
  68. {
  69. // Keep the total path length below the limit.
  70. std::string::size_type max_len = max_total_len - this->TempDir.size();
  71. if(this->Destination.size() > max_len)
  72. {
  73. useMD5 = true;
  74. }
  75. }
  76. else
  77. {
  78. useMD5 = true;
  79. }
  80. if(useMD5)
  81. {
  82. // Replace the destination path with a hash to keep it short.
  83. this->TempDir +=
  84. cmSystemTools::ComputeStringMD5(this->Destination.c_str());
  85. }
  86. else
  87. {
  88. std::string dest = this->Destination;
  89. // Avoid unix full paths.
  90. if(dest[0] == '/')
  91. {
  92. dest[0] = '_';
  93. }
  94. // Avoid windows full paths by removing colons.
  95. cmSystemTools::ReplaceString(dest, ":", "_");
  96. // Avoid relative paths that go up the tree.
  97. cmSystemTools::ReplaceString(dest, "../", "__/");
  98. // Avoid spaces.
  99. cmSystemTools::ReplaceString(dest, " ", "_");
  100. this->TempDir += dest;
  101. }
  102. }
  103. //----------------------------------------------------------------------------
  104. void cmInstallExportGenerator::GenerateScript(std::ostream& os)
  105. {
  106. // Skip empty sets.
  107. if(ExportSet->GetTargetExports()->empty())
  108. {
  109. cmOStringStream e;
  110. e << "INSTALL(EXPORT) given unknown export \""
  111. << ExportSet->GetName() << "\"";
  112. cmSystemTools::Error(e.str().c_str());
  113. return;
  114. }
  115. // Create the temporary directory in which to store the files.
  116. this->ComputeTempDir();
  117. cmSystemTools::MakeDirectory(this->TempDir.c_str());
  118. // Construct a temporary location for the file.
  119. this->MainImportFile = this->TempDir;
  120. this->MainImportFile += "/";
  121. this->MainImportFile += this->FileName;
  122. // Generate the import file for this export set.
  123. this->EFGen->SetExportFile(this->MainImportFile.c_str());
  124. this->EFGen->SetNamespace(this->Namespace.c_str());
  125. if(this->ConfigurationTypes->empty())
  126. {
  127. if(this->ConfigurationName && *this->ConfigurationName)
  128. {
  129. this->EFGen->AddConfiguration(this->ConfigurationName);
  130. }
  131. else
  132. {
  133. this->EFGen->AddConfiguration("");
  134. }
  135. }
  136. else
  137. {
  138. for(std::vector<std::string>::const_iterator
  139. ci = this->ConfigurationTypes->begin();
  140. ci != this->ConfigurationTypes->end(); ++ci)
  141. {
  142. this->EFGen->AddConfiguration(ci->c_str());
  143. }
  144. }
  145. this->EFGen->GenerateImportFile();
  146. // Perform the main install script generation.
  147. this->cmInstallGenerator::GenerateScript(os);
  148. }
  149. //----------------------------------------------------------------------------
  150. void
  151. cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
  152. Indent const& indent)
  153. {
  154. // Create the main install rules first.
  155. this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
  156. // Now create a configuration-specific install rule for the import
  157. // file of each configuration.
  158. std::vector<std::string> files;
  159. for(std::map<cmStdString, cmStdString>::const_iterator
  160. i = this->EFGen->GetConfigImportFiles().begin();
  161. i != this->EFGen->GetConfigImportFiles().end(); ++i)
  162. {
  163. files.push_back(i->second);
  164. std::string config_test = this->CreateConfigTest(i->first.c_str());
  165. os << indent << "IF(" << config_test << ")\n";
  166. this->AddInstallRule(os, cmInstallType_FILES, files, false,
  167. this->FilePermissions.c_str(), 0, 0, 0,
  168. indent.Next());
  169. os << indent << "ENDIF(" << config_test << ")\n";
  170. files.clear();
  171. }
  172. }
  173. //----------------------------------------------------------------------------
  174. void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
  175. Indent const& indent)
  176. {
  177. // Remove old per-configuration export files if the main changes.
  178. std::string installedDir = "$ENV{DESTDIR}";
  179. installedDir += this->GetInstallDestination();
  180. installedDir += "/";
  181. std::string installedFile = installedDir;
  182. installedFile += this->FileName;
  183. os << indent << "IF(EXISTS \"" << installedFile << "\")\n";
  184. Indent indentN = indent.Next();
  185. Indent indentNN = indentN.Next();
  186. Indent indentNNN = indentNN.Next();
  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(OLD_CONFIG_FILES)\n";
  198. os << indentN << "ENDIF(EXPORT_FILE_CHANGED)\n";
  199. os << indent << "ENDIF()\n";
  200. // Install the main export file.
  201. std::vector<std::string> files;
  202. files.push_back(this->MainImportFile);
  203. this->AddInstallRule(os, cmInstallType_FILES, files, false,
  204. this->FilePermissions.c_str(), 0, 0, 0, indent);
  205. }