Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmFileAPICMakeFiles.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 "cmFileAPICMakeFiles.h"
4
5
#include <memory>
6
#include <string>
7
#include <utility>
8
#include <vector>
9
10
#include <cm3p/json/value.h>
11
12
#include "cmFileAPI.h"
13
#include "cmGlobCacheEntry.h"
14
#include "cmGlobalGenerator.h"
15
#include "cmLocalGenerator.h"
16
#include "cmMakefile.h"
17
#include "cmSystemTools.h"
18
#include "cmake.h"
19
20
namespace {
21
22
class CMakeFiles
23
{
24
  cmFileAPI& FileAPI;
25
  unsigned int Version;
26
  std::string CMakeModules;
27
  std::string const& TopSource;
28
  std::string const& TopBuild;
29
  bool OutOfSource;
30
31
  Json::Value DumpPaths();
32
  Json::Value DumpInputs();
33
  Json::Value DumpInput(std::string const& file);
34
  Json::Value DumpGlobsDependent();
35
  Json::Value DumpGlobDependent(cmGlobCacheEntry const& entry);
36
37
public:
38
  CMakeFiles(cmFileAPI& fileAPI, unsigned int version);
39
  Json::Value Dump();
40
};
41
42
CMakeFiles::CMakeFiles(cmFileAPI& fileAPI, unsigned int version)
43
0
  : FileAPI(fileAPI)
44
0
  , Version(version)
45
0
  , CMakeModules(cmSystemTools::GetCMakeRoot() + "/Modules")
46
0
  , TopSource(this->FileAPI.GetCMakeInstance()->GetHomeDirectory())
47
0
  , TopBuild(this->FileAPI.GetCMakeInstance()->GetHomeOutputDirectory())
48
0
  , OutOfSource(this->TopBuild != this->TopSource)
49
0
{
50
0
  static_cast<void>(this->Version);
51
0
}
52
53
Json::Value CMakeFiles::Dump()
54
0
{
55
0
  Json::Value cmakeFiles = Json::objectValue;
56
0
  cmakeFiles["paths"] = this->DumpPaths();
57
0
  cmakeFiles["inputs"] = this->DumpInputs();
58
0
  Json::Value globsDependent = this->DumpGlobsDependent();
59
0
  if (!globsDependent.empty()) {
60
0
    cmakeFiles["globsDependent"] = std::move(globsDependent);
61
0
  }
62
0
  return cmakeFiles;
63
0
}
64
65
Json::Value CMakeFiles::DumpPaths()
66
0
{
67
0
  Json::Value paths = Json::objectValue;
68
0
  paths["source"] = this->TopSource;
69
0
  paths["build"] = this->TopBuild;
70
0
  return paths;
71
0
}
72
73
Json::Value CMakeFiles::DumpInputs()
74
0
{
75
0
  Json::Value inputs = Json::arrayValue;
76
77
0
  cmGlobalGenerator* gg =
78
0
    this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
79
0
  for (auto const& lg : gg->GetLocalGenerators()) {
80
0
    cmMakefile const* mf = lg->GetMakefile();
81
0
    for (std::string const& file : mf->GetListFiles()) {
82
0
      inputs.append(this->DumpInput(file));
83
0
    }
84
0
  }
85
86
0
  return inputs;
87
0
}
88
89
Json::Value CMakeFiles::DumpInput(std::string const& file)
90
0
{
91
0
  Json::Value input = Json::objectValue;
92
93
0
  bool const isCMake = cmSystemTools::IsSubDirectory(file, this->CMakeModules);
94
0
  if (isCMake) {
95
0
    input["isCMake"] = true;
96
0
  }
97
98
0
  if (!cmSystemTools::IsSubDirectory(file, this->TopSource) &&
99
0
      !cmSystemTools::IsSubDirectory(file, this->TopBuild)) {
100
0
    input["isExternal"] = true;
101
0
  }
102
103
0
  if (this->OutOfSource &&
104
0
      cmSystemTools::IsSubDirectory(file, this->TopBuild)) {
105
0
    input["isGenerated"] = true;
106
0
  }
107
108
0
  std::string path = file;
109
0
  if (!isCMake && cmSystemTools::IsSubDirectory(path, this->TopSource)) {
110
    // Use a relative path within the source directory.
111
0
    path = cmSystemTools::RelativePath(this->TopSource, path);
112
0
  }
113
0
  input["path"] = path;
114
115
0
  return input;
116
0
}
117
118
Json::Value CMakeFiles::DumpGlobsDependent()
119
0
{
120
0
  Json::Value globsDependent = Json::arrayValue;
121
0
  for (cmGlobCacheEntry const& entry :
122
0
       this->FileAPI.GetCMakeInstance()->GetGlobCacheEntries()) {
123
0
    globsDependent.append(this->DumpGlobDependent(entry));
124
0
  }
125
0
  return globsDependent;
126
0
}
127
128
Json::Value CMakeFiles::DumpGlobDependent(cmGlobCacheEntry const& entry)
129
0
{
130
0
  Json::Value globDependent = Json::objectValue;
131
0
  globDependent["expression"] = entry.Expression;
132
0
  if (entry.Recurse) {
133
0
    globDependent["recurse"] = true;
134
0
  }
135
0
  if (entry.ListDirectories) {
136
0
    globDependent["listDirectories"] = true;
137
0
  }
138
0
  if (entry.FollowSymlinks) {
139
0
    globDependent["followSymlinks"] = true;
140
0
  }
141
0
  if (!entry.Relative.empty()) {
142
0
    globDependent["relative"] = entry.Relative;
143
0
  }
144
0
  Json::Value paths = Json::arrayValue;
145
0
  for (std::string const& file : entry.Files) {
146
0
    paths.append(file);
147
0
  }
148
0
  globDependent["paths"] = std::move(paths);
149
0
  return globDependent;
150
0
}
151
}
152
153
Json::Value cmFileAPICMakeFilesDump(cmFileAPI& fileAPI, unsigned int version)
154
0
{
155
0
  CMakeFiles cmakeFiles(fileAPI, version);
156
0
  return cmakeFiles.Dump();
157
0
}