Coverage Report

Created: 2026-07-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmMakefileTargetGenerator.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 "cmMakefileTargetGenerator.h"
4
5
#include <algorithm>
6
#include <array>
7
#include <cassert>
8
#include <cstdio>
9
#include <functional>
10
#include <iterator>
11
#include <sstream>
12
#include <unordered_map>
13
#include <unordered_set>
14
#include <utility>
15
16
#include <cm/memory>
17
#include <cm/optional>
18
#include <cm/string_view>
19
#include <cmext/algorithm>
20
#include <cmext/string_view>
21
22
#include "cm_codecvt_Encoding.hxx"
23
24
#include "cmComputeLinkInformation.h"
25
#include "cmCustomCommand.h"
26
#include "cmCustomCommandGenerator.h"
27
#include "cmFileSetMetadata.h"
28
#include "cmGenExContext.h"
29
#include "cmGeneratedFileStream.h"
30
#include "cmGeneratorExpression.h"
31
#include "cmGeneratorFileSet.h"
32
#include "cmGeneratorOptions.h"
33
#include "cmGeneratorTarget.h"
34
#include "cmGlobalUnixMakefileGenerator3.h"
35
#include "cmLinkLineComputer.h" // IWYU pragma: keep
36
#include "cmList.h"
37
#include "cmListFileCache.h"
38
#include "cmLocalCommonGenerator.h"
39
#include "cmLocalGenerator.h"
40
#include "cmLocalUnixMakefileGenerator3.h"
41
#include "cmMakefile.h"
42
#include "cmMakefileExecutableTargetGenerator.h"
43
#include "cmMakefileLibraryTargetGenerator.h"
44
#include "cmMakefileUtilityTargetGenerator.h"
45
#include "cmMessageType.h"
46
#include "cmOutputConverter.h"
47
#include "cmPolicies.h"
48
#include "cmRange.h"
49
#include "cmRulePlaceholderExpander.h"
50
#include "cmScriptGenerator.h"
51
#include "cmSourceFile.h"
52
#include "cmSourceFileLocationKind.h"
53
#include "cmState.h"
54
#include "cmStateDirectory.h"
55
#include "cmStateSnapshot.h"
56
#include "cmStateTypes.h"
57
#include "cmStringAlgorithms.h"
58
#include "cmSystemTools.h"
59
#include "cmTargetTypes.h"
60
#include "cmValue.h"
61
#include "cmake.h"
62
63
cmMakefileTargetGenerator::cmMakefileTargetGenerator(cmGeneratorTarget* target)
64
0
  : cmCommonTargetGenerator(target)
65
0
{
66
0
  this->CustomCommandDriver = OnBuild;
67
0
  this->LocalGenerator =
68
0
    static_cast<cmLocalUnixMakefileGenerator3*>(target->GetLocalGenerator());
69
0
  this->GlobalGenerator = static_cast<cmGlobalUnixMakefileGenerator3*>(
70
0
    this->LocalGenerator->GetGlobalGenerator());
71
0
  cmake* cm = this->GlobalGenerator->GetCMakeInstance();
72
0
  this->NoRuleMessages = false;
73
0
  if (cmValue ruleStatus =
74
0
        cm->GetState()->GetGlobalProperty("RULE_MESSAGES")) {
75
0
    this->NoRuleMessages = ruleStatus.IsOff();
76
0
  }
77
0
  switch (this->GeneratorTarget->GetPolicyStatusCMP0113()) {
78
0
    case cmPolicies::WARN:
79
0
      CM_FALLTHROUGH;
80
0
    case cmPolicies::OLD:
81
0
      this->CMP0113New = false;
82
0
      break;
83
0
    case cmPolicies::NEW:
84
0
      this->CMP0113New = true;
85
0
      break;
86
0
  }
87
0
  this->MacOSXContentGenerator =
88
0
    cm::make_unique<MacOSXContentGeneratorType>(this);
89
0
}
90
91
0
cmMakefileTargetGenerator::~cmMakefileTargetGenerator() = default;
92
93
std::unique_ptr<cmMakefileTargetGenerator> cmMakefileTargetGenerator::New(
94
  cmGeneratorTarget* tgt)
95
0
{
96
0
  std::unique_ptr<cmMakefileTargetGenerator> result;
97
98
0
  switch (tgt->GetType()) {
99
0
    case cm::TargetType::EXECUTABLE:
100
0
      result = cm::make_unique<cmMakefileExecutableTargetGenerator>(tgt);
101
0
      break;
102
0
    case cm::TargetType::STATIC_LIBRARY:
103
0
    case cm::TargetType::SHARED_LIBRARY:
104
0
    case cm::TargetType::MODULE_LIBRARY:
105
0
    case cm::TargetType::OBJECT_LIBRARY:
106
0
      result = cm::make_unique<cmMakefileLibraryTargetGenerator>(tgt);
107
0
      break;
108
0
    case cm::TargetType::INTERFACE_LIBRARY:
109
0
    case cm::TargetType::UTILITY:
110
0
      result = cm::make_unique<cmMakefileUtilityTargetGenerator>(tgt);
111
0
      break;
112
0
    default:
113
0
      return result;
114
      // break; /* unreachable */
115
0
  }
116
0
  return result;
117
0
}
118
119
std::string const& cmMakefileTargetGenerator::GetConfigName() const
120
0
{
121
0
  auto const& configNames = this->LocalGenerator->GetConfigNames();
122
0
  assert(configNames.size() == 1);
123
0
  return configNames.front();
124
0
}
125
126
void cmMakefileTargetGenerator::GetDeviceLinkFlags(
127
  std::string& linkFlags, std::string const& linkLanguage)
128
0
{
129
0
  cmGeneratorTarget::DeviceLinkSetter setter(*this->GetGeneratorTarget());
130
131
0
  std::vector<std::string> linkOpts;
132
0
  this->GeneratorTarget->GetLinkOptions(linkOpts, this->GetConfigName(),
133
0
                                        linkLanguage);
134
0
  this->LocalGenerator->SetLinkScriptShell(
135
0
    this->GlobalGenerator->GetUseLinkScript());
136
  // LINK_OPTIONS are escaped.
137
0
  this->LocalGenerator->AppendCompileOptions(linkFlags, linkOpts);
138
0
  this->LocalGenerator->SetLinkScriptShell(false);
139
0
}
140
141
void cmMakefileTargetGenerator::GetTargetLinkFlags(
142
  std::string& flags, std::string const& linkLanguage)
