cmInstallDirectoryGenerator.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmInstallDirectoryGenerator.h"
  4. #include <algorithm>
  5. #include <utility>
  6. #include "cmGeneratorExpression.h"
  7. #include "cmInstallType.h"
  8. #include "cmList.h"
  9. #include "cmListFileCache.h"
  10. #include "cmLocalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. cmInstallDirectoryGenerator::cmInstallDirectoryGenerator(
  15. std::vector<std::string> const& dirs, std::string const& dest,
  16. std::string file_permissions, std::string dir_permissions,
  17. std::vector<std::string> const& configurations, std::string const& component,
  18. MessageLevel message, bool exclude_from_all, std::string literal_args,
  19. bool optional, cmListFileBacktrace backtrace)
  20. : cmInstallGenerator(dest, configurations, component, message,
  21. exclude_from_all, false, std::move(backtrace))
  22. , Directories(dirs)
  23. , FilePermissions(std::move(file_permissions))
  24. , DirPermissions(std::move(dir_permissions))
  25. , LiteralArguments(std::move(literal_args))
  26. , Optional(optional)
  27. {
  28. // We need per-config actions if destination have generator expressions.
  29. if (cmGeneratorExpression::Find(this->Destination) != std::string::npos) {
  30. this->ActionsPerConfig = true;
  31. }
  32. // We need per-config actions if any directories have generator expressions.
  33. if (!this->ActionsPerConfig) {
  34. for (std::string const& dir : dirs) {
  35. if (cmGeneratorExpression::Find(dir) != std::string::npos) {
  36. this->ActionsPerConfig = true;
  37. break;
  38. }
  39. }
  40. }
  41. }
  42. cmInstallDirectoryGenerator::~cmInstallDirectoryGenerator() = default;
  43. bool cmInstallDirectoryGenerator::Compute(cmLocalGenerator* lg)
  44. {
  45. this->LocalGenerator = lg;
  46. return true;
  47. }
  48. std::vector<std::string> cmInstallDirectoryGenerator::GetDirectories(
  49. std::string const& config) const
  50. {
  51. // If given only empty directories, collapse into a single specification to
  52. // avoid redundant calls. This supports the use case of installing an empty
  53. // directory into a destination when a directory is not specified.
  54. if (std::all_of(this->Directories.begin(), this->Directories.end(),
  55. [](std::string const& d) { return d.empty(); })) {
  56. return std::vector<std::string>{ "" };
  57. }
  58. cmList directories;
  59. if (this->ActionsPerConfig) {
  60. for (std::string const& f : this->Directories) {
  61. directories.append(
  62. cmGeneratorExpression::Evaluate(f, this->LocalGenerator, config));
  63. }
  64. } else {
  65. directories = this->Directories;
  66. }
  67. return std::move(directories.data());
  68. }
  69. void cmInstallDirectoryGenerator::GenerateScriptActions(std::ostream& os,
  70. Indent indent)
  71. {
  72. if (this->ActionsPerConfig) {
  73. this->cmInstallGenerator::GenerateScriptActions(os, indent);
  74. } else {
  75. this->AddDirectoryInstallRule(os, "", indent, this->Directories);
  76. }
  77. }
  78. void cmInstallDirectoryGenerator::GenerateScriptForConfig(
  79. std::ostream& os, std::string const& config, Indent indent)
  80. {
  81. std::vector<std::string> dirs = this->GetDirectories(config);
  82. if (!(dirs.size() == 1 && dirs[0].empty())) {
  83. // Make sure all dirs have absolute paths.
  84. cmMakefile const& mf = *this->LocalGenerator->GetMakefile();
  85. for (std::string& d : dirs) {
  86. if (!cmSystemTools::FileIsFullPath(d)) {
  87. d = cmStrCat(mf.GetCurrentSourceDirectory(), '/', d);
  88. }
  89. }
  90. }
  91. this->AddDirectoryInstallRule(os, config, indent, dirs);
  92. }
  93. void cmInstallDirectoryGenerator::AddDirectoryInstallRule(
  94. std::ostream& os, std::string const& config, Indent indent,
  95. std::vector<std::string> const& dirs)
  96. {
  97. // Write code to install the directories.
  98. char const* no_rename = nullptr;
  99. this->AddInstallRule(os, this->GetDestination(config),
  100. cmInstallType_DIRECTORY, dirs, this->Optional,
  101. this->FilePermissions.c_str(),
  102. this->DirPermissions.c_str(), no_rename,
  103. this->LiteralArguments.c_str(), indent);
  104. }
  105. std::string cmInstallDirectoryGenerator::GetDestination(
  106. std::string const& config) const
  107. {
  108. return cmGeneratorExpression::Evaluate(this->Destination,
  109. this->LocalGenerator, config);
  110. }