Coverage Report

Created: 2026-09-14 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmCommonTargetGenerator.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 "cmCommonTargetGenerator.h"
4
5
#include <algorithm>
6
#include <sstream>
7
#include <utility>
8
9
#include <cm/filesystem>
10
#include <cm/string_view>
11
#include <cmext/string_view>
12
13
#include "cmComputeLinkInformation.h"
14
#include "cmGenExContext.h"
15
#include "cmGeneratorExpression.h"
16
#include "cmGeneratorExpressionDAGChecker.h"
17
#include "cmGeneratorTarget.h"
18
#include "cmGlobalCommonGenerator.h"
19
#include "cmGlobalGenerator.h"
20
#include "cmList.h"
21
#include "cmLocalCommonGenerator.h"
22
#include "cmLocalGenerator.h"
23
#include "cmMakefile.h"
24
#include "cmMessageType.h"
25
#include "cmOutputConverter.h"
26
#include "cmRange.h"
27
#include "cmSourceFile.h"
28
#include "cmState.h"
29
#include "cmStringAlgorithms.h"
30
#include "cmSystemTools.h"
31
#include "cmTargetTypes.h"
32
#include "cmValue.h"
33
34
namespace {
35
bool LinkedTargetHasModules(cmGeneratorTarget const* target,
36
                            std::string const& lang, std::string const& config,
37
                            cmGlobalCommonGenerator const* gg,
38
                            cmGeneratorTarget const* currentTarget)
39
0
{
40
0
  if (!target) {
41
0
    return false;
42
0
  }
43
0
  if (lang == "CXX"_s && target->HasCxxImportModuleErrors()) {
44
0
    return true;
45
0
  }
46
0
  if (target->IsImported()) {
47
0
    return false;
48
0
  }
49
0
  if (!gg->TargetOrderIndexLess(target, currentTarget)) {
50
0
    return false;
51
0
  }
52
0
  if (target->GetType() == cm::TargetType::INTERFACE_LIBRARY &&
53
0
      !target->IsSynthetic()) {
54
0
    return false;
55
0
  }
56
0
  if (lang == "CXX"_s && target->HaveCxx20ModuleSources()) {
57
0
    return true;
58
0
  }
59
0
  if (lang == "Fortran"_s && target->HaveFortranSources(config)) {
60
0
    return true;
61
0
  }
62
0
  return false;
63
0
}
64
}
65
66
cmCommonTargetGenerator::cmCommonTargetGenerator(cmGeneratorTarget* gt)
67
0
  : GeneratorTarget(gt)
68
0
  , Makefile(gt->Makefile)
69
  , LocalCommonGenerator(
70
0
      static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator))
71
0
  , GlobalCommonGenerator(static_cast<cmGlobalCommonGenerator*>(
72
0
      gt->LocalGenerator->GetGlobalGenerator()))
73
0
  , ConfigNames(this->LocalCommonGenerator->GetConfigNames())
74
0
{
75
0
}
76
77
0
cmCommonTargetGenerator::~cmCommonTargetGenerator() = default;
78
79
std::vector<std::string> const& cmCommonTargetGenerator::GetConfigNames() const
80
0
{
81
0
  return this->ConfigNames;
82
0
}
83
84
cmValue cmCommonTargetGenerator::GetFeature(std::string const& feature,
85
                                            std::string const& config)
86
0
{
87
0
  return this->GeneratorTarget->GetFeature(feature, config);
88
0
}
89
90
void cmCommonTargetGenerator::AppendFortranFormatFlags(
91
  std::string& flags, cmSourceFile const& source)