143
0
{
144
0
  this->LocalGenerator->AddTargetPropertyLinkFlags(
145
0
    flags, this->GeneratorTarget, this->GetConfigName());
146
147
0
  std::vector<std::string> opts;
148
0
  this->GeneratorTarget->GetLinkOptions(opts, this->GetConfigName(),
149
0
                                        linkLanguage);
150
0
  this->LocalGenerator->SetLinkScriptShell(
151
0
    this->GlobalGenerator->GetUseLinkScript());
152
  // LINK_OPTIONS are escaped.
153
0
  this->LocalGenerator->AppendCompileOptions(flags, opts);
154
0
  this->LocalGenerator->SetLinkScriptShell(false);
155
156
0
  this->LocalGenerator->AppendLinkerTypeFlags(
157
0
    flags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
158
0
  this->LocalGenerator->AppendPositionIndependentLinkerFlags(
159
0
    flags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
160
0
  this->LocalGenerator->AppendWarningAsErrorLinkerFlags(
161
0
    flags, this->GeneratorTarget, linkLanguage);
162
0
  this->LocalGenerator->AppendDependencyInfoLinkerFlags(
163
0
    flags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
164
0
}
165
166
void cmMakefileTargetGenerator::CreateRuleFile()
167
0
{
168
  // Create a directory for this target.
169
0
  this->TargetBuildDirectoryFull =
170
0
    this->GeneratorTarget->GetSupportDirectory();
171
0
  this->TargetBuildDirectory = this->LocalGenerator->MaybeRelativeToCurBinDir(
172
0
    this->TargetBuildDirectoryFull);
173
0
  cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull);
174
175
  // Construct the rule file name.
176
0
  this->BuildFileName = cmStrCat(this->TargetBuildDirectory, "/build.make");
177
0
  this->BuildFileNameFull =
178
0
    cmStrCat(this->TargetBuildDirectoryFull, "/build.make");
179
180
  // Construct the rule file name.
181
0
  this->ProgressFileNameFull =
182
0
    cmStrCat(this->TargetBuildDirectoryFull, "/progress.make");
183
184
  // reset the progress count
185
0
  this->NumberOfProgressActions = 0;
186
187
  // Open the rule file.  This should be copy-if-different because the
188
  // rules may depend on this file itself.
189
0
  this->BuildFileStream = cm::make_unique<cmGeneratedFileStream>(
190
0
    this->BuildFileNameFull, false,
191
0
    this->GlobalGenerator->GetMakefileEncoding());
192
0
  if (!this->BuildFileStream) {
193
0
    return;
194
0
  }
195
0
  this->BuildFileStream->SetCopyIfDifferent(true);
196
0
  this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
197
0
  if (this->GlobalGenerator->AllowDeleteOnError()) {
198
0
    std::vector<std::string> no_depends;
199
0
    std::vector<std::string> no_commands;
200
0
    this->LocalGenerator->WriteMakeRule(
201
0
      *this->BuildFileStream, "Delete rule output on recipe failure.",
202
0
      ".DELETE_ON_ERROR", no_depends, no_commands, false);
203
0
  }
204
0
  this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
205
0
}
206
207
void cmMakefileTargetGenerator::WriteTargetBuildRules()
208
0
{
209
0
  cm::GenEx::Context context(this->LocalGenerator, this->GetConfigName());
210
0
  this->GeneratorTarget->CheckCxxModuleStatus(this->GetConfigName());
211
212
  // -- Write the custom commands for this target
213
214
  // Evaluates generator expressions and expands prop_value
215
0
  auto evaluatedFiles = [this](std::string const& prop_value) -> cmList {
216
0
    cmList files{ cmGeneratorExpression::Evaluate(
217
0
      prop_value, this->LocalGenerator, this->GetConfigName(),
218
0
      this->GeneratorTarget) };
219
0
    return files;
220
0
  };
221
222
  // Look for additional files registered for cleaning in this directory.
223
0
  if (cmValue prop_value =
224
0
        this->Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES")) {
225
0
    auto const files = evaluatedFiles(*prop_value);
226
0
    this->CleanFiles.insert(files.begin(), files.end());
227
0
  }
228
229
  // Look for additional files registered for cleaning in this target.
230
0
  if (cmValue prop_value =
231
0
        this->GeneratorTarget->GetProperty("ADDITIONAL_CLEAN_FILES")) {
232
0
    auto const files = evaluatedFiles(*prop_value);
233
    // For relative path support
234
0
    std::string const& binaryDir =
235
0
      this->LocalGenerator->GetCurrentBinaryDirectory();
236
0
    for (std::string const& cfl : files) {
237
0
      this->CleanFiles.insert(cmSystemTools::CollapseFullPath(cfl, binaryDir));
238
0
    }
239
0
  }
240
241
  // Look for ISPC extra object files generated by this target
242
0
  auto const ispcAdditionalObjs =
243
0
    this->GeneratorTarget->GetGeneratedISPCObjects(this->GetConfigName());
244
0
  for (auto const& ispcObj : ispcAdditionalObjs) {
245
0
    this->CleanFiles.insert(
246
0
      this->LocalGenerator->MaybeRelativeToCurBinDir(ispcObj.second));
247
0
  }
248
249
  // add custom commands to the clean rules?
250
0
  bool const clean = this->Makefile->GetProperty("CLEAN_NO_CUSTOM").IsOff();
251
252
  // First generate the object rule files.  Save a list of all object
253
  // files for this target.
254
0
  std::vector<cmSourceFile const*> customCommands;
255
0
  this->GeneratorTarget->GetCustomCommands(customCommands,
256
0
                                           this->GetConfigName());
257
0
  std::vector<std::string> codegen_depends;
258
0
  codegen_depends.reserve(customCommands.size());
259
0
  for (cmSourceFile const* sf : customCommands) {
260
0
    if (this->CMP0113New &&
261
0
        !this->LocalGenerator->GetCommandsVisited(this->GeneratorTarget)
262
0
           .insert(sf)
263
0
           .second) {
264
0
      continue;
265
0
    }
266
0
    cmCustomCommandGenerator ccg(*sf->GetCustomCommand(),
267
0
                                 this->GetConfigName(), this->LocalGenerator);
268
0
    this->GenerateCustomRuleFile(ccg);
269
0
    if (clean) {
270
0
      std::vector<std::string> const& outputs = ccg.GetOutputs();
271
0
      for (std::string const& output : outputs) {
272
0
        this->CleanFiles.insert(
273
0
          this->LocalGenerator->MaybeRelativeToCurBinDir(output));
274
0
      }
275
0
      std::vector<std::string> const& byproducts = ccg.GetByproducts();
276
0
      for (std::string const& byproduct : byproducts) {
277
0
        this->CleanFiles.insert(
278
0
          this->LocalGenerator->MaybeRelativeToCurBinDir(byproduct));
279
0
      }
280
0
    }
281
282
0
    if (ccg.GetCC().GetCodegen()) {
283
0
      std::string const& output = ccg.GetOutputs().front();
284
285
      // We always attach the actual commands to the first output.
286
0
      codegen_depends.emplace_back(output);
287
0
    }
288
0
  }
289
290
  // Some make tools need a special dependency for an empty rule.
291
0
  if (codegen_depends.empty()) {
292
0
    std::string hack = this->GlobalGenerator->GetEmptyRuleHackDepends();
293
0
    if (!hack.empty()) {
294
0
      codegen_depends.emplace_back(std::move(hack));
295
0
    }
296
0
  }
297
298
  // Construct the codegen target.
299
0
  {
300
0
    std::string const codegenTarget = cmStrCat(
301
0
      this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget),
302
0
      "/codegen");
303
304
    // Write the rule.
305
0
    this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
306
0
                                        codegenTarget, codegen_depends, {},
307
0
                                        true);
308
0
  }
309
310
  // Add byproducts from build events to the clean rules
311
0
  if (clean) {
312
0
    std::vector<cmCustomCommand> buildEventCommands =
313
0
      this->GeneratorTarget->GetPreBuildCommands();
314
315
0
    cm::append(buildEventCommands,
316
0
               this->GeneratorTarget->GetPreLinkCommands());
317
0
    cm::append(buildEventCommands,
318
0
               this->GeneratorTarget->GetPostBuildCommands());
319
320
0
    for (auto const& be : buildEventCommands) {
321
0
      cmCustomCommandGenerator beg(be, this->GetConfigName(),
322
0
                                   this->LocalGenerator);
323
0
      std::vector<std::string> const& byproducts = beg.GetByproducts();
324
0
      for (std::string const& byproduct : byproducts) {
325
0
        this->CleanFiles.insert(
326
0
          this->LocalGenerator->MaybeRelativeToCurBinDir(byproduct));
327
0
      }
328
0
    }
329
0
  }
330
0
  std::vector<cmSourceFile const*> headerSources;
331
0
  this->GeneratorTarget->GetHeaderSources(headerSources,
332
0
                                          this->GetConfigName());
333
0
  this->OSXBundleGenerator->GenerateMacOSXContentStatements(
334
0
    headerSources, this->MacOSXContentGenerator.get(), this->GetConfigName());
335
0
  std::vector<cmSourceFile const*> extraSources;
336
0
  this->GeneratorTarget->GetExtraSources(extraSources, this->GetConfigName());
337
0
  this->OSXBundleGenerator->GenerateMacOSXContentStatements(
338
0
    extraSources, this->MacOSXContentGenerator.get(), this->GetConfigName());
339
0
  cmValue pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
340
0
  std::vector<cmSourceFile const*> externalObjects;
341
0
  this->GeneratorTarget->GetExternalObjects(externalObjects,
342
0
                                            this->GetConfigName());
343
0
  for (cmSourceFile const* sf : externalObjects) {
344
0
    auto const& objectFileName = sf->GetFullPath();
345
0
    if (!cmHasSuffix(objectFileName, pchExtension)) {
346
0
      this->ExternalObjects.push_back(objectFileName);
347
0
    }
348
0
  }
349
350
0
  std::vector<cmSourceFile const*> objectSources;
351
0
  this->GeneratorTarget->GetObjectSources(objectSources,
352
0
                                          this->GetConfigName());
353
354
  // validate that all languages requested are enabled.
355
0
  std::set<std::string> requiredLangs;
356
0
  if (this->HaveRequiredLanguages(objectSources, requiredLangs)) {
357
0
    for (cmSourceFile const* sf : objectSources) {
358
      // Generate this object file's rule file.
359
0
      this->WriteObjectRuleFiles(*sf);
360
0
    }
361
0
  }
362
363
0
  for (cmSourceFile const* sf : objectSources) {
364
0
    cmGeneratorFileSet const* fileSet =
365
0
      this->GeneratorTarget->GetFileSetForSource(this->GetConfigName(), sf);
366
0
    if (fileSet && fileSet->GetType() == cm::FileSetMetadata::CXX_MODULES &&
367
0
        sf->GetLanguage() != "CXX"_s) {
368
0
      this->Makefile->IssueMessage(
369
0
        MessageType::FATAL_ERROR,
370
0
        cmStrCat("Target \"", this->GeneratorTarget->GetName(),
371
0
                 "\" contains the source\n  ", sf->GetFullPath(),
372
0
                 "\nin a file set of type \"",
373
0
                 cm::FileSetMetadata::CXX_MODULES,
374
0
                 R"(" but the source is not classified as a "CXX" source.)"));
375
0
    }
376
0
  }
377
0
}
378
379
void cmMakefileTargetGenerator::WriteCommonCodeRules()
380
0
{
381
0
  char const* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")
382
0
                        ? "$(CMAKE_BINARY_DIR)/"
383
0
                        : "");
384
385
  // Include the dependencies for the target.
386
0
  std::string dependFileNameFull =
387
0
    cmStrCat(this->TargetBuildDirectoryFull, "/depend.make");
388
0
  *this->BuildFileStream
389
0
    << "# Include any dependencies generated for this target.\n"
390
0
    << this->GlobalGenerator->IncludeDirective << " " << root
391
0
    << cmSystemTools::ConvertToOutputPath(
392
0
         this->LocalGenerator->MaybeRelativeToTopBinDir(dependFileNameFull))
393
0
    << "\n";
394
395
  // Scan any custom commands to check if DEPFILE option is specified
396
0
  bool ccGenerateDeps = false;
397
0
  std::vector<cmSourceFile const*> customCommands;
398
0
  this->GeneratorTarget->GetCustomCommands(customCommands,
399
0
                                           this->GetConfigName());
400
0
  for (cmSourceFile const* sf : customCommands) {
401
0
    if (!sf->GetCustomCommand()->GetDepfile().empty()) {
402
0
      ccGenerateDeps = true;
403
0
      break;
404
0
    }
405
0
  }
406
407
0
  std::string depsUseCompiler = "CMAKE_DEPENDS_USE_COMPILER";
408
0
  bool compilerGenerateDeps =
409
0
    this->GlobalGenerator->SupportsCompilerDependencies() &&
410
0
    (!this->Makefile->IsDefinitionSet(depsUseCompiler) ||
411
0
     this->Makefile->IsOn(depsUseCompiler));
412
0
  bool linkerGenerateDeps =
413
0
    this->GeneratorTarget->HasLinkDependencyFile(this->GetConfigName());
414
415
0
  if (compilerGenerateDeps || linkerGenerateDeps || ccGenerateDeps) {
416
0
    std::string const compilerDependFile =
417
0
      cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.make");
418
0
    *this->BuildFileStream << "# Include any dependencies generated by the "
419
0
                              "compiler for this target.\n"
420
0
                           << this->GlobalGenerator->IncludeDirective << " "
421
0
                           << root
422
0
                           << cmSystemTools::ConvertToOutputPath(
423
0
                                this->LocalGenerator->MaybeRelativeToTopBinDir(
424
0
                                  compilerDependFile))
425
0
                           << "\n\n";
426
427
    // Write an empty dependency file.
428
0
    cmGeneratedFileStream depFileStream(
429
0
      compilerDependFile, false, this->GlobalGenerator->GetMakefileEncoding());
430
0
    depFileStream << "# Empty compiler generated dependencies file for "
431
0
                  << this->GeneratorTarget->GetName() << ".\n"
432
0
                  << "# This may be replaced when dependencies are built.\n";
433
    // remove internal dependency file
434
0
    cmSystemTools::RemoveFile(
435
0
      cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.internal"));
436
437
0
    std::string compilerDependTimestamp =
438
0
      cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts");
439
0
    if (!cmSystemTools::FileExists(compilerDependTimestamp)) {
440
      // Write a dependency timestamp file.
441
0
      cmGeneratedFileStream timestampFileStream(
442
0
        compilerDependTimestamp, false,
443
0
        this->GlobalGenerator->GetMakefileEncoding());
444
0
      timestampFileStream
445
0
        << "# CMAKE generated file: DO NOT EDIT!\n"
446
0
        << "# Timestamp file for compiler generated dependencies "
447
0
           "management for "
448
0
        << this->GeneratorTarget->GetName() << ".\n";
449
0
    }
450
0
  }
451
452
0
  if (compilerGenerateDeps) {
453
    // deactivate no longer needed legacy dependency files
454
    // Write an empty dependency file.
455
0
    cmGeneratedFileStream legacyDepFileStream(
456
0
      dependFileNameFull, false, this->GlobalGenerator->GetMakefileEncoding());
457
0
    legacyDepFileStream
458
0
      << "# Empty dependencies file for " << this->GeneratorTarget->GetName()
459
0
      << ".\n"
460
0
      << "# This may be replaced when dependencies are built.\n";
461
    // remove internal dependency file
462
0
    cmSystemTools::RemoveFile(
463
0
      cmStrCat(this->TargetBuildDirectoryFull, "/depend.internal"));
464
0
  } else {
465
    // make sure the depend file exists
466
0
    if (!cmSystemTools::FileExists(dependFileNameFull)) {
467
      // Write an empty dependency file.
468
0
      cmGeneratedFileStream depFileStream(
469
0
        dependFileNameFull, false,
470
0
        this->GlobalGenerator->GetMakefileEncoding());
471
0
      depFileStream << "# Empty dependencies file for "
472
0
                    << this->GeneratorTarget->GetName() << ".\n"
473
0
                    << "# This may be replaced when dependencies are built.\n";
474
0
    }
475
0
  }
476
477
0
  if (!this->NoRuleMessages) {
478
    // Include the progress variables for the target.
479
0
    *this->BuildFileStream
480
0
      << "# Include the progress variables for this target.\n"
481
0
      << this->GlobalGenerator->IncludeDirective << " " << root
482
0
      << cmSystemTools::ConvertToOutputPath(
483
0
           this->LocalGenerator->MaybeRelativeToTopBinDir(
484
0
             this->ProgressFileNameFull))
485
0
      << "\n\n";
486
0
  }
487
488
  // Open the flags file.  This should be copy-if-different because the
489
  // rules may depend on this file itself.
490
0
  this->FlagFileNameFull =
491
0
    cmStrCat(this->TargetBuildDirectoryFull, "/flags.make");
492
0
  this->FlagFileStream = cm::make_unique<cmGeneratedFileStream>(
493
0
    this->FlagFileNameFull, false,
494
0
    this->GlobalGenerator->GetMakefileEncoding());
495
0
  if (!this->FlagFileStream) {
496
0
    return;
497
0
  }
498
0
  this->FlagFileStream->SetCopyIfDifferent(true);
499
0
  this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
500
501
  // Include the flags for the target.
502
0
  *this->BuildFileStream
503
0
    << "# Include the compile flags for this target's objects.\n"
504
0
    << this->GlobalGenerator->IncludeDirective << " " << root
505
0
    << cmSystemTools::ConvertToOutputPath(
506
0
         this->LocalGenerator->MaybeRelativeToTopBinDir(
507
0
           this->FlagFileNameFull))
508
0
    << "\n\n";
509
0
}
510
511
void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
512
0
{
513
  // write language flags for target
514
0
  std::set<std::string> languages;
515
0
  this->GeneratorTarget->GetLanguages(
516
0
    languages, this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
517
  // put the compiler in the rules.make file so that if it changes
518
  // things rebuild
519
0
  for (std::string const& language : languages) {
520
0
    std::string compiler = cmStrCat("CMAKE_", language, "_COMPILER");
521
0
    *this->FlagFileStream << "# compile " << language << " with "
522
0
                          << this->Makefile->GetSafeDefinition(compiler)
523
0
                          << "\n";
524
0
  }
525
526
0
  bool const escapeOctothorpe = this->GlobalGenerator->CanEscapeOctothorpe();
527
528
0
  for (std::string const& language : languages) {
529
0
    std::string defines = this->GetDefines(language, this->GetConfigName());
530
0
    std::string includes = this->GetIncludes(language, this->GetConfigName());
531
0
    if (escapeOctothorpe) {
532
      // Escape comment characters so they do not terminate assignment.
533
0
      cmSystemTools::ReplaceString(defines, "#", "\\#");
534
0
      cmSystemTools::ReplaceString(includes, "#", "\\#");
535
0
    }
536
0
    *this->FlagFileStream << language << "_DEFINES = " << defines << "\n\n";
537
0
    *this->FlagFileStream << language << "_INCLUDES = " << includes << "\n\n";
538
539
0
    std::vector<std::string> architectures =
540
0
      this->GeneratorTarget->GetAppleArchs(this->GetConfigName(), language);
541
0
    architectures.emplace_back();
542
543
0
    for (std::string const& arch : architectures) {
544
0
      std::string flags =
545
0
        this->GetFlags(language, this->GetConfigName(), arch);
546
0
      if (escapeOctothorpe) {
547
0
        cmSystemTools::ReplaceString(flags, "#", "\\#");
548
0
      }
549
0
      *this->FlagFileStream << language << "_FLAGS" << arch << " = " << flags
550
0
                            << "\n\n";
551
0
    }
552
0
  }
553
0
}
554
555
void cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()(
556
  cmSourceFile const& source, char const* pkgloc, std::string const& config)
557
0
{
558
  // Skip OS X content when not building a Framework or Bundle.
559
0
  if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
560
0
    return;
561
0
  }
562
563
0
  std::string macdir =
564
0
    this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc,
565
0
                                                                    config);
566
567
  // Get the input file location.
568
0
  std::string const& input = source.GetFullPath();
569
570
  // Get the output file location.
571
0
  std::string output =
572
0
    cmStrCat(macdir, '/', cmSystemTools::GetFilenameName(input));
573
0
  this->Generator->CleanFiles.insert(
574
0
    this->Generator->LocalGenerator->MaybeRelativeToCurBinDir(output));
575
0
  output = this->Generator->LocalGenerator->MaybeRelativeToTopBinDir(output);
576
577
  // Create a rule to copy the content into the bundle.
578
0
  std::vector<std::string> depends;
579
0
  std::vector<std::string> commands;
580
0
  depends.push_back(input);
581
0
  std::string copyEcho = cmStrCat("Copying OS X content ", output);
582
0
  this->Generator->LocalGenerator->AppendEcho(
583
0
    commands, copyEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
584
0
  std::string copyCommand =
585
0
    cmStrCat(cmSystemTools::FileIsDirectory(input)
586
0
               ? "$(CMAKE_COMMAND) -E copy_directory "
587
0
               : "$(CMAKE_COMMAND) -E copy ",
588
0
             this->Generator->LocalGenerator->ConvertToOutputFormat(
589
0
               input, cmOutputConverter::SHELL),
590
0
             ' ',
591
0
             this->Generator->LocalGenerator->ConvertToOutputFormat(
592
0
               output, cmOutputConverter::SHELL));
593
0
  commands.push_back(std::move(copyCommand));
594
0
  this->Generator->LocalGenerator->WriteMakeRule(
595
0
    *this->Generator->BuildFileStream, nullptr, output, depends, commands,
596
0
    false);
597
0
  this->Generator->ExtraFiles.insert(output);
598
0
}
599
600
void cmMakefileTargetGenerator::WriteObjectRuleFiles(
601
  cmSourceFile const& source)
602
0
{
603
  // Identify the language of the source file.
604
0
  std::string const& lang = source.GetLanguage();
605
0
  if (lang.empty()) {
606
    // don't know anything about this file so skip it
607
0
    return;
608
0
  }
609
610
  // Use compiler to generate dependencies, if supported.
611
0
  bool const compilerGenerateDeps =
612
0
    this->GlobalGenerator->SupportsCompilerDependencies() &&
613
0
    this->Makefile
614
0
      ->GetDefinition(cmStrCat("CMAKE_", lang, "_DEPENDS_USE_COMPILER"))
615
0
      .IsOn();
616
0
  auto const scanner = compilerGenerateDeps ? cmDependencyScannerKind::Compiler
617
0
                                            : cmDependencyScannerKind::CMake;
618
619
  // Get the full path name of the object file.
620
0
  std::string const& objectName =
621
0
    this->GeneratorTarget->GetObjectName(&source);
622
0
  std::string const obj =
623
0
    cmStrCat(this->TargetBuildDirectory, '/', objectName);
624
625
  // Avoid generating duplicate rules.
626
0
  if (this->ObjectFiles.find(obj) == this->ObjectFiles.end()) {
627
0
    this->ObjectFiles.insert(obj);
628
0
  } else {
629
0
    std::ostringstream err;
630
0
    err << "Warning: Source file \"" << source.GetFullPath()
631
0
        << "\" is listed multiple times for target \""
632
0
        << this->GeneratorTarget->GetName() << "\".";
633
0
    cmSystemTools::Message(err.str(), "Warning");
634
0
    return;
635
0
  }
636
637
  // Create the directory containing the object file.  This may be a
638
  // subdirectory under the target's directory.
639
0
  {
640
0
    std::string const dir = cmSystemTools::GetFilenamePath(obj);
641
0
    cmSystemTools::MakeDirectory(this->LocalGenerator->ConvertToFullPath(dir));
642
0
  }
643
644
  // Save this in the target's list of object files.
645
0
  this->Objects.push_back(obj);
646
0
  this->CleanFiles.insert(obj);
647
648
0
  std::vector<std::string> depends;
649
650
  // The object file should be checked for dependency integrity.
651
0
  std::string objFullPath =
652
0
    cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/', obj);
653
0
  std::string const srcFullPath = source.GetFullPath();
654
0
  this->LocalGenerator->AddImplicitDepends(this->GeneratorTarget, lang,
655
0
                                           objFullPath, srcFullPath, scanner);
656
657
0
  this->LocalGenerator->AppendRuleDepend(depends,
658
0
                                         this->FlagFileNameFull.c_str());
659
0
  this->LocalGenerator->AppendRuleDepends(depends,
660
0
                                          this->FlagFileDepends[lang]);
661
662
  // generate the depend scanning rule
663
0
  this->WriteObjectDependRules(source, depends);
664
665
0
  std::string const config = this->GetConfigName();
666
0
  std::string const configUpper = cmSystemTools::UpperCase(config);
667
668
  // lookup for the associated file set, if any.
669
0
  auto const* fileSet =
670
0
    this->GeneratorTarget->GetFileSetForSource(config, &source);
671
672
  // Add precompile headers dependencies
673
0
  std::vector<std::string> pchArchs =
674
0
    this->GeneratorTarget->GetPchArchs(config, lang);
675
676
0
  std::string filterArch;
677
0
  std::unordered_map<std::string, std::string> pchSources;
678
0
  for (std::string const& arch : pchArchs) {
679
0
    std::string const pchSource =
680
0
      this->GeneratorTarget->GetPchSource(config, lang, arch);
681
0
    if (pchSource == source.GetFullPath()) {
682
0
      filterArch = arch;
683
0
    }
684
0
    if (!pchSource.empty()) {
685
0
      pchSources.insert(std::make_pair(pchSource, arch));
686
0
    }
687
0
  }
688
689
0
  if (!pchSources.empty() &&
690
0
      !((fileSet && fileSet->GetProperty("SKIP_PRECOMPILE_HEADERS")) ||
691
0
        source.GetProperty("SKIP_PRECOMPILE_HEADERS"))) {
692
0
    for (std::string const& arch : pchArchs) {
693
0
      std::string const& pchHeader =
694
0
        this->GeneratorTarget->GetPchHeader(config, lang, arch);
695
0
      depends.push_back(pchHeader);
696
0
      if (pchSources.find(source.GetFullPath()) == pchSources.end()) {
697
0
        depends.push_back(
698
0
          this->GeneratorTarget->GetPchFile(config, lang, arch));
699
0
      }
700
0
      this->LocalGenerator->AddImplicitDepends(
701
0
        this->GeneratorTarget, lang, objFullPath, pchHeader, scanner);
702
0
    }
703
0
  }
704
705
0
  if (lang != "ISPC") {
706
0
    auto const& headers =
707
0
      this->GeneratorTarget->GetGeneratedISPCHeaders(config);
708
0
    if (!headers.empty()) {
709
0
      depends.insert(depends.end(), headers.begin(), headers.end());
710
0
    }
711
0
  }
712
713
0
  std::string relativeObj =
714
0
    cmStrCat(this->LocalGenerator->GetHomeRelativeOutputPath(), obj);
715
  // Write the build rule.
716
717
  // Build the set of compiler flags.
718
0
  std::string flags;
719
720
  // Explicitly add the explicit language flag before any other flag
721
  // so user flags can override it.
722
0
  this->GeneratorTarget->AddExplicitLanguageFlags(flags, source);
723
724
  // Add language-specific flags.
725
0
  std::string const langFlags =
726
0
    cmStrCat("$(", lang, "_FLAGS", filterArch, ')');
727
0
  this->LocalGenerator->AppendFlags(flags, langFlags);
728
729
0
  cmGeneratorExpressionInterpreter genexInterpreter(
730
0
    this->LocalGenerator, config, this->GeneratorTarget, lang);
731
732
  // Add Fortran format flags.
733
0
  if (lang == "Fortran") {
734
0
    this->AppendFortranFormatFlags(flags, source);
735
0
    this->AppendFortranPreprocessFlags(flags, source);
736
0
  }
737
738
0
  std::string ispcHeaderRelative;
739
0
  std::string ispcHeaderForShell;
740
0
  if (lang == "ISPC") {
741
0
    std::string ispcSource =
742
0
      cmSystemTools::GetFilenameWithoutLastExtension(objectName);
743
0
    ispcSource = cmSystemTools::GetFilenameWithoutLastExtension(ispcSource);
744
745
0
    cmValue const ispcSuffixProp =
746
0
      this->GeneratorTarget->GetProperty("ISPC_HEADER_SUFFIX");
747
0
    assert(ispcSuffixProp);
748
749
0
    std::string directory = this->GeneratorTarget->GetObjectDirectory(config);
750
0
    if (cmValue prop =
751
0
          this->GeneratorTarget->GetProperty("ISPC_HEADER_DIRECTORY")) {
752
0
      directory =
753
0
        cmStrCat(this->LocalGenerator->GetBinaryDirectory(), '/', *prop);
754
0
    }
755
0
    ispcHeaderRelative = cmStrCat(directory, '/', ispcSource, *ispcSuffixProp);
756
0
    ispcHeaderForShell = this->LocalGenerator->ConvertToOutputFormat(
757
0
      ispcHeaderRelative, cmOutputConverter::SHELL);
758
0
  }
759
760
  // Add flags from source file properties.
761
0
  std::string const COMPILE_FLAGS("COMPILE_FLAGS");
762
0
  if (cmValue cflags = source.GetProperty(COMPILE_FLAGS)) {
763
0
    std::string const& evaluatedFlags =
764
0
      genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS);
765
0
    this->LocalGenerator->AppendFlags(flags, evaluatedFlags);
766
0
    *this->FlagFileStream << "# Custom flags: " << relativeObj
767
0
                          << "_FLAGS = " << evaluatedFlags << "\n"
768
0
                          << "\n";
769
0
  }
770
771
0
  std::string const COMPILE_OPTIONS("COMPILE_OPTIONS");
772
0
  if (cmValue coptions = source.GetProperty(COMPILE_OPTIONS)) {
773
0
    std::string const& evaluatedOptions =
774
0
      genexInterpreter.Evaluate(*coptions, COMPILE_OPTIONS);
775
0
    this->LocalGenerator->AppendCompileOptions(flags, evaluatedOptions);
776
0
    *this->FlagFileStream << "# Custom options: " << relativeObj
777
0
                          << "_OPTIONS = " << evaluatedOptions << "\n"
778
0
                          << "\n";
779
0
  }
780
781
  // Add flags from file set properties.
782
0
  if (fileSet) {
783
0
    auto options = fileSet->BelongsTo(this->GeneratorTarget)
784
0
      ? fileSet->GetCompileOptions(config, lang)
785
0
      : fileSet->GetInterfaceCompileOptions(config, lang);
786
0
    if (!options.empty()) {
787
0
      this->LocalGenerator->AppendCompileOptions(flags,
788
0
                                                 cm::remove_BT(options));
789
0
      *this->FlagFileStream << "# Custom options (File Set '"
790
0
                            << fileSet->GetName() << "'): " << relativeObj
791
0
                            << "_OPTIONS = " << cmList::to_string(options)
792
0
                            << "\n"
793
0
                            << "\n";
794
0
    }
795
0
  }
796
797
  // Add precompile headers compile options.
798
0
  if (!pchSources.empty() &&
799
0
      !((fileSet && fileSet->GetProperty("SKIP_PRECOMPILE_HEADERS")) ||
800
0
        source.GetProperty("SKIP_PRECOMPILE_HEADERS"))) {
801
0
    std::string pchOptions;
802
0
    auto const pchIt = pchSources.find(source.GetFullPath());
803
0
    if (pchIt != pchSources.end()) {
804
0
      pchOptions = this->GeneratorTarget->GetPchCreateCompileOptions(
805
0
        config, lang, pchIt->second);
806
0
    } else {
807
0
      pchOptions =
808
0
        this->GeneratorTarget->GetPchUseCompileOptions(config, lang);
809
0
    }
810
811
0
    std::string const& evaluatedFlags =
812
0
      genexInterpreter.Evaluate(pchOptions, COMPILE_OPTIONS);
813
814
0
    this->LocalGenerator->AppendCompileOptions(flags, evaluatedFlags);
815
0
    *this->FlagFileStream << "# PCH options: " << relativeObj
816
0
                          << "_OPTIONS = " << evaluatedFlags << "\n"
817
0
                          << "\n";
818
0
  }
819
820
  // Add include directories from file set properties.
821
0
  std::vector<std::string> includes;
822
823
0
  if (fileSet) {
824
0
    auto fsIncludes = fileSet->BelongsTo(this->GeneratorTarget)
825
0
      ? fileSet->GetIncludeDirectories(config, lang)
826
0
      : fileSet->GetInterfaceIncludeDirectories(config, lang);
827
0
    if (!fsIncludes.empty()) {
828
0
      this->LocalGenerator->AppendIncludeDirectories(
829
0
        includes, cm::remove_BT(fsIncludes), source);
830
0
      *this->FlagFileStream
831
0
        << "# Custom include directories (File Set '" << fileSet->GetName()
832
0
        << "'): " << relativeObj
833
0
        << "_INCLUDE_DIRECTORIES = " << cmList::to_string(fsIncludes) << "\n"
834
0
        << "\n";
835
0
    }
836
0
  }
837
838
  // Add include directories from source file properties.
839
0
  std::string const INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
840
0
  if (cmValue cincludes = source.GetProperty(INCLUDE_DIRECTORIES)) {
841
0
    std::string const& evaluatedIncludes =
842
0
      genexInterpreter.Evaluate(*cincludes, INCLUDE_DIRECTORIES);
843
0
    this->LocalGenerator->AppendIncludeDirectories(includes, evaluatedIncludes,
844
0
                                                   source);
845
0
    *this->FlagFileStream << "# Custom include directories: " << relativeObj
846
0
                          << "_INCLUDE_DIRECTORIES = " << evaluatedIncludes
847
0
                          << "\n"
848
0
                          << "\n";
849
0
  }
850
851
  // Add language-specific defines.
852
0
  std::set<std::string> defines;
853
854
  // Add source-specific preprocessor definitions.
855
0
  std::string const COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
856
0
  if (cmValue compile_defs = source.GetProperty(COMPILE_DEFINITIONS)) {
857
0
    std::string const& evaluatedDefs =
858
0
      genexInterpreter.Evaluate(*compile_defs, COMPILE_DEFINITIONS);
859
0
    this->LocalGenerator->AppendDefines(defines, evaluatedDefs);
860
0
    *this->FlagFileStream << "# Custom defines: " << relativeObj
861
0
                          << "_DEFINES = " << evaluatedDefs << "\n"
862
0
                          << "\n";
863
0
  }
864
0
  std::string const defPropName =
865
0
    cmStrCat("COMPILE_DEFINITIONS_", configUpper);
866
0
  if (cmValue config_compile_defs = source.GetProperty(defPropName)) {
867
0
    std::string const& evaluatedDefs =
868
0
      genexInterpreter.Evaluate(*config_compile_defs, COMPILE_DEFINITIONS);
869
0
    this->LocalGenerator->AppendDefines(defines, evaluatedDefs);
870
0
    *this->FlagFileStream << "# Custom defines: " << relativeObj << "_DEFINES_"
871
0
                          << configUpper << " = " << evaluatedDefs << "\n"
872
0
                          << "\n";
873
0
  }
874
875
  // Add file set preprocessor definitions
876
0
  if (fileSet) {
877
0
    auto fsDefines = fileSet->BelongsTo(this->GeneratorTarget)
878
0
      ? fileSet->GetCompileDefinitions(config, lang)
879
0
      : fileSet->GetInterfaceCompileDefinitions(config, lang);
880
0
    if (!fsDefines.empty()) {
881
0
      this->LocalGenerator->AppendDefines(defines, fsDefines);
882
0
      *this->FlagFileStream << "# Custom defines (File Set '"
883
0
                            << fileSet->GetName() << "'): " << relativeObj
884
0
                            << "_DEFINES = " << cmList::to_string(fsDefines)
885
0
                            << "\n"
886
0
                            << "\n";
887
0
    }
888
0
  }
889
890
  // Get the output paths for source and object files.
891
0
  std::string const sourceFile = this->LocalGenerator->ConvertToOutputFormat(
892
0
    source.GetFullPath(), cmOutputConverter::SHELL);
893
894
  // Construct the build message.
895
0
  std::vector<std::string> no_depends;
896
0
  std::vector<std::string> commands;
897
898
  // add in a progress call if needed
899
0
  this->NumberOfProgressActions++;
900
901
0
  if (!this->NoRuleMessages) {
902
0
    cmLocalUnixMakefileGenerator3::EchoProgress progress;
903
0
    this->MakeEchoProgress(progress);
904
0
    std::string buildEcho =
905
0
      cmStrCat("Building ", lang, " object ", relativeObj);
906
0
    this->LocalGenerator->AppendEcho(commands, buildEcho,
907
0
                                     cmLocalUnixMakefileGenerator3::EchoBuild,
908
0
                                     &progress);
909
0
  }
910
911
0
  std::string targetOutPathReal;
912
0
  std::string targetOutPathPDB;
913
0
  std::string targetOutPathCompilePDB;
914
0
  {
915
0
    std::string targetFullPathReal;
916
0
    std::string targetFullPathPDB;
917
0
    std::string targetFullPathCompilePDB =
918
0
      this->ComputeTargetCompilePDB(this->GetConfigName());
919
0
    if (this->GeneratorTarget->GetType() == cm::TargetType::EXECUTABLE ||
920
0
        this->GeneratorTarget->GetType() == cm::TargetType::STATIC_LIBRARY ||
921
0
        this->GeneratorTarget->GetType() == cm::TargetType::SHARED_LIBRARY ||
922
0
        this->GeneratorTarget->GetType() == cm::TargetType::MODULE_LIBRARY) {
923
0
      targetFullPathReal = this->GeneratorTarget->GetFullPath(
924
0
        this->GetConfigName(), cmStateEnums::RuntimeBinaryArtifact, true);
925
0
      targetFullPathPDB = cmStrCat(
926
0
        this->GeneratorTarget->GetPDBDirectory(this->GetConfigName()), '/',
927
0
        this->GeneratorTarget->GetPDBName(this->GetConfigName()));
928
0
    }
929
930
0
    targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
931
0
      this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
932
0
      cmOutputConverter::SHELL);
933
0
    targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
934
0
      targetFullPathPDB, cmOutputConverter::SHELL);
