Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmFindBase.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 "cmFindBase.h"
4
5
#include <algorithm>
6
#include <cstddef>
7
#include <deque>
8
#include <iterator>
9
#include <map>
10
#include <memory>
11
#include <utility>
12
13
#include <cm/optional>
14
#include <cmext/algorithm>
15
#include <cmext/string_view>
16
17
#include "cmCMakePath.h"
18
#include "cmConfigureLog.h"
19
#include "cmExecutionStatus.h"
20
#include "cmList.h"
21
#include "cmListFileCache.h"
22
#include "cmMakefile.h"
23
#include "cmMessageType.h"
24
#include "cmPolicies.h"
25
#include "cmRange.h"
26
#include "cmSearchPath.h"
27
#include "cmState.h"
28
#include "cmStateTypes.h"
29
#include "cmStringAlgorithms.h"
30
#include "cmSystemTools.h"
31
#include "cmValue.h"
32
#include "cmWindowsRegistry.h"
33
#include "cmake.h"
34
35
cmFindBase::cmFindBase(std::string findCommandName, cmExecutionStatus& status)
36
0
  : cmFindCommon(status)
37
0
  , FindCommandName(std::move(findCommandName))
38
0
{
39
0
}
40
41
cmFindBase::~cmFindBase()
42
0
{
43
0
  if (this->DebugState) {
44
0
    this->DebugState->Write();
45
0
  }
46
0
}
47
48
bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
49
0
{
50
0
  if (argsIn.size() < 2) {
51
0
    this->SetError("called with incorrect number of arguments");
52
0
    return false;
53
0
  }
54
55
  // copy argsIn into args so it can be modified,
56
  // in the process extract the DOC "documentation"
57
  // and handle options NO_CACHE and ENV
58
0
  size_t size = argsIn.size();
59
0
  std::vector<std::string> args;
60
0
  bool foundDoc = false;
61
0
  for (unsigned int j = 0; j < size; ++j) {
62
0
    if (foundDoc || argsIn[j] != "DOC") {
63
0
      if (argsIn[j] == "NO_CACHE") {
64
0
        this->StoreResultInCache = false;
65
0
      } else if (argsIn[j] == "ENV") {
66
0
        if (j + 1 < size) {
67
0
          j++;
68
0
          std::vector<std::string> p =
69
0
            cmSystemTools::GetEnvPathNormalized(argsIn[j]);
70
0
          std::move(p.begin(), p.end(), std::back_inserter(args));
71
0
        }
72
0
      } else {
73
0
        args.push_back(argsIn[j]);
74
0
      }
75
0
    } else {
76
0
      if (j + 1 < size) {
77
0
        foundDoc = true;
78
0
        this->VariableDocumentation = argsIn[j + 1];
79
0
        j++;
80
0
        if (j >= size) {
81
0
          break;
82
0
        }
83
0
      }
84
0
    }
85
0
  }
86
0
  if (args.size() < 2) {
87
0
    this->SetError("called with incorrect number of arguments");
88
0
    return false;
89
0
  }
90
0
  this->VariableName = args[0];
91
0
  this->InitialState = this->GetInitialState();
92
0
  if (this->IsFound()) {
93
0
    if (this->DebugState) {
94
0
      this->DebugState->FoundAt(
95
0
        *this->Makefile->GetDefinition(this->VariableName));
96
0
    }
97
0
    return true;
98
0
  }
99
100
  // Find what search path locations have been enabled/disable
101
0
  this->SelectDefaultSearchModes();
102
103
  // Find the current root path mode.
104
0
  this->SelectDefaultRootPathMode();
105
106
  // Find the current bundle/framework search policy.
107
0
  this->SelectDefaultMacMode();
108
109
0
  bool newStyle = false;
110
0
  bool haveRequiredOrOptional = false;
111
0
  enum Doing
112
0
  {
113
0
    DoingNone,
114
0
    DoingNames,
115
0
    DoingPaths,
116
0
    DoingPathSuffixes,
117
0
    DoingHints
118
0
  };
119
0
  Doing doing = DoingNames; // assume it starts with a name
120
0
  for (unsigned int j = 1; j < args.size(); ++j) {
121
0
    if (args[j] == "NAMES") {
122
0
      doing = DoingNames;
123
0
      newStyle = true;
124
0
    } else if (args[j] == "PATHS") {
125
0
      doing = DoingPaths;
126
0
      newStyle = true;
127
0
    } else if (args[j] == "HINTS") {
128
0
      doing = DoingHints;
129
0
      newStyle = true;
130
0
    } else if (args[j] == "PATH_SUFFIXES") {
131
0
      doing = DoingPathSuffixes;
132
0
      newStyle = true;
133
0
    } else if (args[j] == "NAMES_PER_DIR") {
134
0
      doing = DoingNone;
135
0
      if (this->NamesPerDirAllowed) {
136
0
        this->NamesPerDir = true;
137
0
      } else {
138
0
        this->SetError("does not support NAMES_PER_DIR");
139
0
        return false;
140
0
      }
141
0
    } else if (args[j] == "NO_SYSTEM_PATH") {
142
0
      doing = DoingNone;
143
0
      this->NoDefaultPath = true;
144
0
    } else if (args[j] == "REQUIRED") {
145
0
      doing = DoingNone;
146
0
      if (haveRequiredOrOptional && !this->Required) {
147
0
        this->SetError("cannot be both REQUIRED and OPTIONAL");
148
0
        return false;
149
0
      }
150
0
      this->Required = true;
151
0
      newStyle = true;
152
0
      haveRequiredOrOptional = true;
153
0
    } else if (args[j] == "OPTIONAL") {
154
0
      doing = DoingNone;
155
0
      if (haveRequiredOrOptional && this->Required) {
156
0
        this->SetError("cannot be both REQUIRED and OPTIONAL");
157
0
        return false;
158
0
      }
159
0
      newStyle = true;
160
0
      haveRequiredOrOptional = true;
161
0
    } else if (args[j] == "REGISTRY_VIEW") {
162
0
      if (++j == args.size()) {
163
0
        this->SetError("missing required argument for REGISTRY_VIEW");
164
0
        return false;
165
0
      }
166
0
      auto view = cmWindowsRegistry::ToView(args[j]);
167
0
      if (view) {
168
0
        this->RegistryView = *view;
169
0
      } else {
170
0
        this->SetError(
171
0
          cmStrCat("given invalid value for REGISTRY_VIEW: ", args[j]));
172
0
        return false;
173
0
      }
174
0
    } else if (args[j] == "VALIDATOR") {
175
0
      if (++j == args.size()) {
176
0
        this->SetError("missing required argument for VALIDATOR");
177
0
        return false;
178
0
      }
179
0
      auto command = this->Makefile->GetState()->GetCommand(args[j]);
180
0
      if (!command) {
181
0
        this->SetError(cmStrCat(
182
0
          "command specified for VALIDATOR is undefined: ", args[j], '.'));
183
0
        return false;
184
0
      }
185
      // ensure a macro is not specified as validator
186
0
      auto const& validatorName = args[j];
187
0
      if (this->Makefile->GetState()
188
0
            ->GetCommandType(validatorName)
189
0
            .value_or(cmStateEnums::CommandType::Macro) !=
190
0
          cmStateEnums::CommandType::Function) {
191
0
        this->SetError(cmStrCat(
192
0
          "command specified for VALIDATOR is not a function: ", args[j],
193
0
          '.'));
194
0
        return false;
195
0
      }
196
0
      this->ValidatorName = validatorName;
197
0
    } else if (this->CheckCommonArgument(args[j])) {
198
0
      doing = DoingNone;
199
0
    } else {
200
      // Some common arguments were accidentally supported by CMake
201
      // 2.4 and 2.6.0 in the short-hand form of the command, so we
202
      // must support it even though it is not documented.
203
0
      if (doing == DoingNames) {
204
0
        this->Names.push_back(args[j]);
205
0
      } else if (doing == DoingPaths) {
206
0
        this->UserGuessArgs.push_back(args[j]);
207
0
      } else if (doing == DoingHints) {
208
0
        this->UserHintsArgs.push_back(args[j]);
209
0
      } else if (doing == DoingPathSuffixes) {
210
0
        this->AddPathSuffix(args[j]);
211
0
      }
212
0
    }
213
0
  }
214
215
0
  if (!haveRequiredOrOptional) {
216
0
    this->Required = this->Makefile->IsOn("CMAKE_FIND_REQUIRED");
217
0
  }
218
219
0
  if (this->VariableDocumentation.empty()) {
220
0
    this->VariableDocumentation = "Where can ";
221
0
    if (this->Names.empty()) {
222
0
      this->VariableDocumentation += "the (unknown) library be found";
223
0
    } else if (this->Names.size() == 1) {
224
0
      this->VariableDocumentation +=
225
0
        cmStrCat("the ", this->Names.front(), " library be found");
226
0
    } else {
227
0
      this->VariableDocumentation += cmStrCat(
228
0
        "one of the ", cmJoin(cmMakeRange(this->Names).retreat(1), ", "),
229
0
        " or ", this->Names.back(), " libraries be found");
230
0
    }
231
0
  }
232
233
  // look for old style
234
  // FIND_*(VAR name path1 path2 ...)
235
0
  if (!newStyle && !this->Names.empty()) {
236
    // All the short-hand arguments have been recorded as names.
237
0
    std::vector<std::string> shortArgs = this->Names;
238
0
    this->Names.clear(); // clear out any values in Names
239
0
    this->Names.push_back(shortArgs[0]);
240
0
    cm::append(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
241
0
  }
242
0
  this->ExpandPaths();
243
244
0
  this->ComputeFinalPaths(IgnorePaths::Yes);
245
246
0
  return true;
247
0
}
248
249
bool cmFindBase::Validate(std::string const& path) const
250
0
{
251
0
  if (this->ValidatorName.empty()) {
252
0
    return true;
253
0
  }
254
255
  // The validator command will be executed in an isolated scope.
256
0
  cmMakefile::ScopePushPop varScope(this->Makefile);
257
0
  cmMakefile::PolicyPushPop polScope(this->Makefile);
258
0
  static_cast<void>(varScope);
259
0
  static_cast<void>(polScope);
260
261
0
  auto resultName =
262
0
    cmStrCat("CMAKE_"_s, cmSystemTools::UpperCase(this->FindCommandName),
263
0
             "_VALIDATOR_STATUS"_s);
264
265
0
  this->Makefile->AddDefinitionBool(resultName, true);
266
267
0
  cmListFileFunction validator(
268
0
    this->ValidatorName, 0, 0,
269
0
    { cmListFileArgument(resultName, cmListFileArgument::Unquoted, 0),
270
0
      cmListFileArgument(path, cmListFileArgument::Quoted, 0) });
271
0
  cmExecutionStatus status(*this->Makefile);
272
273
0
  if (this->Makefile->ExecuteCommand(validator, status)) {
274
0
    return this->Makefile->GetDefinition(resultName).IsOn();
275
0
  }
276
0
  return false;
277
0
}
278
279
void cmFindBase::ExpandPaths()
280
0
{
281
0
  if (!this->NoDefaultPath) {
282
0
    if (!this->NoPackageRootPath) {
283
0
      this->FillPackageRootPath();
284
0
    }
285
0
    if (!this->NoCMakePath) {
286
0
      this->FillCMakeVariablePath();
287
0
    }
288
0
    if (!this->NoCMakeEnvironmentPath) {
289
0
      this->FillCMakeEnvironmentPath();
290
0
    }
291
0
  }
292
0
  this->FillUserHintsPath();
293
0
  if (!this->NoDefaultPath) {
294
0
    if (!this->NoSystemEnvironmentPath) {
295
0
      this->FillSystemEnvironmentPath();
296
0
    }
297
0
    if (!this->NoCMakeSystemPath) {
298
0
      this->FillCMakeSystemVariablePath();
299
0
    }
300
0
  }
301
0
  this->FillUserGuessPath();
302
0
}
303
304
void cmFindBase::FillCMakeEnvironmentPath()
305
0
{
306
0
  cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
307
308
  // Add CMAKE_*_PATH environment variables
309
0
  std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
310
0
  paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
311
0
  paths.AddEnvPath(var);
312
313
0
  if (this->CMakePathName == "PROGRAM") {
314
0
    paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
315
0
  } else {
316
0
    paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
317
0
  }
318
0
  paths.AddSuffixes(this->SearchPathSuffixes);
319
0
}
320
321
void cmFindBase::FillPackageRootPath()
322
0
{
323
0
  cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
324
325
  // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
326
0
  for (std::vector<std::string> const& pkgPaths :
327
0
       cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
328
0
    paths.AddPrefixPaths(pkgPaths);
329
0
  }
330
331
0
  paths.AddSuffixes(this->SearchPathSuffixes);
332
0
}
333
334
void cmFindBase::FillCMakeVariablePath()
335
0
{
336
0
  cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
337
338
  // Add CMake variables of the same name as the previous environment
339
  // variables CMAKE_*_PATH to be used most of the time with -D
340
  // command line options
341
0
  std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
342
0
  paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
343
0
  paths.AddCMakePath(var);
344
345
0
  if (this->CMakePathName == "PROGRAM") {
346
0
    paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
347
0
  } else {
348
0
    paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
349
0
  }
350
0
  paths.AddSuffixes(this->SearchPathSuffixes);
351
0
}
352
353
void cmFindBase::FillSystemEnvironmentPath()
354
0
{
355
0
  cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
356
357
  // Add LIB or INCLUDE
358
0
  if (!this->EnvironmentPath.empty()) {
359
0
    paths.AddEnvPath(this->EnvironmentPath);
360
0
  }
361
  // Add PATH
362
0
  paths.AddEnvPath("PATH");
363
0
  paths.AddSuffixes(this->SearchPathSuffixes);
364
0
}
365
366
namespace {
367
struct entry_to_remove
368
{
369
  entry_to_remove(std::string const& name, cmMakefile* makefile)
370
0
  {
371
0
    if (cmValue to_skip = makefile->GetDefinition(
372
0
          cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_COUNT"))) {
373
0
      cmStrToLong(*to_skip, &count);
374
0
    }
375
0
    if (cmValue prefix_value = makefile->GetDefinition(
376
0
          cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_VALUE"))) {
377
0
      value = *prefix_value;
378
0
    }
379
0
  }
380
0
  bool valid() const { return count > 0 && !value.empty(); }
381
382
  void remove_self(std::vector<std::string>& entries) const
383
0
  {
384
0
    if (this->valid()) {
385
0
      long to_skip = this->count;
386
0
      size_t index_to_remove = 0;
387
0
      for (auto const& path : entries) {
388
0
        if (path == this->value && --to_skip == 0) {
389
0
          break;
390
0
        }
391
0
        ++index_to_remove;
392
0
      }
393
0
      if (index_to_remove < entries.size() && to_skip == 0) {
394
0
        entries.erase(entries.begin() + index_to_remove);
395
0
      }
396
0
    }
397
0
  }
398
399
  long count = -1;
400
  std::string value;
401
};
402
}
403
404
void cmFindBase::FillCMakeSystemVariablePath()
405
0
{
406
0
  cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
407
408
0
  bool const install_prefix_in_list =
409
0
    !this->Makefile->IsOn("CMAKE_FIND_NO_INSTALL_PREFIX");
410
0
  bool const remove_install_prefix = this->NoCMakeInstallPath;
411
0
  bool const add_install_prefix = !this->NoCMakeInstallPath &&
412
0
    this->Makefile->IsDefinitionSet("CMAKE_FIND_USE_INSTALL_PREFIX");
413
414
  // We have 3 possible states for `CMAKE_SYSTEM_PREFIX_PATH` and
415
  // `CMAKE_INSTALL_PREFIX`.
416
  // Either we need to remove `CMAKE_INSTALL_PREFIX`, add
417
  // `CMAKE_INSTALL_PREFIX`, or do nothing.
418
  //
419
  // When we need to remove `CMAKE_INSTALL_PREFIX` we remove the Nth occurrence
420
  // of `CMAKE_INSTALL_PREFIX` from `CMAKE_SYSTEM_PREFIX_PATH`, where `N` is
421
  // computed by `CMakeSystemSpecificInformation.cmake` while constructing
422
  // `CMAKE_SYSTEM_PREFIX_PATH`. This ensures that if projects / toolchains
423
  // have removed `CMAKE_INSTALL_PREFIX` from the list, we don't remove
424
  // some other entry by mistake ( likewise for `CMAKE_STAGING_PREFIX` )
425
0
  entry_to_remove install_entry("INSTALL", this->Makefile);
426
0
  entry_to_remove staging_entry("STAGING", this->Makefile);
427
428
0
  if (remove_install_prefix && install_prefix_in_list &&
429
0
      (install_entry.valid() || staging_entry.valid())) {
430
0
    cmValue prefix_paths =
431
0
      this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH");
432
433
    // remove entries from CMAKE_SYSTEM_PREFIX_PATH
434
0
    cmList expanded{ *prefix_paths };
435
0
    install_entry.remove_self(expanded);
436
0
    staging_entry.remove_self(expanded);
437
0
    for (std::string& p : expanded) {
438
0
      p = cmSystemTools::CollapseFullPath(
439
0
        p, this->Makefile->GetCurrentSourceDirectory());
440
0
    }
441
0
    paths.AddPrefixPaths(expanded);
442
0
  } else if (add_install_prefix && !install_prefix_in_list) {
443
0
    paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX");
444
0
    paths.AddCMakePrefixPath("CMAKE_STAGING_PREFIX");
445
0
    paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
446
0
  } else {
447
    // Otherwise the current setup of `CMAKE_SYSTEM_PREFIX_PATH` is correct
448
0
    paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
449
0
  }
450
451
0
  std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
452
0
  paths.AddCMakePath(var);
453
454
0
  if (this->CMakePathName == "PROGRAM") {
455
0
    paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
456
0
  } else {
457
0
    paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
458
0
  }
459
0
  paths.AddSuffixes(this->SearchPathSuffixes);
460
0
}
461
462
void cmFindBase::FillUserHintsPath()
463
0
{
464
0
  cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
465
466
0
  for (std::string const& p : this->UserHintsArgs) {
467
0
    paths.AddUserPath(p);
468
0
  }
469
0
  paths.AddSuffixes(this->SearchPathSuffixes);
470
0
}
471
472
void cmFindBase::FillUserGuessPath()
473
0
{
474
0
  cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
475
476
0
  for (std::string const& p : this->UserGuessArgs) {
477
0
    paths.AddUserPath(p);
478
0
  }
479
0
  paths.AddSuffixes(this->SearchPathSuffixes);
480
0
}
481
482
cmFindBase::FindState cmFindBase::GetInitialState()
483
0
{
484
0
  if (cmValue value = this->Makefile->GetDefinition(this->VariableName)) {
485
0
    cmState* state = this->Makefile->GetState();
486
0
    cmValue cacheEntry = state->GetCacheEntryValue(this->VariableName);
487
0
    bool found = !cmIsNOTFOUND(*value);
488
0
    bool cached = cacheEntry != nullptr;
489
0
    auto cacheType = cached ? state->GetCacheEntryType(this->VariableName)
490
0
                            : cmStateEnums::UNINITIALIZED;
491
492
0
    if (cached && cacheType != cmStateEnums::UNINITIALIZED) {
493
0
      this->VariableType = cacheType;
494
0
      if (auto const& hs =
495
0
            state->GetCacheEntryProperty(this->VariableName, "HELPSTRING")) {
496
0
        this->VariableDocumentation = *hs;
497
0
      }
498
0
    }
499
500
0
    if (found) {
501
      // If the user specifies the entry on the command line without a
502
      // type we should add the type and docstring but keep the
503
      // original value.  Tell the subclass implementations to do
504
      // this.
505
0
      if (cached && cacheType == cmStateEnums::UNINITIALIZED) {
506
0
        this->AlreadyInCacheWithoutMetaInfo = true;
507
0
      }
508
0
      return FindState::Found;
509
0
    }
510
0
    return FindState::NotFound;
511
0
  }
512
0
  return FindState::Undefined;
513
0
}
514
515
bool cmFindBase::IsFound() const
516
0
{
517
0
  return this->InitialState == FindState::Found;
518
0
}
519
520
bool cmFindBase::IsDefined() const
521
0
{
522
0
  return this->InitialState != FindState::Undefined;
523
0
}
524
525
void cmFindBase::NormalizeFindResult()
526
0
{
527
0
  std::string foundValue;
528
0
  if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) ==
529
0
      cmPolicies::NEW) {
530
    // ensure the path returned by find_* command is absolute
531
0
    auto const& existingValue =
532
0
      this->Makefile->GetDefinition(this->VariableName);
533
0
    std::string value;
534
0
    if (!existingValue->empty()) {
535
0
      value =
536
0
        cmCMakePath(*existingValue, cmCMakePath::auto_format)
537
0
          .Absolute(cmCMakePath(
538
0
            this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory()))
539
0
          .Normal()
540
0
          .GenericString();
541
0
      if (!cmSystemTools::FileExists(value, false)) {
542
0
        value = *existingValue;
543
0
      }
544
0
    }
545
546
0
    foundValue = value;
547
0
    if (this->StoreResultInCache) {
548
      // If the user specifies the entry on the command line without a
549
      // type we should add the type and docstring but keep the original
550
      // value.
551
0
      if (value != *existingValue || this->AlreadyInCacheWithoutMetaInfo) {
552
0
        this->Makefile->GetCMakeInstance()->AddCacheEntry(
553
0
          this->VariableName, value, this->VariableDocumentation,
554
0
          this->VariableType);
555
0
        if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
556
0
            cmPolicies::NEW) {
557
0
          if (this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
558
0
            this->Makefile->AddDefinition(this->VariableName, value);
559
0
          }
560
0
        } else {
561
          // if there was a definition then remove it
562
          // This is required to ensure same behavior as
563
          // cmMakefile::AddCacheDefinition.
564
0
          this->Makefile->RemoveDefinition(this->VariableName);
565
0
        }
566
0
      }
567
0
    } else {
568
      // ensure a normal variable is defined.
569
0
      this->Makefile->AddDefinition(this->VariableName, value);
570
0
    }
571
0
  } else {
572
    // If the user specifies the entry on the command line without a
573
    // type we should add the type and docstring but keep the original
574
    // value.
575
0
    if (this->StoreResultInCache) {
576
0
      auto const& existingValue =
577
0
        this->Makefile->GetCMakeInstance()->GetCacheDefinition(
578
0
          this->VariableName);
579
0
      foundValue = *existingValue;
580
0
      if (this->AlreadyInCacheWithoutMetaInfo) {
581
0
        this->Makefile->AddCacheDefinition(this->VariableName, "",
582
0
                                           this->VariableDocumentation,
583
0
                                           this->VariableType);
584
0
        if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
585
0
              cmPolicies::NEW &&
586
0
            this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
587
0
          this->Makefile->AddDefinition(this->VariableName, *existingValue);
588
0
        }
589
0
      }
590
0
    } else {
591
0
      auto const& existingValue =
592
0
        this->Makefile->GetSafeDefinition(this->VariableName);
593
0
      foundValue = existingValue;
594
      // ensure a normal variable is defined.
595
0
      this->Makefile->AddDefinition(this->VariableName, existingValue);
596
0
    }
597
0
  }