92
0
{
93
0
  std::string const srcfmt = source.GetSafeProperty("Fortran_FORMAT");
94
0
  cmOutputConverter::FortranFormat format =
95
0
    cmOutputConverter::GetFortranFormat(srcfmt);
96
0
  if (format == cmOutputConverter::FortranFormatNone) {
97
0
    std::string const& tgtfmt =
98
0
      this->GeneratorTarget->GetSafeProperty("Fortran_FORMAT");
99
0
    format = cmOutputConverter::GetFortranFormat(tgtfmt);
100
0
  }
101
0
  char const* var = nullptr;
102
0
  switch (format) {
103
0
    case cmOutputConverter::FortranFormatFixed:
104
0
      var = "CMAKE_Fortran_FORMAT_FIXED_FLAG";
105
0
      break;
106
0
    case cmOutputConverter::FortranFormatFree:
107
0
      var = "CMAKE_Fortran_FORMAT_FREE_FLAG";
108
0
      break;
109
0
    default:
110
0
      break;
111
0
  }
112
0
  if (var) {
113
0
    this->LocalCommonGenerator->AppendFlags(
114
0
      flags, this->Makefile->GetSafeDefinition(var));
115
0
  }
116
0
}
117
118
void cmCommonTargetGenerator::AppendFortranPreprocessFlags(
119
  std::string& flags, cmSourceFile const& source,
120
  PreprocessFlagsRequired requires_pp)
121
0
{
122
0
  std::string const srcpp = source.GetSafeProperty("Fortran_PREPROCESS");
123
0
  cmOutputConverter::FortranPreprocess preprocess =
124
0
    cmOutputConverter::GetFortranPreprocess(srcpp);
125
0
  if (preprocess == cmOutputConverter::FortranPreprocess::Unset) {
126
0
    std::string const& tgtpp =
127
0
      this->GeneratorTarget->GetSafeProperty("Fortran_PREPROCESS");
128
0
    preprocess = cmOutputConverter::GetFortranPreprocess(tgtpp);
129
0
  }
130
0
  char const* var = nullptr;
131
0
  switch (preprocess) {
132
0
    case cmOutputConverter::FortranPreprocess::Needed:
133
0
      if (requires_pp == PreprocessFlagsRequired::YES) {
134
0
        var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON";
135
0
      }
136
0
      break;
137
0
    case cmOutputConverter::FortranPreprocess::NotNeeded:
138
0
      var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF";
139
0
      break;
140
0
    default:
141
0
      break;
142
0
  }
143
0
  if (var) {
144
0
    this->LocalCommonGenerator->AppendCompileOptions(
145
0
      flags, this->Makefile->GetSafeDefinition(var));
146
0
  }
147
0
}
148
149
std::string cmCommonTargetGenerator::GetFlags(std::string const& l,
150
                                              std::string const& config,
151
                                              std::string const& arch)
152
0
{
153
0
  std::string const key = config + arch;
154
155
0
  auto i = this->Configs[key].FlagsByLanguage.find(l);
156
0
  if (i == this->Configs[key].FlagsByLanguage.end()) {
157
0
    std::string flags;
158
159
0
    this->LocalCommonGenerator->GetTargetCompileFlags(this->GeneratorTarget,
160
0
                                                      config, l, flags, arch);
161
162
0
    ByLanguageMap::value_type entry(l, flags);
163
0
    i = this->Configs[key].FlagsByLanguage.insert(entry).first;
164
0
  }
165
0
  return i->second;
166
0
}
167
168
std::string cmCommonTargetGenerator::GetDefines(std::string const& l,
169
                                                std::string const& config)
170
0
{
171
0
  auto i = this->Configs[config].DefinesByLanguage.find(l);
172
0
  if (i == this->Configs[config].DefinesByLanguage.end()) {
173
0
    std::set<std::string> defines;
174
0
    this->LocalCommonGenerator->GetTargetDefines(this->GeneratorTarget, config,
175
0
                                                 l, defines);
176
177
0
    std::string definesString;
178
0
    this->LocalCommonGenerator->JoinDefines(defines, definesString, l);
179
180
0
    ByLanguageMap::value_type entry(l, definesString);
181
0
    i = this->Configs[config].DefinesByLanguage.insert(entry).first;
182
0
  }
183
0
  return i->second;
184
0
}
185
186
std::string cmCommonTargetGenerator::GetIncludes(std::string const& l,
187
                                                 std::string const& config)
188
0
{
189
0
  auto i = this->Configs[config].IncludesByLanguage.find(l);
190
0
  if (i == this->Configs[config].IncludesByLanguage.end()) {
191
0
    std::string includes;
192
0
    this->AddIncludeFlags(includes, l, config);
193
0
    ByLanguageMap::value_type entry(l, includes);
194
0
    i = this->Configs[config].IncludesByLanguage.insert(entry).first;
195
0
  }
196
0
  return i->second;
197
0
}
198
199
cmCommonTargetGenerator::LinkedTargetDirs
200
cmCommonTargetGenerator::GetLinkedTargetDirectories(
201
  std::string const& lang, std::string const& config) const
