Coverage Report

Created: 2026-04-29 07:01

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 "cmGeneratorFileSets.h"
33
#include "cmGeneratorOptions.h"
34
#include "cmGeneratorTarget.h"
35
#include "cmGlobalUnixMakefileGenerator3.h"
36
#include "cmLinkLineComputer.h" // IWYU pragma: keep
37
#include "cmList.h"
38
#include "cmListFileCache.h"
39
#include "cmLocalCommonGenerator.h"
40
#include "cmLocalGenerator.h"
41
#include "cmLocalUnixMakefileGenerator3.h"
42
#include "cmMakefile.h"
43
#include "cmMakefileExecutableTargetGenerator.h"
44
#include "cmMakefileLibraryTargetGenerator.h"
45
#include "cmMakefileUtilityTargetGenerator.h"
46
#include "cmMessageType.h"
47
#include "cmOutputConverter.h"
48
#include "cmPolicies.h"
49
#include "cmRange.h"
50
#include "cmRulePlaceholderExpander.h"
51
#include "cmScriptGenerator.h"
52
#include "cmSourceFile.h"
53
#include "cmSourceFileLocationKind.h"
54
#include "cmState.h"
55
#include "cmStateDirectory.h"
56
#include "cmStateSnapshot.h"
57
#include "cmStateTypes.h"
58
#include "cmStringAlgorithms.h"
59
#include "cmSystemTools.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 cmStateEnums::EXECUTABLE:
100
0
      result = cm::make_unique<cmMakefileExecutableTargetGenerator>(tgt);
101
0
      break;
102
0
    case cmStateEnums::STATIC_LIBRARY:
103
0
    case cmStateEnums::SHARED_LIBRARY:
104
0
    case cmStateEnums::MODULE_LIBRARY:
105
0
    case cmStateEnums::OBJECT_LIBRARY:
106
0
      result = cm::make_unique<cmMakefileLibraryTargetGenerator>(tgt);
107
0
      break;
108
0
    case cmStateEnums::INTERFACE_LIBRARY:
109
0
    case cmStateEnums::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
  // Add precompile headers dependencies
669
0
  std::vector<std::string> pchArchs =
670
0
    this->GeneratorTarget->GetPchArchs(config, lang);
671
672
0
  std::string filterArch;
673
0
  std::unordered_map<std::string, std::string> pchSources;
674
0
  for (std::string const& arch : pchArchs) {
675
0
    std::string const pchSource =
676
0
      this->GeneratorTarget->GetPchSource(config, lang, arch);
677
0
    if (pchSource == source.GetFullPath()) {
678
0
      filterArch = arch;
679
0
    }
680
0
    if (!pchSource.empty()) {
681
0
      pchSources.insert(std::make_pair(pchSource, arch));
682
0
    }
683
0
  }
684
685
0
  if (!pchSources.empty() && !source.GetProperty("SKIP_PRECOMPILE_HEADERS")) {
686
0
    for (std::string const& arch : pchArchs) {
687
0
      std::string const& pchHeader =
688
0
        this->GeneratorTarget->GetPchHeader(config, lang, arch);
689
0
      depends.push_back(pchHeader);
690
0
      if (pchSources.find(source.GetFullPath()) == pchSources.end()) {
691
0
        depends.push_back(
692
0
          this->GeneratorTarget->GetPchFile(config, lang, arch));
693
0
      }
694
0
      this->LocalGenerator->AddImplicitDepends(
695
0
        this->GeneratorTarget, lang, objFullPath, pchHeader, scanner);
696
0
    }
697
0
  }
698
699
0
  if (lang != "ISPC") {
700
0
    auto const& headers =
701
0
      this->GeneratorTarget->GetGeneratedISPCHeaders(config);
702
0
    if (!headers.empty()) {
703
0
      depends.insert(depends.end(), headers.begin(), headers.end());
704
0
    }
705
0
  }
706
707
0
  std::string relativeObj =
708
0
    cmStrCat(this->LocalGenerator->GetHomeRelativeOutputPath(), obj);
709
  // Write the build rule.
710
711
  // Build the set of compiler flags.
712
0
  std::string flags;
713
714
  // Explicitly add the explicit language flag before any other flag
715
  // so user flags can override it.
716
0
  this->GeneratorTarget->AddExplicitLanguageFlags(flags, source);
717
718
  // Add language-specific flags.
719
0
  std::string const langFlags =
720
0
    cmStrCat("$(", lang, "_FLAGS", filterArch, ')');
721
0
  this->LocalGenerator->AppendFlags(flags, langFlags);
722
723
0
  cmGeneratorExpressionInterpreter genexInterpreter(
724
0
    this->LocalGenerator, config, this->GeneratorTarget, lang);
725
726
  // Add Fortran format flags.
727
0
  if (lang == "Fortran") {
728
0
    this->AppendFortranFormatFlags(flags, source);
729
0
    this->AppendFortranPreprocessFlags(flags, source);
730
0
  }
731
732
0
  std::string ispcHeaderRelative;
733
0
  std::string ispcHeaderForShell;
734
0
  if (lang == "ISPC") {
735
0
    std::string ispcSource =
736
0
      cmSystemTools::GetFilenameWithoutLastExtension(objectName);
737
0
    ispcSource = cmSystemTools::GetFilenameWithoutLastExtension(ispcSource);
738
739
0
    cmValue const ispcSuffixProp =
740
0
      this->GeneratorTarget->GetProperty("ISPC_HEADER_SUFFIX");
741
0
    assert(ispcSuffixProp);
742
743
0
    std::string directory = this->GeneratorTarget->GetObjectDirectory(config);
744
0
    if (cmValue prop =
745
0
          this->GeneratorTarget->GetProperty("ISPC_HEADER_DIRECTORY")) {
746
0
      directory =
747
0
        cmStrCat(this->LocalGenerator->GetBinaryDirectory(), '/', *prop);
748
0
    }
749
0
    ispcHeaderRelative = cmStrCat(directory, '/', ispcSource, *ispcSuffixProp);
750
0
    ispcHeaderForShell = this->LocalGenerator->ConvertToOutputFormat(
751
0
      ispcHeaderRelative, cmOutputConverter::SHELL);
752
0
  }
753
754
  // lookup for the associated file set, if any.
755
0
  auto const* fileSet =
756
0
    this->GeneratorTarget->GetGeneratorFileSets()->GetFileSetForSource(
757
0
      config, &source);
758
759
  // Add flags from source file properties.
760
0
  std::string const COMPILE_FLAGS("COMPILE_FLAGS");
761
0
  if (cmValue cflags = source.GetProperty(COMPILE_FLAGS)) {
762
0
    std::string const& evaluatedFlags =
763
0
      genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS);
764
0
    this->LocalGenerator->AppendFlags(flags, evaluatedFlags);
765
0
    *this->FlagFileStream << "# Custom flags: " << relativeObj
766
0
                          << "_FLAGS = " << evaluatedFlags << "\n"
767
0
                          << "\n";
768
0
  }
769
770
0
  std::string const COMPILE_OPTIONS("COMPILE_OPTIONS");
771
0
  if (cmValue coptions = source.GetProperty(COMPILE_OPTIONS)) {
772
0
    std::string const& evaluatedOptions =
773
0
      genexInterpreter.Evaluate(*coptions, COMPILE_OPTIONS);
774
0
    this->LocalGenerator->AppendCompileOptions(flags, evaluatedOptions);
775
0
    *this->FlagFileStream << "# Custom options: " << relativeObj
776
0
                          << "_OPTIONS = " << evaluatedOptions << "\n"
777
0
                          << "\n";
778
0
  }
779
780
  // Add flags from file set properties.
781
0
  if (fileSet) {
782
0
    auto options = fileSet->BelongsTo(this->GeneratorTarget)
783
0
      ? fileSet->GetCompileOptions(config, lang)
784
0
      : fileSet->GetInterfaceCompileOptions(config, lang);
785
0
    if (!options.empty()) {
786
0
      this->LocalGenerator->AppendCompileOptions(flags,
787
0
                                                 cm::remove_BT(options));
788
0
      *this->FlagFileStream << "# Custom options (File Set '"
789
0
                            << fileSet->GetName() << "'): " << relativeObj
790
0
                            << "_OPTIONS = " << cmList::to_string(options)
791
0
                            << "\n"
792
0
                            << "\n";
793
0
    }
794
0
  }
795
796
  // Add precompile headers compile options.
797
0
  if (!pchSources.empty() && !source.GetProperty("SKIP_PRECOMPILE_HEADERS")) {
798
0
    std::string pchOptions;
799
0
    auto const pchIt = pchSources.find(source.GetFullPath());
800
0
    if (pchIt != pchSources.end()) {
801
0
      pchOptions = this->GeneratorTarget->GetPchCreateCompileOptions(
802
0
        config, lang, pchIt->second);
803
0
    } else {
804
0
      pchOptions =
805
0
        this->GeneratorTarget->GetPchUseCompileOptions(config, lang);
806
0
    }
807
808
0
    std::string const& evaluatedFlags =
809
0
      genexInterpreter.Evaluate(pchOptions, COMPILE_OPTIONS);
810
811
0
    this->LocalGenerator->AppendCompileOptions(flags, evaluatedFlags);
812
0
    *this->FlagFileStream << "# PCH options: " << relativeObj
813
0
                          << "_OPTIONS = " << evaluatedFlags << "\n"
814
0
                          << "\n";
815
0
  }
816
817
  // Add include directories from file set properties.
818
0
  std::vector<std::string> includes;
819
820
0
  if (fileSet) {
821
0
    auto fsIncludes = fileSet->BelongsTo(this->GeneratorTarget)
822
0
      ? fileSet->GetIncludeDirectories(config, lang)
823
0
      : fileSet->GetInterfaceIncludeDirectories(config, lang);
824
0
    if (!fsIncludes.empty()) {
825
0
      this->LocalGenerator->AppendIncludeDirectories(
826
0
        includes, cm::remove_BT(fsIncludes), source);
827
0
      *this->FlagFileStream
828
0
        << "# Custom include directories (File Set '" << fileSet->GetName()
829
0
        << "'): " << relativeObj
830
0
        << "_INCLUDE_DIRECTORIES = " << cmList::to_string(fsIncludes) << "\n"
831
0
        << "\n";
832
0
    }
833
0
  }
834
835
  // Add include directories from source file properties.
836
0
  std::string const INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
837
0
  if (cmValue cincludes = source.GetProperty(INCLUDE_DIRECTORIES)) {
838
0
    std::string const& evaluatedIncludes =
839
0
      genexInterpreter.Evaluate(*cincludes, INCLUDE_DIRECTORIES);
840
0
    this->LocalGenerator->AppendIncludeDirectories(includes, evaluatedIncludes,
841
0
                                                   source);
842
0
    *this->FlagFileStream << "# Custom include directories: " << relativeObj
843
0
                          << "_INCLUDE_DIRECTORIES = " << evaluatedIncludes
844
0
                          << "\n"
845
0
                          << "\n";
846
0
  }