935
0
    targetOutPathCompilePDB = this->LocalGenerator->ConvertToOutputFormat(
936
0
      this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathCompilePDB),
937
0
      cmOutputConverter::SHELL);
938
939
0
    if (this->LocalGenerator->IsMinGWMake() &&
940
0
        cmHasSuffix(targetOutPathCompilePDB, '\\')) {
941
      // mingw32-make incorrectly interprets 'a\ b c' as 'a b' and 'c'
942
      // (but 'a\ b "c"' as 'a\', 'b', and 'c'!).  Workaround this by
943
      // avoiding a trailing backslash in the argument.
944
0
      targetOutPathCompilePDB.back() = '/';
945
0
    }
946
947
0
    std::string const compilePdbOutputPath =
948
0
      this->GeneratorTarget->GetCompilePDBDirectory(this->GetConfigName());
949
0
    cmSystemTools::MakeDirectory(compilePdbOutputPath);
950
0
  }
951
0
  cmRulePlaceholderExpander::RuleVariables vars;
952
0
  vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
953
0
  vars.CMTargetType =
954
0
    cmState::GetTargetTypeName(this->GeneratorTarget->GetType()).c_str();
955
0
  vars.Language = lang.c_str();
956
0
  vars.Target = targetOutPathReal.c_str();