598
0
  if (this->DebugState) {
599
0
    this->DebugState->FoundAt(foundValue);
600
0
  }
601
0
}
602
603
void cmFindBase::StoreFindResult(std::string const& value)
604
0
{
605
0
  bool force =
606
0
    this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) == cmPolicies::NEW;
607
0
  bool updateNormalVariable =
608
0
    this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) == cmPolicies::NEW;
609
610
0
  if (!value.empty()) {
611
0
    if (this->StoreResultInCache) {
612
0
      this->Makefile->AddCacheDefinition(this->VariableName, value,
613
0
                                         this->VariableDocumentation,
614
0
                                         this->VariableType, force);
615
0
      if (updateNormalVariable &&
616
0
          this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
617
0
        this->Makefile->AddDefinition(this->VariableName, value);
618
0
      }
619
0
    } else {
620
0
      this->Makefile->AddDefinition(this->VariableName, value);
621
0
    }
622
623
0
    if (this->DebugState) {
624
0
      this->DebugState->FoundAt(value);
625
0
    }
626
627
0
    return;
628
0
  }
629
630
0
  auto notFound = cmStrCat(this->VariableName, "-NOTFOUND");
631
0
  if (this->StoreResultInCache) {
632
0
    this->Makefile->AddCacheDefinition(this->VariableName, notFound,
633
0
                                       this->VariableDocumentation,
634
0
                                       this->VariableType, force);
635
0
    if (updateNormalVariable &&
636
0
        this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
637
0
      this->Makefile->AddDefinition(this->VariableName, notFound);
638
0
    }
639
0
  } else {
640
0
    this->Makefile->AddDefinition(this->VariableName, notFound);
641
0
  }