202
0
{
203
0
  LinkedTargetDirs dirs;
204
0
  std::set<cmGeneratorTarget const*> forward_emitted;
205
0
  std::set<cmGeneratorTarget const*> direct_emitted;
206
0
  cmGlobalCommonGenerator* const gg = this->GlobalCommonGenerator;
207
208
0
  enum class Forwarding
209
0
  {
210
0
    Yes,
211
0
    No
212
0
  };
213
214
0
  if (cmComputeLinkInformation* cli =
215
0
        this->GeneratorTarget->GetLinkInformation(config)) {
216
217
0
    auto findSyntheticTarget =
218
0
      [this, &config,
219
0
       cli](cmGeneratorTarget const* linkee) -> cmGeneratorTarget const* {
220
0
      if (!linkee) {
221
0
        return nullptr;
222
0
      }
223
224
      // Check the map of direct synthetic dependencies for a substitute
225
0
      auto const& synthDeps = this->GeneratorTarget->GetSyntheticDeps(config);
226
0
      auto it = synthDeps.find(linkee);
227
0
      if (it != synthDeps.end() && !it->second.empty()) {
228
0
        return *it->second.begin();
229
0
      }
230
231
      // Check linked targets to find synthetic targets for transitive deps
232
0
      std::vector<cmGeneratorTarget const*> pending;
233
0
      std::set<cmGeneratorTarget const*> visited;
234
0
      for (auto const& dep : synthDeps) {
235
0
        for (auto const* synth : dep.second) {
236
0
          if (synth && visited.insert(synth).second) {
237
0
            pending.push_back(synth);
238
0
          }
239
0
        }
240
0
      }
241
242
      // Also seed the search from direct linked targets that have
243
      // CXX20 module sources, in case they hold synthetic deps for
244
      // transitive dependencies that we need to inherit. Skip targets
245
      // that already have synthetic substitutes in our own synthDeps
246
      // (their transitive deps will be explored via the substitute).
247
0
      for (auto const& item : cli->GetItems()) {
248
0
        if (item.Target && item.Target->HaveCxx20ModuleSources() &&
249
0
            synthDeps.find(item.Target) == synthDeps.end() &&
250
0
            visited.insert(item.Target).second) {
251
0
          pending.push_back(item.Target);
252
0
        }
253
0
      }
254
255
0
      while (!pending.empty()) {
256
0
        auto const* current = pending.back();
257
0
        pending.pop_back();
258
0
        auto const& transitiveSynthDeps = current->GetSyntheticDeps(config);
259
0
        auto itLinkeeSynth = transitiveSynthDeps.find(linkee);
260
0
        if (itLinkeeSynth != transitiveSynthDeps.end() &&
261
0
            !itLinkeeSynth->second.empty()) {
262
0
          return *itLinkeeSynth->second.begin();
263
0
        }
264
0
        for (auto const& entry : transitiveSynthDeps) {
265
0
          for (auto const* synth : entry.second) {
266
0
            if (synth && visited.insert(synth).second) {
267
0
              pending.push_back(synth);
268
0
            }
269
0
          }
270
0
        }
271
0
      }
272
273
0
      return nullptr;
274
0
    };
275
276
0
    auto addLinkedTarget = [this, &lang, &config, &dirs, &direct_emitted,
277
0
                            &forward_emitted, &findSyntheticTarget,
278
0
                            gg](cmGeneratorTarget const* linkee,
279
0
                                Forwarding forward) {
280
      // Check if the linkee has a synthetic target to use for importing
281
0
      cmGeneratorTarget const* mappedLinkee = linkee;
282
0
      if (auto const* synth = findSyntheticTarget(linkee)) {
283
0
        mappedLinkee = synth;
284
0
      }
285
286
0
      if (mappedLinkee &&
287
0
          LinkedTargetHasModules(mappedLinkee, lang, config, gg,
288
0
                                 this->GeneratorTarget)) {
289
0
        cmLocalGenerator* lg = mappedLinkee->GetLocalGenerator();
290
0
        std::string di = mappedLinkee->GetSupportDirectory();
291
0
        if (lg->GetGlobalGenerator()->IsMultiConfig()) {
292
0
          di = cmStrCat(di, '/', config);
293
0
        }
294
0
        if (forward == Forwarding::Yes &&
295
0
            forward_emitted.insert(mappedLinkee).second) {
296
0
          dirs.Forward.push_back(di);
297
0
        }
298
0
        if (direct_emitted.insert(mappedLinkee).second) {
299
0
          dirs.Direct.emplace_back(di);
300
0
        }
301
0
      }
302
0
    };
303
0
    for (auto const& item : cli->GetItems()) {
304
0
      if (item.Target) {
305
0
        addLinkedTarget(item.Target, Forwarding::No);
306
0
      } else if (item.ObjectSource && lang == "Fortran"_s
307
                 /* Object source files do not have a language associated with
308
                    them. */
309
0
                 /* && item.ObjectSource->GetLanguage() == "Fortran"_s*/) {
310
        // Fortran modules provided by `$<TARGET_OBJECTS>` as linked items
311
        // should be collated for use in this target.
312
0
        addLinkedTarget(this->LocalCommonGenerator->FindGeneratorTargetToUse(
313
0
                          item.ObjectSource->GetObjectLibrary()),
314
0
                        Forwarding::Yes);
315
0
      }
316
0
    }
317
0
    for (cmGeneratorTarget const* target : cli->GetExternalObjectTargets()) {
318
0
      addLinkedTarget(target, Forwarding::No);
319
0
    }
320
0
    if (lang == "Fortran"_s) {
321
      // Fortran modules provided by `$<TARGET_OBJECTS>` as sources should be
322
      // collated for use in this target.
323
0
      for (cmGeneratorTarget const* target :
324
0
           this->GeneratorTarget->GetSourceObjectLibraries(config)) {
325
0
        addLinkedTarget(target, Forwarding::Yes);
326
0
      }
327
0
    }
328
0
  }
329
0
  return dirs;
330
0
}
331
332
std::string cmCommonTargetGenerator::ComputeTargetCompilePDB(
333
  std::string const& config) const