957
0
  vars.TargetPDB = targetOutPathPDB.c_str();
958
0
  vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
959
0
  vars.Source = sourceFile.c_str();
960
0
  std::string const shellObj =
961
0
    this->LocalGenerator->ConvertToOutputFormat(obj, cmOutputConverter::SHELL);
962
0
  vars.Object = shellObj.c_str();
963
0
  std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
964
0
  objectDir = this->LocalGenerator->ConvertToOutputFormat(
965
0
    this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
966
0
    cmOutputConverter::SHELL);
967
0
  vars.ObjectDir = objectDir.c_str();
968
0
  std::string targetSupportDir =
969
0
    this->GeneratorTarget->GetCMFSupportDirectory();
970
0
  targetSupportDir = this->LocalGenerator->ConvertToOutputFormat(
971
0
    this->LocalGenerator->MaybeRelativeToTopBinDir(targetSupportDir),
972
0
    cmOutputConverter::SHELL);
973
0
  vars.TargetSupportDir = targetSupportDir.c_str();
974
0
  std::string objectFileDir = cmSystemTools::GetFilenamePath(obj);
975
0
  objectFileDir = this->LocalGenerator->ConvertToOutputFormat(
976
0
    this->LocalGenerator->MaybeRelativeToCurBinDir(objectFileDir),
977
0
    cmOutputConverter::SHELL);
978
0
  vars.ObjectFileDir = objectFileDir.c_str();
979
0
  vars.Flags = flags.c_str();
980
0
  vars.ISPCHeader = ispcHeaderForShell.c_str();
981
0
  vars.Config = this->GetConfigName().c_str();
982
0
  vars.RustEmit = source.GetRustEmitProperty()->c_str();
983
984
0
  std::string definesString = cmStrCat("$(", lang, "_DEFINES)");
985
986
0
  this->LocalGenerator->JoinDefines(defines, definesString, lang);
987
988
0
  vars.Defines = definesString.c_str();
989
990
0
  std::string includesString = this->LocalGenerator->GetIncludeFlags(
991
0
    includes, this->GeneratorTarget, lang, config);
992
0
  this->LocalGenerator->AppendFlags(includesString,
993
0
                                    "$(" + lang + "_INCLUDES)");
994
0
  vars.Includes = includesString.c_str();
995
996
0
  std::string dependencyTarget;
997
0
  std::string shellDependencyFile;
998
0
  std::string dependencyTimestamp;
999
0
  if (compilerGenerateDeps) {
1000
0
    dependencyTarget = this->LocalGenerator->EscapeForShell(
1001
0
      this->LocalGenerator->ConvertToMakefilePath(
1002
0
        this->LocalGenerator->MaybeRelativeToTopBinDir(relativeObj)));
1003
0
    vars.DependencyTarget = dependencyTarget.c_str();
1004
1005
0
    auto depFile = cmStrCat(obj, ".d");
1006
0
    shellDependencyFile = this->LocalGenerator->ConvertToOutputFormat(
1007
0
      depFile, cmOutputConverter::SHELL);
1008
0
    vars.DependencyFile = shellDependencyFile.c_str();
1009
0
    this->CleanFiles.insert(depFile);
1010
1011
0
    dependencyTimestamp = this->LocalGenerator->MaybeRelativeToTopBinDir(
1012
0
      cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts"));
1013
0
  }
1014
1015
  // At the moment, it is assumed that C, C++, Fortran, and CUDA have both
1016
  // assembly and preprocessor capabilities. The same is true for the
1017
  // ability to export compile commands
1018
0
  bool const lang_has_preprocessor =
1019
0
    ((lang == "C") || (lang == "CXX") || (lang == "OBJC") ||
1020
0
     (lang == "OBJCXX") || (lang == "Fortran") || (lang == "CUDA") ||
1021
0
     lang == "ISPC" || lang == "HIP" || lang == "ASM");
1022
0
  bool const lang_has_assembly = lang_has_preprocessor;
1023
0
  bool const lang_can_export_cmds = lang_has_preprocessor;
1024
1025
0
  auto rulePlaceholderExpander =
1026
0
    this->LocalGenerator->CreateRulePlaceholderExpander();
1027
1028
  // Construct the compile rules.
1029
0
  {
1030
0
    std::string cudaCompileMode;
1031
0
    if (lang == "CUDA") {
1032
0
      if (this->GeneratorTarget->GetPropertyAsBool(
1033
0
            "CUDA_SEPARABLE_COMPILATION")) {
1034
0
        std::string const& rdcFlag =
1035
0
          this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG");
1036
0
        cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, ' ');
1037
0
      }
1038
1039
0
      static std::array<cm::string_view, 4> const compileModes{
1040
0
        { "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s }
1041
0
      };
1042
0
      bool useNormalCompileMode = true;
1043
0
      for (cm::string_view mode : compileModes) {
1044
0
        auto propName = cmStrCat("CUDA_", mode, "_COMPILATION");
1045
0
        auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG");
1046
0
        if (this->GeneratorTarget->GetPropertyAsBool(propName)) {
1047
0
          std::string const& flag =
1048
0
            this->Makefile->GetRequiredDefinition(defName);
1049
0
          cudaCompileMode = cmStrCat(cudaCompileMode, flag);
1050
0
          useNormalCompileMode = false;
1051
0
          break;
1052
0
        }
1053
0
      }
1054
0
      if (useNormalCompileMode) {
1055
0
        std::string const& wholeFlag =
1056
0
          this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG");
1057
0
        cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag);
1058
0
      }
1059
0
      vars.CudaCompileMode = cudaCompileMode.c_str();
1060
0
    }
1061
1062
0
    cmList compileCommands;
1063
0
    std::string const& compileRule = this->Makefile->GetRequiredDefinition(
1064
0
      "CMAKE_" + lang + "_COMPILE_OBJECT");
1065
0
    compileCommands.assign(compileRule);
1066
1067
0
    if (this->GeneratorTarget->GetPropertyAsBool("EXPORT_COMPILE_COMMANDS") &&
1068
0
        lang_can_export_cmds && compileCommands.size() == 1) {
1069
0
      std::string compileCommand = compileCommands[0];
1070
1071
      // no launcher for CMAKE_EXPORT_COMPILE_COMMANDS
1072
0
      rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
1073
0
                                                   compileCommand, vars);
1074
0
      std::string const workingDirectory =
1075
0
        this->LocalGenerator->GetCurrentBinaryDirectory();
1076
0
      std::string::size_type lfPos = compileCommand.find(langFlags);
1077
0
      if (lfPos != std::string::npos) {
1078
0
        compileCommand.replace(lfPos, langFlags.size(),
1079
0
                               this->GetFlags(lang, this->GetConfigName()));
1080
0
      }
1081
0
      std::string const langDefines = std::string("$(") + lang + "_DEFINES)";
1082
0
      std::string::size_type const ldPos = compileCommand.find(langDefines);
1083
0
      if (ldPos != std::string::npos) {
1084
0
        compileCommand.replace(ldPos, langDefines.size(),
1085
0
                               this->GetDefines(lang, this->GetConfigName()));
1086
0
      }
1087
0
      std::string const langIncludes = std::string("$(") + lang + "_INCLUDES)";
1088
0
      std::string::size_type const liPos = compileCommand.find(langIncludes);
1089
0
      if (liPos != std::string::npos) {
1090
0
        compileCommand.replace(liPos, langIncludes.size(),
1091
0
                               this->GetIncludes(lang, this->GetConfigName()));
1092
0
      }
1093
1094
0
      cmValue const eliminate[] = {
1095
0
        this->Makefile->GetDefinition("CMAKE_START_TEMP_FILE"),
1096
0
        this->Makefile->GetDefinition("CMAKE_END_TEMP_FILE")
1097
0
      };
1098
0
      for (cmValue const& el : eliminate) {
1099
0
        if (el) {
1100
0
          cmSystemTools::ReplaceString(compileCommand, *el, "");
1101
0
        }
1102
0
      }
1103
1104
0
      this->GlobalGenerator->AddCXXCompileCommand(
1105
0
        source.GetFullPath(), workingDirectory, compileCommand, relativeObj);
1106
0
    }
1107
1108
    // See if we need to use a compiler launcher like ccache or distcc
1109
0
    std::string compilerLauncher;
1110
0
    if (!compileCommands.empty()) {
1111
0
      compilerLauncher = GetCompilerLauncher(lang, config);
1112
0
    }
1113
1114
0
    cmValue const fsSkipCodeCheckVal =
1115
0
      fileSet ? fileSet->GetProperty("SKIP_LINTING") : nullptr;
1116
0
    cmValue const srcSkipCodeCheckVal = source.GetProperty("SKIP_LINTING");
1117
0
    bool const skipCodeCheck = fsSkipCodeCheckVal.IsSet()
1118
0
      ? fsSkipCodeCheckVal.IsOn()
1119
0
      : (srcSkipCodeCheckVal.IsSet()
1120
0
           ? srcSkipCodeCheckVal.IsOn()
1121
0
           : this->GetGeneratorTarget()->GetPropertyAsBool("SKIP_LINTING"));
1122
1123
0
    if (!skipCodeCheck) {
1124
0
      std::string const codeCheck = this->GenerateCodeCheckRules(
1125
0
        source, compilerLauncher, "$(CMAKE_COMMAND)", config, nullptr);
1126
0
      if (!codeCheck.empty()) {
1127
0
        compileCommands.front().insert(0, codeCheck);
1128
0
      }
1129
0
    }
1130
1131
    // If compiler launcher was specified and not consumed above, it
1132
    // goes to the beginning of the command line.
1133
0
    if (!compileCommands.empty() && !compilerLauncher.empty()) {
1134
0
      cmList args{ compilerLauncher, cmList::EmptyElements::Yes };
1135
0
      if (!args.empty()) {
1136
0
        args[0] = this->LocalGenerator->ConvertToOutputFormat(
1137
0
          args[0], cmOutputConverter::SHELL);
1138
0
        for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
1139
0
          i = this->LocalGenerator->EscapeForShell(i);
1140
0
        }
1141
0
      }
1142
0
      compileCommands.front().insert(0, args.join(" ") + " ");
1143
0
    }
1144
1145
0
    std::string launcher;
1146
0
    {
1147
0
      std::string val = this->LocalGenerator->GetRuleLauncher(
1148
0
        this->GeneratorTarget, "RULE_LAUNCH_COMPILE",
1149
0
        this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
1150
0
      if (cmNonempty(val)) {
1151
0
        launcher = cmStrCat(val, ' ');
1152
0
      }
1153
0
    }
1154
1155
0
    std::string flagsWithDeps(flags);
1156
1157
0
    if (compilerGenerateDeps) {
1158
      // Injects dependency computation
1159
0
      auto depFlags = this->Makefile->GetSafeDefinition(
1160
0
        cmStrCat("CMAKE_DEPFILE_FLAGS_", lang));
1161
1162
0
      if (!depFlags.empty()) {
1163
        // Add dependency flags
1164
0
        rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
1165
0
                                                     depFlags, vars);
1166
0
        flagsWithDeps.append(1, ' ');
1167
0
        flagsWithDeps.append(depFlags);
1168
0
      }
1169
0
      vars.Flags = flagsWithDeps.c_str();
1170
1171
0
      auto const& extraCommands = this->Makefile->GetSafeDefinition(
1172
0
        cmStrCat("CMAKE_", lang, "_DEPENDS_EXTRA_COMMANDS"));
1173
0
      if (!extraCommands.empty()) {
1174
0
        compileCommands.append(extraCommands);
1175
0
      }
1176
1177
0
      auto const& depFormat = this->Makefile->GetRequiredDefinition(
1178
0
        cmStrCat("CMAKE_", lang, "_DEPFILE_FORMAT"));
1179
1180
0
      if (depFormat == "msvc"_s) {
1181
        // compiler must be launched through a wrapper to pick-up dependencies
1182
0
        std::string depFilter =
1183
0
          "$(CMAKE_COMMAND) -E cmake_cl_compile_depends ";
1184
0
        depFilter += cmStrCat("--dep-file=", shellDependencyFile);
1185
0
        depFilter +=
1186
0
          cmStrCat(" --working-dir=",
1187
0
                   this->LocalGenerator->ConvertToOutputFormat(
1188
0
                     this->LocalGenerator->GetCurrentBinaryDirectory(),
1189
0
                     cmOutputConverter::SHELL));
1190
0
        auto const& prefix = this->Makefile->GetSafeDefinition(
1191
0
          cmStrCat("CMAKE_", lang, "_CL_SHOWINCLUDES_PREFIX"));
1192
0
        depFilter += cmStrCat(" --filter-prefix=",
1193
0
                              this->LocalGenerator->ConvertToOutputFormat(
1194
0
                                prefix, cmOutputConverter::SHELL));
1195
0
        depFilter += " -- ";
1196
0
        compileCommands.front().insert(0, depFilter);
1197
0
      }
1198
0
    }
1199
1200
    // Expand placeholders in the commands.
1201
0
    for (std::string& compileCommand : compileCommands) {
1202
0
      compileCommand = cmStrCat(launcher, compileCommand);
1203
0
      rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
1204
0
                                                   compileCommand, vars);
1205
0
    }
1206
1207
    // Change the command working directory to the local build tree.
1208
0
    this->LocalGenerator->CreateCDCommand(
1209
0
      compileCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
1210
0
      this->LocalGenerator->GetBinaryDirectory());
1211
0
    cm::append(commands, compileCommands);
1212
0
  }
1213
1214
  // Check for extra outputs created by the compilation.
1215
0
  cmList outputs;
1216
0
  outputs.emplace_back(relativeObj);
