Coverage Report

Created: 2026-07-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmNinjaUtilityTargetGenerator.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 "cmNinjaUtilityTargetGenerator.h"
4
5
#include <algorithm>
6
#include <array>
7
#include <iterator>
8
#include <memory>
9
#include <set>
10
#include <string>
11
#include <utility>
12
#include <vector>
13
14
#include "cmCustomCommand.h"
15
#include "cmCustomCommandGenerator.h"
16
#include "cmGeneratedFileStream.h"
17
#include "cmGeneratorExpression.h"
18
#include "cmGeneratorTarget.h"
19
#include "cmGlobalNinjaGenerator.h"
20
#include "cmLocalNinjaGenerator.h"
21
#include "cmNinjaTypes.h"
22
#include "cmOutputConverter.h"
23
#include "cmSourceFile.h"
24
#include "cmStringAlgorithms.h"
25
#include "cmSystemTools.h"
26
#include "cmTarget.h"
27
#include "cmTargetTypes.h"
28
#include "cmValue.h"
29
30
cmNinjaUtilityTargetGenerator::cmNinjaUtilityTargetGenerator(
31
  cmGeneratorTarget* target)
32
0
  : cmNinjaTargetGenerator(target)
33
0
{
34
0
}
35
36
0
cmNinjaUtilityTargetGenerator::~cmNinjaUtilityTargetGenerator() = default;
37
38
void cmNinjaUtilityTargetGenerator::Generate(std::string const& config)
39
0
{
40
0
  if (!this->GetGeneratorTarget()->Target->IsPerConfig()) {
41
0
    this->WriteUtilBuildStatements(config, config);
42
0
    return;
43
0
  }
44
45
0
  for (auto const& fileConfig : this->GetConfigNames()) {
46
0
    if (!this->GetGlobalGenerator()
47
0
           ->GetCrossConfigs(fileConfig)
48
0
           .count(config)) {
49
0
      continue;
50
0
    }
51
0
    if (fileConfig != config &&
52
0
        this->GetGeneratorTarget()->GetType() ==
53
0
          cm::TargetType::GLOBAL_TARGET) {
54
0
      continue;
55
0
    }
56
0
    this->WriteUtilBuildStatements(config, fileConfig);
57
0
  }
58
0
}
59
60
void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements(
61
  std::string const& config, std::string const& fileConfig)
