Coverage Report

Created: 2026-09-14 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmGlobVerificationManager.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 "cmGlobVerificationManager.h"
4
5
#include <sstream>
6
7
#include "cmsys/FStream.hxx"
8
9
#include "cmGeneratedFileStream.h"
10
#include "cmGlobCacheEntry.h"
11
#include "cmListFileCache.h"
12
#include "cmMessageType.h"
13
#include "cmMessenger.h"
14
#include "cmOutputConverter.h"
15
#include "cmStringAlgorithms.h"
16
#include "cmSystemTools.h"
17
#include "cmVersion.h"
18
19
bool cmGlobVerificationManager::SaveVerificationScript(std::string const& path,
20
                                                       cmMessenger* messenger)
21
0
{
22
0
  if (this->Cache.empty()) {
23
0
    return true;
24
0
  }
25
26
0
  std::string scriptFile = cmStrCat(path, "/CMakeFiles");
27
0
  std::string stampFile = scriptFile;
28
0
  cmSystemTools::MakeDirectory(scriptFile);
29
0
  scriptFile += "/VerifyGlobs.cmake";
30
0
  stampFile += "/cmake.verify_globs";
31
0
  cmGeneratedFileStream verifyScriptFile(scriptFile);
32
0
  verifyScriptFile.SetCopyIfDifferent(true);
33
0
  if (!verifyScriptFile) {
34
0
    cmSystemTools::Error("Unable to open verification script file for save. " +
35
0
                         scriptFile);
36
0
    cmSystemTools::ReportLastSystemError("");
37
0
    return false;
38
0
  }
39
40
0
  verifyScriptFile << std::boolalpha;
41
0
  verifyScriptFile << "# CMAKE generated file: DO NOT EDIT!\n"
42
0
                   << "# Generated by CMake Version "
43
0
                   << cmVersion::GetMajorVersion() << "."
44
0
                   << cmVersion::GetMinorVersion() << "\n";
45
46
0
  for (auto const& i : this->Cache) {
47
0
    CacheEntryKey k = std::get<0>(i);
48
0
    CacheEntryValue v = std::get<1>(i);
49
50
0
    if (!v.Initialized) {
51
0
      continue;
52
0
    }
53
54
0
    verifyScriptFile << "\n";
55
56
0
    for (auto const& bt : v.Backtraces) {
57
0
      verifyScriptFile << "# " << std::get<0>(bt);
58
0
      messenger->PrintBacktraceTitle(verifyScriptFile, std::get<1>(bt));
59
0
      verifyScriptFile << "\n";
60
0
    }
61
62
0
    k.PrintGlobCommand(verifyScriptFile, "NEW_GLOB");
63
0
    verifyScriptFile << "\n";
64
65
0
    verifyScriptFile << "set(OLD_GLOB\n";
66
0
    for (std::string const& file : v.Files) {
67
0
      verifyScriptFile << "  " << cmOutputConverter::EscapeForCMake(file)
68
0
                       << '\n';
69
0
    }
70
0
    verifyScriptFile << "  )\n";
71
72
0
    verifyScriptFile << "if(NOT \"${NEW_GLOB}\" STREQUAL \"${OLD_GLOB}\")\n"
73
0
                     << "  message(\"-- GLOB mismatch!\")\n"
74
0
                     << "  set(NEW_ONLY ${NEW_GLOB})\n"
75
0
                     << "  set(OLD_ONLY ${OLD_GLOB})\n"
76
0
                     << "  list(REMOVE_ITEM NEW_ONLY ${OLD_GLOB})\n"
77
0
                     << "  list(REMOVE_ITEM OLD_ONLY ${NEW_GLOB})\n"
78
0
                     << "  if(NEW_ONLY)\n"
79
0
                     << "    message(\"The following files were added:\")\n"
80
0
                     << "    foreach(VAR_FILE IN LISTS NEW_ONLY)\n"
81
0
                     << "      message(\"  +${VAR_FILE}\")\n"
82
0
                     << "    endforeach()\n"
83
0
                     << "  endif()\n"
84
0
                     << "  if(OLD_ONLY)\n"
85
0
                     << "    message(\"The following files were removed:\")\n"
86
0
                     << "    foreach(VAR_FILE IN LISTS OLD_ONLY)\n"
87
0
                     << "      message(\"  -${VAR_FILE}\")\n"
88
0
                     << "    endforeach()\n"
89
0
                     << "  endif()\n"
90
0
                     << "  file(TOUCH_NOCREATE \"" << stampFile << "\")\n"
91
0
                     << "endif()\n";
92
0
  }
93
0
  verifyScriptFile.Close();
94
95
0
  cmsys::ofstream verifyStampFile(stampFile.c_str());
96
0
  if (!verifyStampFile) {
97
0
    cmSystemTools::Error("Unable to open verification stamp file for write. " +
98
0
                         stampFile);
99
0
    return false;
100
0
  }
101
0
  verifyStampFile << "# This file is generated by CMake for checking of the "
102
0
                     "VerifyGlobs.cmake file\n";
103
0
  this->VerifyScript = scriptFile;
104
0
  this->VerifyStamp = stampFile;
105
0
  return true;
106
0
}
107
108
bool cmGlobVerificationManager::DoWriteVerifyTarget() const
109
0
{
110
0
  return !this->VerifyScript.empty() && !this->VerifyStamp.empty();
111
0
}
112
113
bool cmGlobVerificationManager::CacheEntryKey::operator<(
114
  CacheEntryKey const& r) const