1217
0
  if (cmValue extra_outputs_str = source.GetProperty("OBJECT_OUTPUTS")) {
1218
0
    std::string evaluated_outputs = cmGeneratorExpression::Evaluate(
1219
0
      *extra_outputs_str, this->LocalGenerator, config);
1220
1221
0
    if (!evaluated_outputs.empty()) {
1222
      // Register these as extra files to clean.
1223
0
      outputs.append(evaluated_outputs);
1224
0
    }
1225
0
  }
1226
0
  if (!ispcHeaderRelative.empty()) {
1227
    // can't move ispcHeader as vars is using it
1228
0
    outputs.emplace_back(ispcHeaderRelative);
1229
0
  }
1230
1231
0
  if (outputs.size() > 1) {
1232
0
    this->CleanFiles.insert(outputs.begin() + 1, outputs.end());
1233
0
  }
1234
1235
0
  if (compilerGenerateDeps) {
1236
0
    depends.push_back(dependencyTimestamp);
1237
0
  }
1238
1239
  // Write the rule.
1240
0
  this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
1241
0
                      commands);
1242
1243
0
  if (compilerGenerateDeps) {
1244
    // set back flags without dependency generation
1245
0
    vars.Flags = flags.c_str();
1246
0
  }
1247
1248
0
  bool do_preprocess_rules = lang_has_preprocessor &&
1249
0
    this->LocalGenerator->GetCreatePreprocessedSourceRules();
1250
0
  bool do_assembly_rules =
1251
0
    lang_has_assembly && this->LocalGenerator->GetCreateAssemblySourceRules();
1252
0
  if (do_preprocess_rules || do_assembly_rules) {
1253
0
    std::vector<std::string> force_depends;
1254
0
    force_depends.emplace_back("cmake_force");
1255
0
    std::string::size_type dot_pos = relativeObj.rfind('.');
1256
0
    std::string relativeObjBase = relativeObj.substr(0, dot_pos);
1257
0
    dot_pos = obj.rfind('.');
1258
0
    std::string objBase = obj.substr(0, dot_pos);
1259
1260
0
    if (do_preprocess_rules) {
1261
0
      commands.clear();
1262
0
      std::string const relativeObjI = relativeObjBase + ".i";
1263
0
      std::string const objI = objBase + ".i";
1264
1265
0
      std::string preprocessEcho =
1266
0
        cmStrCat("Preprocessing ", lang, " source to ", objI);
1267
0
      this->LocalGenerator->AppendEcho(
1268
0
        commands, preprocessEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
1269
1270
0
      std::string preprocessRuleVar =
1271
0
        cmStrCat("CMAKE_", lang, "_CREATE_PREPROCESSED_SOURCE");
1272
0
      if (cmValue preprocessRule =
1273
0
            this->Makefile->GetDefinition(preprocessRuleVar)) {
1274
0
        cmList preprocessCommands{ *preprocessRule };
1275
1276
0
        std::string shellObjI = this->LocalGenerator->ConvertToOutputFormat(
1277
0
          objI, cmOutputConverter::SHELL);
1278
0
        vars.PreprocessedSource = shellObjI.c_str();
1279
1280
        // Expand placeholders in the commands.
1281
0
        for (std::string& preprocessCommand : preprocessCommands) {
1282
          // no launcher for preprocessor commands
1283
0
          rulePlaceholderExpander->ExpandRuleVariables(
1284
0
            this->LocalGenerator, preprocessCommand, vars);
1285
0
        }
1286
1287
0
        this->LocalGenerator->CreateCDCommand(
1288
0
          preprocessCommands,
1289
0
          this->LocalGenerator->GetCurrentBinaryDirectory(),
1290
0
          this->LocalGenerator->GetBinaryDirectory());
1291
0
        cm::append(commands, preprocessCommands);
1292
0
      } else {
1293
0
        std::string cmd =
1294
0
          cmStrCat("$(CMAKE_COMMAND) -E cmake_unimplemented_variable ",
1295
0
                   preprocessRuleVar);
1296
0
        commands.push_back(std::move(cmd));
1297
0
      }
1298
1299
0
      this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1300
0
                                          relativeObjI, force_depends,
1301
0
                                          commands, false);
1302
0
    }
1303
1304
0
    if (do_assembly_rules) {
1305
0
      commands.clear();
1306
0
      std::string relativeObjS = relativeObjBase + ".s";
1307
0
      std::string objS = objBase + ".s";
1308
1309
0
      std::string assemblyEcho =
1310
0
        cmStrCat("Compiling ", lang, " source to assembly ", objS);
1311
0
      this->LocalGenerator->AppendEcho(
1312
0
        commands, assemblyEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
1313
1314
0
      std::string assemblyRuleVar =
1315
0
        cmStrCat("CMAKE_", lang, "_CREATE_ASSEMBLY_SOURCE");
1316
0
      if (cmValue assemblyRule =
1317
0
            this->Makefile->GetDefinition(assemblyRuleVar)) {
1318
0
        cmList assemblyCommands{ *assemblyRule };
1319
1320
0
        std::string shellObjS = this->LocalGenerator->ConvertToOutputFormat(
1321
0
          objS, cmOutputConverter::SHELL);
1322
0
        vars.AssemblySource = shellObjS.c_str();
1323
1324
        // Expand placeholders in the commands.
1325
0
        for (std::string& assemblyCommand : assemblyCommands) {
1326
          // no launcher for assembly commands
1327
0
          rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
1328
0
                                                       assemblyCommand, vars);
1329
0
        }
1330
1331
0
        this->LocalGenerator->CreateCDCommand(
1332
0
          assemblyCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
1333
0
          this->LocalGenerator->GetBinaryDirectory());
1334
0
        cm::append(commands, assemblyCommands);
1335
0
      } else {
1336
0
        std::string cmd =
1337
0
          cmStrCat("$(CMAKE_COMMAND) -E cmake_unimplemented_variable ",
1338
0
                   assemblyRuleVar);
1339
0
        commands.push_back(std::move(cmd));
1340
0
      }
1341
1342
0
      this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1343
0
                                          relativeObjS, force_depends,
1344
0
                                          commands, false);
1345
0
    }
1346
0
  }
1347
0
}
1348
1349
void cmMakefileTargetGenerator::WriteTargetCleanRules()
1350
0
{
1351
0
  std::vector<std::string> depends;
1352
0
  std::vector<std::string> commands;
1353
1354
  // Construct the clean target name.
1355
0
  std::string const cleanTarget = cmStrCat(
1356
0
    this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget),
1357
0
    "/clean");
1358
1359
  // Construct the clean command.
1360
0
  this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
1361
0
                                           this->GeneratorTarget);
1362
0
  this->LocalGenerator->CreateCDCommand(
1363
0
    commands, this->LocalGenerator->GetCurrentBinaryDirectory(),
1364
0
    this->LocalGenerator->GetBinaryDirectory());
1365
1366
  // Write the rule.
1367
0
  this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1368
0
                                      cleanTarget, depends, commands, true);
1369
0
}
1370
1371
bool cmMakefileTargetGenerator::WriteMakeRule(
1372
  std::ostream& os, char const* comment,
1373
  std::vector<std::string> const& outputs,
1374
  std::vector<std::string> const& depends,
1375
  std::vector<std::string> const& commands, bool in_help)
1376
0
{
1377
0
  bool symbolic = false;
1378
0
  if (outputs.empty()) {
1379
0
    return symbolic;
1380
0
  }
1381
1382
  // Check whether we need to bother checking for a symbolic output.
1383
0
  bool const need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
1384
1385
  // Check whether the first output is marked as symbolic.
1386
0
  if (need_symbolic) {
1387
0
    if (cmSourceFile* sf = this->Makefile->GetSource(outputs[0])) {
1388
0
      symbolic = sf->GetPropertyAsBool("SYMBOLIC");
1389
0
    }
1390
0
  }
1391
1392
  // We always attach the actual commands to the first output.
1393
0
  this->LocalGenerator->WriteMakeRule(os, comment, outputs[0], depends,
1394
0
                                      commands, symbolic, in_help);
1395
1396
  // For single outputs, we are done.
1397
0
  if (outputs.size() == 1) {
1398
0
    return symbolic;
1399
0
  }
1400
1401
  // For multiple outputs, make the extra ones depend on the first one.
1402
0
  std::vector<std::string> const output_depends(1, outputs[0]);
1403
0
  for (std::string const& output : cmMakeRange(outputs).advance(1)) {
1404
    // Touch the extra output so "make" knows that it was updated,
1405
    // but only if the output was actually created.
1406
0
    std::string const out = this->LocalGenerator->ConvertToOutputFormat(
1407
0
      this->LocalGenerator->MaybeRelativeToTopBinDir(output),
1408
0
      cmOutputConverter::SHELL);
1409
0
    std::vector<std::string> output_commands;
1410
1411
0
    bool o_symbolic = false;
1412
0
    if (need_symbolic) {
1413
0
      if (cmSourceFile const* sf = this->Makefile->GetSource(output)) {
1414
0
        o_symbolic = sf->GetPropertyAsBool("SYMBOLIC");
1415
0
      }
1416
0
    }
1417
0
    symbolic = symbolic && o_symbolic;
1418
1419
0
    if (!o_symbolic) {
1420
0
      output_commands.push_back("@$(CMAKE_COMMAND) -E touch_nocreate " + out);
1421
0
    }
1422
0
    this->LocalGenerator->WriteMakeRule(os, nullptr, output, output_depends,
1423
0
                                        output_commands, o_symbolic, in_help);
1424
1425
0
    if (!o_symbolic) {
1426
      // At build time, remove the first output if this one does not exist
1427
      // so that "make" will rerun the real commands that create this one.
1428
0
      MultipleOutputPairsType::value_type p(output, outputs[0]);
1429
0
      this->MultipleOutputPairs.insert(p);
1430
0
    }
1431
0
  }
1432
0
  return symbolic;
1433
0
}
1434
1435
void cmMakefileTargetGenerator::WriteTargetLinkDependRules()
1436
0
{
1437
0
  if (!this->GeneratorTarget->HasLinkDependencyFile(this->GetConfigName())) {
1438
0
    return;
1439
0
  }
1440
1441
0
  auto depFile = this->LocalGenerator->GetLinkDependencyFile(
1442
0
    this->GeneratorTarget, this->GetConfigName());
1443
0
  this->CleanFiles.insert(depFile);
1444
0
  this->LocalGenerator->AddImplicitDepends(
1445
0
    this->GeneratorTarget, "LINK",
1446
0
    this->GeneratorTarget->GetFullPath(this->GetConfigName()), depFile,
1447
0
    cmDependencyScannerKind::Compiler);
1448
0
}
1449
std::string cmMakefileTargetGenerator::GetClangTidyReplacementsFilePath(
1450
  std::string const& directory, cmSourceFile const& source,
1451
  std::string const& config) const
1452
0
{
1453
0
  (void)config;
1454
0
  auto const& objectName = this->GeneratorTarget->GetObjectName(&source);
1455
  // NOTE: This may be better to use `this->TargetBuildDirectory` instead of
1456
  // `MaybeRelativeToTopBinDir(this->TargetBuildDirectoryFull)` here. The main
1457
  // difference is that the current behavior looks odd to relative
1458
  // `<LANG>_CLANG_TIDY_EXPORT_FIXES_DIR` settings. Each subdirectory has its
1459
  // own export fixes directory *and* adds its relative-from-root path
1460
  // underneath it. However, when using an absolute export fixes directory, the
1461
  // source directory structure is preserved. The main benefit of the former is
1462
  // shorter paths everywhere versus the status quo of the existing code.
1463
0
  cmLocalGenerator* lg = this->GeneratorTarget->GetLocalGenerator();
1464
0
  auto fixesFile = cmSystemTools::CollapseFullPath(
1465
0
    cmStrCat(directory, '/',
1466
0
             lg->CreateSafeObjectFileName(
1467
0
               lg->MaybeRelativeToTopBinDir(this->TargetBuildDirectoryFull)),
1468
0
             '/', objectName, ".yaml"));
1469
0
  return fixesFile;
1470
0
}
1471
1472
void cmMakefileTargetGenerator::WriteTargetDependRules()
1473
0
{
1474
  // must write the targets depend info file
1475
0
  this->InfoFileNameFull =
1476
0
    cmStrCat(this->TargetBuildDirectoryFull, "/DependInfo.cmake");
1477
0
  this->InfoFileStream =
1478
0
    cm::make_unique<cmGeneratedFileStream>(this->InfoFileNameFull);
1479
0
  if (!this->InfoFileStream) {
1480
0
    return;
1481
0
  }
1482
0
  this->InfoFileStream->SetCopyIfDifferent(true);
1483
0
  this->LocalGenerator->WriteDependLanguageInfo(*this->InfoFileStream,
1484
0
                                                this->GeneratorTarget);
1485
1486
  // Store multiple output pairs in the depend info file.
1487
0
  if (!this->MultipleOutputPairs.empty()) {
1488
    /* clang-format off */
1489
0
    *this->InfoFileStream
1490
0
      << "\n"
1491
0
      << "# Pairs of files generated by the same build rule.\n"
1492
0
      << "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
1493
    /* clang-format on */
1494
0
    for (auto const& pi : this->MultipleOutputPairs) {
1495
0
      *this->InfoFileStream << "  " << cmScriptGenerator::Quote(pi.first)
1496
0
                            << ' ' << cmScriptGenerator::Quote(pi.second)
1497
0
                            << '\n';
1498
0
    }
1499
0
    *this->InfoFileStream << "  )\n\n";
1500
0
  }
1501
1502
  // Store list of targets linked directly or transitively.
1503
0
  {
1504
    /* clang-format off */
1505
0
  *this->InfoFileStream
1506
0
    << "\n"
1507
0
       "# Targets to which this target links which contain Fortran sources.\n"
1508
0
       "set(CMAKE_Fortran_TARGET_LINKED_INFO_FILES\n";
1509
    /* clang-format on */
1510
0
    auto const dirs =
1511
0
      this->GetLinkedTargetDirectories("Fortran", this->GetConfigName());
1512
0
    for (std::string const& d : dirs.Direct) {
1513
0
      *this->InfoFileStream << "  \"" << d << "/DependInfo.cmake\"\n";
1514
0
    }
1515
0
    *this->InfoFileStream << "  )\n";
1516
1517
    /* clang-format off */
1518
0
  *this->InfoFileStream
1519
0
    << "\n"
1520
0
       "# Targets to which this target links which contain Fortran sources.\n"
1521
0
       "set(CMAKE_Fortran_TARGET_FORWARD_LINKED_INFO_FILES\n";
1522
    /* clang-format on */
1523
0
    for (std::string const& d : dirs.Forward) {
1524
0
      *this->InfoFileStream << "  \"" << d << "/DependInfo.cmake\"\n";
1525
0
    }
1526
0
    *this->InfoFileStream << "  )\n";
1527
0
  }
1528
1529
0
  std::string const& working_dir =
1530
0
    this->LocalGenerator->GetCurrentBinaryDirectory();
1531
1532
  /* clang-format off */
1533
0
  *this->InfoFileStream
1534
0
    << "\n"
1535
0
    << "# Fortran module output directory.\n"
1536
0
    << "set(CMAKE_Fortran_TARGET_MODULE_DIR \""
1537
0
    << this->GeneratorTarget->GetFortranModuleDirectory(working_dir)
1538
0
    << "\")\n";
1539
1540
0
  if (this->GeneratorTarget->IsFortranBuildingIntrinsicModules()) {
1541
0
    *this->InfoFileStream
1542
0
      << "\n"
1543
0
      << "# Fortran compiler is building intrinsic modules.\n"
1544
0
      << "set(CMAKE_Fortran_TARGET_BUILDING_INTRINSIC_MODULES ON) \n";
1545
0
  }
1546
  /* clang-format on */
1547
1548
  // and now write the rule to use it
1549
0
  std::vector<std::string> depends;
1550
0
  std::vector<std::string> commands;
1551
1552
  // Construct the name of the dependency generation target.
1553
0
  std::string const depTarget = cmStrCat(
1554
0
    this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget),
1555
0
    "/depend");
