Coverage Report

Created: 2026-02-09 06:05

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