62
0
{
63
0
  cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
64
0
  cmLocalNinjaGenerator* lg = this->GetLocalGenerator();
65
0
  cmGeneratorTarget* genTarget = this->GetGeneratorTarget();
66
67
0
  std::string configDir;
68
0
  if (genTarget->Target->IsPerConfig()) {
69
0
    configDir = gg->ConfigDirectory(fileConfig);
70
0
  }
71
0
  std::string utilCommandName =
72
0
    cmStrCat(lg->GetCurrentBinaryDirectory(), "/CMakeFiles", configDir, '/',
73
0
             this->GetTargetName(), ".util");
74
0
  utilCommandName = this->ConvertToNinjaPath(utilCommandName);
75
76
0
  cmNinjaBuild phonyBuild("phony");
77
0
  std::vector<std::string> commands;
78
0
  cmNinjaDeps deps;
79
0
  cmGlobalNinjaGenerator::CCOutputs util_outputs(gg);
80
0
  util_outputs.ExplicitOuts.emplace_back(utilCommandName);
81
82
0
  std::string commandDesc;
83
0
  cmGeneratorExpression ge(*this->GetLocalGenerator()->GetCMakeInstance());
84
0
  bool uses_terminal = false;
85
0
  {
86
0
    std::array<std::vector<cmCustomCommand> const*, 2> const cmdLists = {
87
0
      { &genTarget->GetPreBuildCommands(), &genTarget->GetPostBuildCommands() }
88
0
    };
89
90
0
    for (std::vector<cmCustomCommand> const* cmdList : cmdLists) {
91
0
      for (cmCustomCommand const& ci : *cmdList) {
92
0
        cmCustomCommandGenerator ccg(ci, fileConfig, lg);
93
0
        lg->AppendCustomCommandDeps(ccg, deps, fileConfig);
94
0
        lg->AppendCustomCommandLines(ccg, commands);
95
0
        if (ci.GetComment()) {
96
0
          if (!commandDesc.empty()) {
97
0
            commandDesc += "; ";
98
0
          }
99
0
          auto cge = ge.Parse(ci.GetComment());
100
0
          commandDesc += cge->Evaluate(this->GetLocalGenerator(), config);
101
0
        }
102
0
        util_outputs.Add(ccg.GetByproducts());
103
0
        if (ci.GetUsesTerminal()) {
104
0
          uses_terminal = true;
105
0
        }
106
0
      }
107
0
    }
108
0
  }
109
110
0
  {
111
0
    std::vector<cmSourceFile*> sources;
112
0
    genTarget->GetSourceFiles(sources, config);
113
0
    for (cmSourceFile const* source : sources) {
114
0
      if (cmCustomCommand const* cc = source->GetCustomCommand()) {
115
0
        cmCustomCommandGenerator ccg(*cc, config, lg);
116
0
        lg->AddCustomCommandTarget(cc, genTarget);
117
118
        // Depend on all custom command outputs.
119
0
        std::vector<std::string> const& ccOutputs = ccg.GetOutputs();
120
0
        std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
121
0
        std::transform(ccOutputs.begin(), ccOutputs.end(),
122
0
                       std::back_inserter(deps), this->MapToNinjaPath());
123
0
        std::transform(ccByproducts.begin(), ccByproducts.end(),
124
0
                       std::back_inserter(deps), this->MapToNinjaPath());
125
0
      }
126
0
    }
127
0
  }
128
129
0
  std::string outputConfig;
130
0
  if (genTarget->Target->IsPerConfig()) {
131
0
    outputConfig = config;
132
0
  }
133
0
  lg->AppendTargetOutputs(genTarget, phonyBuild.Outputs, outputConfig);
134
0
  if (genTarget->Target->GetType() != cm::TargetType::GLOBAL_TARGET) {
135
0
    lg->AppendTargetOutputs(genTarget, gg->GetByproductsForCleanTarget(),
136
0
                            config);
137
0
    std::copy(util_outputs.ExplicitOuts.begin(),
138
0
              util_outputs.ExplicitOuts.end(),
139
0
              std::back_inserter(gg->GetByproductsForCleanTarget()));
140
0
  }
141
0
  lg->AppendTargetDepends(genTarget, deps, config, fileConfig,
142
0
                          DependOnTargetArtifact);
143
144
0
  if (commands.empty()) {
145
0
    phonyBuild.Comment = "Utility command for " + this->GetTargetName();
146
0
    phonyBuild.ExplicitDeps = std::move(deps);
147
0
    if (genTarget->GetType() != cm::TargetType::GLOBAL_TARGET) {
148
0
      gg->WriteBuild(this->GetImplFileStream(fileConfig), phonyBuild);
149
0
    } else {
150
0
      gg->WriteBuild(this->GetCommonFileStream(), phonyBuild);
151
0
    }
152
0
  } else {
153
0
    std::string command = lg->BuildCommandLine(
154
0
      commands, config, fileConfig, "utility", this->GeneratorTarget);
155
0
    std::string desc;
156
0
    cmValue echoStr = genTarget->GetProperty("EchoString");
157
0
    if (echoStr) {
158
0
      desc = *echoStr;
159
0
    } else if (!commandDesc.empty()) {
160
0
      desc = commandDesc;
161
0
    } else {
162
0
      desc = "Running utility command for " + this->GetTargetName();
163
0
    }
164
165
    // TODO: fix problematic global targets.  For now, search and replace the
166
    // makefile vars.
167
0
    cmSystemTools::ReplaceString(
168
0
      command, "$(CMAKE_SOURCE_DIR)",
169
0
      lg->ConvertToOutputFormat(lg->GetSourceDirectory(),
170
0
                                cmOutputConverter::SHELL));
171
0
    cmSystemTools::ReplaceString(
172
0
      command, "$(CMAKE_BINARY_DIR)",
173
0
      lg->ConvertToOutputFormat(lg->GetBinaryDirectory(),
174
0
                                cmOutputConverter::SHELL));
175
0
    cmSystemTools::ReplaceString(command, "$(ARGS)", "");
176
0
    command = gg->ExpandCFGIntDir(command, config);
177
178
0
    std::string ccConfig;
179
0
    if (genTarget->Target->IsPerConfig() &&
180
0
        genTarget->GetType() != cm::TargetType::GLOBAL_TARGET) {
181
0
      ccConfig = config;
182
0
    }
183
0
    if (config == fileConfig ||
184
0
        gg->GetPerConfigUtilityTargets().count(genTarget->GetName())) {
185
0
      gg->WriteCustomCommandBuild(
186
0
        command, desc, "Utility command for " + this->GetTargetName(),
187
0
        /*depfile*/ "", /*job_pool*/ "", uses_terminal,
188
0
        /*restat*/ true, ccConfig, std::move(util_outputs), std::move(deps));
189
0
    }
190
191
0
    phonyBuild.ExplicitDeps.push_back(utilCommandName);
192
0
    if (genTarget->GetType() != cm::TargetType::GLOBAL_TARGET) {
193
0
      gg->WriteBuild(this->GetImplFileStream(fileConfig), phonyBuild);
194
0
    } else {
195
0
      gg->WriteBuild(this->GetCommonFileStream(), phonyBuild);
196
0
    }
197
0
  }
198
199
  // Find ADDITIONAL_CLEAN_FILES
200
0
  this->AdditionalCleanFiles(config);
201
202
  // Add an alias for the logical target name regardless of what directory
203
  // contains it.  Skip this for GLOBAL_TARGET because they are meant to
204
  // be per-directory and have one at the top-level anyway.
205
0
  if (genTarget->GetType() != cm::TargetType::GLOBAL_TARGET) {
206
0
    gg->AddTargetAlias(this->GetTargetName(), genTarget, config);
207
0
  }
208
0
}