cmInstallExportGenerator.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. const char* name,
  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. ,Name(name)
  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. // Get the export set requested.
  107. cmExportSet const* exportSet =
  108. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  109. ->GetExportSet(this->Name.c_str());
  110. // Skip empty sets.
  111. if(!exportSet)
  112. {
  113. cmOStringStream e;
  114. e << "INSTALL(EXPORT) given unknown export \"" << this->Name << "\"";
  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->SetName(this->Name.c_str());
  127. this->EFGen->SetExportSet(exportSet);
  128. this->EFGen->SetExportFile(this->MainImportFile.c_str());
  129. this->EFGen->SetNamespace(this->Namespace.c_str());
  130. if(this->ConfigurationTypes->empty())
  131. {
  132. if(this->ConfigurationName && *this->ConfigurationName)
  133. {
  134. this->EFGen->AddConfiguration(this->ConfigurationName);
  135. }
  136. else
  137. {
  138. this->EFGen->AddConfiguration("");
  139. }
  140. }
  141. else
  142. {
  143. for(std::vector<std::string>::const_iterator
  144. ci = this->ConfigurationTypes->begin();
  145. ci != this->ConfigurationTypes->end(); ++ci)
  146. {
  147. this->EFGen->AddConfiguration(ci->c_str());
  148. }
  149. }
  150. this->EFGen->GenerateImportFile();
  151. // Perform the main install script generation.
  152. this->cmInstallGenerator::GenerateScript(os);
  153. }
  154. //----------------------------------------------------------------------------
  155. void
  156. cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
  157. Indent const& indent)
  158. {
  159. // Create the main install rules first.
  160. this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
  161. // Now create a configuration-specific install rule for the import
  162. // file of each configuration.
  163. std::vector<std::string> files;
  164. for(std::map<cmStdString, cmStdString>::const_iterator
  165. i = this->EFGen->GetConfigImportFiles().begin();
  166. i != this->EFGen->GetConfigImportFiles().end(); ++i)
  167. {
  168. files.push_back(i->second);
  169. std::string config_test = this->CreateConfigTest(i->first.c_str());
  170. os << indent << "IF(" << config_test << ")\n";
  171. this->AddInstallRule(os, cmInstallType_FILES, files, false,
  172. this->FilePermissions.c_str(), 0, 0, 0,
  173. indent.Next());
  174. os << indent << "ENDIF(" << config_test << ")\n";
  175. files.clear();
  176. }
  177. }
  178. //----------------------------------------------------------------------------
  179. void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
  180. Indent const& indent)
  181. {
  182. // Remove old per-configuration export files if the main changes.
  183. std::string installedDir = "$ENV{DESTDIR}";
  184. installedDir += this->GetInstallDestination();
  185. installedDir += "/";
  186. std::string installedFile = installedDir;
  187. installedFile += this->FileName;
  188. os << indent << "IF(EXISTS \"" << installedFile << "\")\n";
  189. Indent indentN = indent.Next();
  190. Indent indentNN = indentN.Next();
  191. Indent indentNNN = indentNN.Next();
  192. os << indentN << "FILE(DIFFERENT EXPORT_FILE_CHANGED FILES\n"
  193. << indentN << " \"" << installedFile << "\"\n"
  194. << indentN << " \"" << this->MainImportFile << "\")\n";
  195. os << indentN << "IF(EXPORT_FILE_CHANGED)\n";
  196. os << indentNN << "FILE(GLOB OLD_CONFIG_FILES \"" << installedDir
  197. << this->EFGen->GetConfigImportFileGlob() << "\")\n";
  198. os << indentNN << "IF(OLD_CONFIG_FILES)\n";
  199. os << indentNNN << "MESSAGE(STATUS \"Old export file \\\"" << installedFile
  200. << "\\\" will be replaced. Removing files [${OLD_CONFIG_FILES}].\")\n";
  201. os << indentNNN << "FILE(REMOVE ${OLD_CONFIG_FILES})\n";
  202. os << indentNN << "ENDIF(OLD_CONFIG_FILES)\n";
  203. os << indentN << "ENDIF(EXPORT_FILE_CHANGED)\n";
  204. os << indent << "ENDIF()\n";
  205. // Install the main export file.
  206. std::vector<std::string> files;
  207. files.push_back(this->MainImportFile);
  208. this->AddInstallRule(os, cmInstallType_FILES, files, false,
  209. this->FilePermissions.c_str(), 0, 0, 0, indent);
  210. }