cmInstallExportGenerator.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. MessageLevel message,
  29. const char* filename, const char* name_space,
  30. bool exportOld,
  31. cmMakefile* mf)
  32. :cmInstallGenerator(destination, configurations, component, message)
  33. ,ExportSet(exportSet)
  34. ,FilePermissions(file_permissions)
  35. ,FileName(filename)
  36. ,Namespace(name_space)
  37. ,ExportOld(exportOld)
  38. ,Makefile(mf)
  39. {
  40. this->EFGen = new cmExportInstallFileGenerator(this);
  41. exportSet->AddInstallation(this);
  42. }
  43. //----------------------------------------------------------------------------
  44. cmInstallExportGenerator::~cmInstallExportGenerator()
  45. {
  46. delete this->EFGen;
  47. }
  48. //----------------------------------------------------------------------------
  49. void cmInstallExportGenerator::ComputeTempDir()
  50. {
  51. // Choose a temporary directory in which to generate the import
  52. // files to be installed.
  53. this->TempDir = this->Makefile->GetCurrentOutputDirectory();
  54. this->TempDir += cmake::GetCMakeFilesDirectory();
  55. this->TempDir += "/Export";
  56. if(this->Destination.empty())
  57. {
  58. return;
  59. }
  60. else
  61. {
  62. this->TempDir += "/";
  63. }
  64. // Enforce a maximum length.
  65. bool useMD5 = false;
  66. #if defined(_WIN32) || defined(__CYGWIN__)
  67. std::string::size_type const max_total_len = 250;
  68. #else
  69. std::string::size_type const max_total_len = 1000;
  70. #endif
  71. if(this->TempDir.size() < max_total_len)
  72. {
  73. // Keep the total path length below the limit.
  74. std::string::size_type max_len = max_total_len - this->TempDir.size();
  75. if(this->Destination.size() > max_len)
  76. {
  77. useMD5 = true;
  78. }
  79. }
  80. else
  81. {
  82. useMD5 = true;
  83. }
  84. if(useMD5)
  85. {
  86. // Replace the destination path with a hash to keep it short.
  87. this->TempDir +=
  88. cmSystemTools::ComputeStringMD5(this->Destination);
  89. }
  90. else
  91. {
  92. std::string dest = this->Destination;
  93. // Avoid unix full paths.
  94. if(dest[0] == '/')
  95. {
  96. dest[0] = '_';
  97. }
  98. // Avoid windows full paths by removing colons.
  99. cmSystemTools::ReplaceString(dest, ":", "_");
  100. // Avoid relative paths that go up the tree.
  101. cmSystemTools::ReplaceString(dest, "../", "__/");
  102. // Avoid spaces.
  103. cmSystemTools::ReplaceString(dest, " ", "_");
  104. this->TempDir += dest;
  105. }
  106. }
  107. //----------------------------------------------------------------------------
  108. void cmInstallExportGenerator::GenerateScript(std::ostream& os)
  109. {
  110. // Skip empty sets.
  111. if(ExportSet->GetTargetExports()->empty())
  112. {
  113. cmOStringStream e;
  114. e << "INSTALL(EXPORT) given unknown export \""
  115. << ExportSet->GetName() << "\"";
  116. cmSystemTools::Error(e.str().c_str());
  117. return;
  118. }
  119. // Create the temporary directory in which to store the files.
  120. this->ComputeTempDir();
  121. cmSystemTools::MakeDirectory(this->TempDir.c_str());
  122. // Construct a temporary location for the file.
  123. this->MainImportFile = this->TempDir;
  124. this->MainImportFile += "/";
  125. this->MainImportFile += this->FileName;
  126. // Generate the import file for this export set.
  127. this->EFGen->SetExportFile(this->MainImportFile.c_str());
  128. this->EFGen->SetNamespace(this->Namespace);
  129. this->EFGen->SetExportOld(this->ExportOld);
  130. if(this->ConfigurationTypes->empty())
  131. {
  132. if(!this->ConfigurationName.empty())
  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);
  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<std::string, std::string>::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);
  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()\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()\n";
  203. os << indentN << "endif()\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. }