cmInstallExportGenerator.cxx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmInstallExportGenerator.h"
  4. #include <map>
  5. #include <sstream>
  6. #include <utility>
  7. #include <cm/memory>
  8. #ifndef CMAKE_BOOTSTRAP
  9. # include "cmExportInstallAndroidMKGenerator.h"
  10. #endif
  11. #include "cmExportInstallFileGenerator.h"
  12. #include "cmExportSet.h"
  13. #include "cmInstallType.h"
  14. #include "cmListFileCache.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmScriptGenerator.h"
  17. #include "cmStringAlgorithms.h"
  18. #include "cmSystemTools.h"
  19. cmInstallExportGenerator::cmInstallExportGenerator(
  20. cmExportSet* exportSet, std::string const& destination,
  21. std::string file_permissions, std::vector<std::string> const& configurations,
  22. std::string const& component, MessageLevel message, bool exclude_from_all,
  23. std::string filename, std::string name_space,
  24. std::string cxx_modules_directory, bool exportOld, bool android,
  25. cmListFileBacktrace backtrace)
  26. : cmInstallGenerator(destination, configurations, component, message,
  27. exclude_from_all, false, std::move(backtrace))
  28. , ExportSet(exportSet)
  29. , FilePermissions(std::move(file_permissions))
  30. , FileName(std::move(filename))
  31. , Namespace(std::move(name_space))
  32. , CxxModulesDirectory(std::move(cxx_modules_directory))
  33. , ExportOld(exportOld)
  34. {
  35. if (android) {
  36. #ifndef CMAKE_BOOTSTRAP
  37. this->EFGen = cm::make_unique<cmExportInstallAndroidMKGenerator>(this);
  38. #endif
  39. } else {
  40. this->EFGen = cm::make_unique<cmExportInstallFileGenerator>(this);
  41. }
  42. exportSet->AddInstallation(this);
  43. }
  44. cmInstallExportGenerator::~cmInstallExportGenerator() = default;
  45. bool cmInstallExportGenerator::Compute(cmLocalGenerator* lg)
  46. {
  47. this->LocalGenerator = lg;
  48. return this->ExportSet->Compute(lg);
  49. }
  50. std::string cmInstallExportGenerator::TempDirCalculate() const
  51. {
  52. // Choose a temporary directory in which to generate the import
  53. // files to be installed.
  54. std::string path = cmStrCat(
  55. this->LocalGenerator->GetCurrentBinaryDirectory(), "/CMakeFiles/Export");
  56. if (this->Destination.empty()) {
  57. return path;
  58. }
  59. #ifndef CMAKE_BOOTSTRAP
  60. path += '/';
  61. // Replace the destination path with a hash to keep it short.
  62. path += cmSystemTools::ComputeStringMD5(this->Destination);
  63. #endif
  64. return path;
  65. }
  66. void cmInstallExportGenerator::ComputeTempDir()
  67. {
  68. this->TempDir = this->TempDirCalculate();
  69. }
  70. std::string cmInstallExportGenerator::GetTempDir() const
  71. {
  72. if (this->TempDir.empty()) {
  73. return this->TempDirCalculate();
  74. }
  75. return this->TempDir;
  76. }
  77. void cmInstallExportGenerator::GenerateScript(std::ostream& os)
  78. {
  79. // Skip empty sets.
  80. if (this->ExportSet->GetTargetExports().empty()) {
  81. std::ostringstream e;
  82. e << "INSTALL(EXPORT) given unknown export \""
  83. << this->ExportSet->GetName() << "\"";
  84. cmSystemTools::Error(e.str());
  85. return;
  86. }
  87. // Create the temporary directory in which to store the files.
  88. this->ComputeTempDir();
  89. cmSystemTools::MakeDirectory(this->TempDir);
  90. // Construct a temporary location for the file.
  91. this->MainImportFile = cmStrCat(this->TempDir, '/', this->FileName);
  92. // Generate the import file for this export set.
  93. this->EFGen->SetExportFile(this->MainImportFile.c_str());
  94. this->EFGen->SetNamespace(this->Namespace);
  95. this->EFGen->SetExportOld(this->ExportOld);
  96. if (this->ConfigurationTypes->empty()) {
  97. if (!this->ConfigurationName.empty()) {
  98. this->EFGen->AddConfiguration(this->ConfigurationName);
  99. } else {
  100. this->EFGen->AddConfiguration("");
  101. }
  102. } else {
  103. for (std::string const& c : *this->ConfigurationTypes) {
  104. this->EFGen->AddConfiguration(c);
  105. }
  106. }
  107. this->EFGen->GenerateImportFile();
  108. // Perform the main install script generation.
  109. this->cmInstallGenerator::GenerateScript(os);
  110. }
  111. void cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
  112. Indent indent)
  113. {
  114. // Create the main install rules first.
  115. this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
  116. // Now create a configuration-specific install rule for the import
  117. // file of each configuration.
  118. std::vector<std::string> files;
  119. for (auto const& i : this->EFGen->GetConfigImportFiles()) {
  120. files.push_back(i.second);
  121. std::string config_test = this->CreateConfigTest(i.first);
  122. os << indent << "if(" << config_test << ")\n";
  123. this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
  124. false, this->FilePermissions.c_str(), nullptr,
  125. nullptr, nullptr, indent.Next());
  126. os << indent << "endif()\n";
  127. files.clear();
  128. }
  129. // Now create a configuration-specific install rule for the C++ module import
  130. // property file of each configuration.
  131. auto cxx_module_dest =
  132. cmStrCat(this->Destination, '/', this->CxxModulesDirectory);
  133. std::string config_file_example;
  134. for (auto const& i : this->EFGen->GetConfigCxxModuleFiles()) {
  135. config_file_example = i.second;
  136. break;
  137. }
  138. if (!config_file_example.empty()) {
  139. // Remove old per-configuration export files if the main changes.
  140. std::string installedDir = cmStrCat(
  141. "$ENV{DESTDIR}", ConvertToAbsoluteDestination(cxx_module_dest), '/');
  142. std::string installedFile = cmStrCat(installedDir, "/cxx-modules.cmake");
  143. std::string toInstallFile =
  144. cmStrCat(cmSystemTools::GetFilenamePath(config_file_example),
  145. "/cxx-modules.cmake");
  146. os << indent << "if(EXISTS \"" << installedFile << "\")\n";
  147. Indent indentN = indent.Next();
  148. Indent indentNN = indentN.Next();
  149. Indent indentNNN = indentNN.Next();
  150. /* clang-format off */
  151. os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
  152. << indentN << " \"" << installedFile << "\"\n"
  153. << indentN << " \"" << toInstallFile << "\")\n";
  154. os << indentN << "if(_cmake_export_file_changed)\n";
  155. os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
  156. << this->EFGen->GetConfigImportFileGlob() << "\")\n";
  157. os << indentNN << "if(_cmake_old_config_files)\n";
  158. os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
  159. os << indentNNN << R"(message(STATUS "Old C++ module export file \")" << installedFile
  160. << "\\\" will be replaced. Removing files [${_cmake_old_config_files_text}].\")\n";
  161. os << indentNNN << "unset(_cmake_old_config_files_text)\n";
  162. os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
  163. os << indentNN << "endif()\n";
  164. os << indentNN << "unset(_cmake_old_config_files)\n";
  165. os << indentN << "endif()\n";
  166. os << indentN << "unset(_cmake_export_file_changed)\n";
  167. os << indent << "endif()\n";
  168. /* clang-format on */
  169. // All of these files are siblings; get its location to know where the
  170. // "anchor" file is.
  171. files.push_back(toInstallFile);
  172. this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, files,
  173. false, this->FilePermissions.c_str(), nullptr,
  174. nullptr, nullptr, indent);
  175. files.clear();
  176. }
  177. for (auto const& i : this->EFGen->GetConfigCxxModuleFiles()) {
  178. files.push_back(i.second);
  179. std::string config_test = this->CreateConfigTest(i.first);
  180. os << indent << "if(" << config_test << ")\n";
  181. this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, files,
  182. false, this->FilePermissions.c_str(), nullptr,
  183. nullptr, nullptr, indent.Next());
  184. os << indent << "endif()\n";
  185. files.clear();
  186. }
  187. for (auto const& i : this->EFGen->GetConfigCxxModuleTargetFiles()) {
  188. std::string config_test = this->CreateConfigTest(i.first);
  189. os << indent << "if(" << config_test << ")\n";
  190. this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, i.second,
  191. false, this->FilePermissions.c_str(), nullptr,
  192. nullptr, nullptr, indent.Next());
  193. os << indent << "endif()\n";
  194. files.clear();
  195. }
  196. }
  197. void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
  198. Indent indent)
  199. {
  200. // Remove old per-configuration export files if the main changes.
  201. std::string installedDir = cmStrCat(
  202. "$ENV{DESTDIR}", ConvertToAbsoluteDestination(this->Destination), '/');
  203. std::string installedFile = cmStrCat(installedDir, this->FileName);
  204. os << indent << "if(EXISTS \"" << installedFile << "\")\n";
  205. Indent indentN = indent.Next();
  206. Indent indentNN = indentN.Next();
  207. Indent indentNNN = indentNN.Next();
  208. /* clang-format off */
  209. os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
  210. << indentN << " \"" << installedFile << "\"\n"
  211. << indentN << " \"" << this->MainImportFile << "\")\n";
  212. os << indentN << "if(_cmake_export_file_changed)\n";
  213. os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
  214. << this->EFGen->GetConfigImportFileGlob() << "\")\n";
  215. os << indentNN << "if(_cmake_old_config_files)\n";
  216. os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
  217. os << indentNNN << R"(message(STATUS "Old export file \")" << installedFile
  218. << "\\\" will be replaced. Removing files [${_cmake_old_config_files_text}].\")\n";
  219. os << indentNNN << "unset(_cmake_old_config_files_text)\n";
  220. os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
  221. os << indentNN << "endif()\n";
  222. os << indentNN << "unset(_cmake_old_config_files)\n";
  223. os << indentN << "endif()\n";
  224. os << indentN << "unset(_cmake_export_file_changed)\n";
  225. os << indent << "endif()\n";
  226. /* clang-format on */
  227. // Install the main export file.
  228. std::vector<std::string> files;
  229. files.push_back(this->MainImportFile);
  230. this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
  231. false, this->FilePermissions.c_str(), nullptr, nullptr,
  232. nullptr, indent);
  233. }
  234. std::string cmInstallExportGenerator::GetDestinationFile() const
  235. {
  236. return this->Destination + '/' + this->FileName;
  237. }