642
643
0
  if (this->Required) {
644
0
    this->Makefile->IssueMessage(
645
0
      MessageType::FATAL_ERROR,
646
0
      cmStrCat("Could not find ", this->VariableName, " using the following ",
647
0
               (this->FindCommandName == "find_file" ||
648
0
                    this->FindCommandName == "find_path"
649
0
                  ? "files"
650
0
                  : "names"),
651
0
               ": ", cmJoin(this->Names, ", ")));
652
0
    cmSystemTools::SetFatalErrorOccurred();
653
0
  }
654
0
}
655
656
cmFindBaseDebugState::cmFindBaseDebugState(cmFindBase const* findBase)
657
0
  : cmFindCommonDebugState(findBase->FindCommandName, findBase)
658
0
  , FindBaseCommand(findBase)
659
0
{
660
0
}
661
662
0
cmFindBaseDebugState::~cmFindBaseDebugState() = default;
663
664
void cmFindBaseDebugState::FoundAtImpl(std::string const& path,
665
                                       std::string regexName)
666
0
{
667
0
  this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
668
0
}
669
670
void cmFindBaseDebugState::FailedAtImpl(std::string const& path,
671
                                        std::string regexName)
672
0
{
673
0
  this->FailedSearchLocations.emplace_back(std::move(regexName), path);
674
0
}
675
676
void cmFindBaseDebugState::WriteDebug() const
677
0
{
678
  // clang-format off
679
0
  auto buffer =
680
0
    cmStrCat(
681
0
      this->CommandName, " called with the following settings:"
682
0
      "\n  VAR: ", this->FindBaseCommand->VariableName,
683
0
      "\n  NAMES: ", cmWrap('"', this->FindBaseCommand->Names, '"', "\n         "),
684
0
      "\n  Documentation: ", this->FindBaseCommand->VariableDocumentation,
685
0
      "\n  Framework"
686
0
      "\n    Only Search Frameworks: ", this->FindBaseCommand->SearchFrameworkOnly,
687
0
      "\n    Search Frameworks Last: ", this->FindBaseCommand->SearchFrameworkLast,
688
0
      "\n    Search Frameworks First: ", this->FindBaseCommand->SearchFrameworkFirst,
689
0
      "\n  AppBundle"
690
0
      "\n    Only Search AppBundle: ", this->FindBaseCommand->SearchAppBundleOnly,
691
0
      "\n    Search AppBundle Last: ", this->FindBaseCommand->SearchAppBundleLast,
692
0
      "\n    Search AppBundle First: ", this->FindBaseCommand->SearchAppBundleFirst,
693
0
      '\n'
694
0
    );
695
  // clang-format on
696
697
0
  if (this->FindCommand->NoDefaultPath) {
698
0
    buffer += "  NO_DEFAULT_PATH Enabled\n";
699
0
  } else {
700
    // clang-format off
701
0
    buffer += cmStrCat(
702
0
      "  CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath,
703
0
      "\n  CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ", !this->FindCommand->NoCMakeEnvironmentPath,
704
0
      "\n  CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ", !this->FindCommand->NoSystemEnvironmentPath,
705
0
      "\n  CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ", !this->FindCommand->NoCMakeSystemPath,
706
0
      "\n  CMAKE_FIND_USE_INSTALL_PREFIX: ", !this->FindCommand->NoCMakeInstallPath,
707
0
      '\n'
708
0
     );
709
    // clang-format on
710
0
  }
711
712
0
  buffer +=
713
0
    cmStrCat(this->CommandName, " considered the following locations:\n");
714
0
  for (auto const& state : this->FailedSearchLocations) {
715
0
    std::string path = cmStrCat("  ", state.path);
716
0
    if (!state.regexName.empty()) {
717
0
      path = cmStrCat(path, '/', state.regexName);
718
0
    }
719
0
    buffer += cmStrCat(path, '\n');
720
0
  }
721
722
0
  if (this->HasBeenFound()) {
723
0
    buffer += cmStrCat("The item was found at\n  ",
724
0
                       this->FoundSearchLocation.path, '\n');
725
0
  } else {
726
0
    buffer += "The item was not found.\n";
727
0
  }
728
729
0
  this->FindCommand->DebugMessage(buffer);
730
0
}
731
732
#ifndef CMAKE_BOOTSTRAP
733
void cmFindBaseDebugState::WriteEvent(cmConfigureLog& log,
734
                                      cmMakefile const& mf) const
