cmGeneratorTarget_LinkDirectories.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. /* clang-format off */
  4. #include "cmGeneratorTarget.h"
  5. /* clang-format on */
  6. #include <map>
  7. #include <sstream>
  8. #include <string>
  9. #include <unordered_set>
  10. #include <utility>
  11. #include <vector>
  12. #include <cmext/algorithm>
  13. #include "cmEvaluatedTargetProperty.h"
  14. #include "cmGenExContext.h"
  15. #include "cmGeneratorExpressionDAGChecker.h"
  16. #include "cmLinkItem.h"
  17. #include "cmList.h"
  18. #include "cmListFileCache.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmMakefile.h"
  21. #include "cmMessageType.h"
  22. #include "cmPolicies.h"
  23. #include "cmStringAlgorithms.h"
  24. #include "cmSystemTools.h"
  25. #include "cmValue.h"
  26. #include "cmake.h"
  27. namespace {
  28. void processLinkDirectories(cmGeneratorTarget const* tgt,
  29. EvaluatedTargetPropertyEntries& entries,
  30. std::vector<BT<std::string>>& directories,
  31. std::unordered_set<std::string>& uniqueDirectories,
  32. bool debugDirectories)
  33. {
  34. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  35. cmLinkItem const& item = entry.LinkItem;
  36. std::string const& targetName = item.AsStr();
  37. std::string usedDirectories;
  38. for (std::string& entryDirectory : entry.Values) {
  39. if (!cmSystemTools::FileIsFullPath(entryDirectory)) {
  40. std::ostringstream e;
  41. bool noMessage = false;
  42. MessageType messageType = MessageType::FATAL_ERROR;
  43. if (!targetName.empty()) {
  44. /* clang-format off */
  45. e << "Target \"" << targetName << "\" contains relative "
  46. "path in its INTERFACE_LINK_DIRECTORIES:\n"
  47. " \"" << entryDirectory << "\"";
  48. /* clang-format on */
  49. } else {
  50. switch (tgt->GetPolicyStatusCMP0081()) {
  51. case cmPolicies::WARN: {
  52. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0081) << "\n";
  53. messageType = MessageType::AUTHOR_WARNING;
  54. } break;
  55. case cmPolicies::OLD:
  56. noMessage = true;
  57. break;
  58. case cmPolicies::NEW:
  59. // Issue the fatal message.
  60. break;
  61. }
  62. e << "Found relative path while evaluating link directories of "
  63. "\""
  64. << tgt->GetName() << "\":\n \"" << entryDirectory << "\"\n";
  65. }
  66. if (!noMessage) {
  67. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  68. if (messageType == MessageType::FATAL_ERROR) {
  69. return;
  70. }
  71. }
  72. }
  73. // Sanitize the path the same way the link_directories command does
  74. // in case projects set the LINK_DIRECTORIES property directly.
  75. cmSystemTools::ConvertToUnixSlashes(entryDirectory);
  76. if (uniqueDirectories.insert(entryDirectory).second) {
  77. directories.emplace_back(entryDirectory, entry.Backtrace);
  78. if (debugDirectories) {
  79. usedDirectories += " * " + entryDirectory + "\n";
  80. }
  81. }
  82. }
  83. if (!usedDirectories.empty()) {
  84. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  85. MessageType::LOG,
  86. std::string("Used link directories for target ") + tgt->GetName() +
  87. ":\n" + usedDirectories,
  88. entry.Backtrace);
  89. }
  90. }
  91. }
  92. }
  93. void cmGeneratorTarget::GetLinkDirectories(std::vector<std::string>& result,
  94. std::string const& config,
  95. std::string const& language) const
  96. {
  97. std::vector<BT<std::string>> tmp =
  98. this->GetLinkDirectories(config, language);
  99. result.reserve(tmp.size());
  100. for (BT<std::string>& v : tmp) {
  101. result.emplace_back(std::move(v.Value));
  102. }
  103. }
  104. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDirectories(
  105. std::string const& config, std::string const& language) const
  106. {
  107. ConfigAndLanguage cacheKey(
  108. config, cmStrCat(language, this->IsDeviceLink() ? "-device" : ""));
  109. {
  110. auto it = this->LinkDirectoriesCache.find(cacheKey);
  111. if (it != this->LinkDirectoriesCache.end()) {
  112. return it->second;
  113. }
  114. }
  115. std::vector<BT<std::string>> result;
  116. std::unordered_set<std::string> uniqueDirectories;
  117. cm::GenEx::Context context(this->LocalGenerator, config, language);
  118. cmGeneratorExpressionDAGChecker dagChecker{
  119. this, "LINK_DIRECTORIES", nullptr, nullptr, context,
  120. };
  121. cmList debugProperties{ this->Makefile->GetDefinition(
  122. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  123. bool debugDirectories = !this->DebugLinkDirectoriesDone &&
  124. cm::contains(debugProperties, "LINK_DIRECTORIES");
  125. this->DebugLinkDirectoriesDone = true;
  126. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  127. this, context, &dagChecker, this->LinkDirectoriesEntries);
  128. AddInterfaceEntries(this, "INTERFACE_LINK_DIRECTORIES", context, &dagChecker,
  129. entries, IncludeRuntimeInterface::Yes,
  130. this->GetPolicyStatusCMP0099() == cmPolicies::NEW
  131. ? UseTo::Link
  132. : UseTo::Compile);
  133. processLinkDirectories(this, entries, result, uniqueDirectories,
  134. debugDirectories);
  135. this->LinkDirectoriesCache.emplace(cacheKey, result);
  136. return result;
  137. }