cmInstallExportGenerator.cxx 9.9 KB

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