Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmSearchPath.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
#include "cmSearchPath.h"
4
5
#include <algorithm>
6
#include <cassert>
7
#include <utility>
8
9
#include <cm/optional>
10
11
#include "cmFindCommon.h"
12
#include "cmList.h"
13
#include "cmMakefile.h"
14
#include "cmStringAlgorithms.h"
15
#include "cmSystemTools.h"
16
#include "cmValue.h"
17
#include "cmWindowsRegistry.h"
18
19
cmSearchPath::cmSearchPath(cmFindCommon* findCmd)
20
0
  : FC(findCmd)
21
0
{
22
0
}
23
24
0
cmSearchPath::~cmSearchPath() = default;
25
26
void cmSearchPath::ExtractWithout(std::set<std::string> const& ignorePaths,
27
                                  std::set<std::string> const& ignorePrefixes,
28
                                  std::vector<std::string>& outPaths) const
29
0
{
30
0
  for (auto const& path : this->Paths) {
31
0
    if (ignorePaths.find(path.Path) == ignorePaths.end() &&
32
0
        ignorePrefixes.find(path.Prefix) == ignorePrefixes.end()) {
33
0
      outPaths.push_back(path.Path);
34
0
    }
35
0
  }
36
0
}
37
38
void cmSearchPath::AddPath(std::string const& path)
39
0
{
40
0
  this->AddPathInternal(path, "");
41
0
}
42
43
void cmSearchPath::AddUserPath(std::string const& path)
44
0
{
45
0
  assert(this->FC);
46
47
0
  std::vector<std::string> outPaths;
48
49
0
  cmWindowsRegistry registry(*this->FC->Makefile,
50
0
                             cmWindowsRegistry::SimpleTypes);
51
0
  auto expandedPaths = registry.ExpandExpression(path, this->FC->RegistryView);
52
0
  if (expandedPaths) {
53
0
    for (auto const& expandedPath : expandedPaths.value()) {
54
0
      cmSystemTools::GlobDirs(expandedPath, outPaths);
55
0
    }
56
0
  }
57
58
  // Process them all from the current directory
59
0
  for (std::string const& p : outPaths) {
60
0
    this->AddPathInternal(
61
0
      cmSystemTools::CollapseFullPath(
62
0
        p, this->FC->Makefile->GetCurrentSourceDirectory()),
63
0
      "");
64
0
  }
65
0
}
66
67
void cmSearchPath::AddCMakePath(std::string const& variable)
68
0
{
69
0
  assert(this->FC);
70
71
  // Get a path from a CMake variable.
72
0
  if (cmValue value = this->FC->Makefile->GetDefinition(variable)) {
73
0
    cmList expanded{ *value };
74
75
0
    for (std::string const& p : expanded) {
76
0
      this->AddPathInternal(
77
0
        cmSystemTools::CollapseFullPath(
78
0
          p, this->FC->Makefile->GetCurrentSourceDirectory()),
79
0
        "");
80
0
    }
81
0
  }
82
0
}
83
84
void cmSearchPath::AddEnvPath(std::string const& variable)
85
0
{
86
0
  std::vector<std::string> expanded =
87
0
    cmSystemTools::GetEnvPathNormalized(variable);
88
0
  for (std::string const& p : expanded) {
89
0
    this->AddPathInternal(p, "");
90
0
  }
91
0
}
92
93
void cmSearchPath::AddCMakePrefixPath(std::string const& variable)
94
0
{
95
0
  assert(this->FC);
96
97
  // Get a path from a CMake variable.
98
0
  if (cmValue value = this->FC->Makefile->GetDefinition(variable)) {
99
0
    cmList expanded{ *value };
100
0
    for (std::string& p : expanded) {
101
0
      p = cmSystemTools::CollapseFullPath(
102
0
        p, this->FC->Makefile->GetCurrentSourceDirectory());
103
0
    }
104
0
    this->AddPrefixPaths(expanded);
105
0
  }
106
0
}
107
108
static std::string cmSearchPathStripBin(std::string const& s)
109
0
{
110
  // If the path is a PREFIX/bin case then add its parent instead.
111
0
  if ((cmHasLiteralSuffix(s, "/bin")) || (cmHasLiteralSuffix(s, "/sbin"))) {
112
0
    return cmSystemTools::GetFilenamePath(s);
113
0
  }
114
0
  return s;
115
0
}
116
117
void cmSearchPath::AddEnvPrefixPath(std::string const& variable, bool stripBin)
118
0
{
119
0
  std::vector<std::string> expanded =
120
0
    cmSystemTools::GetEnvPathNormalized(variable);
121
0
  if (stripBin) {
122
0
    std::transform(expanded.begin(), expanded.end(), expanded.begin(),
123
0
                   cmSearchPathStripBin);
124
0
  }
125
0
  this->AddPrefixPaths(expanded);
126
0
}
127
128
void cmSearchPath::AddSuffixes(std::vector<std::string> const& suffixes)
129
0
{
130
0
  std::vector<PathWithPrefix> inPaths;
131
0
  inPaths.swap(this->Paths);
132
0
  this->Paths.reserve(inPaths.size() * (suffixes.size() + 1));
133
134
0
  for (PathWithPrefix& inPath : inPaths) {
135
0
    cmSystemTools::ConvertToUnixSlashes(inPath.Path);
136
0
    cmSystemTools::ConvertToUnixSlashes(inPath.Prefix);
137
138
    // if *i is only / then do not add a //
139
    // this will get incorrectly considered a network
140
    // path on windows and cause huge delays.
141
0
    std::string p = inPath.Path;
142
0
    if (!p.empty() && p.back() != '/') {
143
0
      p += '/';
144
0
    }
145
146
    // Combine with all the suffixes
147
0
    for (std::string const& suffix : suffixes) {
148
0
      this->Paths.emplace_back(PathWithPrefix{ p + suffix, inPath.Prefix });
149
0
    }
150
151
    // And now the original w/o any suffix
152
0
    this->Paths.emplace_back(std::move(inPath));
153
0
  }
154
0
}
155
156
void cmSearchPath::AddPrefixPaths(std::vector<std::string> const& paths)
157
0
{
158
0
  assert(this->FC);
159
160
  // default for programs
161
0
  std::string subdir = "bin";
162
163
0
  if (this->FC->CMakePathName == "INCLUDE") {
164
0
    subdir = "include";
165
0
  } else if (this->FC->CMakePathName == "LIBRARY") {
166
0
    subdir = "lib";
167
0
  } else if (this->FC->CMakePathName == "FRAMEWORK") {
168
0
    subdir.clear(); // ? what to do for frameworks ?
169
0
  }
170
171
0
  for (std::string const& path : paths) {
172
0
    std::string dir = path;
173
0
    if (!subdir.empty() && !dir.empty() && dir.back() != '/') {
174
0
      dir += "/";
175
0
    }
176
0
    std::string prefix = dir;
177
0
    if (!prefix.empty() && prefix != "/") {
178
0
      prefix.erase(prefix.size() - 1);
179
0
    }
180
0
    if (subdir == "include" || subdir == "lib") {
181
0
      cmValue arch =
182
0
        this->FC->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE");
183
0
      if (cmNonempty(arch)) {
184
0
        std::string archNoUnknown = *arch;
185
0
        auto unknownAtPos = archNoUnknown.find("-unknown-");
186
0
        bool foundUnknown = unknownAtPos != std::string::npos;
187
0
        if (foundUnknown) {
188
          // Replace "-unknown-" with "-".
189
0
          archNoUnknown.replace(unknownAtPos, 9, "-");
190
0
        }
191
0
        if (this->FC->Makefile->IsDefinitionSet("CMAKE_SYSROOT") &&
192
0
            this->FC->Makefile->IsDefinitionSet(
193
0
              "CMAKE_PREFIX_LIBRARY_ARCHITECTURE")) {
194
0
          if (foundUnknown) {
195
0
            this->AddPathInternal(cmStrCat('/', archNoUnknown, dir, subdir),
196
0
                                  cmStrCat('/', archNoUnknown, prefix));
197
0
          }
198
0
          this->AddPathInternal(cmStrCat('/', *arch, dir, subdir),
199
0
                                cmStrCat('/', *arch, prefix));
200
0
        } else {
201
0
          if (foundUnknown) {
202
0
            this->AddPathInternal(cmStrCat(dir, subdir, '/', archNoUnknown),
203
0
                                  prefix);
204
0
          }
205
0
          this->AddPathInternal(cmStrCat(dir, subdir, '/', *arch), prefix);
206
0
        }
207
0
      }
208
0
    }
209
0
    std::string add = dir + subdir;
210
0
    if (add != "/") {
211
0
      this->AddPathInternal(add, prefix);
212
0
    }
213
0
    if (subdir == "bin") {
214
0
      this->AddPathInternal(dir + "sbin", prefix);
215
0
    }
216
0
    if (!subdir.empty() && path != "/") {
217
0
      this->AddPathInternal(path, prefix);
218
0
    }
219
0
  }
220
0
}
221
222
void cmSearchPath::AddPathInternal(std::string const& path,
223
                                   std::string const& prefix)
224
0
{
225
0
  assert(this->FC);
226
227
0
  if (path.empty()) {
228
0
    return;
229
0
  }
230
231
  // Insert the path if has not already been emitted.
232
0
  PathWithPrefix pathWithPrefix{ path, prefix };
233
0
  if (this->FC->SearchPathsEmitted.insert(pathWithPrefix).second) {
234
0
    this->Paths.emplace_back(std::move(pathWithPrefix));
235
0
  }
236
0
}