Coverage Report

Created: 2026-07-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmLDConfigLDConfigTool.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
4
#include "cmLDConfigLDConfigTool.h"
5
6
#include <istream>
7
#include <string>
8
#include <vector>
9
10
#include "cmsys/RegularExpression.hxx"
11
12
#include "cmList.h"
13
#include "cmMakefile.h"
14
#include "cmRuntimeDependencyArchive.h"
15
#include "cmSystemTools.h"
16
#include "cmUVProcessChain.h"
17
#include "cmUVStream.h"
18
19
cmLDConfigLDConfigTool::cmLDConfigLDConfigTool(
20
  cmRuntimeDependencyArchive* archive)
21
0
  : cmLDConfigTool(archive)
22
0
{
23
0
}
24
25
bool cmLDConfigLDConfigTool::GetLDConfigPaths(std::vector<std::string>& paths)
26
0
{
27
0
  std::string ldConfigPath =
28
0
    this->Archive->GetMakefile()->GetSafeDefinition("CMAKE_LDCONFIG_COMMAND");
29
0
  if (ldConfigPath.empty()) {
30
0
    ldConfigPath = cmSystemTools::FindProgram(
31
0
      "ldconfig", { "/sbin", "/usr/sbin", "/usr/local/sbin" });
32
0
    if (ldConfigPath.empty()) {
33
0
      this->Archive->SetError("Could not find ldconfig");
34
0
      return false;
35
0
    }
36
0
  }
37
38
0
  cmList ldConfigCommand{ ldConfigPath };
39
#ifdef __FreeBSD__
40
  ldConfigCommand.emplace_back("-r"); // List directories
41
#else
42
0
  ldConfigCommand.emplace_back("-v");
43
0
  ldConfigCommand.emplace_back("-N"); // Don't rebuild the cache.
44
0
  ldConfigCommand.emplace_back("-X"); // Don't update links.
45
0
#endif
46
47
0
  cmUVProcessChainBuilder builder;
48
0
  builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
49
0
    .AddCommand(ldConfigCommand);
50
0
  auto process = builder.Start();
51
0
  if (!process.Valid() || process.GetStatus(0).SpawnResult != 0) {
52
0
    this->Archive->SetError("Failed to start ldconfig process");
53
0
    return false;
54
0
  }
55
56
0
  std::string line;
57
0
  cmUVIStream output(process.OutputStream());
58
#ifdef __FreeBSD__
59
  // FreeBSD ldconfig output:
60
  // /var/run/ld-elf.so.hints:
61
  //    search directories: /lib:/usr/lib:...
62
  // Extract everything after "search directories: " and split by ':'
63
  static cmsys::RegularExpression regex("search directories: (.*)$");
64
  while (std::getline(output, line)) {
65
    cmsys::RegularExpressionMatch match;
66
    if (!regex.find(line.c_str(), match))
67
      continue;
68
    auto allPaths = std::stringstream(match.match(1));
69
    std::string path;
70
    while (std::getline(allPaths, path, ':'))
71
      if (!path.empty())
72
        paths.push_back(path);
73
  }
74
#else
75
  // Linux ldconfig output:
76
  // /lib64: (from <builtin>:0)
77
  // Extract the "/lib64" part
78
0
  static cmsys::RegularExpression const regex("^([^\t:]*):");
79
0
  while (std::getline(output, line)) {
80
0
    cmsys::RegularExpressionMatch match;
81
0
    if (regex.find(line.c_str(), match)) {
82
0
      paths.push_back(match.match(1));
83
0
    }
84
0
  }
85
0
#endif
86
87
0
  if (!process.Wait()) {
88
0
    this->Archive->SetError("Failed to wait on ldconfig process");
89
0
    return false;
90
0
  }
91
0
  if (process.GetStatus(0).ExitStatus != 0) {
92
0
    this->Archive->SetError("Failed to run ldconfig");
93
0
    return false;
94
0
  }
95
96
0
  return true;
97
0
}