847
848
  // Add language-specific defines.
849
0
  std::set<std::string> defines;
850
851
  // Add source-specific preprocessor definitions.
852
0
  std::string const COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
853
0
  if (cmValue compile_defs = source.GetProperty(COMPILE_DEFINITIONS)) {
854
0
    std::string const& evaluatedDefs =
855
0
      genexInterpreter.Evaluate(*compile_defs, COMPILE_DEFINITIONS);
856
0
    this->LocalGenerator->AppendDefines(defines, evaluatedDefs);
857
0
    *this->FlagFileStream << "# Custom defines: " << relativeObj
858
0
                          << "_DEFINES = " << evaluatedDefs << "\n"
859
0
                          << "\n";
860
0
  }
861
0
  std::string const defPropName =
862
0
    cmStrCat("COMPILE_DEFINITIONS_", configUpper);
863
0
  if (cmValue config_compile_defs = source.GetProperty(defPropName)) {
864
0
    std::string const& evaluatedDefs =
865
0
      genexInterpreter.Evaluate(*config_compile_defs, COMPILE_DEFINITIONS);
866
0
    this->LocalGenerator->AppendDefines(defines, evaluatedDefs);
867
0
    *this->FlagFileStream << "# Custom defines: " << relativeObj << "_DEFINES_"
868
0
                          << configUpper << " = " << evaluatedDefs << "\n"
869
0
                          << "\n";
870
0
  }
871
872
  // Add file set preprocessor definitions
873
0
  if (fileSet) {
874
0
    auto fsDefines = fileSet->BelongsTo(this->GeneratorTarget)
875
0
      ? fileSet->GetCompileDefinitions(config, lang)
876
0
      : fileSet->GetInterfaceCompileDefinitions(config, lang);
877
0
    if (!fsDefines.empty()) {
878
0
      this->LocalGenerator->AppendDefines(defines, fsDefines);
879
0
      *this->FlagFileStream << "# Custom defines (File Set '"
880
0
                            << fileSet->GetName() << "'): " << relativeObj
881
0
                            << "_DEFINES = " << cmList::to_string(fsDefines)
882
0
                            << "\n"
883
0
                            << "\n";
884
0
    }
885
0
  }
886
887
  // Get the output paths for source and object files.
888
0
  std::string const sourceFile = this->LocalGenerator->ConvertToOutputFormat(
889
0
    source.GetFullPath(), cmOutputConverter::SHELL);
890
891
  // Construct the build message.
892
0
  std::vector<std::string> no_depends;
893
0
  std::vector<std::string> commands;
894
895
  // add in a progress call if needed
896
0
  this->NumberOfProgressActions++;
897
898
0
  if (!this->NoRuleMessages) {
899
0
    cmLocalUnixMakefileGenerator3::EchoProgress progress;
900
0
    this->MakeEchoProgress(progress);
901
0
    std::string buildEcho =
902
0
      cmStrCat("Building ", lang, " object ", relativeObj);
903
0
    this->LocalGenerator->AppendEcho(commands, buildEcho,
904
0
                                     cmLocalUnixMakefileGenerator3::EchoBuild,
905
0
                                     &progress);
906
0
  }
907
908
0
  std::string targetOutPathReal;
909
0
  std::string targetOutPathPDB;
910
0
  std::string targetOutPathCompilePDB;
911
0
  {
912
0
    std::string targetFullPathReal;
913
0
    std::string targetFullPathPDB;
914
0
    std::string targetFullPathCompilePDB =
915
0
      this->ComputeTargetCompilePDB(this->GetConfigName());
916
0
    if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
917
0
        this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
918
0
        this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
919
0
        this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
920
0
      targetFullPathReal = this->GeneratorTarget->GetFullPath(
921
0
        this->GetConfigName(), cmStateEnums::RuntimeBinaryArtifact, true);
922
0
      targetFullPathPDB = cmStrCat(
923
0
        this->GeneratorTarget->GetPDBDirectory(this->GetConfigName()), '/',
924
0
        this->GeneratorTarget->GetPDBName(this->GetConfigName()));
925
0
    }
926
927
0
    targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
928
0
      this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
929
0
      cmOutputConverter::SHELL);
930
0
    targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
931
0
      targetFullPathPDB, cmOutputConverter::SHELL);
932
0
    targetOutPathCompilePDB = this->LocalGenerator->ConvertToOutputFormat(
933
0
      this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathCompilePDB),
934
0
      cmOutputConverter::SHELL);
935
936
0
    if (this->LocalGenerator->IsMinGWMake() &&
937
0
        cmHasSuffix(targetOutPathCompilePDB, '\\')) {
938
      // mingw32-make incorrectly interprets 'a\ b c' as 'a b' and 'c'
939
      // (but 'a\ b "c"' as 'a\', 'b', and 'c'!).  Workaround this by
940
      // avoiding a trailing backslash in the argument.
941
0
      targetOutPathCompilePDB.back() = '/';
942
0
    }
943
944
0
    std::string const compilePdbOutputPath =
945
0
      this->GeneratorTarget->GetCompilePDBDirectory(this->GetConfigName());
946
0
    cmSystemTools::MakeDirectory(compilePdbOutputPath);
947
0
  }
948
0
  cmRulePlaceholderExpander::RuleVariables vars;
949
0
  vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
950
0
  vars.CMTargetType =
951
0
    cmState::GetTargetTypeName(this->GeneratorTarget->GetType()).c_str();
952
0
  vars.Language = lang.c_str();
953
0
  vars.Target = targetOutPathReal.c_str();
954
0
  vars.TargetPDB = targetOutPathPDB.c_str();
955
0
  vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
956
0
  vars.Source = sourceFile.c_str();
957
0
  std::string const shellObj =
958
0
    this->LocalGenerator->ConvertToOutputFormat(obj, cmOutputConverter::SHELL);
959
0
  vars.Object = shellObj.c_str();
960
0
  std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
961
0
  objectDir = this->LocalGenerator->ConvertToOutputFormat(
962
0
    this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
963
0
    cmOutputConverter::SHELL);
964
0
  vars.ObjectDir = objectDir.c_str();
965
0
  std::string targetSupportDir =
966
0
    this->GeneratorTarget->GetCMFSupportDirectory();
967
0
  targetSupportDir = this->LocalGenerator->ConvertToOutputFormat(
968
0
    this->LocalGenerator->MaybeRelativeToTopBinDir(targetSupportDir),
969
0
    cmOutputConverter::SHELL);
970
0
  vars.TargetSupportDir = targetSupportDir.c_str();
971
0
  std::string objectFileDir = cmSystemTools::GetFilenamePath(obj);
972
0
  objectFileDir = this->LocalGenerator->ConvertToOutputFormat(
973
0
    this->LocalGenerator->MaybeRelativeToCurBinDir(objectFileDir),
974
0
    cmOutputConverter::SHELL);
975
0
  vars.ObjectFileDir = objectFileDir.c_str();
976
0
  vars.Flags = flags.c_str();
977
0
  vars.ISPCHeader = ispcHeaderForShell.c_str();
978
0
  vars.Config = this->GetConfigName().c_str();
979
980
0
  std::string definesString = cmStrCat("$(", lang, "_DEFINES)");
981
982
0
  this->LocalGenerator->JoinDefines(defines, definesString, lang);
983
984
0
  vars.Defines = definesString.c_str();
985
986
0
  std::string includesString = this->LocalGenerator->GetIncludeFlags(
987
0
    includes, this->GeneratorTarget, lang, config);
988
0
  this->LocalGenerator->AppendFlags(includesString,
989
0
                                    "$(" + lang + "_INCLUDES)");
990
0
  vars.Includes = includesString.c_str();
991
992
0
  std::string dependencyTarget;
993
0
  std::string shellDependencyFile;
994
0
  std::string dependencyTimestamp;
995
0
  if (compilerGenerateDeps) {
996
0
    dependencyTarget = this->LocalGenerator->EscapeForShell(
997
0
      this->LocalGenerator->ConvertToMakefilePath(
998
0
        this->LocalGenerator->MaybeRelativeToTopBinDir(relativeObj)));
999
0
    vars.DependencyTarget = dependencyTarget.c_str();
1000
1001
0
    auto depFile = cmStrCat(obj, ".d");
1002
0
    shellDependencyFile = this->LocalGenerator->ConvertToOutputFormat(
1003
0
      depFile, cmOutputConverter::SHELL);
1004
0
    vars.DependencyFile = shellDependencyFile.c_str();
1005
0
    this->CleanFiles.insert(depFile);
1006
1007
0
    dependencyTimestamp = this->LocalGenerator->MaybeRelativeToTopBinDir(
1008
0
      cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts"));
1009
0
  }
1010
1011
  // At the moment, it is assumed that C, C++, Fortran, and CUDA have both
1012
  // assembly and preprocessor capabilities. The same is true for the
1013
  // ability to export compile commands
1014
0
  bool const lang_has_preprocessor =
1015
0
    ((lang == "C") || (lang == "CXX") || (lang == "OBJC") ||
1016
0
     (lang == "OBJCXX") || (lang == "Fortran") || (lang == "CUDA") ||
1017
0
     lang == "ISPC" || lang == "HIP" || lang == "ASM");
1018
0
  bool const lang_has_assembly = lang_has_preprocessor;
1019
0
  bool const lang_can_export_cmds = lang_has_preprocessor;
1020
1021
0
  auto rulePlaceholderExpander =
1022
0
    this->LocalGenerator->CreateRulePlaceholderExpander();
1023
1024
  // Construct the compile rules.
1025
0
  {
1026
0
    std::string cudaCompileMode;
1027
0
    if (lang == "CUDA") {
1028
0
      if (this->GeneratorTarget->GetPropertyAsBool(
1029
0
            "CUDA_SEPARABLE_COMPILATION")) {
1030
0
        std::string const& rdcFlag =
1031
0
          this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG");
1032
0
        cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, ' ');
1033
0
      }
1034
1035
0
      static std::array<cm::string_view, 4> const compileModes{
1036
0
        { "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s }
1037
0
      };
1038
0
      bool useNormalCompileMode = true;
1039
0
      for (cm::string_view mode : compileModes) {
1040
0
        auto propName = cmStrCat("CUDA_", mode, "_COMPILATION");
1041
0
        auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG");
1042
0
        if (this->GeneratorTarget->GetPropertyAsBool(propName)) {
1043
0
          std::string const& flag =
1044
0
            this->Makefile->GetRequiredDefinition(defName);
1045
0
          cudaCompileMode = cmStrCat(cudaCompileMode, flag);
1046
0
          useNormalCompileMode = false;
1047
0
          break;
1048
0
        }
1049
0
      }
1050
0
      if (useNormalCompileMode) {
1051
0
        std::string const& wholeFlag =
1052
0
          this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG");
1053
0
        cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag);
1054
0
      }
1055
0
      vars.CudaCompileMode = cudaCompileMode.c_str();
1056
0
    }
1057
1058
0
    cmList compileCommands;
1059
0
    std::string const& compileRule = this->Makefile->GetRequiredDefinition(
1060
0
      "CMAKE_" + lang + "_COMPILE_OBJECT");
1061
0
    compileCommands.assign(compileRule);
1062
1063
0
    if (this->GeneratorTarget->GetPropertyAsBool("EXPORT_COMPILE_COMMANDS") &&
1064
0
        lang_can_export_cmds && compileCommands.size() == 1) {
1065
0
      std::string compileCommand = compileCommands[0];
1066
1067
      // no launcher for CMAKE_EXPORT_COMPILE_COMMANDS
1068
0
      rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
1069
0
                                                   compileCommand, vars);
1070
0
      std::string const workingDirectory =
1071
0
        this->LocalGenerator->GetCurrentBinaryDirectory();
1072
0
      std::string::size_type lfPos = compileCommand.find(langFlags);
1073
0
      if (lfPos != std::string::npos) {
1074
0
        compileCommand.replace(lfPos, langFlags.size(),
1075
0
                               this->GetFlags(lang, this->GetConfigName()));
1076
0
      }
1077
0
      std::string const langDefines = std::string("$(") + lang + "_DEFINES)";
1078
0
      std::string::size_type const ldPos = compileCommand.find(langDefines);
1079
0
      if (ldPos != std::string::npos) {
1080
0
        compileCommand.replace(ldPos, langDefines.size(),
1081
0
                               this->GetDefines(lang, this->GetConfigName()));
1082
0
      }
1083
0
      std::string const langIncludes = std::string("$(") + lang + "_INCLUDES)";
1084
0
      std::string::size_type const liPos = compileCommand.find(langIncludes);
1085
0
      if (liPos != std::string::npos) {
1086
0
        compileCommand.replace(liPos, langIncludes.size(),
1087
0
                               this->GetIncludes(lang, this->GetConfigName()));
1088
0
      }
1089
1090
0
      cmValue const eliminate[] = {
1091
0
        this->Makefile->GetDefinition("CMAKE_START_TEMP_FILE"),
1092
0
        this->Makefile->GetDefinition("CMAKE_END_TEMP_FILE")
1093
0
      };
1094
0
      for (cmValue const& el : eliminate) {
1095
0
        if (el) {
1096
0
          cmSystemTools::ReplaceString(compileCommand, *el, "");
1097
0
        }
1098
0
      }
1099
1100
0
      this->GlobalGenerator->AddCXXCompileCommand(
1101
0
        source.GetFullPath(), workingDirectory, compileCommand, relativeObj);
1102
0
    }
