Coverage Report

Created: 2026-03-12 06:35

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