334
0
{
335
0
  std::string compilePdbPath;
336
0
  if (this->GeneratorTarget->GetType() > cm::TargetType::OBJECT_LIBRARY) {
337
0
    return compilePdbPath;
338
0
  }
339
340
0
  compilePdbPath = this->GeneratorTarget->GetCompilePDBPath(config);
341
0
  if (compilePdbPath.empty()) {
342
    // Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
343
    // A trailing slash tells the toolchain to add its default file name.
344
0
    compilePdbPath = this->GeneratorTarget->GetSupportDirectory();
345
0
    if (this->GlobalCommonGenerator->IsMultiConfig()) {
346
0
      compilePdbPath += "/";
347
0
      compilePdbPath += config;
348
0
    }
349
0
    compilePdbPath += "/";
350
0
    if (this->GeneratorTarget->GetType() == cm::TargetType::STATIC_LIBRARY) {
351
      // Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
352
0
      compilePdbPath += this->GeneratorTarget->GetName();
353
0
      compilePdbPath += ".pdb";
354
0
    }
355
0
  }
356
357
0
  return compilePdbPath;
358
0
}
359
360
std::string cmCommonTargetGenerator::GetManifests(std::string const& config)
361
0
{
362
0
  std::vector<cmSourceFile const*> manifest_srcs;
363
0
  this->GeneratorTarget->GetManifests(manifest_srcs, config);
364
365
0
  std::vector<std::string> manifests;
366
0
  manifests.reserve(manifest_srcs.size());
367
368
0
  std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
369
0
  std::string manifestFlag = this->Makefile->GetDefinition(
370
0
    cmStrCat("CMAKE_", lang, "_LINKER_MANIFEST_FLAG"));
371
0
  for (cmSourceFile const* manifest_src : manifest_srcs) {
372
0
    manifests.push_back(manifestFlag +
373
0
                        this->LocalCommonGenerator->ConvertToOutputFormat(
374
0
                          this->LocalCommonGenerator->MaybeRelativeToWorkDir(
375
0
                            manifest_src->GetFullPath()),
376
0
                          cmOutputConverter::SHELL));
377
0
  }
378
379
0
  return cmJoin(manifests, " ");
380
0
}
381
382
std::string cmCommonTargetGenerator::GetAIXExports(std::string const&)
383
0
{
384
0
  std::string aixExports;
385
0
  if (this->GeneratorTarget->IsAIX()) {
386
0
    if (cmValue exportAll =
387
0
          this->GeneratorTarget->GetProperty("AIX_EXPORT_ALL_SYMBOLS")) {
388
0
      if (exportAll.IsOff()) {
389
0
        aixExports = "-n";
390
0
      }
391
0
    }
392
0
  }
393
0
  return aixExports;
394
0
}
395
396
void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
397
                                               std::string const& lang,