1103
1104
    // See if we need to use a compiler launcher like ccache or distcc
1105
0
    std::string compilerLauncher;
1106
0
    if (!compileCommands.empty()) {
1107
0
      compilerLauncher = GetCompilerLauncher(lang, config);
1108
0
    }
1109
1110
0
    cmValue const srcSkipCodeCheckVal = source.GetProperty("SKIP_LINTING");
1111
0
    bool const skipCodeCheck = srcSkipCodeCheckVal.IsSet()
1112
0
      ? srcSkipCodeCheckVal.IsOn()
1113
0
      : this->GetGeneratorTarget()->GetPropertyAsBool("SKIP_LINTING");
1114
1115
0
    if (!skipCodeCheck) {
1116
0
      std::string const codeCheck = this->GenerateCodeCheckRules(
1117
0
        source, compilerLauncher, "$(CMAKE_COMMAND)", config, nullptr);
1118
0
      if (!codeCheck.empty()) {
1119
0
        compileCommands.front().insert(0, codeCheck);
1120
0
      }
1121
0
    }
1122
1123
    // If compiler launcher was specified and not consumed above, it
1124
    // goes to the beginning of the command line.
1125
0
    if (!compileCommands.empty() && !compilerLauncher.empty()) {
1126
0
      cmList args{ compilerLauncher, cmList::EmptyElements::Yes };
1127
0
      if (!args.empty()) {
1128
0
        args[0] = this->LocalGenerator->ConvertToOutputFormat(
1129
0
          args[0], cmOutputConverter::SHELL);
1130
0
        for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
1131
0
          i = this->LocalGenerator->EscapeForShell(i);
1132
0
        }
1133
0
      }
1134
0
      compileCommands.front().insert(0, args.join(" ") + " ");
1135
0
    }
1136
1137
0
    std::string launcher;
1138
0
    {
1139
0
      std::string val = this->LocalGenerator->GetRuleLauncher(
1140
0
        this->GeneratorTarget, "RULE_LAUNCH_COMPILE",
1141
0
        this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
1142
0
      if (cmNonempty(val)) {
1143
0
        launcher = cmStrCat(val, ' ');
1144
0
      }
1145
0
    }
1146
1147
0
    std::string flagsWithDeps(flags);
1148
1149
0
    if (compilerGenerateDeps) {
1150
      // Injects dependency computation
1151
0
      auto depFlags = this->Makefile->GetSafeDefinition(
1152
0
        cmStrCat("CMAKE_DEPFILE_FLAGS_", lang));
1153
1154
0
      if (!depFlags.empty()) {
1155
        // Add dependency flags
1156
0
        rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
1157
0
                                                     depFlags, vars);
1158
0
        flagsWithDeps.append(1, ' ');
1159
0
        flagsWithDeps.append(depFlags);
1160
0
      }
1161
0
      vars.Flags = flagsWithDeps.c_str();
1162
1163
0
      auto const& extraCommands = this->Makefile->GetSafeDefinition(
1164
0
        cmStrCat("CMAKE_", lang, "_DEPENDS_EXTRA_COMMANDS"));
1165
0
      if (!extraCommands.empty()) {
1166
0
        compileCommands.append(extraCommands);
1167
0
      }
1168
1169
0
      auto const& depFormat = this->Makefile->GetRequiredDefinition(
1170
0
        cmStrCat("CMAKE_", lang, "_DEPFILE_FORMAT"));
1171
1172
0
      if (depFormat == "msvc"_s) {
1173
        // compiler must be launched through a wrapper to pick-up dependencies
1174
0
        std::string depFilter =
1175
0
          "$(CMAKE_COMMAND) -E cmake_cl_compile_depends ";
1176
0
        depFilter += cmStrCat("--dep-file=", shellDependencyFile);
1177
0
        depFilter +=
1178
0
          cmStrCat(" --working-dir=",
1179
0
                   this->LocalGenerator->ConvertToOutputFormat(
1180
0
                     this->LocalGenerator->GetCurrentBinaryDirectory(),
1181
0
                     cmOutputConverter::SHELL));
1182
0
        auto const& prefix = this->Makefile->GetSafeDefinition(
1183
0
          cmStrCat("CMAKE_", lang, "_CL_SHOWINCLUDES_PREFIX"));
1184
0
        depFilter += cmStrCat(" --filter-prefix=",
1185
0
                              this->LocalGenerator->ConvertToOutputFormat(
1186
0
                                prefix, cmOutputConverter::SHELL));
1187
0
        depFilter += " -- ";
1188
0
        compileCommands.front().insert(0, depFilter);
1189
0
      }
1190
0
    }
1191
1192
    // Expand placeholders in the commands.
1193
0
    for (std::string& compileCommand : compileCommands) {
1194
0
      compileCommand = cmStrCat(launcher, compileCommand);
1195
0
      rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
1196
0
                                                   compileCommand, vars);
1197
0
    }
1198
1199
    // Change the command working directory to the local build tree.
1200
0
    this->LocalGenerator->CreateCDCommand(
1201
0
      compileCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
1202
0
      this->LocalGenerator->GetBinaryDirectory());
1203
0
    cm::append(commands, compileCommands);
1204
0
  }
1205
1206
  // Check for extra outputs created by the compilation.
1207
0
  cmList outputs;
1208
0
  outputs.emplace_back(relativeObj);
1209
0
  if (cmValue extra_outputs_str = source.GetProperty("OBJECT_OUTPUTS")) {
1210
0
    std::string evaluated_outputs = cmGeneratorExpression::Evaluate(
1211
0
      *extra_outputs_str, this->LocalGenerator, config);
1212
1213
0
    if (!evaluated_outputs.empty()) {
1214
      // Register these as extra files to clean.
1215
0
      outputs.append(evaluated_outputs);
1216
0
    }
1217
0
  }
1218
0
  if (!ispcHeaderRelative.empty()) {
1219
    // can't move ispcHeader as vars is using it
1220
0
    outputs.emplace_back(ispcHeaderRelative);
1221
0
  }
1222
1223
0
  if (outputs.size() > 1) {
1224
0
    this->CleanFiles.insert(outputs.begin() + 1, outputs.end());
1225
0
  }
1226
1227
0
  if (compilerGenerateDeps) {
1228
0
    depends.push_back(dependencyTimestamp);
1229
0
  }
1230
1231
  // Write the rule.
1232
0
  this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
1233
0
                      commands);
1234
1235
0
  if (compilerGenerateDeps) {
1236
    // set back flags without dependency generation
1237
0
    vars.Flags = flags.c_str();
1238
0
  }
1239
1240
0
  bool do_preprocess_rules = lang_has_preprocessor &&
1241
0
    this->LocalGenerator->GetCreatePreprocessedSourceRules();
1242
0
  bool do_assembly_rules =
1243
0
    lang_has_assembly && this->LocalGenerator->GetCreateAssemblySourceRules();