1556
1557
  // Add a command to call CMake to scan dependencies.  CMake will
1558
  // touch the corresponding depends file after scanning dependencies.
1559
0
  std::ostringstream depCmd;
1560
// TODO: Account for source file properties and directory-level
1561
// definitions when scanning for dependencies.
1562
0
#if !defined(_WIN32) || defined(__CYGWIN__)
1563
  // This platform supports symlinks, so cmSystemTools will translate
1564
  // paths.  Make sure PWD is set to the original name of the home
1565
  // output directory to help cmSystemTools to create the same
1566
  // translation table for the dependency scanning process.
1567
0
  depCmd << "cd "
1568
0
         << (this->LocalGenerator->ConvertToOutputFormat(
1569
0
              this->LocalGenerator->GetBinaryDirectory(),
1570
0
              cmOutputConverter::SHELL))
1571
0
         << " && ";
1572
0
#endif
1573
  // Generate a call this signature:
1574
  //
1575
  //   cmake -E cmake_depends <generator>
1576
  //                          <home-src-dir> <start-src-dir>
1577
  //                          <home-out-dir> <start-out-dir>
1578
  //                          <dep-info> --color=$(COLOR)
1579
  //                          <target-name>
1580
  //
1581
  // This gives the dependency scanner enough information to recreate
1582
  // the state of our local generator sufficiently for its needs.
1583
0
  depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
1584
0
         << this->GlobalGenerator->GetName() << "\" "
1585
0
         << this->LocalGenerator->ConvertToOutputFormat(
1586
0
              this->LocalGenerator->GetSourceDirectory(),
1587
0
              cmOutputConverter::SHELL)
1588
0
         << " "
1589
0
         << this->LocalGenerator->ConvertToOutputFormat(
1590
0
              this->LocalGenerator->GetCurrentSourceDirectory(),
1591
0
              cmOutputConverter::SHELL)
1592
0
         << " "
1593
0
         << this->LocalGenerator->ConvertToOutputFormat(
1594
0
              this->LocalGenerator->GetBinaryDirectory(),
1595
0
              cmOutputConverter::SHELL)
1596
0
         << " "
1597
0
         << this->LocalGenerator->ConvertToOutputFormat(
1598
0
              this->LocalGenerator->GetCurrentBinaryDirectory(),
1599
0
              cmOutputConverter::SHELL)
1600
0
         << " "
1601
0
         << this->LocalGenerator->ConvertToOutputFormat(
1602
0
              this->InfoFileNameFull, cmOutputConverter::SHELL);
1603
0
  if (this->LocalGenerator->GetColorMakefile()) {
1604
0
    depCmd << " \"--color=$(COLOR)\"";
1605
0
  }
1606
0
  depCmd << ' '
1607
0
         << this->LocalGenerator->ConvertToOutputFormat(
1608
0
              this->GeneratorTarget->GetName(), cmOutputConverter::SHELL);
1609
0
  commands.push_back(depCmd.str());
1610
1611
  // Make sure all custom command outputs in this target are built.
1612
0
  if (this->CustomCommandDriver == OnDepends) {
1613
0
    this->DriveCustomCommands(depends);
1614
0
  }
1615
1616
  // Write the rule.
1617
0
  this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1618
0
                                      depTarget, depends, commands, true);
1619
0
}
1620
1621
void cmMakefileTargetGenerator::DriveCustomCommands(
1622
  std::vector<std::string>& depends)
1623
0
{
1624
  // Depend on all custom command outputs.
1625
0
  cm::append(depends, this->CustomCommandOutputs);
1626
0
}
1627
1628
void cmMakefileTargetGenerator::WriteObjectDependRules(
1629
  cmSourceFile const& source, std::vector<std::string>& depends)
1630
0
{
1631
  // Create the list of dependencies known at cmake time.  These are
1632
  // shared between the object file and dependency scanning rule.
1633
0
  depends.push_back(source.GetFullPath());
1634
0
  if (cmValue objectDeps = source.GetProperty("OBJECT_DEPENDS")) {
1635
0
    cmExpandList(*objectDeps, depends);
1636
0
  }
1637
0
}
1638
1639
void cmMakefileTargetGenerator::WriteDeviceLinkRule(
1640
  std::vector<std::string>& commands, std::string const& output)
1641
0
{
1642
0
  std::string architecturesStr =
1643
0
    this->GeneratorTarget->GetSafeProperty("CUDA_ARCHITECTURES");
1644
1645
0
  if (cmIsOff(architecturesStr)) {
1646
0
    this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
1647
0
                                 "CUDA_SEPARABLE_COMPILATION on Clang "
1648
0
                                 "requires CUDA_ARCHITECTURES to be set.");
1649
0
    return;
1650
0
  }
1651
1652
0
  cmLocalUnixMakefileGenerator3* localGen{ this->LocalGenerator };
1653
0
  cmList architectures{ architecturesStr };
1654
0
  std::string const& relPath = localGen->GetHomeRelativeOutputPath();
1655
1656
  // Ensure there are no duplicates.
1657
0
  std::vector<std::string> const linkDeps = [&]() -> std::vector<std::string> {
1658
0
    std::vector<std::string> deps;
1659
0
    this->AppendTargetDepends(deps, true);
1660
0
    this->GeneratorTarget->GetLinkDepends(deps, this->GetConfigName(), "CUDA");
1661
1662
0
    for (std::string const& obj : this->Objects) {
1663
0
      deps.emplace_back(cmStrCat(relPath, obj));
1664
0
    }
1665
1666
0
    std::unordered_set<std::string> const depsSet(deps.begin(), deps.end());
1667
0
    deps.clear();
1668
0
    std::copy(depsSet.begin(), depsSet.end(), std::back_inserter(deps));
1669
0
    return deps;
1670
0
  }();
1671
1672
0
  std::string const objectDir = this->GeneratorTarget->ObjectDirectory;
1673
0
  std::string const relObjectDir =
1674
0
    localGen->MaybeRelativeToCurBinDir(objectDir);
1675
1676
  // Construct a list of files associated with this executable that
1677
  // may need to be cleaned.
1678
0
  std::vector<std::string> cleanFiles;
1679
0
  cleanFiles.push_back(localGen->MaybeRelativeToCurBinDir(output));
1680
1681
0
  std::string profiles;
1682
0
  std::vector<std::string> fatbinaryDepends;
1683
0
  std::string const registerFile =
1684
0
    cmStrCat(objectDir, "cmake_cuda_register.h");
1685
1686
  // Link device code for each architecture.
1687
0
  for (std::string const& architectureKind : architectures) {
1688
0
    std::string registerFileCmd;
1689
1690
    // The generated register file contains macros that when expanded
1691
    // register the device routines. Because the routines are the same for
1692
    // all architectures the register file will be the same too. Thus
1693
    // generate it only on the first invocation to reduce overhead.
1694
0
    if (fatbinaryDepends.empty()) {
1695
0
      std::string const registerFileRel =
1696
0
        cmStrCat(relPath, relObjectDir, "cmake_cuda_register.h");
1697
0
      registerFileCmd =
1698
0
        cmStrCat(" --register-link-binaries=", registerFileRel);
1699
0
      cleanFiles.push_back(registerFileRel);
1700
0
    }
1701
1702
    // Clang always generates real code, so strip the specifier.
1703
0
    std::string const architecture =
1704
0
      architectureKind.substr(0, architectureKind.find('-'));
1705
0
    std::string const cubin =
1706
0
      cmStrCat(objectDir, "sm_", architecture, ".cubin");
1707
1708
0
    profiles += cmStrCat(" -im=profile=sm_", architecture, ",file=", cubin);
1709
0
    fatbinaryDepends.emplace_back(cubin);
1710
1711
0
    std::string command = cmStrCat(
1712
0
      this->Makefile->GetRequiredDefinition("CMAKE_CUDA_DEVICE_LINKER"),
1713
0
      " -arch=sm_", architecture, registerFileCmd, " -o=$@ ",
1714
0
      cmJoin(linkDeps, " "));
1715
1716
0
    localGen->WriteMakeRule(*this->BuildFileStream, nullptr, cubin, linkDeps,
1717
0
                            { command }, false);
1718
0
  }
1719
1720
  // Combine all architectures into a single fatbinary.
1721
0
  std::string const fatbinaryCommand =
1722
0
    cmStrCat(this->Makefile->GetRequiredDefinition("CMAKE_CUDA_FATBINARY"),
1723
0
             " -64 -cmdline=--compile-only -compress-all -link "
1724
0
             "--embedded-fatbin=$@",
1725
0
             profiles);
1726
0
  std::string const fatbinaryOutput =
1727
0
    cmStrCat(objectDir, "cmake_cuda_fatbin.h");
1728
0
  std::string const fatbinaryOutputRel =
1729
0
    cmStrCat(relPath, relObjectDir, "cmake_cuda_fatbin.h");
1730
1731
0
  localGen->WriteMakeRule(*this->BuildFileStream, nullptr, fatbinaryOutputRel,
1732
0
                          fatbinaryDepends, { fatbinaryCommand }, false);
1733
1734
  // Compile the stub that registers the kernels and contains the
1735
  // fatbinaries.
1736
0
  cmRulePlaceholderExpander::RuleVariables vars;
1737
0
  vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
1738
0
  vars.CMTargetType =
1739
0
    cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()).c_str();
1740
0
  vars.Language = "CUDA";
1741
0
  vars.Object = output.c_str();
1742
0
  vars.Fatbinary = fatbinaryOutput.c_str();
1743
0
  vars.RegisterFile = registerFile.c_str();
1744
0
  vars.Config = this->GetConfigName().c_str();
1745
1746
0
  std::string linkFlags;
1747
0
  this->GetDeviceLinkFlags(linkFlags, "CUDA");
1748
0
  vars.LinkFlags = linkFlags.c_str();
1749
1750
0
  std::string const flags = this->GetFlags("CUDA", this->GetConfigName());
1751
0
  vars.Flags = flags.c_str();
1752
1753
0
  std::string compileCmd = this->GetLinkRule("CMAKE_CUDA_DEVICE_LINK_COMPILE");
1754
0
  auto rulePlaceholderExpander =
1755
0
    localGen->CreateRulePlaceholderExpander(cmBuildStep::Link);
1756
0
  rulePlaceholderExpander->ExpandRuleVariables(localGen, compileCmd, vars);
1757
1758
0
  commands.emplace_back(compileCmd);
1759
0
  localGen->WriteMakeRule(*this->BuildFileStream, nullptr, output,
1760
0
                          { fatbinaryOutputRel }, commands, false);
1761
1762
  // Clean all the possible executable names and symlinks.
1763
0
  this->CleanFiles.insert(cleanFiles.begin(), cleanFiles.end());
1764
0
}
1765
1766
void cmMakefileTargetGenerator::GenerateCustomRuleFile(
1767
  cmCustomCommandGenerator const& ccg)
1768
0
{
1769
  // Collect the commands.
1770
0
  std::vector<std::string> commands;
1771
0
  std::string comment = this->LocalGenerator->ConstructComment(ccg);
1772
0
  if (!comment.empty()) {
1773
    // add in a progress call if needed
1774
0
    this->NumberOfProgressActions++;
1775
0
    if (!this->NoRuleMessages) {
1776
0
      cmLocalUnixMakefileGenerator3::EchoProgress progress;
1777
0
      this->MakeEchoProgress(progress);
1778
0
      this->LocalGenerator->AppendEcho(
1779
0
        commands, comment, cmLocalUnixMakefileGenerator3::EchoGenerate,
1780
0
        &progress);
1781
0
    }
1782
0
  }
1783
1784
  // Now append the actual user-specified commands.
1785
0
  std::ostringstream content;
1786
0
  this->LocalGenerator->AppendCustomCommand(
1787
0
    commands, ccg, this->GeneratorTarget,
1788
0
    this->LocalGenerator->GetBinaryDirectory(), false, &content);
1789
1790
  // Collect the dependencies.
1791
0
  std::vector<std::string> depends;
1792
0
  this->LocalGenerator->AppendCustomDepend(depends, ccg);
1793
1794
0
  if (!ccg.GetCC().GetDepfile().empty()) {
1795
    // Add dependency over timestamp file for dependencies management
1796
0
    auto dependTimestamp = this->LocalGenerator->MaybeRelativeToTopBinDir(
1797
0
      cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts"));
1798
1799
0
    depends.emplace_back(std::move(dependTimestamp));
1800
0
  }
1801
1802
  // Write the rule.
1803
0
  std::vector<std::string> const& outputs = ccg.GetOutputs();
1804
0
  bool const symbolic = this->WriteMakeRule(*this->BuildFileStream, nullptr,
1805
0
                                            outputs, depends, commands);
1806
1807
  // Symbolic inputs are not expected to exist, so add dummy rules.
1808
0
  if (this->CMP0113New && !depends.empty()) {
1809
0
    std::vector<std::string> no_depends;
1810
0
    std::vector<std::string> no_commands;
1811
0
    for (std::string const& dep : depends) {
1812
0
      if (cmSourceFile* dsf =
1813
0
            this->Makefile->GetSource(dep, cmSourceFileLocationKind::Known)) {
1814
0
        if (dsf->GetPropertyAsBool("SYMBOLIC")) {
1815
0
          this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1816
0
                                              dep, no_depends, no_commands,
1817
0
                                              true);
1818
0
        }
1819
0
      }
1820
0
    }
1821
0
  }
1822
1823
  // If the rule has changed make sure the output is rebuilt.
1824
0
  if (!symbolic) {
1825
0
    this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
1826
0
  }
1827
1828
  // Setup implicit dependency scanning.
1829
0
  for (auto const& idi : ccg.GetCC().GetImplicitDepends()) {
1830
0
    std::string objFullPath = cmSystemTools::CollapseFullPath(
1831
0
      outputs[0], this->LocalGenerator->GetCurrentBinaryDirectory());
1832
0
    std::string srcFullPath = cmSystemTools::CollapseFullPath(
1833
0
      idi.second, this->LocalGenerator->GetCurrentBinaryDirectory());
1834
0
    this->LocalGenerator->AddImplicitDepends(this->GeneratorTarget, idi.first,
1835
0
                                             objFullPath, srcFullPath);
1836
0
  }
1837
1838
  // Setup implicit depend for depfile if any
1839
0
  if (!ccg.GetCC().GetDepfile().empty()) {
1840
0
    std::string objFullPath = cmSystemTools::CollapseFullPath(
1841
0
      outputs[0], this->LocalGenerator->GetCurrentBinaryDirectory());
1842
0
    this->LocalGenerator->AddImplicitDepends(
1843
0
      this->GeneratorTarget, "CUSTOM", objFullPath, ccg.GetFullDepfile(),
1844
0
      cmDependencyScannerKind::Compiler);
1845
0
  }
1846
1847
0
  this->CustomCommandOutputs.insert(outputs.begin(), outputs.end());
1848
0
}
1849
1850
void cmMakefileTargetGenerator::MakeEchoProgress(
1851
  cmLocalUnixMakefileGenerator3::EchoProgress& progress) const