398
                                               char const* name, bool so)
399
0
{
400
  // Lookup the flag to specify the version.
401
0
  std::string fvar = cmStrCat("CMAKE_", lang, "_OSX_", name, "_VERSION_FLAG");
402
0
  cmValue flag = this->Makefile->GetDefinition(fvar);
403
404
  // Skip if no such flag.
405
0
  if (!flag) {
406
0
    return;
407
0
  }
408
409
  // Lookup the target version information.
410
0
  int major;
411
0
  int minor;
412
0
  int patch;
413
0
  std::string prop = cmStrCat("MACHO_", name, "_VERSION");
414
0
  std::string fallback_prop = so ? "SOVERSION" : "VERSION";
415
0
  this->GeneratorTarget->GetTargetVersionFallback(prop, fallback_prop, major,
416
0
                                                  minor, patch);
417
0
  if (major > 0 || minor > 0 || patch > 0) {
418
    // Append the flag since a non-zero version is specified.
419
0
    std::ostringstream vflag;
420
0
    vflag << *flag << major << "." << minor << "." << patch;
421
0
    this->LocalCommonGenerator->AppendFlags(flags, vflag.str());
422
0
  }
423
0
}
424
425
std::string cmCommonTargetGenerator::GetCompilerLauncher(
426
  std::string const& lang, std::string const& config)
427
0
{
428
0
  std::string compilerLauncher;
429
0
  if (lang == "C" || lang == "CXX" || lang == "Fortran" || lang == "CUDA" ||
430
0
      lang == "HIP" || lang == "ISPC" || lang == "OBJC" || lang == "OBJCXX") {
431
0
    std::string const clauncher_prop = cmStrCat(lang, "_COMPILER_LAUNCHER");
432
0
    cmValue clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
433
0
    std::string const evaluatedClauncher = cmGeneratorExpression::Evaluate(
434
0
      *clauncher, this->GeneratorTarget->GetLocalGenerator(), config,
435
0
      this->GeneratorTarget, nullptr, this->GeneratorTarget, lang);
436
0
    if (!evaluatedClauncher.empty()) {
437
0
      compilerLauncher = evaluatedClauncher;
438
0
    }
439
0
  }
440
0
  return compilerLauncher;
441
0
}
442
443
std::string cmCommonTargetGenerator::GenerateCodeCheckRules(
444
  cmSourceFile const& source, std::string& compilerLauncher,
445
  std::string const& cmakeCmd, std::string const& config,
446
  std::function<std::string(std::string const&)> const& pathConverter)