1244
0
  if (do_preprocess_rules || do_assembly_rules) {
1245
0
    std::vector<std::string> force_depends;
1246
0
    force_depends.emplace_back("cmake_force");
1247
0
    std::string::size_type dot_pos = relativeObj.rfind('.');
1248
0
    std::string relativeObjBase = relativeObj.substr(0, dot_pos);
1249
0
    dot_pos = obj.rfind('.');
1250
0
    std::string objBase = obj.substr(0, dot_pos);
1251
1252
0
    if (do_preprocess_rules) {
1253
0
      commands.clear();
1254
0
      std::string const relativeObjI = relativeObjBase + ".i";
1255
0
      std::string const objI = objBase + ".i";
1256
1257
0
      std::string preprocessEcho =
1258
0
        cmStrCat("Preprocessing ", lang, " source to ", objI);
1259
0
      this->LocalGenerator->AppendEcho(
1260
0
        commands, preprocessEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
1261
1262
0
      std::string preprocessRuleVar =
1263
0
        cmStrCat("CMAKE_", lang, "_CREATE_PREPROCESSED_SOURCE");
1264
0
      if (cmValue preprocessRule =
1265
0
            this->Makefile->GetDefinition(preprocessRuleVar)) {
1266
0
        cmList preprocessCommands{ *preprocessRule };
1267
1268
0
        std::string shellObjI = this->LocalGenerator->ConvertToOutputFormat(
1269
0
          objI, cmOutputConverter::SHELL);
1270
0
        vars.PreprocessedSource = shellObjI.c_str();
1271
1272
        // Expand placeholders in the commands.
1273
0
        for (std::string& preprocessCommand : preprocessCommands) {
1274
          // no launcher for preprocessor commands
1275
0
          rulePlaceholderExpander->ExpandRuleVariables(
1276
0
            this->LocalGenerator, preprocessCommand, vars);
1277
0
        }
1278
1279
0
        this->LocalGenerator->CreateCDCommand(
1280
0
          preprocessCommands,
1281
0
          this->LocalGenerator->GetCurrentBinaryDirectory(),
1282
0
          this->LocalGenerator->GetBinaryDirectory());
1283
0
        cm::append(commands, preprocessCommands);
1284
0
      } else {
1285
0
        std::string cmd =
1286
0
          cmStrCat("$(CMAKE_COMMAND) -E cmake_unimplemented_variable ",
1287
0
                   preprocessRuleVar);
1288
0
        commands.push_back(std::move(cmd));
1289
0
      }
1290
1291
0
      this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1292
0
                                          relativeObjI, force_depends,
1293
0
                                          commands, false);
1294
0
    }
1295
1296
0
    if (do_assembly_rules) {
1297
0
      commands.clear();
1298
0
      std::string relativeObjS = relativeObjBase + ".s";
1299
0
      std::string objS = objBase + ".s";
1300
1301
0
      std::string assemblyEcho =
1302
0
        cmStrCat("Compiling ", lang, " source to assembly ", objS);
1303
0
      this->LocalGenerator->AppendEcho(
1304
0
        commands, assemblyEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
1305
1306
0
      std::string assemblyRuleVar =
1307
0
        cmStrCat("CMAKE_", lang, "_CREATE_ASSEMBLY_SOURCE");
1308
0
      if (cmValue assemblyRule =
1309
0
            this->Makefile->GetDefinition(assemblyRuleVar)) {
1310
0
        cmList assemblyCommands{ *assemblyRule };
1311
1312
0
        std::string shellObjS = this->LocalGenerator->ConvertToOutputFormat(
1313
0
          objS, cmOutputConverter::SHELL);
1314
0
        vars.AssemblySource = shellObjS.c_str();
1315
1316
        // Expand placeholders in the commands.
1317
0
        for (std::string& assemblyCommand : assemblyCommands) {
1318
          // no launcher for assembly commands
1319
0
          rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
1320
0
                                                       assemblyCommand, vars);
1321
0
        }
1322
1323
0
        this->LocalGenerator->CreateCDCommand(
1324
0
          assemblyCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
1325
0
          this->LocalGenerator->GetBinaryDirectory());
1326
0
        cm::append(commands, assemblyCommands);
1327
0
      } else {
1328
0
        std::string cmd =
1329
0
          cmStrCat("$(CMAKE_COMMAND) -E cmake_unimplemented_variable ",
1330
0
                   assemblyRuleVar);
1331
0
        commands.push_back(std::move(cmd));
1332
0
      }
1333
1334
0
      this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1335
0
                                          relativeObjS, force_depends,
1336
0
                                          commands, false);
1337
0
    }
1338
0
  }
1339
0
}
1340
1341
void cmMakefileTargetGenerator::WriteTargetCleanRules()
1342
0
{
1343
0
  std::vector<std::string> depends;
1344
0
  std::vector<std::string> commands;
1345
1346
  // Construct the clean target name.
1347
0
  std::string const cleanTarget = cmStrCat(
1348
0
    this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget),
1349
0
    "/clean");
1350
1351
  // Construct the clean command.
1352
0
  this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
1353
0
                                           this->GeneratorTarget);
1354
0
  this->LocalGenerator->CreateCDCommand(
1355
0
    commands, this->LocalGenerator->GetCurrentBinaryDirectory(),
1356
0
    this->LocalGenerator->GetBinaryDirectory());
1357
1358
  // Write the rule.
1359
0
  this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1360
0
                                      cleanTarget, depends, commands, true);
1361
0
}
1362
1363
bool cmMakefileTargetGenerator::WriteMakeRule(
1364
  std::ostream& os, char const* comment,
1365
  std::vector<std::string> const& outputs,
1366
  std::vector<std::string> const& depends,
1367
  std::vector<std::string> const& commands, bool in_help)
1368
0
{
1369
0
  bool symbolic = false;
1370
0
  if (outputs.empty()) {
1371
0
    return symbolic;
1372
0
  }
1373
1374
  // Check whether we need to bother checking for a symbolic output.
1375
0
  bool const need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
1376
1377
  // Check whether the first output is marked as symbolic.
1378
0
  if (need_symbolic) {
1379
0
    if (cmSourceFile* sf = this->Makefile->GetSource(outputs[0])) {
1380
0
      symbolic = sf->GetPropertyAsBool("SYMBOLIC");
1381
0
    }
1382
0
  }
1383
1384
  // We always attach the actual commands to the first output.
1385
0
  this->LocalGenerator->WriteMakeRule(os, comment, outputs[0], depends,
1386
0
                                      commands, symbolic, in_help);
1387
1388
  // For single outputs, we are done.
1389
0
  if (outputs.size() == 1) {
1390
0
    return symbolic;
1391
0
  }
1392
1393
  // For multiple outputs, make the extra ones depend on the first one.
1394
0
  std::vector<std::string> const output_depends(1, outputs[0]);
1395
0
  for (std::string const& output : cmMakeRange(outputs).advance(1)) {
1396
    // Touch the extra output so "make" knows that it was updated,
1397
    // but only if the output was actually created.
1398
0
    std::string const out = this->LocalGenerator->ConvertToOutputFormat(
1399
0
      this->LocalGenerator->MaybeRelativeToTopBinDir(output),
1400
0
      cmOutputConverter::SHELL);
1401
0
    std::vector<std::string> output_commands;
1402
1403
0
    bool o_symbolic = false;
1404
0
    if (need_symbolic) {
1405
0
      if (cmSourceFile const* sf = this->Makefile->GetSource(output)) {
1406
0
        o_symbolic = sf->GetPropertyAsBool("SYMBOLIC");
1407
0
      }
1408
0
    }
1409
0
    symbolic = symbolic && o_symbolic;
1410
1411
0
    if (!o_symbolic) {
1412
0
      output_commands.push_back("@$(CMAKE_COMMAND) -E touch_nocreate " + out);
1413
0
    }
1414
0
    this->LocalGenerator->WriteMakeRule(os, nullptr, output, output_depends,
1415
0
                                        output_commands, o_symbolic, in_help);
1416
1417
0
    if (!o_symbolic) {
1418
      // At build time, remove the first output if this one does not exist
1419
      // so that "make" will rerun the real commands that create this one.
1420
0
      MultipleOutputPairsType::value_type p(output, outputs[0]);
1421
0
      this->MultipleOutputPairs.insert(p);
1422
0
    }
1423
0
  }
1424
0
  return symbolic;
1425
0
}
1426
1427
void cmMakefileTargetGenerator::WriteTargetLinkDependRules()
1428
0
{
1429
0
  if (!this->GeneratorTarget->HasLinkDependencyFile(this->GetConfigName())) {
1430
0
    return;
1431
0
  }
1432
1433
0
  auto depFile = this->LocalGenerator->GetLinkDependencyFile(
1434
0
    this->GeneratorTarget, this->GetConfigName());
1435
0
  this->CleanFiles.insert(depFile);
1436
0
  this->LocalGenerator->AddImplicitDepends(
1437
0
    this->GeneratorTarget, "LINK",
1438
0
    this->GeneratorTarget->GetFullPath(this->GetConfigName()), depFile,
1439
0
    cmDependencyScannerKind::Compiler);
1440
0
}
1441
std::string cmMakefileTargetGenerator::GetClangTidyReplacementsFilePath(
1442
  std::string const& directory, cmSourceFile const& source,
1443
  std::string const& config) const
1444
0
{
1445
0
  (void)config;
1446
0
  auto const& objectName = this->GeneratorTarget->GetObjectName(&source);
1447
  // NOTE: This may be better to use `this->TargetBuildDirectory` instead of
1448
  // `MaybeRelativeToTopBinDir(this->TargetBuildDirectoryFull)` here. The main
1449
  // difference is that the current behavior looks odd to relative
1450
  // `<LANG>_CLANG_TIDY_EXPORT_FIXES_DIR` settings. Each subdirectory has its
1451
  // own export fixes directory *and* adds its relative-from-root path
1452
  // underneath it. However, when using an absolute export fixes directory, the
1453
  // source directory structure is preserved. The main benefit of the former is
1454
  // shorter paths everywhere versus the status quo of the existing code.
1455
0
  cmLocalGenerator* lg = this->GeneratorTarget->GetLocalGenerator();
1456
0
  auto fixesFile = cmSystemTools::CollapseFullPath(
1457
0
    cmStrCat(directory, '/',
1458
0
             lg->CreateSafeObjectFileName(
1459
0
               lg->MaybeRelativeToTopBinDir(this->TargetBuildDirectoryFull)),
1460
0
             '/', objectName, ".yaml"));
1461
0
  return fixesFile;
1462
0
}
1463
1464
void cmMakefileTargetGenerator::WriteTargetDependRules()
1465
0
{
1466
  // must write the targets depend info file
1467
0
  this->InfoFileNameFull =
1468
0
    cmStrCat(this->TargetBuildDirectoryFull, "/DependInfo.cmake");
1469
0
  this->InfoFileStream =
1470
0
    cm::make_unique<cmGeneratedFileStream>(this->InfoFileNameFull);
1471
0
  if (!this->InfoFileStream) {
1472
0
    return;
1473
0
  }
1474
0
  this->InfoFileStream->SetCopyIfDifferent(true);
1475
0
  this->LocalGenerator->WriteDependLanguageInfo(*this->InfoFileStream,
1476
0
                                                this->GeneratorTarget);
1477
1478
  // Store multiple output pairs in the depend info file.
1479
0
  if (!this->MultipleOutputPairs.empty()) {
1480
    /* clang-format off */
1481
0
    *this->InfoFileStream
1482
0
      << "\n"
1483
0
      << "# Pairs of files generated by the same build rule.\n"
1484
0
      << "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
1485
    /* clang-format on */
1486
0
    for (auto const& pi : this->MultipleOutputPairs) {
1487
0
      *this->InfoFileStream << "  " << cmScriptGenerator::Quote(pi.first)
1488
0
                            << ' ' << cmScriptGenerator::Quote(pi.second)
1489
0
                            << '\n';
1490
0
    }
1491
0
    *this->InfoFileStream << "  )\n\n";
1492
0
  }
1493
1494
  // Store list of targets linked directly or transitively.
1495
0
  {
1496
    /* clang-format off */
1497
0
  *this->InfoFileStream
1498
0
    << "\n"
1499
0
       "# Targets to which this target links which contain Fortran sources.\n"
1500
0
       "set(CMAKE_Fortran_TARGET_LINKED_INFO_FILES\n";
1501
    /* clang-format on */
1502
0
    auto const dirs =
1503
0
      this->GetLinkedTargetDirectories("Fortran", this->GetConfigName());
1504
0
    for (std::string const& d : dirs.Direct) {
1505
0
      *this->InfoFileStream << "  \"" << d << "/DependInfo.cmake\"\n";
1506
0
    }
1507
0
    *this->InfoFileStream << "  )\n";
1508
1509
    /* clang-format off */
1510
0
  *this->InfoFileStream
1511
0
    << "\n"
1512
0
       "# Targets to which this target links which contain Fortran sources.\n"
1513
0
       "set(CMAKE_Fortran_TARGET_FORWARD_LINKED_INFO_FILES\n";
1514
    /* clang-format on */
1515
0
    for (std::string const& d : dirs.Forward) {
1516
0
      *this->InfoFileStream << "  \"" << d << "/DependInfo.cmake\"\n";
1517
0
    }
1518
0
    *this->InfoFileStream << "  )\n";
1519
0
  }
1520
1521
0
  std::string const& working_dir =
1522
0
    this->LocalGenerator->GetCurrentBinaryDirectory();
1523
1524
  /* clang-format off */
1525
0
  *this->InfoFileStream
1526
0
    << "\n"
1527
0
    << "# Fortran module output directory.\n"
1528
0
    << "set(CMAKE_Fortran_TARGET_MODULE_DIR \""
1529
0
    << this->GeneratorTarget->GetFortranModuleDirectory(working_dir)
1530
0
    << "\")\n";
1531
1532
0
  if (this->GeneratorTarget->IsFortranBuildingIntrinsicModules()) {
1533
0
    *this->InfoFileStream
1534
0
      << "\n"
1535
0
      << "# Fortran compiler is building intrinsic modules.\n"
1536
0
      << "set(CMAKE_Fortran_TARGET_BUILDING_INTRINSIC_MODULES ON) \n";
1537
0
  }
1538
  /* clang-format on */
1539
1540
  // and now write the rule to use it
1541
0
  std::vector<std::string> depends;
1542
0
  std::vector<std::string> commands;
1543
1544
  // Construct the name of the dependency generation target.
1545
0
  std::string const depTarget = cmStrCat(
1546
0
    this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget),
1547
0
    "/depend");