735
0
{
736
0
  log.BeginEvent("find-v1", mf);
737
738
  // Mode is the Command name without the "find_" prefix
739
0
  log.WriteValue("mode"_s, this->CommandName.substr(5));
740
0
  log.WriteValue("variable"_s, this->FindBaseCommand->VariableName);
741
0
  log.WriteValue("description"_s,
742
0
                 this->FindBaseCommand->VariableDocumentation);
743
744
  // Yes, this needs to return a `std::string`. If it returns a `const char*`,
745
  // the `WriteValue` method prefers the `bool` overload. There's no overload
746
  // for a `cm::string_view` because the underlying JSON library doesn't
747
  // support `string_view` arguments itself.
748
0
  auto search_opt_to_str = [](bool first, bool last,
749
0
                              bool only) -> std::string {
750
0
    return first ? "FIRST" : (last ? "LAST" : (only ? "ONLY" : "NEVER"));
751
0
  };
752
0
  log.BeginObject("settings"_s);
753
0
  log.WriteValue("SearchFramework"_s,
754
0
                 search_opt_to_str(this->FindCommand->SearchFrameworkFirst,
755
0
                                   this->FindCommand->SearchFrameworkLast,
756
0
                                   this->FindCommand->SearchFrameworkOnly));
757
0
  log.WriteValue("SearchAppBundle"_s,
758
0
                 search_opt_to_str(this->FindCommand->SearchAppBundleFirst,
759
0
                                   this->FindCommand->SearchAppBundleLast,
760
0
                                   this->FindCommand->SearchAppBundleOnly));
761
0
  log.WriteValue("CMAKE_FIND_USE_CMAKE_PATH"_s,
762
0
                 !this->FindCommand->NoCMakePath);
763
0
  log.WriteValue("CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH"_s,
764
0
                 !this->FindCommand->NoCMakeEnvironmentPath);
765
0
  log.WriteValue("CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH"_s,
766
0
                 !this->FindCommand->NoSystemEnvironmentPath);
767
0
  log.WriteValue("CMAKE_FIND_USE_CMAKE_SYSTEM_PATH"_s,
768
0
                 !this->FindCommand->NoCMakeSystemPath);
769
0
  log.WriteValue("CMAKE_FIND_USE_INSTALL_PREFIX"_s,
770
0
                 !this->FindCommand->NoCMakeInstallPath);
771
0
  log.EndObject();
772
773
0
  if (!this->FindBaseCommand->Names.empty()) {
774
0
    log.WriteValue("names"_s, this->FindBaseCommand->Names);
775
0
  }
776
0
  std::vector<std::string> directories;
777
0
  directories.reserve(this->FailedSearchLocations.size());
778
0
  for (auto const& location : this->FailedSearchLocations) {
779
0
    directories.push_back(location.path);
780
0
  }
781
0
  if (!this->FindCommand->SearchPaths.empty()) {
782
0
    log.WriteValue("candidate_directories"_s, this->FindCommand->SearchPaths);
783
0
  }
784
0
  if (!directories.empty()) {
785
0
    log.WriteValue("searched_directories"_s, directories);
786
0
  }
787
0
  if (!this->FoundSearchLocation.path.empty()) {
788
0
    log.WriteValue("found"_s, this->FoundSearchLocation.path);
789
0
  } else {
790
0
    log.WriteValue("found"_s, false);
791
0
  }
792
793
0
  this->WriteSearchVariables(log, mf);
794
795
0
  log.EndEvent();
796
0
}
797
798
std::vector<std::pair<cmFindCommonDebugState::VariableSource, std::string>>
799
cmFindBaseDebugState::ExtraSearchVariables() const
800
0
{
801
0
  std::vector<std::pair<cmFindCommonDebugState::VariableSource, std::string>>
802
0
    extraSearches;
803
0
  if (!this->FindBaseCommand->EnvironmentPath.empty()) {
804
0
    extraSearches.emplace_back(VariableSource::EnvironmentList,
805
0
                               this->FindBaseCommand->EnvironmentPath);
806
0
  }
807
0
  return extraSearches;
808
0
}
809
#endif