Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.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 "cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.h"
5
6
#include <sstream>
7
#include <vector>
8
9
#include <cmsys/RegularExpression.hxx>
10
11
#include "cmRuntimeDependencyArchive.h"
12
#include "cmUVProcessChain.h"
13
#include "cmUVStream.h"
14
15
cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool::
16
  cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool(
17
    cmRuntimeDependencyArchive* archive)
18
0
  : cmBinUtilsMacOSMachOGetRuntimeDependenciesTool(archive)
19
0
{
20
0
}
21
22
bool cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool::GetFileInfo(
23
  std::string const& file, std::vector<std::string>& libs,
24
  std::vector<std::string>& rpaths)
25
0
{
26
0
  std::vector<std::string> command;
27
0
  if (!this->Archive->GetGetRuntimeDependenciesCommand("otool", command)) {
28
0
    this->SetError("Could not find otool");
29
0
    return false;
30
0
  }
31
0
  command.emplace_back("-l");
32
0
  command.emplace_back(file);
33
34
0
  cmUVProcessChainBuilder builder;
35
0
  builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
36
0
    .AddCommand(command);
37
38
0
  auto process = builder.Start();
39
0
  if (!process.Valid() || process.GetStatus(0).SpawnResult != 0) {
40
0
    std::ostringstream e;
41
0
    e << "Failed to start otool process for:\n  " << file;
42
0
    this->SetError(e.str());
43
0
    return false;
44
0
  }
45
46
0
  std::string line;
47
0
  static cmsys::RegularExpression const rpathRegex("^ *cmd LC_RPATH$");
48
0
  static cmsys::RegularExpression const loadDylibRegex(
49
0
    "^ *cmd LC_LOAD(_WEAK)?_DYLIB$");
50
0
  static cmsys::RegularExpression const pathRegex(
51
0
    "^ *path (.*) \\(offset [0-9]+\\)$");
52
0
  static cmsys::RegularExpression const nameRegex(
53
0
    "^ *name (.*) \\(offset [0-9]+\\)$");
54
0
  cmUVIStream output(process.OutputStream());
55
0
  while (std::getline(output, line)) {
56
0
    cmsys::RegularExpressionMatch cmdMatch;
57
0
    if (rpathRegex.find(line.c_str(), cmdMatch)) {
58
      // NOLINTNEXTLINE(misc-redundant-expression)
59
0
      if (!std::getline(output, line) || !std::getline(output, line)) {
60
0
        this->SetError("Invalid output from otool");
61
0
        return false;
62
0
      }
63
64
0
      cmsys::RegularExpressionMatch pathMatch;
65
0
      if (pathRegex.find(line.c_str(), pathMatch)) {
66
0
        rpaths.push_back(pathMatch.match(1));
67
0
      } else {
68
0
        this->SetError("Invalid output from otool");
69
0
        return false;
70
0
      }
71
0
    } else if (loadDylibRegex.find(line.c_str(), cmdMatch)) {
72
      // NOLINTNEXTLINE(misc-redundant-expression)
73
0
      if (!std::getline(output, line) || !std::getline(output, line)) {
74
0
        this->SetError("Invalid output from otool");
75
0
        return false;
76
0
      }
77
78
0
      cmsys::RegularExpressionMatch nameMatch;
79
0
      if (nameRegex.find(line.c_str(), nameMatch)) {
80
0
        libs.push_back(nameMatch.match(1));
81
0
      } else {
82
0
        this->SetError("Invalid output from otool");
83
0
        return false;
84
0
      }
85
0
    }
86
0
  }
87
88
0
  if (!process.Wait()) {
89
0
    std::ostringstream e;
90
0
    e << "Failed to wait on otool process for:\n  " << file;
91
0
    this->SetError(e.str());
92
0
    return false;
93
0
  }
94
0
  if (process.GetStatus(0).ExitStatus != 0) {
95
0
    std::ostringstream e;
96
0
    e << "Failed to run otool on:\n  " << file;
97
0
    this->SetError(e.str());
98
0
    return false;
99
0
  }
100
101
0
  return true;
102
0
}