1548
1549
  // Add a command to call CMake to scan dependencies.  CMake will
1550
  // touch the corresponding depends file after scanning dependencies.
1551
0
  std::ostringstream depCmd;
1552
// TODO: Account for source file properties and directory-level
1553
// definitions when scanning for dependencies.
1554
0
#if !defined(_WIN32) || defined(__CYGWIN__)
1555
  // This platform supports symlinks, so cmSystemTools will translate
1556
  // paths.  Make sure PWD is set to the original name of the home
1557
  // output directory to help cmSystemTools to create the same
1558
  // translation table for the dependency scanning process.
1559
0
  depCmd << "cd "
1560
0
         << (this->LocalGenerator->ConvertToOutputFormat(
1561
0
              this->LocalGenerator->GetBinaryDirectory(),
1562
0
              cmOutputConverter::SHELL))
1563
0
         << " && ";
1564
0
#endif
1565
  // Generate a call this signature:
1566
  //
1567
  //   cmake -E cmake_depends <generator>
1568
  //                          <home-src-dir> <start-src-dir>
1569
  //                          <home-out-dir> <start-out-dir>
1570
  //                          <dep-info> --color=$(COLOR)
1571
  //                          <target-name>
1572
  //
1573
  // This gives the dependency scanner enough information to recreate
1574
  // the state of our local generator sufficiently for its needs.
1575
0
  depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
1576
0
         << this->GlobalGenerator->GetName() << "\" "
1577
0
         << this->LocalGenerator->ConvertToOutputFormat(
1578
0
              this->LocalGenerator->GetSourceDirectory(),
1579
0
              cmOutputConverter::SHELL)
1580
0
         << " "
1581
0
         << this->LocalGenerator->ConvertToOutputFormat(
1582
0
              this->LocalGenerator->GetCurrentSourceDirectory(),
1583
0
              cmOutputConverter::SHELL)
1584
0
         << " "
1585
0
         << this->LocalGenerator->ConvertToOutputFormat(
1586
0
              this->LocalGenerator->GetBinaryDirectory(),
1587
0
              cmOutputConverter::SHELL)
1588
0
         << " "
1589
0
         << this->LocalGenerator->ConvertToOutputFormat(
1590
0
              this->LocalGenerator->GetCurrentBinaryDirectory(),
1591
0
              cmOutputConverter::SHELL)
1592
0
         << " "
1593
0
         << this->LocalGenerator->ConvertToOutputFormat(
1594
0
              this->InfoFileNameFull, cmOutputConverter::SHELL);
1595
0
  if (this->LocalGenerator->GetColorMakefile()) {
1596
0
    depCmd << " \"--color=$(COLOR)\"";
1597
0
  }
1598
0
  depCmd << ' '
1599
0
         << this->LocalGenerator->ConvertToOutputFormat(
1600
0
              this->GeneratorTarget->GetName(), cmOutputConverter::SHELL);
1601
0
  commands.push_back(depCmd.str());
1602
1603
  // Make sure all custom command outputs in this target are built.
1604
0
  if (this->CustomCommandDriver == OnDepends) {
1605
0
    this->DriveCustomCommands(depends);
1606
0
  }
1607
1608
  // Write the rule.
1609
0
  this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1610
0
                                      depTarget, depends, commands, true);
1611
0
}
1612
1613
void cmMakefileTargetGenerator::DriveCustomCommands(
1614
  std::vector<std::string>& depends)
1615
0
{
1616
  // Depend on all custom command outputs.
1617
0
  cm::append(depends, this->CustomCommandOutputs);
1618
0
}
1619
1620
void cmMakefileTargetGenerator::WriteObjectDependRules(
1621
  cmSourceFile const& source, std::vector<std::string>& depends)
1622
0
{
1623
  // Create the list of dependencies known at cmake time.  These are
1624
  // shared between the object file and dependency scanning rule.
1625
0
  depends.push_back(source.GetFullPath());
1626
0
  if (cmValue objectDeps = source.GetProperty("OBJECT_DEPENDS")) {
1627
0
    cmExpandList(*objectDeps, depends);
1628
0
  }
1629
0
}
1630
1631
void cmMakefileTargetGenerator::WriteDeviceLinkRule(
1632
  std::vector<std::string>& commands, std::string const& output)
1633
0
{
1634
0
  std::string architecturesStr =
1635
0
    this->GeneratorTarget->GetSafeProperty("CUDA_ARCHITECTURES");
1636
1637
0
  if (cmIsOff(architecturesStr)) {
1638
0
    this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
1639
0
                                 "CUDA_SEPARABLE_COMPILATION on Clang "
1640
0
                                 "requires CUDA_ARCHITECTURES to be set.");
1641
0
    return;
1642
0
  }
1643
1644
0
  cmLocalUnixMakefileGenerator3* localGen{ this->LocalGenerator };
1645
0
  cmList architectures{ architecturesStr };
1646
0
  std::string const& relPath = localGen->GetHomeRelativeOutputPath();
1647
1648
  // Ensure there are no duplicates.
1649
0
  std::vector<std::string> const linkDeps = [&]() -> std::vector<std::string> {
1650
0
    std::vector<std::string> deps;
1651
0
    this->AppendTargetDepends(deps, true);
1652
0
    this->GeneratorTarget->GetLinkDepends(deps, this->GetConfigName(), "CUDA");
1653
1654
0
    for (std::string const& obj : this->Objects) {
1655
0
      deps.emplace_back(cmStrCat(relPath, obj));
1656
0
    }
1657
1658
0
    std::unordered_set<std::string> const depsSet(deps.begin(), deps.end());
1659
0
    deps.clear();
1660
0
    std::copy(depsSet.begin(), depsSet.end(), std::back_inserter(deps));
1661
0
    return deps;
1662
0
  }();
1663
1664
0
  std::string const objectDir = this->GeneratorTarget->ObjectDirectory;
1665
0
  std::string const relObjectDir =
1666
0
    localGen->MaybeRelativeToCurBinDir(objectDir);
1667
1668
  // Construct a list of files associated with this executable that
1669
  // may need to be cleaned.
1670
0
  std::vector<std::string> cleanFiles;
1671
0
  cleanFiles.push_back(localGen->MaybeRelativeToCurBinDir(output));
1672
1673
0
  std::string profiles;
1674
0
  std::vector<std::string> fatbinaryDepends;
1675
0
  std::string const registerFile =
1676
0
    cmStrCat(objectDir, "cmake_cuda_register.h");
1677
1678
  // Link device code for each architecture.
1679
0
  for (std::string const& architectureKind : architectures) {
1680
0
    std::string registerFileCmd;
1681
1682
    // The generated register file contains macros that when expanded
1683
    // register the device routines. Because the routines are the same for
1684
    // all architectures the register file will be the same too. Thus
1685
    // generate it only on the first invocation to reduce overhead.
1686
0
    if (fatbinaryDepends.empty()) {
1687
0
      std::string const registerFileRel =
1688
0
        cmStrCat(relPath, relObjectDir, "cmake_cuda_register.h");
1689
0
      registerFileCmd =
1690
0
        cmStrCat(" --register-link-binaries=", registerFileRel);
1691
0
      cleanFiles.push_back(registerFileRel);
1692
0
    }
1693
1694
    // Clang always generates real code, so strip the specifier.
1695
0
    std::string const architecture =
1696
0
      architectureKind.substr(0, architectureKind.find('-'));
1697
0
    std::string const cubin =
1698
0
      cmStrCat(objectDir, "sm_", architecture, ".cubin");
1699
1700
0
    profiles += cmStrCat(" -im=profile=sm_", architecture, ",file=", cubin);
1701
0
    fatbinaryDepends.emplace_back(cubin);
1702
1703
0
    std::string command = cmStrCat(
1704
0
      this->Makefile->GetRequiredDefinition("CMAKE_CUDA_DEVICE_LINKER"),
1705
0
      " -arch=sm_", architecture, registerFileCmd, " -o=$@ ",
1706
0
      cmJoin(linkDeps, " "));
1707
1708
0
    localGen->WriteMakeRule(*this->BuildFileStream, nullptr, cubin, linkDeps,
1709
0
                            { command }, false);
1710
0
  }
1711
1712
  // Combine all architectures into a single fatbinary.
1713
0
  std::string const fatbinaryCommand =
1714
0
    cmStrCat(this->Makefile->GetRequiredDefinition("CMAKE_CUDA_FATBINARY"),
1715
0
             " -64 -cmdline=--compile-only -compress-all -link "
1716
0
             "--embedded-fatbin=$@",
1717
0
             profiles);
1718
0
  std::string const fatbinaryOutput =
1719
0
    cmStrCat(objectDir, "cmake_cuda_fatbin.h");
1720
0
  std::string const fatbinaryOutputRel =
1721
0
    cmStrCat(relPath, relObjectDir, "cmake_cuda_fatbin.h");
1722
1723
0
  localGen->WriteMakeRule(*this->BuildFileStream, nullptr, fatbinaryOutputRel,
1724
0
                          fatbinaryDepends, { fatbinaryCommand }, false);