447
0
{
448
0
  auto const lang = source.GetLanguage();
449
0
  std::string tidy;
450
0
  std::string iwyu;
451
0
  std::string cpplint;
452
0
  std::string cppcheck;
453
0
  std::string icstat;
454
0
  std::string pvs;
455
456
0
  auto evaluateProp = [&](std::string const& prop) -> std::string {
457
0
    auto const value = this->GeneratorTarget->GetProperty(prop);
458
0
    if (!value) {
459
0
      return std::string{};
460
0
    }
461
0
    auto evaluatedProp = cmGeneratorExpression::Evaluate(
462
0
      *value, this->GeneratorTarget->GetLocalGenerator(), config,
463
0
      this->GeneratorTarget, nullptr, this->GeneratorTarget, lang);
464
0
    return evaluatedProp;
465
0
  };
466
0
  std::string const tidy_prop = cmStrCat(lang, "_CLANG_TIDY");
467
0
  tidy = evaluateProp(tidy_prop);
468
469
0
  if (lang == "C" || lang == "CXX") {
470
0
    std::string const iwyu_prop = cmStrCat(lang, "_INCLUDE_WHAT_YOU_USE");
471
0
    iwyu = evaluateProp(iwyu_prop);
472
473
0
    std::string const cpplint_prop = cmStrCat(lang, "_CPPLINT");
474
0
    cpplint = evaluateProp(cpplint_prop);
475
476
0
    std::string const cppcheck_prop = cmStrCat(lang, "_CPPCHECK");
477
0
    cppcheck = evaluateProp(cppcheck_prop);
478
479
0
    std::string const icstat_prop = cmStrCat(lang, "_ICSTAT");
480
0
    icstat = evaluateProp(icstat_prop);
481
482
0
    std::string const pvs_prop = cmStrCat(lang, "_PVS_STUDIO");
483
0
    pvs = evaluateProp(pvs_prop);
484
0
  }
485
486
0
  if (cmNonempty(iwyu) || cmNonempty(tidy) || cmNonempty(cpplint) ||
487
0
      cmNonempty(cppcheck) || cmNonempty(icstat) || cmNonempty(pvs)) {
488
0
    std::string code_check = cmakeCmd + " -E __run_co_compile";
489
0
    if (!compilerLauncher.empty()) {
490
      // In __run_co_compile case the launcher command is supplied
491
      // via --launcher=<maybe-list> and consumed
492
0
      code_check =
493
0
        cmStrCat(std::move(code_check), " --launcher=",
494
0
                 this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
495
0
                   compilerLauncher));
496
0
      compilerLauncher.clear();
497
0
    }
498
0
    if (cmNonempty(iwyu)) {
499
0
      code_check += " --iwyu=";
500
501
      // Only add --driver-mode if it is not already specified, as adding
502
      // it unconditionally might override a user-specified driver-mode
503
0
      if (iwyu.find("--driver-mode=") == std::string::npos) {
504
0
        cmValue const p = this->Makefile->GetDefinition(
505
0
          cmStrCat("CMAKE_", lang, "_INCLUDE_WHAT_YOU_USE_DRIVER_MODE"));
506
0
        std::string driverMode;
507
508
0
        if (cmNonempty(p)) {
509
0
          driverMode = *p;
510
0
        } else {
511
0
          driverMode = lang == "C" ? "gcc" : "g++";
512
0
        }
513
514
0
        code_check +=
515
0
          this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
516
0
            cmStrCat(iwyu, ";--driver-mode=", driverMode));
517
0
      } else {
518
0
        code_check +=
519
0
          this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(iwyu);
520
0
      }
521
0
    }
522
0
    if (cmNonempty(tidy)) {
523
0
      code_check += " --tidy=";
524
0
      cmValue const p = this->Makefile->GetDefinition(
525
0
        cmStrCat("CMAKE_", lang, "_CLANG_TIDY_DRIVER_MODE"));
526
0
      std::string driverMode;
527
0
      if (cmNonempty(p)) {
528
0
        driverMode = *p;
529
0
      } else {
530
0
        driverMode = lang == "C" ? "gcc" : "g++";
531
0
      }
532
533
0
      auto const generatorName = this->GeneratorTarget->GetLocalGenerator()
534
0
                                   ->GetGlobalGenerator()
535
0
                                   ->GetName();
536
0
      auto const clangTidyExportFixedDir =
537
0
        this->GeneratorTarget->GetClangTidyExportFixesDirectory(lang);
538
0
      auto fixesFile = this->GetClangTidyReplacementsFilePath(
539
0
        clangTidyExportFixedDir, source, config);
540
0
      std::string exportFixes;
541
0
      if (!clangTidyExportFixedDir.empty()) {
542
0
        this->GlobalCommonGenerator->AddClangTidyExportFixesDir(
543
0
          clangTidyExportFixedDir);
544
0
      }
545
0
      if (generatorName.find("Make") != std::string::npos) {
546
0
        if (!clangTidyExportFixedDir.empty()) {
547
0
          this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile);
548
0
          cmSystemTools::MakeDirectory(
549
0
            cmSystemTools::GetFilenamePath(fixesFile));
550
0
          fixesFile = this->GeneratorTarget->GetLocalGenerator()
551
0
                        ->MaybeRelativeToCurBinDir(fixesFile);
552
0
          exportFixes = cmStrCat(";--export-fixes=", fixesFile);
553
0
        }
554
0
        code_check +=
555
0
          this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
556
0
            cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode,
557
0
                     exportFixes));
