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