1725
1726
  // Compile the stub that registers the kernels and contains the
1727
  // fatbinaries.
1728
0
  cmRulePlaceholderExpander::RuleVariables vars;
1729
0
  vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
1730
0
  vars.CMTargetType =
1731
0
    cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()).c_str();
1732
0
  vars.Language = "CUDA";
1733
0
  vars.Object = output.c_str();
1734
0
  vars.Fatbinary = fatbinaryOutput.c_str();
1735
0
  vars.RegisterFile = registerFile.c_str();
1736
0
  vars.Config = this->GetConfigName().c_str();
1737
1738
0
  std::string linkFlags;
1739
0
  this->GetDeviceLinkFlags(linkFlags, "CUDA");
1740
0
  vars.LinkFlags = linkFlags.c_str();
1741
1742
0
  std::string const flags = this->GetFlags("CUDA", this->GetConfigName());
1743
0
  vars.Flags = flags.c_str();
1744
1745
0
  std::string compileCmd = this->GetLinkRule("CMAKE_CUDA_DEVICE_LINK_COMPILE");
1746
0
  auto rulePlaceholderExpander =
1747
0
    localGen->CreateRulePlaceholderExpander(cmBuildStep::Link);
1748
0
  rulePlaceholderExpander->ExpandRuleVariables(localGen, compileCmd, vars);
1749
1750
0
  commands.emplace_back(compileCmd);
1751
0
  localGen->WriteMakeRule(*this->BuildFileStream, nullptr, output,
1752
0
                          { fatbinaryOutputRel }, commands, false);
1753
1754
  // Clean all the possible executable names and symlinks.
1755
0
  this->CleanFiles.insert(cleanFiles.begin(), cleanFiles.end());
1756
0
}
1757
1758
void cmMakefileTargetGenerator::GenerateCustomRuleFile(
1759
  cmCustomCommandGenerator const& ccg)
1760
0
{
1761
  // Collect the commands.
1762
0
  std::vector<std::string> commands;
1763
0
  std::string comment = this->LocalGenerator->ConstructComment(ccg);
1764
0
  if (!comment.empty()) {
1765
    // add in a progress call if needed
1766
0
    this->NumberOfProgressActions++;
1767
0
    if (!this->NoRuleMessages) {
1768
0
      cmLocalUnixMakefileGenerator3::EchoProgress progress;
1769
0
      this->MakeEchoProgress(progress);
1770
0
      this->LocalGenerator->AppendEcho(
1771
0
        commands, comment, cmLocalUnixMakefileGenerator3::EchoGenerate,
1772
0
        &progress);
1773
0
    }
1774
0
  }
1775
1776
  // Now append the actual user-specified commands.
1777
0
  std::ostringstream content;
1778
0
  this->LocalGenerator->AppendCustomCommand(
1779
0
    commands, ccg, this->GeneratorTarget,
1780
0
    this->LocalGenerator->GetBinaryDirectory(), false, &content);
1781
1782
  // Collect the dependencies.
1783
0
  std::vector<std::string> depends;
1784
0
  this->LocalGenerator->AppendCustomDepend(depends, ccg);
1785
1786
0
  if (!ccg.GetCC().GetDepfile().empty()) {
1787
    // Add dependency over timestamp file for dependencies management
1788
0
    auto dependTimestamp = this->LocalGenerator->MaybeRelativeToTopBinDir(
1789
0
      cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts"));
1790
1791
0
    depends.emplace_back(std::move(dependTimestamp));
1792
0
  }
1793
1794
  // Write the rule.
1795
0
  std::vector<std::string> const& outputs = ccg.GetOutputs();
1796
0
  bool const symbolic = this->WriteMakeRule(*this->BuildFileStream, nullptr,
1797
0
                                            outputs, depends, commands);
1798
1799
  // Symbolic inputs are not expected to exist, so add dummy rules.
1800
0
  if (this->CMP0113New && !depends.empty()) {
1801
0
    std::vector<std::string> no_depends;
1802
0
    std::vector<std::string> no_commands;
1803
0
    for (std::string const& dep : depends) {
1804
0
      if (cmSourceFile* dsf =
1805
0
            this->Makefile->GetSource(dep, cmSourceFileLocationKind::Known)) {
1806
0
        if (dsf->GetPropertyAsBool("SYMBOLIC")) {
1807
0
          this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
1808
0
                                              dep, no_depends, no_commands,
1809
0
                                              true);
1810
0
        }
1811
0
      }
1812
0
    }
1813
0
  }
1814
1815
  // If the rule has changed make sure the output is rebuilt.
1816
0
  if (!symbolic) {
1817
0
    this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
1818
0
  }
1819
1820
  // Setup implicit dependency scanning.
1821
0
  for (auto const& idi : ccg.GetCC().GetImplicitDepends()) {
1822
0
    std::string objFullPath = cmSystemTools::CollapseFullPath(
1823
0
      outputs[0], this->LocalGenerator->GetCurrentBinaryDirectory());
1824
0
    std::string srcFullPath = cmSystemTools::CollapseFullPath(
1825
0
      idi.second, this->LocalGenerator->GetCurrentBinaryDirectory());
1826
0
    this->LocalGenerator->AddImplicitDepends(this->GeneratorTarget, idi.first,
1827
0
                                             objFullPath, srcFullPath);
1828
0
  }
1829
1830
  // Setup implicit depend for depfile if any
1831
0
  if (!ccg.GetCC().GetDepfile().empty()) {
1832
0
    std::string objFullPath = cmSystemTools::CollapseFullPath(
1833
0
      outputs[0], this->LocalGenerator->GetCurrentBinaryDirectory());
1834
0
    this->LocalGenerator->AddImplicitDepends(
1835
0
      this->GeneratorTarget, "CUSTOM", objFullPath, ccg.GetFullDepfile(),
1836
0
      cmDependencyScannerKind::Compiler);
1837
0
  }
1838
1839
0
  this->CustomCommandOutputs.insert(outputs.begin(), outputs.end());
1840
0
}
1841
1842
void cmMakefileTargetGenerator::MakeEchoProgress(
1843
  cmLocalUnixMakefileGenerator3::EchoProgress& progress) const
1844
0
{
1845
0
  progress.Dir =
1846
0
    cmStrCat(this->LocalGenerator->GetBinaryDirectory(), "/CMakeFiles");
1847
0
  std::ostringstream progressArg;
1848
0
  progressArg << "$(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
1849
0
  progress.Arg = progressArg.str();
1850
0
}
1851
1852
void cmMakefileTargetGenerator::WriteObjectsVariable(
1853
  std::string& variableName, std::string& variableNameExternal,
1854
  bool useWatcomQuote)
1855
0
{
1856
  // Write a make variable assignment that lists all objects for the
1857
  // target.
1858
0
  variableName = this->LocalGenerator->CreateMakeVariable(
1859
0
    this->GeneratorTarget->GetName(), "_OBJECTS");
1860
0
  *this->BuildFileStream << "# Object files for target "
1861
0
                         << this->GeneratorTarget->GetName() << "\n"
1862
0
                         << variableName << " =";
1863
0
  auto const& lineContinue = this->GlobalGenerator->LineContinueDirective;
1864
1865
0
  cmValue pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
1866
1867
0
  for (std::string const& obj : this->Objects) {
1868
0
    if (cmHasSuffix(obj, pchExtension)) {
1869
0
      continue;
1870
0
    }
1871
0
    *this->BuildFileStream << " " << lineContinue;
1872
0
    *this->BuildFileStream
1873
0
      << cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(
1874
0
           obj, useWatcomQuote);
1875
0
  }
1876
0
  *this->BuildFileStream << "\n";
1877
1878
  // Write a make variable assignment that lists all external objects
1879
  // for the target.
1880
0
  variableNameExternal = this->LocalGenerator->CreateMakeVariable(
1881
0
    this->GeneratorTarget->GetName(), "_EXTERNAL_OBJECTS");
1882
  /* clang-format off */
1883
0
  *this->BuildFileStream
1884
0
    << "\n"
1885
0
    << "# External object files for target "
1886
0
    << this->GeneratorTarget->GetName() << "\n"
1887
0
    << variableNameExternal << " =";
1888
  /* clang-format on */
1889
0
  for (std::string const& obj : this->ExternalObjects) {
1890
0
    *this->BuildFileStream << " " << lineContinue;
1891
0
    *this->BuildFileStream
1892
0
      << cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(
1893
0
           obj, useWatcomQuote);
1894
0
  }
1895
0
  *this->BuildFileStream << "\n"
1896
0
                         << "\n";
1897
0
}
1898
1899
class cmMakefileTargetGeneratorObjectStrings
1900
{
1901
public:
1902
  cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
1903
                                         cmOutputConverter* outputConverter,
1904
                                         bool useWatcomQuote,
1905
                                         cmStateDirectory const& stateDir,
1906
                                         std::string::size_type limit)
1907
0
    : Strings(strings)
1908
0
    , OutputConverter(outputConverter)
1909
0
    , UseWatcomQuote(useWatcomQuote)
1910
0
    , StateDir(stateDir)
1911
0
    , LengthLimit(limit)
1912
0
  {
1913
0
    this->Space = "";
1914
0
  }
1915
  void Feed(std::string const& obj)
1916
0
  {
1917
    // Construct the name of the next object.
1918
0
    this->NextObject = this->OutputConverter->ConvertToOutputFormat(
1919
0
      this->OutputConverter->MaybeRelativeToCurBinDir(obj),
1920
0
      cmOutputConverter::RESPONSE, this->UseWatcomQuote);
1921
1922
    // Roll over to next string if the limit will be exceeded.
1923
0
    if (this->LengthLimit != std::string::npos &&
1924
0
        (this->CurrentString.length() + 1 + this->NextObject.length() >
1925
0
         this->LengthLimit)) {
1926
0
      this->Strings.push_back(this->CurrentString);
1927
0
      this->CurrentString.clear();
1928
0
      this->Space = "";
1929
0
    }
1930
1931
    // Separate from previous object.
1932
0
    this->CurrentString += this->Space;
1933
0
    this->Space = " ";
1934
1935
    // Append this object.
1936
0
    this->CurrentString += this->NextObject;
1937
0
  }
1938
0
  void Done() { this->Strings.push_back(this->CurrentString); }
1939
1940
private:
1941
  std::vector<std::string>& Strings;
1942
  cmOutputConverter* OutputConverter;
1943
  bool UseWatcomQuote;
1944
  cmStateDirectory StateDir;
1945
  std::string::size_type LengthLimit;
1946
  std::string CurrentString;
1947
  std::string NextObject;
1948
  char const* Space;
1949
};
1950
1951
void cmMakefileTargetGenerator::WriteObjectsStrings(
1952
  std::vector<std::string>& objStrings, bool useWatcomQuote,
1953
  std::string::size_type limit)