558
0
      } else if (generatorName.find("Ninja") != std::string::npos ||
559
0
                 generatorName.find("FASTBuild") != std::string::npos) {
560
0
        if (!clangTidyExportFixedDir.empty()) {
561
0
          this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile);
562
0
          cmSystemTools::MakeDirectory(
563
0
            cmSystemTools::GetFilenamePath(fixesFile));
564
0
          if (pathConverter) {
565
0
            fixesFile = pathConverter(fixesFile);
566
0
          }
567
0
          exportFixes = cmStrCat(";--export-fixes=", fixesFile);
568
0
        }
569
0
        code_check +=
570
0
          this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
571
0
            cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode,
572
0
                     exportFixes));
573
0
      }
574
0
    }
575
0
    if (cmNonempty(pvs)) {
576
0
      cmMakefile* mf =
577
0
        this->GeneratorTarget->GetLocalGenerator()->GetMakefile();
578
0
      std::string extraPvsArgs;
579
0
      if (lang == "CXX") {
580
0
        extraPvsArgs +=
581
0
          cmStrCat(";--cxx;", mf->GetDefinition("CMAKE_CXX_COMPILER"));
582
0
      } else if (lang == "C") {
583
0
        extraPvsArgs +=
584
0
          cmStrCat(";--cc;", mf->GetDefinition("CMAKE_C_COMPILER"));
585
0
      }
586
      // cocompile args
587
0
      code_check += " --pvs-studio=";
588
0
      code_check += this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
589
0
        cmStrCat(pvs, extraPvsArgs));
590
0
      code_check += " --object=";
591
0
      code_check +=
592
0
        this->GeneratorTarget->GetLocalGenerator()->ConvertToOutputFormat(
593
0
          cmSystemTools::CollapseFullPath(
594
0
            cmStrCat(this->GeneratorTarget->GetObjectDirectory(config), '/',
595
0
                     this->GeneratorTarget->GetObjectName(&source))),
596
0
          cmOutputConverter::SHELL);
597
0
    }
598
0
    if (cmNonempty(cpplint)) {
599
0
      code_check += " --cpplint=";
600
0
      code_check +=
601
0
        this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cpplint);
602
0
    }
603
0
    if (cmNonempty(cppcheck)) {
604
0
      code_check += " --cppcheck=";
605
0
      code_check +=
606
0
        this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cppcheck);
607
0
    }
608
0
    if (cmNonempty(icstat)) {
609
0
      code_check += " --icstat=";
610
      // Unless specified otherwise via CMAKE_<LANG>_ICSTAT,
611
      // populate the icstat command line using default options
612
      // for its mandatory parameters.
613
0
      std::string checksParam{};
614
0
      if (icstat.find("--checks=") == std::string::npos) {
615
0
        std::string const checksFile{ "cstat_sel_checks.txt" };
616
0
        checksParam = cmStrCat(";--checks=", checksFile);
617
0
      }
618
0
      std::string dbParam{};
619
0
      if (icstat.find("--db=") == std::string::npos) {
620
0
        std::string const dbFile{ "cstat.db" };
621
0
        dbParam = cmStrCat(";--db=", dbFile);
622
0
      }
623
0
      std::string analyzeCmd{ ";analyze" };
624
0
      code_check += this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
625
0
        cmStrCat(icstat, checksParam, dbParam, analyzeCmd));
626
0
    }
627
0
    if (cmNonempty(tidy) || (cmNonempty(cpplint)) || (cmNonempty(cppcheck)) ||
628
0
        cmNonempty(pvs) || cmNonempty(icstat)) {
629
0
      code_check += " --source=";
630
0
      code_check +=
631
0
        this->GeneratorTarget->GetLocalGenerator()->ConvertToOutputFormat(
632
0
          source.GetFullPath(), cmOutputConverter::SHELL);
633
0
    }
634
0
    code_check += " -- ";
635
0
    return code_check;
636
0
  }
637
0
  return "";
