summaryrefslogtreecommitdiff
path: root/Source/cmLinkItemGraphVisitor.cxx
blob: 0ad846b7ea8bff29fcef93e3c880bbcfc3192c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmLinkItemGraphVisitor.h"

#include <map>
#include <utility>
#include <vector>

#include "cmGeneratorTarget.h"
#include "cmLinkItem.h"
#include "cmMakefile.h"

void cmLinkItemGraphVisitor::VisitItem(cmLinkItem const& item)
{
  if (this->ItemVisited(item)) {
    return;
  }

  this->OnItem(item);

  this->VisitLinks(item, item);
}

void cmLinkItemGraphVisitor::VisitLinks(cmLinkItem const& item,
                                        cmLinkItem const& rootItem)
{
  if (item.Target == nullptr) {
    return;
  }

  for (auto const& config : item.Target->Makefile->GetGeneratorConfigs(
         cmMakefile::IncludeEmptyConfig)) {
    this->VisitLinks(item, rootItem, config);
  }
}

void cmLinkItemGraphVisitor::VisitLinks(cmLinkItem const& item,
                                        cmLinkItem const& rootItem,
                                        std::string const& config)
{
  auto const& target = *item.Target;

  DependencyMap dependencies;
  cmLinkItemGraphVisitor::GetDependencies(target, config, dependencies);

  for (auto const& d : dependencies) {
    auto const& dependency = d.second;
    auto const& dependencyType = dependency.first;
    auto const& dependee = dependency.second;
    this->VisitItem(dependee);

    if (this->LinkVisited(item, dependee)) {
      continue;
    }

    this->OnDirectLink(item, dependee, dependencyType);

    if (rootItem.AsStr() != item.AsStr()) {
      this->OnIndirectLink(rootItem, dependee);
    }

    // Visit all the direct and indirect links.
    this->VisitLinks(dependee, dependee);
    this->VisitLinks(dependee, item);
    this->VisitLinks(dependee, rootItem);
  }
}

bool cmLinkItemGraphVisitor::ItemVisited(cmLinkItem const& item)
{
  auto& collection = this->VisitedItems;

  bool const visited = collection.find(item.AsStr()) != collection.cend();

  if (!visited) {
    collection.insert(item.AsStr());
  }

  return visited;
}

bool cmLinkItemGraphVisitor::LinkVisited(cmLinkItem const& depender,
                                         cmLinkItem const& dependee)
{
  auto const link = std::make_pair<>(depender.AsStr(), dependee.AsStr());

  bool const linkVisited =
    this->VisitedLinks.find(link) != this->VisitedLinks.cend();

  if (!linkVisited) {
    this->VisitedLinks.insert(link);
  }

  return linkVisited;
}

void cmLinkItemGraphVisitor::GetDependencies(cmGeneratorTarget const& target,
                                             std::string const& config,
                                             DependencyMap& dependencies)
{
  const auto* implementationLibraries = target.GetLinkImplementationLibraries(
    config, cmGeneratorTarget::LinkInterfaceFor::Link);
  if (implementationLibraries != nullptr) {
    for (auto const& lib : implementationLibraries->Libraries) {
      auto const& name = lib.AsStr();
      dependencies[name] = Dependency(DependencyType::LinkPrivate, lib);
    }
  }

  const auto* interfaceLibraries = target.GetLinkInterfaceLibraries(
    config, &target, cmGeneratorTarget::LinkInterfaceFor::Usage);
  if (interfaceLibraries != nullptr) {
    for (auto const& lib : interfaceLibraries->Libraries) {
      auto const& name = lib.AsStr();
      if (dependencies.find(name) != dependencies.cend()) {
        dependencies[name] = Dependency(DependencyType::LinkPublic, lib);
      } else {
        dependencies[name] = Dependency(DependencyType::LinkInterface, lib);
      }
    }
  }

  std::vector<cmGeneratorTarget*> objectLibraries;
  target.GetObjectLibrariesCMP0026(objectLibraries);
  for (auto const& lib : objectLibraries) {
    auto const& name = lib->GetName();
    if (dependencies.find(name) == dependencies.cend()) {
      auto objectItem = cmLinkItem(lib, false, lib->GetBacktrace());
      dependencies[name] = Dependency(DependencyType::Object, objectItem);
    }
  }

  auto const& utilityItems = target.GetUtilityItems();
  for (auto const& item : utilityItems) {
    auto const& name = item.AsStr();
    if (dependencies.find(name) == dependencies.cend()) {
      dependencies[name] = Dependency(DependencyType::Utility, item);
    }
  }
}