Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmVariableWatchCommand.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 "cmVariableWatchCommand.h"
4
5
#include <limits>
6
#include <memory>
7
#include <utility>
8
9
#include "cmExecutionStatus.h"
10
#include "cmListFileCache.h"
11
#include "cmMakefile.h"
12
#include "cmMessageType.h"
13
#include "cmStringAlgorithms.h"
14
#include "cmSystemTools.h"
15
#include "cmValue.h"
16
#include "cmVariableWatch.h"
17
#include "cmake.h"
18
19
class cmLocalGenerator;
20
21
namespace {
22
struct cmVariableWatchCallbackData
23
{
24
  bool InCallback;
25
  std::string Command;
26
};
27
28
void cmVariableWatchCommandVariableAccessed(std::string const& variable,
29
                                            int access_type, void* client_data,
30
                                            char const* newValue,
31
                                            cmMakefile const* mf)
32
0
{
33
0
  cmVariableWatchCallbackData* data =
34
0
    static_cast<cmVariableWatchCallbackData*>(client_data);
35
36
0
  if (data->InCallback) {
37
0
    return;
38
0
  }
39
0
  data->InCallback = true;
40
41
0
  auto accessString = cmVariableWatch::GetAccessAsString(access_type);
42
43
  /// Ultra bad!!
44
0
  cmMakefile* makefile = const_cast<cmMakefile*>(mf);
45
46
0
  std::string stack = *mf->GetProperty("LISTFILE_STACK");
47
0
  if (!data->Command.empty()) {
48
0
    cmValue const currentListFile =
49
0
      mf->GetDefinition("CMAKE_CURRENT_LIST_FILE");
50
0
    auto const fakeLineNo =
51
0
      std::numeric_limits<decltype(cmListFileArgument::Line)>::max();
52
53
0
    std::vector<cmListFileArgument> newLFFArgs{
54
0
      { variable, cmListFileArgument::Quoted, fakeLineNo },
55
0
      { accessString, cmListFileArgument::Quoted, fakeLineNo },
56
0
      { newValue ? newValue : "", cmListFileArgument::Quoted, fakeLineNo },
57
0
      { *currentListFile, cmListFileArgument::Quoted, fakeLineNo },
58
0
      { stack, cmListFileArgument::Quoted, fakeLineNo }
59
0
    };
60
61
0
    cmListFileFunction newLFF{ data->Command, fakeLineNo, fakeLineNo,
62
0
                               std::move(newLFFArgs) };
63
0
    cmExecutionStatus status(*makefile);
64
0
    if (!makefile->ExecuteCommand(newLFF, status)) {
65
0
      cmSystemTools::Error(
66
0
        cmStrCat("Error in cmake code at\nUnknown:0:\nA command failed "
67
0
                 "during the invocation of callback \"",
68
0
                 data->Command, "\"."));
69
0
    }
70
0
  } else {
71
0
    makefile->IssueMessage(
72
0
      MessageType::LOG,
73
0
      cmStrCat("Variable \"", variable, "\" was accessed using ", accessString,
74
0
               " with value \"", (newValue ? newValue : ""), "\"."));
75
0
  }
76
77
0
  data->InCallback = false;
78
0
}
79
80
void deleteVariableWatchCallbackData(void* client_data)
81
0
{
82
0
  cmVariableWatchCallbackData* data =
83
0
    static_cast<cmVariableWatchCallbackData*>(client_data);
84
0
  delete data;
85
0
}
86
87
/** This command does not really have a final pass but it needs to
88
    stay alive since it owns variable watch callback information. */
89
class FinalAction
90
{
91
public:
92
  /* NOLINTNEXTLINE(performance-unnecessary-value-param) */
93
  FinalAction(cmMakefile* makefile, std::string variable)
94
0
    : Action{ std::make_shared<Impl>(makefile, std::move(variable)) }
95
0
  {
96
0
  }
97
98
0
  void operator()(cmLocalGenerator&, cmListFileBacktrace const&) const {}
99
100
private:
101
  struct Impl
102
  {
103
    Impl(cmMakefile* makefile, std::string variable)
104
0
      : Makefile{ makefile }
105
0
      , Variable{ std::move(variable) }
106
0
    {
107
0
    }
108
109
    ~Impl()
110
0
    {
111
0
      this->Makefile->GetCMakeInstance()->GetVariableWatch()->RemoveWatch(
112
0
        this->Variable, cmVariableWatchCommandVariableAccessed);
113
0
    }
114
115
    cmMakefile* const Makefile;
116
    std::string const Variable;
117
  };
118
119
  std::shared_ptr<Impl const> Action;
120
};
121
} // anonymous namespace
122
123
bool cmVariableWatchCommand(std::vector<std::string> const& args,
124
                            cmExecutionStatus& status)
125
0
{
126
0
  if (args.empty()) {
127
0
    status.SetError("must be called with at least one argument.");
128
0
    return false;
129
0
  }
130
0
  std::string const& variable = args[0];
131
0
  std::string command;
132
0
  if (args.size() > 1) {
133
0
    command = args[1];
134
0
  }
135
0
  if (variable == "CMAKE_CURRENT_LIST_FILE") {
136
0
    status.SetError(cmStrCat("cannot be set on the variable: ", variable));
137
0
    return false;
138
0
  }
139
140
0
  auto* const data = new cmVariableWatchCallbackData;
141
142
0
  data->InCallback = false;
143
0
  data->Command = std::move(command);
144
145
0
  if (!status.GetMakefile().GetCMakeInstance()->GetVariableWatch()->AddWatch(
146
0
        variable, cmVariableWatchCommandVariableAccessed, data,
147
0
        deleteVariableWatchCallbackData)) {
148
0
    deleteVariableWatchCallbackData(data);
149
0
    return false;
150
0
  }
151
152
0
  status.GetMakefile().AddGeneratorAction(
153
0
    FinalAction{ &status.GetMakefile(), variable });
154
0
  return true;
155
0
}