638
0
}
639
640
std::string cmCommonTargetGenerator::GetLinkerLauncher(
641
  std::string const& config)
642
0
{
643
0
  std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
644
0
  std::string propName = lang + "_LINKER_LAUNCHER";
645
0
  cmValue launcherProp = this->GeneratorTarget->GetProperty(propName);
646
0
  if (cmNonempty(launcherProp)) {
647
0
    cm::GenEx::Context context(this->LocalCommonGenerator, config, lang);
648
0
    cmGeneratorExpressionDAGChecker dagChecker{ this->GeneratorTarget,
649
0
                                                propName, nullptr, nullptr,
650
0
                                                context };
651
0
    std::string evaluatedLinklauncher = cmGeneratorExpression::Evaluate(
652
0
      *launcherProp, context.LG, context.Config, this->GeneratorTarget,
653
0
      &dagChecker, this->GeneratorTarget, context.Language);
654
    // Convert ;-delimited list to single string
655
0
    cmList args{ evaluatedLinklauncher, cmList::EmptyElements::Yes };
656
0
    if (!args.empty()) {
657
0
      args[0] = this->LocalCommonGenerator->ConvertToOutputFormat(
658
0
        args[0], cmOutputConverter::SHELL);
659
0
      for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
660
0
        i = this->LocalCommonGenerator->EscapeForShell(i);
661
0
      }
662
0
      return cmJoin(args, " ");
663
0
    }
664
0
  }
665
0
  return std::string();
666
0
}
667
668
bool cmCommonTargetGenerator::HaveRequiredLanguages(
669
  std::vector<cmSourceFile const*> const& sources,
670
  std::set<std::string>& languagesNeeded) const
671
0
{
672
0
  for (cmSourceFile const* sf : sources) {
673
0
    languagesNeeded.insert(sf->GetLanguage());
674
0
  }
675
676
0
  auto* makefile = this->Makefile;
677
0
  auto* state = makefile->GetState();
678
0
  auto unary = [&state, &makefile](std::string const& lang) -> bool {
679
0
    bool const valid = state->GetLanguageEnabled(lang);
680
0
    if (!valid) {
681
0
      makefile->IssueMessage(
682
0
        MessageType::FATAL_ERROR,
683
0
        cmStrCat("The language ", lang,
684
0
                 " was requested for compilation but was not enabled."
685
0
                 " To enable a language it needs to be specified in a"
686
0
                 " 'project' or 'enable_language' command in the root"
687
0
                 " CMakeLists.txt"));
688
0
    }
689
0
    return valid;
690
0
  };
691
0
  return std::all_of(languagesNeeded.cbegin(), languagesNeeded.cend(), unary);
692
0
}
693
694
void cmCommonTargetGenerator::ComputeRustFlagsForObjects(
695
  std::string& linkCrates, std::string& nativeObjects,
696
  std::vector<std::string> const& objects)
697
0
{
698
0
  std::stringstream rlibsArgs;
699
0
  std::stringstream objectsArgs;
700
0
  auto const processObject = [&](std::string const& obj) {
701
0
    cm::filesystem::path const objPath(obj);
702
0
    if (objPath.extension() == ".rlib") {
703
      // Drop the "lib..." prefix and the ".rs" suffix. The prefix is required
704
      // by Rust on the crate rlib file, but is hidden from the user when using
705
      // the crate from Rust source code, so we drop it to be consistent with
706
      // common usage in Rust.
707
0
      std::string objStem = objPath.stem().string();
708
0
      objStem = objStem.substr(3, objStem.length() - 6);
709
0
      rlibsArgs << " --extern=" << objStem << "="
710
0
                << this->LocalCommonGenerator->ConvertToOutputFormat(
711
0
                     obj, cmOutputConverter::SHELL);
712
0
    } else {
713
0
      objectsArgs << " -Clink-arg="
714
0
                  << this->LocalCommonGenerator->ConvertToOutputFormat(
715
0
                       obj, cmOutputConverter::SHELL);
716
0
    }
717
0
  };
718
0
  for (auto const& obj : objects) {
719
0
    processObject(obj);
720
0
  }
721
0
  linkCrates += rlibsArgs.str();
722
0
  nativeObjects += objectsArgs.str();
723
0
}