1954
0
{
1955
0
  cmValue pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
1956
1957
0
  cmMakefileTargetGeneratorObjectStrings helper(
1958
0
    objStrings, this->LocalGenerator, useWatcomQuote,
1959
0
    this->LocalGenerator->GetStateSnapshot().GetDirectory(), limit);
1960
0
  for (std::string const& obj : this->Objects) {
1961
0
    if (cmHasSuffix(obj, pchExtension)) {
1962
0
      continue;
1963
0
    }
1964
0
    helper.Feed(obj);
1965
0
  }
1966
0
  for (std::string const& obj : this->ExternalObjects) {
1967
0
    helper.Feed(obj);
1968
0
  }
1969
0
  auto const ispcAdditionalObjs =
1970
0
    this->GeneratorTarget->GetGeneratedISPCObjects(this->GetConfigName());
1971
0
  for (auto const& obj : ispcAdditionalObjs) {
1972
0
    helper.Feed(obj.second);
1973
0
  }
1974
0
  helper.Done();
1975
0
}
1976
1977
void cmMakefileTargetGenerator::WriteTargetDriverRule(
1978
  std::string const& main_output, bool relink)
1979
0
{
1980
  // Compute the name of the driver target.
1981
0
  std::string dir =
1982
0
    this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
1983
0
  std::string buildTargetRuleName =
1984
0
    cmStrCat(std::move(dir), relink ? "/preinstall" : "/build");
1985
0
  buildTargetRuleName =
1986
0
    this->LocalGenerator->MaybeRelativeToTopBinDir(buildTargetRuleName);
1987
1988
  // Build the list of target outputs to drive.
1989
0
  std::vector<std::string> depends{ main_output };
1990
1991
0
  char const* comment = nullptr;
1992
0
  if (relink) {
1993
    // Setup the comment for the preinstall driver.
1994
0
    comment = "Rule to relink during preinstall.";
1995
0
  } else {
1996
    // Setup the comment for the main build driver.
1997
0
    comment = "Rule to build all files generated by this target.";
1998
1999
    // Make sure all custom command outputs in this target are built.
2000
0
    if (this->CustomCommandDriver == OnBuild) {
2001
0
      this->DriveCustomCommands(depends);
2002
0
    }
2003
2004
    // Make sure the extra files are built.
2005
0
    cm::append(depends, this->ExtraFiles);
2006
0
  }
2007
2008
  // Write the driver rule.
2009
0
  std::vector<std::string> no_commands;
2010
0
  this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
2011
0
                                      buildTargetRuleName, depends,
2012
0
                                      no_commands, true);
2013
0
}
2014
2015
void cmMakefileTargetGenerator::AppendTargetDepends(
2016
  std::vector<std::string>& depends, bool ignoreType)
2017
0
{
2018
  // Static libraries never depend on anything for linking.
2019
0
  if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY &&
2020
0
      !ignoreType) {
2021
0
    return;
2022
0
  }
2023
2024
0
  std::string const& cfg = this->GetConfigName();
2025
2026
0
  if (this->GeneratorTarget->HasLinkDependencyFile(cfg)) {
2027
0
    depends.push_back(
2028
0
      cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts"));
2029
0
  }
2030
2031
  // Loop over all library dependencies.
2032
0
  if (cmComputeLinkInformation const* cli =
2033
0
        this->GeneratorTarget->GetLinkInformation(cfg)) {
2034
0
    cm::append(depends, cli->GetDepends());
2035
0
  }
2036
0
}
2037
2038
void cmMakefileTargetGenerator::AppendObjectDepends(
2039
  std::vector<std::string>& depends)
2040
0
{
2041
  // Add dependencies on the compiled object files.
2042
0
  std::string const& relPath =
2043
0
    this->LocalGenerator->GetHomeRelativeOutputPath();
2044
0
  for (std::string const& obj : this->Objects) {
2045
0
    std::string objTarget = cmStrCat(relPath, obj);
2046
0
    depends.push_back(std::move(objTarget));
2047
0
  }
2048
2049
  // Add dependencies on the external object files.
2050
0
  cm::append(depends, this->ExternalObjects);
2051
2052
  // Add a dependency on the rule file itself.
2053
0
  this->LocalGenerator->AppendRuleDepend(depends,
2054
0
                                         this->BuildFileNameFull.c_str());
2055
0
}
2056
2057
void cmMakefileTargetGenerator::AppendLinkDepends(
2058
  std::vector<std::string>& depends, std::string const& linkLanguage)
2059
0
{
2060
0
  this->AppendObjectDepends(depends);
2061
2062
  // Add dependencies on targets that must be built first.
2063
0
  this->AppendTargetDepends(depends);
2064
2065
  // Add a dependency on the link definitions file, if any.
2066
0
  if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
2067
0
        this->GeneratorTarget->GetModuleDefinitionInfo(
2068
0
          this->GetConfigName())) {
2069
0
    for (cmSourceFile const* src : mdi->Sources) {
2070
0
      depends.push_back(src->GetFullPath());
2071
0
    }
2072
0
  }
2073
2074
  // Add a dependency on user-specified manifest files, if any.
2075
0
  std::vector<cmSourceFile const*> manifest_srcs;
2076
0
  this->GeneratorTarget->GetManifests(manifest_srcs, this->GetConfigName());
2077
0
  for (cmSourceFile const* manifest_src : manifest_srcs) {
2078
0
    depends.push_back(manifest_src->GetFullPath());
2079
0
  }
2080
2081
  // Add user-specified dependencies.
2082
0
  this->GeneratorTarget->GetLinkDepends(depends, this->GetConfigName(),
2083
0
                                        linkLanguage);
2084
0
}
2085
2086
std::string cmMakefileTargetGenerator::GetLinkRule(
2087
  std::string const& linkRuleVar)
2088
0
{
2089
0
  std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
2090
0
  if (this->GeneratorTarget->HasImplibGNUtoMS(this->GetConfigName())) {
2091
0
    std::string ruleVar =
2092
0
      cmStrCat("CMAKE_",
2093
0
               this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName()),
2094
0
               "_GNUtoMS_RULE");
2095
0
    if (cmValue rule = this->Makefile->GetDefinition(ruleVar)) {
2096
0
      linkRule += *rule;
2097
0
    }
2098
0
  }
2099
0
  return linkRule;
2100
0
}
2101
2102
void cmMakefileTargetGenerator::CloseFileStreams()
2103
0
{
2104
0
  this->BuildFileStream.reset();
2105
0
  this->InfoFileStream.reset();
2106
0
  this->FlagFileStream.reset();
2107
0
}
2108
2109
void cmMakefileTargetGenerator::CreateLinkScript(
2110
  char const* name, std::vector<std::string> const& link_commands,
2111
  std::vector<std::string>& makefile_commands,
2112
  std::vector<std::string>& makefile_depends)
2113
0
{
2114
  // Create the link script file.
2115
0
  std::string linkScriptName =
2116
0
    cmStrCat(this->TargetBuildDirectoryFull, '/', name);
2117
0
  cmGeneratedFileStream linkScriptStream(linkScriptName);
2118
0
  linkScriptStream.SetCopyIfDifferent(true);
2119
0
  for (std::string const& link_command : link_commands) {
2120
    // Do not write out empty commands or commands beginning in the
2121
    // shell no-op ":".
2122
0
    if (!link_command.empty() && link_command[0] != ':') {
2123
0
      linkScriptStream << link_command << "\n";
2124
0
    }
2125
0
  }
2126
2127
  // Create the makefile command to invoke the link script.
2128
0
  std::string link_command =
2129
0
    cmStrCat("$(CMAKE_COMMAND) -E cmake_link_script ",
2130
0
             this->LocalGenerator->ConvertToOutputFormat(
2131
0
               this->LocalGenerator->MaybeRelativeToCurBinDir(linkScriptName),
2132
0
               cmOutputConverter::SHELL),
2133
0
             " --verbose=$(VERBOSE)");
2134
0
  makefile_commands.push_back(std::move(link_command));
2135
0
  makefile_depends.push_back(std::move(linkScriptName));
2136
0
}
2137
2138
bool cmMakefileTargetGenerator::CheckUseResponseFileForObjects(
2139
  std::string const& l) const
2140
0
{
2141
  // Check for an explicit setting one way or the other.
2142
0
  std::string const responseVar =
2143
0
    "CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_OBJECTS";
2144
0
  if (cmValue val = this->Makefile->GetDefinition(responseVar)) {
2145
0
    if (!val->empty()) {
2146
0
      return val.IsOn();
2147
0
    }
2148
0
  }
2149
2150
  // Check for a system limit.
2151
0
  if (size_t const limit = cmSystemTools::CalculateCommandLineLengthLimit()) {
2152
    // Compute the total length of our list of object files with room
2153
    // for argument separation and quoting.  This does not convert paths
2154
    // relative to CMAKE_CURRENT_BINARY_DIR like the final list will be, so
2155
    // the actual list will likely be much shorter than this.  However, in
2156
    // the worst case all objects will remain as absolute paths.
2157
0
    size_t length = 0;
2158
0
    for (std::string const& obj : this->Objects) {
2159
0
      length += obj.size() + 3;
2160
0
    }
2161
0
    for (std::string const& ext_obj : this->ExternalObjects) {
2162
0
      length += ext_obj.size() + 3;
2163
0
    }
2164
2165
    // We need to guarantee room for both objects and libraries, so
2166
    // if the objects take up more than half then use a response file
2167
    // for them.
2168
0
    if (length > (limit / 2)) {
2169
0
      return true;
2170
0
    }
2171
0
  }
2172
2173
  // We do not need a response file for objects.
2174
0
  return false;
2175
0
}
2176
2177
bool cmMakefileTargetGenerator::CheckUseResponseFileForLibraries(
2178
  std::string const& l) const
2179
0
{
2180
  // Check for an explicit setting one way or the other.
2181
0
  std::string const responseVar =
2182
0
    "CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_LIBRARIES";
2183
0
  if (cmValue val = this->Makefile->GetDefinition(responseVar)) {
2184
0
    if (!val->empty()) {
2185
0
      return val.IsOn();
2186
0
    }
2187
0
  }
2188
2189
  // We do not need a response file for libraries.
2190
0
  return false;
2191
0
}
2192
2193
std::string cmMakefileTargetGenerator::CreateResponseFile(
2194
  std::string const& name, std::string const& options,
2195
  std::vector<std::string>& makefile_depends, std::string const& language)