1852
0
{
1853
0
  progress.Dir =
1854
0
    cmStrCat(this->LocalGenerator->GetBinaryDirectory(), "/CMakeFiles");
1855
0
  std::ostringstream progressArg;
1856
0
  progressArg << "$(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
1857
0
  progress.Arg = progressArg.str();
1858
0
}
1859
1860
void cmMakefileTargetGenerator::WriteObjectsVariable(
1861
  std::string& variableName, std::string& variableNameExternal,
1862
  bool useWatcomQuote)
1863
0
{
1864
  // Write a make variable assignment that lists all objects for the
1865
  // target.
1866
0
  variableName = this->LocalGenerator->CreateMakeVariable(
1867
0
    this->GeneratorTarget->GetName(), "_OBJECTS");
1868
0
  *this->BuildFileStream << "# Object files for target "
1869
0
                         << this->GeneratorTarget->GetName() << "\n"
1870
0
                         << variableName << " =";
1871
0
  auto const& lineContinue = this->GlobalGenerator->LineContinueDirective;
1872
1873
0
  cmValue pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
1874
1875
0
  for (std::string const& obj : this->Objects) {
1876
0
    if (cmHasSuffix(obj, pchExtension)) {
1877
0
      continue;
1878
0
    }
1879
0
    *this->BuildFileStream << " " << lineContinue;
1880
0
    *this->BuildFileStream
1881
0
      << cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(
1882
0
           obj, useWatcomQuote);
1883
0
  }
1884
0
  *this->BuildFileStream << "\n";
1885
1886
  // Write a make variable assignment that lists all external objects
1887
  // for the target.
1888
0
  variableNameExternal = this->LocalGenerator->CreateMakeVariable(
1889
0
    this->GeneratorTarget->GetName(), "_EXTERNAL_OBJECTS");
1890
  /* clang-format off */
1891
0
  *this->BuildFileStream
1892
0
    << "\n"
1893
0
    << "# External object files for target "
1894
0
    << this->GeneratorTarget->GetName() << "\n"
1895
0
    << variableNameExternal << " =";
1896
  /* clang-format on */
1897
0
  for (std::string const& obj : this->ExternalObjects) {
1898
0
    *this->BuildFileStream << " " << lineContinue;
1899
0
    *this->BuildFileStream
1900
0
      << cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(
1901
0
           obj, useWatcomQuote);
1902
0
  }
1903
0
  *this->BuildFileStream << "\n"
1904
0
                         << "\n";
1905
0
}
1906
1907
class cmMakefileTargetGeneratorObjectStrings
1908
{
1909
public:
1910
  cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
1911
                                         cmOutputConverter* outputConverter,
1912
                                         bool useWatcomQuote,
1913
                                         cmStateDirectory const& stateDir,
1914
                                         std::string::size_type limit)
1915
0
    : Strings(strings)
1916
0
    , OutputConverter(outputConverter)
1917
0
    , UseWatcomQuote(useWatcomQuote)
1918
0
    , StateDir(stateDir)
1919
0
    , LengthLimit(limit)
1920
0
  {
1921
0
    this->Space = "";
1922
0
  }
1923
  void Feed(std::string const& obj)
1924
0
  {
1925
    // Construct the name of the next object.
1926
0
    this->NextObject = this->OutputConverter->ConvertToOutputFormat(
1927
0
      this->OutputConverter->MaybeRelativeToCurBinDir(obj),
1928
0
      cmOutputConverter::RESPONSE, this->UseWatcomQuote);
1929
1930
    // Roll over to next string if the limit will be exceeded.
1931
0
    if (this->LengthLimit != std::string::npos &&
1932
0
        (this->CurrentString.length() + 1 + this->NextObject.length() >
1933
0
         this->LengthLimit)) {
1934
0
      this->Strings.push_back(this->CurrentString);
1935
0
      this->CurrentString.clear();
1936
0
      this->Space = "";
1937
0
    }
1938
1939
    // Separate from previous object.
1940
0
    this->CurrentString += this->Space;
1941
0
    this->Space = " ";
1942
1943
    // Append this object.
1944
0
    this->CurrentString += this->NextObject;
1945
0
  }
1946
0
  void Done() { this->Strings.push_back(this->CurrentString); }
1947
1948
private:
1949
  std::vector<std::string>& Strings;
1950
  cmOutputConverter* OutputConverter;
1951
  bool UseWatcomQuote;
1952
  cmStateDirectory StateDir;
1953
  std::string::size_type LengthLimit;
1954
  std::string CurrentString;
1955
  std::string NextObject;
1956
  char const* Space;
1957
};
1958
1959
void cmMakefileTargetGenerator::WriteObjectsStrings(
1960
  std::vector<std::string>& objStrings, bool useWatcomQuote,
1961
  std::string::size_type limit)
1962
0
{
1963
0
  cmValue pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
1964
1965
0
  cmMakefileTargetGeneratorObjectStrings helper(
1966
0
    objStrings, this->LocalGenerator, useWatcomQuote,
1967
0
    this->LocalGenerator->GetStateSnapshot().GetDirectory(), limit);
1968
0
  for (std::string const& obj : this->Objects) {
1969
0
    if (cmHasSuffix(obj, pchExtension)) {
1970
0
      continue;
1971
0
    }
1972
0
    helper.Feed(obj);
1973
0
  }
1974
0
  for (std::string const& obj : this->ExternalObjects) {
1975
0
    helper.Feed(obj);
1976
0
  }
1977
0
  auto const ispcAdditionalObjs =
1978
0
    this->GeneratorTarget->GetGeneratedISPCObjects(this->GetConfigName());
1979
0
  for (auto const& obj : ispcAdditionalObjs) {
1980
0
    helper.Feed(obj.second);
1981
0
  }
1982
0
  helper.Done();
1983
0
}
1984
1985
void cmMakefileTargetGenerator::WriteTargetDriverRule(
1986
  std::string const& main_output, bool relink)
1987
0
{
1988
  // Compute the name of the driver target.
1989
0
  std::string dir =
1990
0
    this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
1991
0
  std::string buildTargetRuleName =
1992
0
    cmStrCat(std::move(dir), relink ? "/preinstall" : "/build");
1993
0
  buildTargetRuleName =
1994
0
    this->LocalGenerator->MaybeRelativeToTopBinDir(buildTargetRuleName);
1995
1996
  // Build the list of target outputs to drive.
1997
0
  std::vector<std::string> depends{ main_output };
1998
1999
0
  char const* comment = nullptr;
2000
0
  if (relink) {
2001
    // Setup the comment for the preinstall driver.
2002
0
    comment = "Rule to relink during preinstall.";
2003
0
  } else {
2004
    // Setup the comment for the main build driver.
2005
0
    comment = "Rule to build all files generated by this target.";
2006
2007
    // Make sure all custom command outputs in this target are built.
2008
0
    if (this->CustomCommandDriver == OnBuild) {
2009
0
      this->DriveCustomCommands(depends);
2010
0
    }
2011
2012
    // Make sure the extra files are built.
2013
0
    cm::append(depends, this->ExtraFiles);
2014
0
  }
2015
2016
  // Write the driver rule.
2017
0
  std::vector<std::string> no_commands;
2018
0
  this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
2019
0
                                      buildTargetRuleName, depends,
2020
0
                                      no_commands, true);
2021
0
}
2022
2023
void cmMakefileTargetGenerator::AppendTargetDepends(
2024
  std::vector<std::string>& depends, bool ignoreType)
2025
0
{
2026
  // Static libraries never depend on anything for linking.
2027
0
  if (this->GeneratorTarget->GetType() == cm::TargetType::STATIC_LIBRARY &&
2028
0
      !ignoreType) {
2029
0
    return;
2030
0
  }
2031
2032
0
  std::string const& cfg = this->GetConfigName();
2033
2034
0
  if (this->GeneratorTarget->HasLinkDependencyFile(cfg)) {
2035
0
    depends.push_back(
2036
0
      cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts"));
2037
0
  }
2038
2039
  // Loop over all library dependencies.
2040
0
  if (cmComputeLinkInformation const* cli =
2041
0
        this->GeneratorTarget->GetLinkInformation(cfg)) {
2042
0
    cm::append(depends, cli->GetDepends());
2043
0
  }
2044
0
}
2045
2046
void cmMakefileTargetGenerator::AppendObjectDepends(
2047
  std::vector<std::string>& depends)
2048
0
{
2049
  // Add dependencies on the compiled object files.
2050
0
  std::string const& relPath =
2051
0
    this->LocalGenerator->GetHomeRelativeOutputPath();
2052
0
  for (std::string const& obj : this->Objects) {
2053
0
    std::string objTarget = cmStrCat(relPath, obj);
2054
0
    depends.push_back(std::move(objTarget));
2055
0
  }
2056
2057
  // Add dependencies on the external object files.
2058
0
  cm::append(depends, this->ExternalObjects);
2059
2060
  // Add dependency on the Rust main crate root file.
2061
0
  if (cmSourceFile const* mainCrateRoot =
2062
0
        this->GeneratorTarget->GetRustMainCrateRoot(this->GetConfigName())) {
2063
0
    depends.push_back(mainCrateRoot->GetFullPath());
2064
0
  }
2065
2066
  // Add a dependency on the rule file itself.
2067
0
  this->LocalGenerator->AppendRuleDepend(depends,
2068
0
                                         this->BuildFileNameFull.c_str());
2069
0
}
2070
2071
void cmMakefileTargetGenerator::AppendLinkDepends(
2072
  std::vector<std::string>& depends, std::string const& linkLanguage)
2073
0
{
2074
0
  this->AppendObjectDepends(depends);
2075
2076
  // Add dependencies on targets that must be built first.
2077
0
  this->AppendTargetDepends(depends);
2078
2079
  // Add a dependency on the link definitions file, if any.
2080
0
  if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
2081
0
        this->GeneratorTarget->GetModuleDefinitionInfo(
2082
0
          this->GetConfigName())) {
2083
0
    for (cmSourceFile const* src : mdi->Sources) {
2084
0
      depends.push_back(src->GetFullPath());
2085
0
    }
2086
0
  }
2087
2088
  // Add a dependency on user-specified manifest files, if any.
2089
0
  std::vector<cmSourceFile const*> manifest_srcs;
2090
0
  this->GeneratorTarget->GetManifests(manifest_srcs, this->GetConfigName());
2091
0
  for (cmSourceFile const* manifest_src : manifest_srcs) {
2092
0
    depends.push_back(manifest_src->GetFullPath());
2093
0
  }
2094
2095
  // Add user-specified dependencies.
2096
0
  this->GeneratorTarget->GetLinkDepends(depends, this->GetConfigName(),
2097
0
                                        linkLanguage);
2098
0
}
2099
2100
std::string cmMakefileTargetGenerator::GetLinkRule(
2101
  std::string const& linkRuleVar)
2102
0
{
2103
0
  std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
2104
0
  if (this->GeneratorTarget->HasImplibGNUtoMS(this->GetConfigName())) {
2105
0
    std::string ruleVar =
2106
0
      cmStrCat("CMAKE_",
2107
0
               this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName()),
2108
0
               "_GNUtoMS_RULE");
2109
0
    if (cmValue rule = this->Makefile->GetDefinition(ruleVar)) {
2110
0
      linkRule += *rule;
2111
0
    }
2112
0
  }
2113
0
  return linkRule;
2114
0
}
2115
2116
void cmMakefileTargetGenerator::CloseFileStreams()
2117
0
{
2118
0
  this->BuildFileStream.reset();
2119
0
  this->InfoFileStream.reset();
2120
0
  this->FlagFileStream.reset();
2121
0
}
2122
2123
void cmMakefileTargetGenerator::CreateLinkScript(
2124
  char const* name, std::vector<std::string> const& link_commands,
2125
  std::vector<std::string>& makefile_commands,
2126
  std::vector<std::string>& makefile_depends)
2127
0
{
2128
  // Create the link script file.
2129
0
  std::string linkScriptName =
2130
0
    cmStrCat(this->TargetBuildDirectoryFull, '/', name);
2131
0
  cmGeneratedFileStream linkScriptStream(linkScriptName);
2132
0
  linkScriptStream.SetCopyIfDifferent(true);
2133
0
  for (std::string const& link_command : link_commands) {
2134
    // Do not write out empty commands or commands beginning in the
2135
    // shell no-op ":".
2136
0
    if (!link_command.empty() && link_command[0] != ':') {
2137
0
      linkScriptStream << link_command << "\n";
2138
0
    }
2139
0
  }
2140
2141
  // Create the makefile command to invoke the link script.
2142
0
  std::string link_command =
2143
0
    cmStrCat("$(CMAKE_COMMAND) -E cmake_link_script ",
2144
0
             this->LocalGenerator->ConvertToOutputFormat(
2145
0
               this->LocalGenerator->MaybeRelativeToCurBinDir(linkScriptName),
2146
0
               cmOutputConverter::SHELL),
2147
0
             " --verbose=$(VERBOSE)");
2148
0
  makefile_commands.push_back(std::move(link_command));
2149
0
  makefile_depends.push_back(std::move(linkScriptName));
2150
0
}
2151
2152
bool cmMakefileTargetGenerator::CheckUseResponseFileForObjects(
2153
  std::string const& l) const
2154
0
{
2155
  // Check for an explicit setting one way or the other.
2156
0
  std::string const responseVar =
2157
0
    "CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_OBJECTS";
2158
0
  if (cmValue val = this->Makefile->GetDefinition(responseVar)) {
2159
0
    if (!val->empty()) {
2160
0
      return val.IsOn();
2161
0
    }
2162
0
  }
2163
2164
  // Check for a system limit.
2165
0
  if (size_t const limit = cmSystemTools::CalculateCommandLineLengthLimit()) {
2166
    // Compute the total length of our list of object files with room
2167
    // for argument separation and quoting.  This does not convert paths
2168
    // relative to CMAKE_CURRENT_BINARY_DIR like the final list will be, so
2169
    // the actual list will likely be much shorter than this.  However, in
2170
    // the worst case all objects will remain as absolute paths.
2171
0
    size_t length = 0;
2172
0
    for (std::string const& obj : this->Objects) {
2173
0
      length += obj.size() + 3;
2174
0
    }
2175
0
    for (std::string const& ext_obj : this->ExternalObjects) {
2176
0
      length += ext_obj.size() + 3;
2177
0
    }
2178
2179
    // We need to guarantee room for both objects and libraries, so
2180
    // if the objects take up more than half then use a response file
2181
    // for them.
2182
0
    if (length > (limit / 2)) {
2183
0
      return true;
2184
0
    }
2185
0
  }
2186
2187
  // We do not need a response file for objects.
2188
0
  return false;
2189
0
}
2190
2191
bool cmMakefileTargetGenerator::CheckUseResponseFileForLibraries(
2192
  std::string const& l) const
