Coverage Report

Created: 2026-03-12 06:35

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