2196
0
{
2197
  // FIXME: Find a better way to determine the response file encoding,
2198
  // perhaps using tool-specific platform information variables.
2199
  // For now, use the makefile encoding as a heuristic.
2200
0
  codecvt_Encoding responseEncoding =
2201
0
    this->GlobalGenerator->GetMakefileEncoding();
2202
  // Non-MSVC tooling doesn't understand BOM encoded files.
2203
0
  if (responseEncoding == codecvt_Encoding::UTF8_WITH_BOM &&
2204
0
      (language == "CUDA" || !this->Makefile->IsOn("MSVC"))) {
2205
0
    responseEncoding = codecvt_Encoding::UTF8;
2206
0
  }
2207
2208
  // Create the response file.
2209
0
  std::string responseFileNameFull =
2210
0
    cmStrCat(this->TargetBuildDirectoryFull, '/', name);
2211
0
  cmGeneratedFileStream responseStream(responseFileNameFull, false,
2212
0
                                       responseEncoding);
2213
0
  responseStream.SetCopyIfDifferent(true);
2214
0
  responseStream << options << "\n";
2215
2216
  // Add a dependency so the target will rebuild when the set of
2217
  // objects changes.
2218
0
  makefile_depends.push_back(std::move(responseFileNameFull));
2219
2220
  // Construct the name to be used on the command line.
2221
0
  std::string responseFileName =
2222
0
    cmStrCat(this->TargetBuildDirectory, '/', name);
2223
0
  return responseFileName;
2224
0
}
2225
2226
std::unique_ptr<cmLinkLineComputer>
2227
cmMakefileTargetGenerator::CreateLinkLineComputer(
2228
  cmOutputConverter* outputConverter, cmStateDirectory const& stateDir)
2229
0
{
2230
0
  if (this->Makefile->IsOn("MSVC60")) {
2231
0
    return this->GlobalGenerator->CreateMSVC60LinkLineComputer(outputConverter,
2232
0
                                                               stateDir);
2233
0
  }
2234
0
  return this->GlobalGenerator->CreateLinkLineComputer(outputConverter,
2235
0
                                                       stateDir);
2236
0
}
2237
2238
void cmMakefileTargetGenerator::CreateLinkLibs(
2239
  cmLinkLineComputer* linkLineComputer, std::string& linkLibs,
2240
  bool useResponseFile, std::vector<std::string>& makefile_depends,
2241
  std::string const& linkLanguage, ResponseFlagFor responseMode)
2242
0
{
2243
0
  if (cmComputeLinkInformation* pcli =
2244
0
        this->GeneratorTarget->GetLinkInformation(this->GetConfigName())) {
2245
0
    std::string frameworkPath;
2246
0
    std::string linkPath;
2247
0
    this->LocalGenerator->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
2248
0
                                              frameworkPath, linkPath);
2249
0
    linkLibs = frameworkPath + linkPath + linkLibs;
2250
0
  }
2251
2252
0
  if (useResponseFile &&
2253
0
      linkLibs.find_first_not_of(' ') != std::string::npos) {
2254
    // Lookup the response file reference flag.
2255
0
    std::string responseFlag = this->GetResponseFlag(responseMode);
2256
2257
    // Create this response file.
2258
0
    std::string const responseFileName =
2259
0
      (responseMode == Link) ? "linkLibs.rsp" : "deviceLinkLibs.rsp";
2260
0
    std::string const responseLang =
2261
0
      (responseMode == Link) ? linkLanguage : "CUDA";
2262
0
    std::string link_rsp = this->CreateResponseFile(
2263
0
      responseFileName, linkLibs, makefile_depends, responseLang);
2264
2265
    // Reference the response file.
2266
0
    linkLibs = cmStrCat(std::move(responseFlag),
2267
0
                        this->LocalGenerator->ConvertToOutputFormat(
2268
0
                          std::move(link_rsp), cmOutputConverter::SHELL));
2269
0
  }
2270
0
}
2271
2272
void cmMakefileTargetGenerator::CreateObjectLists(
2273
  bool useLinkScript, bool useArchiveRules, bool useResponseFile,
2274
  std::string& buildObjs, std::vector<std::string>& makefile_depends,
2275
  bool useWatcomQuote, std::string const& linkLanguage,
2276
  ResponseFlagFor responseMode)
2277
0
{
2278
0
  std::string variableName;
2279
0
  std::string variableNameExternal;
2280
0
  this->WriteObjectsVariable(variableName, variableNameExternal,
2281
0
                             useWatcomQuote);
2282
0
  if (useResponseFile) {
2283
    // MSVC response files cannot exceed 128K.
2284
0
    std::string::size_type constexpr responseFileLimit = 131000;
2285
2286
    // Construct the individual object list strings.
2287
0
    std::vector<std::string> object_strings;
2288
0
    this->WriteObjectsStrings(object_strings, useWatcomQuote,
2289
0
                              responseFileLimit);
2290
2291
    // Lookup the response file reference flag.
2292
0
    std::string const responseFlag = this->GetResponseFlag(responseMode);
2293
2294
    // Write a response file for each string.
2295
0
    char const* sep = "";
2296
0
    for (unsigned int i = 0; i < object_strings.size(); ++i) {
2297
      // Number the response files.
2298
0
      std::string responseFileName = cmStrCat(
2299
0
        (responseMode == Link) ? "objects" : "deviceObjects", i + 1, ".rsp");
2300
2301
      // Create this response file.
2302
0
      std::string objects_rsp = this->CreateResponseFile(
2303
0
        responseFileName, object_strings[i], makefile_depends, linkLanguage);
2304
2305
0
      buildObjs +=
2306
0
        cmStrCat(sep, // Separate from previous response file references.
2307
0
                 responseFlag, // Reference the response file.
2308
0
                 this->LocalGenerator->ConvertToOutputFormat(
2309
0
                   objects_rsp, cmOutputConverter::SHELL));
2310
0
      sep = " ";
2311
0
    }
2312
0
  } else if (useLinkScript) {
2313
0
    if (!useArchiveRules) {
2314
0
      std::vector<std::string> objStrings;
2315
0
      this->WriteObjectsStrings(objStrings, useWatcomQuote);
2316
0
      buildObjs = objStrings[0];
2317
0
    }
2318
0
  } else {
2319
0
    buildObjs =
2320
0
      cmStrCat("$(", variableName, ") $(", variableNameExternal, ')');
2321
0
  }
2322
0
}
2323
2324
void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
2325
                                                std::string const& lang,
2326
                                                std::string const& /*config*/)
2327
0
{
2328
0
  std::string const responseVar =
2329
0
    cmStrCat("CMAKE_", lang, "_USE_RESPONSE_FILE_FOR_INCLUDES");
2330
0
  bool const useResponseFile = this->Makefile->IsOn(responseVar);
2331
2332
0
  std::vector<std::string> includes;
2333
0
  this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
2334
0
                                              lang, this->GetConfigName());
2335
2336
0
  std::string const includeFlags = this->LocalGenerator->GetIncludeFlags(
2337
0
    includes, this->GeneratorTarget, lang, this->GetConfigName(),
2338
0
    useResponseFile);
2339
0
  if (includeFlags.empty()) {
2340
0
    return;
2341
0
  }
2342
2343
0
  if (useResponseFile) {
2344
0
    std::string const responseFlagVar =
2345
0
      "CMAKE_" + lang + "_RESPONSE_FILE_FLAG";
2346
0
    std::string responseFlag =
2347
0
      this->Makefile->GetSafeDefinition(responseFlagVar);
2348
0
    if (responseFlag.empty()) {
2349
0
      responseFlag = "@";
2350
0
    }
2351
0
    std::string const name = cmStrCat("includes_", lang, ".rsp");
2352
0
    std::string const includes_rsp = this->CreateResponseFile(
2353
0
      name, includeFlags, this->FlagFileDepends[lang], lang);
2354
0
    std::string const arg =
2355
0
      cmStrCat(std::move(responseFlag),
2356
0
               this->LocalGenerator->ConvertToOutputFormat(
2357
0
                 includes_rsp, cmOutputConverter::SHELL));
2358
0
    this->LocalGenerator->AppendFlags(flags, arg);
2359
0
  } else {
2360
0
    this->LocalGenerator->AppendFlags(flags, includeFlags);
2361
0
  }
2362
0
}
2363
2364
void cmMakefileTargetGenerator::GenDefFile(
2365
  std::vector<std::string>& real_link_commands)
2366
0
{
2367
0
  cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
2368
0
    this->GeneratorTarget->GetModuleDefinitionInfo(this->GetConfigName());
2369
0
  if (!mdi || !mdi->DefFileGenerated) {
2370
0
    return;
2371
0
  }
2372
0
  std::string cmd = cmSystemTools::GetCMakeCommand();
2373
0
  cmd = cmStrCat(
2374
0
    this->LocalGenerator->ConvertToOutputFormat(cmd, cmOutputConverter::SHELL),
2375
0
    " -E __create_def ",
2376
0
    this->LocalGenerator->ConvertToOutputFormat(
2377
0
      this->LocalGenerator->MaybeRelativeToCurBinDir(mdi->DefFile),
2378
0
      cmOutputConverter::SHELL),
2379
0
    ' ');
2380
0
  std::string objlist_file = mdi->DefFile + ".objs";
2381
0
  cmd += this->LocalGenerator->ConvertToOutputFormat(
2382
0
    this->LocalGenerator->MaybeRelativeToCurBinDir(objlist_file),
2383
0
    cmOutputConverter::SHELL);
2384
0
  cmValue const nm_executable = this->Makefile->GetDefinition("CMAKE_NM");
2385
0
  if (cmNonempty(nm_executable)) {
2386
0
    cmd += " --nm=";
2387
0
    cmd += this->LocalCommonGenerator->ConvertToOutputFormat(
2388
0
      *nm_executable, cmOutputConverter::SHELL);
2389
0
  }
2390
0
  real_link_commands.insert(real_link_commands.begin(), cmd);
2391
  // create a list of obj files for the -E __create_def to read
2392
0
  cmGeneratedFileStream fout(objlist_file);
2393
2394
0
  if (mdi->WindowsExportAllSymbols) {
2395
0
    for (std::string const& obj : this->Objects) {
2396
0
      if (cmHasLiteralSuffix(obj, ".obj")) {
2397
0
        fout << obj << "\n";
2398
0
      }
2399
0
    }
2400
0
    for (std::string const& obj : this->ExternalObjects) {
2401
0
      fout << obj << "\n";
2402
0
    }
2403
0
  }
2404
2405
0
  for (cmSourceFile const* src : mdi->Sources) {
2406
0
    fout << src->GetFullPath() << "\n";
2407
0
  }
2408
0
}
2409
2410
std::string cmMakefileTargetGenerator::GetResponseFlag(
2411
  ResponseFlagFor mode) const
2412
0
{
2413
0
  std::string responseFlag = "@";
2414
0
  std::string responseFlagVar;
2415
2416
0
  auto const lang =
2417
0
    this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
2418
0
  if (mode == cmMakefileTargetGenerator::ResponseFlagFor::Link) {
2419
0
    responseFlagVar = cmStrCat("CMAKE_", lang, "_RESPONSE_FILE_LINK_FLAG");
2420
0
  } else if (mode == cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink) {
2421
0
    responseFlagVar = "CMAKE_CUDA_RESPONSE_FILE_DEVICE_LINK_FLAG";
2422
0
  }
2423
2424
0
  if (cmValue const p = this->Makefile->GetDefinition(responseFlagVar)) {
2425
0
    responseFlag = *p;
2426
0
  }
2427
0
  return responseFlag;
2428
0
}