2193
0
{
2194
  // Check for an explicit setting one way or the other.
2195
0
  std::string const responseVar =
2196
0
    "CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_LIBRARIES";
2197
0
  if (cmValue val = this->Makefile->GetDefinition(responseVar)) {
2198
0
    if (!val->empty()) {
2199
0
      return val.IsOn();
2200
0
    }
2201
0
  }
2202
2203
  // We do not need a response file for libraries.
2204
0
  return false;
2205
0
}
2206
2207
std::string cmMakefileTargetGenerator::CreateResponseFile(
2208
  std::string const& name, std::string const& options,
2209
  std::vector<std::string>& makefile_depends, std::string const& language)
2210
0
{
2211
  // FIXME: Find a better way to determine the response file encoding,
2212
  // perhaps using tool-specific platform information variables.
2213
  // For now, use the makefile encoding as a heuristic.
2214
0
  codecvt_Encoding responseEncoding =
2215
0
    this->GlobalGenerator->GetMakefileEncoding();
2216
  // Non-MSVC tooling doesn't understand BOM encoded files.
2217
0
  if (responseEncoding == codecvt_Encoding::UTF8_WITH_BOM &&
2218
0
      (language == "CUDA" || !this->Makefile->IsOn("MSVC"))) {
2219
0
    responseEncoding = codecvt_Encoding::UTF8;
2220
0
  }
2221
2222
  // Create the response file.
2223
0
  std::string responseFileNameFull =
2224
0
    cmStrCat(this->TargetBuildDirectoryFull, '/', name);
2225
0
  cmGeneratedFileStream responseStream(responseFileNameFull, false,
2226
0
                                       responseEncoding);
2227
0
  responseStream.SetCopyIfDifferent(true);
2228
0
  responseStream << options << "\n";
2229
2230
  // Add a dependency so the target will rebuild when the set of
2231
  // objects changes.
2232
0
  makefile_depends.push_back(std::move(responseFileNameFull));
2233
2234
  // Construct the name to be used on the command line.
2235
0
  std::string responseFileName =
2236
0
    cmStrCat(this->TargetBuildDirectory, '/', name);
2237
0
  return responseFileName;
2238
0
}
2239
2240
std::unique_ptr<cmLinkLineComputer>
2241
cmMakefileTargetGenerator::CreateLinkLineComputer(
2242
  cmOutputConverter* outputConverter, cmStateDirectory const& stateDir)
2243
0
{
2244
0
  if (this->Makefile->IsOn("MSVC60")) {
2245
0
    return this->GlobalGenerator->CreateMSVC60LinkLineComputer(outputConverter,
2246
0
                                                               stateDir);
2247
0
  }
2248
0
  return this->GlobalGenerator->CreateLinkLineComputer(outputConverter,
2249
0
                                                       stateDir);
2250
0
}
2251
2252
void cmMakefileTargetGenerator::CreateLinkLibs(
2253
  cmLinkLineComputer* linkLineComputer, std::string& linkLibs,
2254
  bool useResponseFile, std::vector<std::string>& makefile_depends,
2255
  std::string const& linkLanguage, ResponseFlagFor responseMode)
2256
0
{
2257
0
  if (cmComputeLinkInformation* pcli =
2258
0
        this->GeneratorTarget->GetLinkInformation(this->GetConfigName())) {
2259
0
    std::string frameworkPath;
2260
0
    std::string linkPath;
2261
0
    this->LocalGenerator->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
2262
0
                                              frameworkPath, linkPath);
2263
0
    linkLibs = frameworkPath + linkPath + linkLibs;
2264
0
  }
2265
2266
0
  if (useResponseFile &&
2267
0
      linkLibs.find_first_not_of(' ') != std::string::npos) {
2268
    // Lookup the response file reference flag.
2269
0
    std::string responseFlag = this->GetResponseFlag(responseMode);
2270
2271
    // Create this response file.
2272
0
    std::string const responseFileName =
2273
0
      (responseMode == DeviceLink) ? "deviceLinkLibs.rsp" : "linkLibs.rsp";
2274
0
    std::string const responseLang =
2275
0
      (responseMode == DeviceLink) ? "CUDA" : linkLanguage;
2276
0
    std::string link_rsp = this->CreateResponseFile(
2277
0
      responseFileName, linkLibs, makefile_depends, responseLang);
2278
2279
    // Reference the response file.
2280
0
    linkLibs = cmStrCat(std::move(responseFlag),
2281
0
                        this->LocalGenerator->ConvertToOutputFormat(
2282
0
                          std::move(link_rsp), cmOutputConverter::SHELL));
2283
0
  }
2284
0
}
2285
2286
void cmMakefileTargetGenerator::CreateObjectLists(
2287
  bool useLinkScript, bool useArchiveRules, bool useResponseFile,
2288
  std::string& buildObjs, std::vector<std::string>& makefile_depends,
2289
  bool useWatcomQuote, std::string const& linkLanguage,
2290
  ResponseFlagFor responseMode)
2291
0
{
2292
0
  std::string variableName;
2293
0
  std::string variableNameExternal;
2294
0
  this->WriteObjectsVariable(variableName, variableNameExternal,
2295
0
                             useWatcomQuote);
2296
0
  if (useResponseFile) {
2297
    // MSVC response files cannot exceed 128K.
2298
0
    std::string::size_type constexpr responseFileLimit = 131000;
2299
2300
    // Construct the individual object list strings.
2301
0
    std::vector<std::string> object_strings;
2302
0
    this->WriteObjectsStrings(object_strings, useWatcomQuote,
2303
0
                              responseFileLimit);
2304
2305
    // Lookup the response file reference flag.
2306
0
    std::string const responseFlag = this->GetResponseFlag(responseMode);
2307
2308
    // Write a response file for each string.
2309
0
    char const* sep = "";
2310
0
    for (unsigned int i = 0; i < object_strings.size(); ++i) {
2311
      // Number the response files.
2312
0
      std::string responseFileName =
2313
0
        cmStrCat((responseMode == DeviceLink) ? "deviceObjects" : "objects",
2314
0
                 i + 1, ".rsp");
2315
2316
      // Create this response file.
2317
0
      std::string objects_rsp = this->CreateResponseFile(
2318
0
        responseFileName, object_strings[i], makefile_depends, linkLanguage);
2319
2320
0
      buildObjs +=
2321
0
        cmStrCat(sep, // Separate from previous response file references.
2322
0
                 responseFlag, // Reference the response file.
2323
0
                 this->LocalGenerator->ConvertToOutputFormat(
2324
0
                   objects_rsp, cmOutputConverter::SHELL));
2325
0
      sep = " ";
2326
0
    }
2327
0
  } else if (useLinkScript) {
2328
0
    if (!useArchiveRules) {
2329
0
      std::vector<std::string> objStrings;
2330
0
      this->WriteObjectsStrings(objStrings, useWatcomQuote);
2331
0
      buildObjs = objStrings[0];
2332
0
    }
2333
0
  } else {
2334
0
    buildObjs =
2335
0
      cmStrCat("$(", variableName, ") $(", variableNameExternal, ')');
2336
0
  }
2337
0
}
2338
2339
bool cmMakefileTargetGenerator::CreateRustLinkArguments(
2340
  std::string const& linkLanguage, std::string& rustMainCrateRootPath,
2341
  std::string& rustLinkCrates, std::string& rustNativeObjects)
2342
0
{
2343
0
  if (linkLanguage == "Rust") {
2344
0
    this->ComputeRustFlagsForObjects(rustLinkCrates, rustNativeObjects,
2345
0
                                     this->Objects);
2346
0
    this->ComputeRustFlagsForObjects(rustLinkCrates, rustNativeObjects,
2347
0
                                     this->ExternalObjects);
2348
2349
0
    cmSourceFile const* mainCrateRoot =
2350
0
      this->GeneratorTarget->GetRustMainCrateRoot(this->GetConfigName());
2351
0
    if (!mainCrateRoot) {
2352
0
      this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
2353
0
                                   "Target " +
2354
0
                                     this->GeneratorTarget->GetName() +
2355
0
                                     " has no main crate root.");
2356
0
      return false;
2357
0
    }
2358
0
    rustMainCrateRootPath = mainCrateRoot->GetFullPath();
2359
0
    rustMainCrateRootPath = this->LocalGenerator->ConvertToOutputFormat(
2360
0
      rustMainCrateRootPath, cmOutputConverter::SHELL);
2361
0
    return true;
2362
0
  }
2363
0
  return false;
2364
0
}
2365
2366
void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
2367
                                                std::string const& lang,
2368
                                                std::string const& /*config*/)
2369
0
{
2370
0
  std::string const responseVar =
2371
0
    cmStrCat("CMAKE_", lang, "_USE_RESPONSE_FILE_FOR_INCLUDES");
2372
0
  bool const useResponseFile = this->Makefile->IsOn(responseVar);
2373
2374
0
  std::vector<std::string> includes;
2375
0
  this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
2376
0
                                              lang, this->GetConfigName());
2377
2378
0
  std::string const includeFlags = this->LocalGenerator->GetIncludeFlags(
2379
0
    includes, this->GeneratorTarget, lang, this->GetConfigName(),
2380
0
    useResponseFile);
2381
0
  if (includeFlags.empty()) {
2382
0
    return;
2383
0
  }
2384
2385
0
  if (useResponseFile) {
2386
0
    std::string const responseFlagVar =
2387
0
      "CMAKE_" + lang + "_RESPONSE_FILE_FLAG";
2388
0
    std::string responseFlag =
2389
0
      this->Makefile->GetSafeDefinition(responseFlagVar);
2390
0
    if (responseFlag.empty()) {
2391
0
      responseFlag = "@";
2392
0
    }
2393
0
    std::string const name = cmStrCat("includes_", lang, ".rsp");
2394
0
    std::string const includes_rsp = this->CreateResponseFile(
2395
0
      name, includeFlags, this->FlagFileDepends[lang], lang);
2396
0
    std::string const arg =
2397
0
      cmStrCat(std::move(responseFlag),
2398
0
               this->LocalGenerator->ConvertToOutputFormat(
2399
0
                 includes_rsp, cmOutputConverter::SHELL));
2400
0
    this->LocalGenerator->AppendFlags(flags, arg);
2401
0
  } else {
2402
0
    this->LocalGenerator->AppendFlags(flags, includeFlags);
2403
0
  }
2404
0
}
2405
2406
void cmMakefileTargetGenerator::GenDefFile(
2407
  std::vector<std::string>& real_link_commands)
2408
0
{
2409
0
  cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
2410
0
    this->GeneratorTarget->GetModuleDefinitionInfo(this->GetConfigName());
2411
0
  if (!mdi || !mdi->DefFileGenerated) {
2412
0
    return;
2413
0
  }
2414
0
  std::string cmd = cmSystemTools::GetCMakeCommand();
2415
0
  cmd = cmStrCat(
2416
0
    this->LocalGenerator->ConvertToOutputFormat(cmd, cmOutputConverter::SHELL),
2417
0
    " -E __create_def ",
2418
0
    this->LocalGenerator->ConvertToOutputFormat(
2419
0
      this->LocalGenerator->MaybeRelativeToCurBinDir(mdi->DefFile),
2420
0
      cmOutputConverter::SHELL),
2421
0
    ' ');
2422
0
  std::string objlist_file = mdi->DefFile + ".objs";
2423
0
  cmd += this->LocalGenerator->ConvertToOutputFormat(
2424
0
    this->LocalGenerator->MaybeRelativeToCurBinDir(objlist_file),
2425
0
    cmOutputConverter::SHELL);
2426
0
  cmValue const nm_executable = this->Makefile->GetDefinition("CMAKE_NM");
2427
0
  if (cmNonempty(nm_executable)) {
2428
0
    cmd += " --nm=";
2429
0
    cmd += this->LocalCommonGenerator->ConvertToOutputFormat(
2430
0
      *nm_executable, cmOutputConverter::SHELL);
2431
0
  }
2432
0
  real_link_commands.insert(real_link_commands.begin(), cmd);
2433
  // create a list of obj files for the -E __create_def to read
2434
0
  cmGeneratedFileStream fout(objlist_file);
2435
2436
0
  if (mdi->WindowsExportAllSymbols) {
2437
0
    for (std::string const& obj : this->Objects) {
2438
0
      if (cmHasLiteralSuffix(obj, ".obj")) {
2439
0
        fout << obj << "\n";
2440
0
      }
2441
0
    }
2442
0
    for (std::string const& obj : this->ExternalObjects) {
2443
0
      fout << obj << "\n";
2444
0
    }
2445
0
  }
2446
2447
0
  for (cmSourceFile const* src : mdi->Sources) {
2448
0
    fout << src->GetFullPath() << "\n";
2449
0
  }
2450
0
}
2451
2452
std::string cmMakefileTargetGenerator::GetResponseFlag(
2453
  ResponseFlagFor mode) const
2454
0
{
2455
0
  std::string responseFlag = "@";
2456
0
  std::string responseFlagVar;
2457
2458
0
  auto const lang =
2459
0
    this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
2460
0
  if (mode == cmMakefileTargetGenerator::ResponseFlagFor::Link) {
2461
0
    responseFlagVar = cmStrCat("CMAKE_", lang, "_RESPONSE_FILE_LINK_FLAG");
2462
0
  } else if (mode == cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink) {
2463
0
    responseFlagVar = "CMAKE_CUDA_RESPONSE_FILE_DEVICE_LINK_FLAG";
2464
0
  } else if (mode == cmMakefileTargetGenerator::ResponseFlagFor::Archive) {
2465
0
    responseFlagVar = cmStrCat("CMAKE_", lang, "_RESPONSE_FILE_ARCHIVE_FLAG");
2466
0
  }
2467
2468
0
  if (cmValue const p = this->Makefile->GetDefinition(responseFlagVar)) {
2469
0
    responseFlag = *p;
2470
0
  } else if (mode == cmMakefileTargetGenerator::ResponseFlagFor::Archive) {
2471
0
    responseFlagVar = cmStrCat("CMAKE_", lang, "_RESPONSE_FILE_LINK_FLAG");
2472
0
    if (cmValue const q = this->Makefile->GetDefinition(responseFlagVar)) {
2473
0
      responseFlag = *q;
2474
0
    }
2475
0
  }
2476
0
  return responseFlag;
2477
0
}