115
0
{
116
0
  if (this->Recurse < r.Recurse) {
117
0
    return true;
118
0
  }
119
0
  if (this->Recurse > r.Recurse) {
120
0
    return false;
121
0
  }
122
0
  if (this->ListDirectories < r.ListDirectories) {
123
0
    return true;
124
0
  }
125
0
  if (this->ListDirectories > r.ListDirectories) {
126
0
    return false;
127
0
  }
128
0
  if (this->FollowSymlinks < r.FollowSymlinks) {
129
0
    return true;
130
0
  }
131
0
  if (this->FollowSymlinks > r.FollowSymlinks) {
132
0
    return false;
133
0
  }
134
0
  if (this->Relative < r.Relative) {
135
0
    return true;
136
0
  }
137
0
  if (this->Relative > r.Relative) {
138
0
    return false;
139
0
  }
140
0
  if (this->Expression < r.Expression) {
141
0
    return true;
142
0
  }
143
0
  if (this->Expression > r.Expression) {
144
0
    return false;
145
0
  }
146
0
  return false;
147
0
}
148
149
void cmGlobVerificationManager::CacheEntryKey::PrintGlobCommand(
150
  std::ostream& out, std::string const& cmdVar)
151
0
{
152
0
  out << "file(GLOB" << (this->Recurse ? "_RECURSE " : " ");
153
0
  out << cmdVar << " ";
154
0
  if (this->Recurse && this->FollowSymlinks) {
155
0
    out << "FOLLOW_SYMLINKS ";
156
0
  }
157
0
  out << "LIST_DIRECTORIES " << this->ListDirectories << " ";
158
0
  if (!this->Relative.empty()) {
159
0
    out << "RELATIVE \"" << this->Relative << "\" ";
160
0
  }
161
0
  out << "\"" << this->Expression << "\")";
162
0
}
163
164
void cmGlobVerificationManager::AddCacheEntry(
165
  cmGlobCacheEntry const& entry, std::string const& variable,
166
  cmListFileBacktrace const& backtrace, cmMessenger* messenger)
167
0
{
168
0
  CacheEntryKey key =
169
0
    CacheEntryKey(entry.Recurse, entry.ListDirectories, entry.FollowSymlinks,
170
0
                  entry.Relative, entry.Expression);
171
0
  CacheEntryValue& value = this->Cache[key];
172
0
  if (!value.Initialized) {
173
0
    value.Files = entry.Files;
174
0
    value.Initialized = true;
175
0
    value.Backtraces.emplace_back(variable, backtrace);
176
0
  } else if (value.Initialized && value.Files != entry.Files) {
177
0
    std::ostringstream message;
178
0
    message << std::boolalpha;
179
0
    message << "The glob expression\n ";
180
0
    key.PrintGlobCommand(message, variable);
181
0
    message << "\nwas already present in the glob cache but the directory "
182
0
               "contents have changed during the configuration run.\n";
183
0
    message << "Matching glob expressions:";
184
0
    for (auto const& bt : value.Backtraces) {
185
0
      message << "\n  " << std::get<0>(bt);
186
0
      messenger->PrintBacktraceTitle(message, std::get<1>(bt));
187
0
    }
188
0
    messenger->IssueMessage(MessageType::FATAL_ERROR, message.str(),
189
0
                            backtrace);
190
0
  } else {
191
0
    value.Backtraces.emplace_back(variable, backtrace);
192
0
  }
193
0
}
194
195
std::vector<cmGlobCacheEntry> cmGlobVerificationManager::GetCacheEntries()
196
  const
197
0
{
198
0
  std::vector<cmGlobCacheEntry> entries;
199
0
  for (auto const& i : this->Cache) {
200
0
    CacheEntryKey k = std::get<0>(i);
201
0
    CacheEntryValue v = std::get<1>(i);
202
0
    if (v.Initialized) {
203
0
      entries.emplace_back(k.Recurse, k.ListDirectories, k.FollowSymlinks,
204
0
                           k.Relative, k.Expression, v.Files);
205
0
    }
206
0
  }
207
0
  return entries;
208
0
}
209
210
void cmGlobVerificationManager::Reset()
211
1
{
212
1
  this->Cache.clear();
213
1
  this->VerifyScript.clear();
214
1
  this->VerifyStamp.clear();
215
1
}