cmInstallExportGenerator.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmInstallExportGenerator.h"
  14. #include <stdio.h>
  15. #include "cmake.h"
  16. #include "cmInstallTargetGenerator.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmTarget.h"
  19. #include "cmMakefile.h"
  20. #include "cmLocalGenerator.h"
  21. #include "cmGlobalGenerator.h"
  22. #include "cmInstallFilesGenerator.h"
  23. #include "cmExportInstallFileGenerator.h"
  24. //----------------------------------------------------------------------------
  25. cmInstallExportGenerator::cmInstallExportGenerator(
  26. const char* name,
  27. const char* destination,
  28. const char* file_permissions,
  29. std::vector<std::string> const& configurations,
  30. const char* component,
  31. const char* filename, const char* name_space,
  32. cmMakefile* mf)
  33. :cmInstallGenerator(destination, configurations, component)
  34. ,Name(name)
  35. ,FilePermissions(file_permissions)
  36. ,FileName(filename)
  37. ,Namespace(name_space)
  38. ,Makefile(mf)
  39. {
  40. this->EFGen = new cmExportInstallFileGenerator(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. // Get the export set requested.
  110. ExportSet const* exportSet =
  111. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  112. ->GetExportSet(this->Name.c_str());
  113. // Skip empty sets.
  114. if(!exportSet)
  115. {
  116. cmOStringStream e;
  117. e << "INSTALL(EXPORT) given unknown export \"" << this->Name << "\"";
  118. cmSystemTools::Error(e.str().c_str());
  119. return;
  120. }
  121. // Create the temporary directory in which to store the files.
  122. this->ComputeTempDir();
  123. cmSystemTools::MakeDirectory(this->TempDir.c_str());
  124. // Construct a temporary location for the file.
  125. this->MainImportFile = this->TempDir;
  126. this->MainImportFile += "/";
  127. this->MainImportFile += this->FileName;
  128. // Generate the import file for this export set.
  129. this->EFGen->SetName(this->Name.c_str());
  130. this->EFGen->SetExportSet(exportSet);
  131. this->EFGen->SetExportFile(this->MainImportFile.c_str());
  132. this->EFGen->SetNamespace(this->Namespace.c_str());
  133. if(this->ConfigurationTypes->empty())
  134. {
  135. if(this->ConfigurationName && *this->ConfigurationName)
  136. {
  137. this->EFGen->AddConfiguration(this->ConfigurationName);
  138. }
  139. else
  140. {
  141. this->EFGen->AddConfiguration("");
  142. }
  143. }
  144. else
  145. {
  146. for(std::vector<std::string>::const_iterator
  147. ci = this->ConfigurationTypes->begin();
  148. ci != this->ConfigurationTypes->end(); ++ci)
  149. {
  150. this->EFGen->AddConfiguration(ci->c_str());
  151. }
  152. }
  153. this->EFGen->GenerateImportFile();
  154. // Perform the main install script generation.
  155. this->cmInstallGenerator::GenerateScript(os);
  156. }
  157. //----------------------------------------------------------------------------
  158. void
  159. cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
  160. Indent const& indent)
  161. {
  162. // Create the main install rules first.
  163. this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
  164. // Now create a configuration-specific install rule for the import
  165. // file of each configuration.
  166. std::vector<std::string> files;
  167. for(std::map<cmStdString, cmStdString>::const_iterator
  168. i = this->EFGen->GetConfigImportFiles().begin();
  169. i != this->EFGen->GetConfigImportFiles().end(); ++i)
  170. {
  171. files.push_back(i->second);
  172. std::string config_test = this->CreateConfigTest(i->first.c_str());
  173. os << indent << "IF(" << config_test << ")\n";
  174. this->AddInstallRule(os, cmTarget::INSTALL_FILES, files, false, 0,
  175. this->FilePermissions.c_str(), 0, 0, 0,
  176. indent.Next());
  177. os << indent << "ENDIF(" << config_test << ")\n";
  178. files.clear();
  179. }
  180. }
  181. //----------------------------------------------------------------------------
  182. void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
  183. Indent const& indent)
  184. {
  185. // Install the main export file.
  186. std::vector<std::string> files;
  187. files.push_back(this->MainImportFile);
  188. this->AddInstallRule(os, cmTarget::INSTALL_FILES, files, false, 0,
  189. this->FilePermissions.c_str(), 0, 0, 0, indent);
  190. }