cmInstallFileSetGenerator.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "cmInstallFileSetGenerator.h"
  4. #include <algorithm>
  5. #include <map>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include <cm/string_view>
  10. #include <cmext/string_view>
  11. #include "cmFileSet.h"
  12. #include "cmGeneratorExpression.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmInstallType.h"
  16. #include "cmList.h"
  17. #include "cmListFileCache.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmMessageType.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmTarget.h"
  22. cmInstallFileSetGenerator::cmInstallFileSetGenerator(
  23. std::string targetName, std::string fileSetName, cmFileSetDestinations dests,
  24. std::string file_permissions, std::vector<std::string> const& configurations,
  25. std::string const& component, MessageLevel message, bool exclude_from_all,
  26. bool optional, cmListFileBacktrace backtrace)
  27. : cmInstallGenerator("", configurations, component, message,
  28. exclude_from_all, false, std::move(backtrace))
  29. , TargetName(std::move(targetName))
  30. , FileSetName(std::move(fileSetName))
  31. , FilePermissions(std::move(file_permissions))
  32. , FileSetDestinations(std::move(dests))
  33. , Optional(optional)
  34. {
  35. this->ActionsPerConfig = true;
  36. }
  37. cmInstallFileSetGenerator::~cmInstallFileSetGenerator() = default;
  38. bool cmInstallFileSetGenerator::Compute(cmLocalGenerator* lg)
  39. {
  40. this->LocalGenerator = lg;
  41. // Lookup this target in the current directory.
  42. this->Target = lg->FindLocalNonAliasGeneratorTarget(this->TargetName);
  43. if (!this->Target) {
  44. // If no local target has been found, find it in the global scope.
  45. this->Target =
  46. lg->GetGlobalGenerator()->FindGeneratorTarget(this->TargetName);
  47. }
  48. auto const& target = *this->Target->Target;
  49. this->FileSet = target.GetFileSet(this->FileSetName);
  50. if (!this->FileSet) {
  51. // No file set of the given name was ever provided for this target, nothing
  52. // for this generator to do
  53. return true;
  54. }
  55. cmList interfaceFileSetEntries{ target.GetSafeProperty(
  56. cmTarget::GetInterfaceFileSetsPropertyName(this->FileSet->GetType())) };
  57. if (std::find(interfaceFileSetEntries.begin(), interfaceFileSetEntries.end(),
  58. this->FileSetName) != interfaceFileSetEntries.end()) {
  59. if (this->FileSet->GetType() == "HEADERS"_s) {
  60. this->Destination = this->FileSetDestinations.Headers;
  61. } else {
  62. this->Destination = this->FileSetDestinations.CXXModules;
  63. }
  64. } else {
  65. // File set of the given name was provided but it's private, so give up
  66. this->FileSet = nullptr;
  67. return true;
  68. }
  69. if (this->Destination.empty()) {
  70. lg->IssueMessage(MessageType::FATAL_ERROR,
  71. cmStrCat("File set \"", this->FileSetName,
  72. "\" installed by target \"", this->TargetName,
  73. "\" has no destination."));
  74. return false;
  75. }
  76. return true;
  77. }
  78. std::string cmInstallFileSetGenerator::GetDestination(
  79. std::string const& config) const
  80. {
  81. return cmGeneratorExpression::Evaluate(this->Destination,
  82. this->LocalGenerator, config);
  83. }
  84. void cmInstallFileSetGenerator::GenerateScriptForConfig(
  85. std::ostream& os, std::string const& config, Indent indent)
  86. {
  87. if (!this->FileSet) {
  88. return;
  89. }
  90. for (auto const& dirEntry : this->CalculateFilesPerDir(config)) {
  91. std::string destSub;
  92. if (!dirEntry.first.empty()) {
  93. destSub = cmStrCat('/', dirEntry.first);
  94. }
  95. this->AddInstallRule(os, cmStrCat(this->GetDestination(config), destSub),
  96. cmInstallType_FILES, dirEntry.second,
  97. this->GetOptional(), this->FilePermissions.c_str(),
  98. nullptr, nullptr, nullptr, indent);
  99. }
  100. }
  101. std::map<std::string, std::vector<std::string>>
  102. cmInstallFileSetGenerator::CalculateFilesPerDir(
  103. std::string const& config) const
  104. {
  105. std::map<std::string, std::vector<std::string>> result;
  106. auto dirCges = this->FileSet->CompileDirectoryEntries();
  107. auto dirs = this->FileSet->EvaluateDirectoryEntries(
  108. dirCges, this->LocalGenerator, config, this->Target);
  109. auto fileCges = this->FileSet->CompileFileEntries();
  110. for (auto const& fileCge : fileCges) {
  111. this->FileSet->EvaluateFileEntry(
  112. dirs, result, fileCge, this->LocalGenerator, config, this->Target);
  113. }
  114. return result;
  115. }