Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmLocalGenerator.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 "cmLocalGenerator.h"
4
5
#include <algorithm>
6
#include <array>
7
#include <cassert>
8
#include <cstdio>
9
#include <cstdlib>
10
#include <initializer_list>
11
#include <iterator>
12
#include <queue>
13
#include <sstream>
14
#include <unordered_set>
15
#include <utility>
16
#include <vector>
17
18
#include <cm/memory>
19
#include <cm/optional>
20
#include <cm/string_view>
21
#include <cmext/algorithm>
22
#include <cmext/string_view>
23
24
#include "cmsys/RegularExpression.hxx"
25
#include "cmsys/String.h"
26
27
#include "cmAlgorithms.h"
28
#include "cmCMakePath.h"
29
#include "cmComputeLinkInformation.h"
30
#include "cmCryptoHash.h"
31
#include "cmCustomCommand.h"
32
#include "cmCustomCommandGenerator.h"
33
#include "cmCustomCommandLines.h"
34
#include "cmCustomCommandTypes.h"
35
#include "cmFileSetMetadata.h"
36
#include "cmGeneratedFileStream.h"
37
#include "cmGeneratorExpression.h"
38
#include "cmGeneratorExpressionEvaluationFile.h"
39
#include "cmGeneratorFileSet.h"
40
#include "cmGeneratorTarget.h"
41
#include "cmGlobalGenerator.h"
42
#include "cmInstallGenerator.h"
43
#include "cmInstallScriptGenerator.h"
44
#include "cmInstallTargetGenerator.h"
45
#include "cmLinkLineComputer.h"
46
#include "cmLinkLineDeviceComputer.h"
47
#include "cmList.h"
48
#include "cmMakefile.h"
49
#include "cmMessageType.h"
50
#include "cmObjectLocation.h"
51
#include "cmRange.h"
52
#include "cmRulePlaceholderExpander.h"
53
#include "cmScriptGenerator.h"
54
#include "cmSourceFile.h"
55
#include "cmSourceFileLocation.h"
56
#include "cmSourceFileLocationKind.h"
57
#include "cmSourceGroup.h"
58
#include "cmStandardLevelResolver.h"
59
#include "cmState.h"
60
#include "cmStateDirectory.h"
61
#include "cmStateTypes.h"
62
#include "cmStringAlgorithms.h"
63
#include "cmSystemTools.h"
64
#include "cmTarget.h"
65
#include "cmTestGenerator.h"
66
#include "cmValue.h"
67
#include "cmake.h"
68
69
#if defined(__HAIKU__)
70
#  include <FindDirectory.h>
71
#  include <StorageDefs.h>
72
#endif
73
74
namespace {
75
// List of variables that are replaced when
76
// rules are expanded.  These variables are
77
// replaced in the form <var> with GetSafeDefinition(var).
78
// ${LANG} is replaced in the variable first with all enabled
79
// languages.
80
auto ruleReplaceVars = {
81
  "CMAKE_${LANG}_COMPILER",
82
  "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
83
  "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
84
  "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
85
  "CMAKE_${LANG}_ARCHIVE",
86
  "CMAKE_AR",
87
  "CMAKE_SOURCE_DIR",
88
  "CMAKE_BINARY_DIR",
89
  "CMAKE_CURRENT_SOURCE_DIR",
90
  "CMAKE_CURRENT_BINARY_DIR",
91
  "CMAKE_RANLIB",
92
  "CMAKE_MT",
93
  "CMAKE_TAPI",
94
  "CMAKE_CUDA_HOST_COMPILER",
95
  "CMAKE_CUDA_HOST_LINK_LAUNCHER",
96
  "CMAKE_HIP_HOST_COMPILER",
97
  "CMAKE_HIP_HOST_LINK_LAUNCHER",
98
  "CMAKE_CL_SHOWINCLUDES_PREFIX",
99
};
100
101
// Variables whose placeholders now map to an empty string.
102
// Our platform modules no longer use these, but third-party code might.
103
auto ruleReplaceEmptyVars = {
104
  "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
105
  "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
106
  "CMAKE_${LANG}_LINK_FLAGS",
107
};
108
}
109
110
cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile)
111
0
  : cmOutputConverter(makefile->GetStateSnapshot())
112
0
  , DirectoryBacktrace(makefile->GetBacktrace())
113
0
{
114
0
  this->GlobalGenerator = gg;
115
116
0
  this->Makefile = makefile;
117
118
0
  this->AliasTargets = makefile->GetAliasTargets();
119
120
0
  this->EmitUniversalBinaryFlags = true;
121
122
0
  this->ComputeObjectMaxPath();
123
124
  // Canonicalize entries of the CPATH environment variable the same
125
  // way detection of CMAKE_<LANG>_IMPLICIT_INCLUDE_DIRECTORIES does.
126
0
  {
127
0
    std::vector<std::string> cpath;
128
0
    cmSystemTools::GetPath(cpath, "CPATH");
129
0
    for (std::string const& cp : cpath) {
130
0
      if (cmSystemTools::FileIsFullPath(cp)) {
131
0
        this->EnvCPATH.emplace_back(cmSystemTools::CollapseFullPath(cp));
132
0
      }
133
0
    }
134
0
  }
135
136
0
  std::vector<std::string> enabledLanguages =
137
0
    this->GetState()->GetEnabledLanguages();
138
139
0
  if (cmValue sysrootCompile =
140
0
        this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
141
0
    this->CompilerSysroot = *sysrootCompile;
142
0
  } else {
143
0
    this->CompilerSysroot = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
144
0
  }
145
146
0
  if (cmValue sysrootLink =
147
0
        this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
148
0
    this->LinkerSysroot = *sysrootLink;
149
0
  } else {
150
0
    this->LinkerSysroot = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
151
0
  }
152
153
  // OSX SYSROOT can be required by some tools, like tapi
154
0
  {
155
0
    cmValue osxSysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
156
0
    this->VariableMappings["CMAKE_OSX_SYSROOT"] =
157
0
      osxSysroot.IsEmpty() ? "/" : this->EscapeForShell(*osxSysroot, true);
158
0
  }
159
160
0
  if (cmValue appleArchSysroots =
161
0
        this->Makefile->GetDefinition("CMAKE_APPLE_ARCH_SYSROOTS")) {
162
0
    std::string const& appleArchs =
163
0
      this->Makefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES");
164
0
    cmList archs(appleArchs);
165
0
    cmList sysroots{ appleArchSysroots, cmList::EmptyElements::Yes };
166
0
    if (archs.size() == sysroots.size()) {
167
0
      for (cmList::size_type i = 0; i < archs.size(); ++i) {
168
0
        this->AppleArchSysroots[archs[i]] = sysroots[i];
169
0
      }
170
0
    } else {
171
0
      std::string const e =
172
0
        cmStrCat("CMAKE_APPLE_ARCH_SYSROOTS:\n  ", *appleArchSysroots,
173
0
                 "\n"
174
0
                 "is not the same length as CMAKE_OSX_ARCHITECTURES:\n  ",
175
0
                 appleArchs);
176
0
      this->IssueMessage(MessageType::FATAL_ERROR, e);
177
0
    }
178
0
  }
179
180
0
  for (std::string const& lang : enabledLanguages) {
181
0
    if (lang == "NONE") {
182
0
      continue;
183
0
    }
184
0
    this->Compilers["CMAKE_" + lang + "_COMPILER"] = lang;
185
186
0
    this->VariableMappings["CMAKE_" + lang + "_COMPILER"] =
187
0
      this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER");
188
189
0
    std::string const& compilerArg1 = "CMAKE_" + lang + "_COMPILER_ARG1";
190
0
    std::string const& compilerTarget = "CMAKE_" + lang + "_COMPILER_TARGET";
191
0
    std::string const& compilerOptionTarget =
192
0
      "CMAKE_" + lang + "_COMPILE_OPTIONS_TARGET";
193
0
    std::string const& compilerExternalToolchain =
194
0
      "CMAKE_" + lang + "_COMPILER_EXTERNAL_TOOLCHAIN";
195
0
    std::string const& compilerOptionExternalToolchain =
196
0
      "CMAKE_" + lang + "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN";
197
0
    std::string const& compilerOptionSysroot =
198
0
      "CMAKE_" + lang + "_COMPILE_OPTIONS_SYSROOT";
199
200
0
    this->VariableMappings[compilerArg1] =
201
0
      this->Makefile->GetSafeDefinition(compilerArg1);
202
0
    this->VariableMappings[compilerTarget] =
203
0
      this->Makefile->GetSafeDefinition(compilerTarget);
204
0
    this->VariableMappings[compilerOptionTarget] =
205
0
      this->Makefile->GetSafeDefinition(compilerOptionTarget);
206
0
    this->VariableMappings[compilerExternalToolchain] =
207
0
      this->Makefile->GetSafeDefinition(compilerExternalToolchain);
208
0
    this->VariableMappings[compilerOptionExternalToolchain] =
209
0
      this->Makefile->GetSafeDefinition(compilerOptionExternalToolchain);
210
0
    this->VariableMappings[compilerOptionSysroot] =
211
0
      this->Makefile->GetSafeDefinition(compilerOptionSysroot);
212
213
0
    for (std::string replaceVar : ruleReplaceVars) {
214
0
      if (replaceVar.find("${LANG}") != std::string::npos) {
215
0
        cmSystemTools::ReplaceString(replaceVar, "${LANG}", lang);
216
0
      }
217
218
0
      this->VariableMappings[replaceVar] =
219
0
        this->Makefile->GetSafeDefinition(replaceVar);
220
0
    }
221
222
0
    for (std::string replaceVar : ruleReplaceEmptyVars) {
223
0
      if (replaceVar.find("${LANG}") != std::string::npos) {
224
0
        cmSystemTools::ReplaceString(replaceVar, "${LANG}", lang);
225
0
      }
226
227
0
      this->VariableMappings[replaceVar] = std::string();
228
0
    }
229
0
  }
230
0
}
231
232
std::unique_ptr<cmRulePlaceholderExpander>
233
cmLocalGenerator::CreateRulePlaceholderExpander(cmBuildStep buildStep) const
234
0
{
235
0
  return cm::make_unique<cmRulePlaceholderExpander>(
236
0
    buildStep, this->Compilers, this->VariableMappings, this->CompilerSysroot,
237
0
    this->LinkerSysroot,
238
0
    this->GetState()->UseWatcomWMake() || this->GetState()->UseBorlandMake()
239
0
      ? cmRulePlaceholderExpander::UseShortPaths::Yes
240
0
      : cmRulePlaceholderExpander::UseShortPaths::No);
241
0
}
242
243
0
cmLocalGenerator::~cmLocalGenerator() = default;
244
245
void cmLocalGenerator::IssueMessage(MessageType type, std::string const& text,
246
                                    cmListFileBacktrace const& bt) const
247
0
{
248
0
  this->GetMakefile()->IssueMessage(type, text, bt);
249
0
}
250
251
void cmLocalGenerator::IssueDiagnostic(
252
  cmDiagnosticCategory category, std::string const& text,
253
  cmDiagnosticContext const& context) const
254
0
{
255
0
  this->GetMakefile()->IssueDiagnostic(category, text, context);
256
0
}
257
258
void cmLocalGenerator::IssuePolicyWarning(cmPolicies::PolicyID policy,
259
                                          cm::string_view preface,
260
                                          cm::string_view postface,
261
                                          cmListFileBacktrace const& bt) const
262
0
{
263
0
  this->GetMakefile()->IssuePolicyWarning(policy, preface, postface, bt);
264
0
}
265
266
void cmLocalGenerator::ComputeObjectMaxPath()
267
0
{
268
// Choose a maximum object file name length.
269
#if defined(_WIN32) || defined(__CYGWIN__)
270
  this->ObjectPathMax = 250;
271
#else
272
0
  this->ObjectPathMax = 1000;
273
0
#endif
274
0
  cmValue plen = this->Makefile->GetDefinition("CMAKE_OBJECT_PATH_MAX");
275
0
  if (cmNonempty(plen)) {
276
0
    unsigned int pmax;
277
0
    if (sscanf(plen->c_str(), "%u", &pmax) == 1) {
278
0
      if (pmax >= 128) {
279
0
        this->ObjectPathMax = pmax;
280
0
      } else {
281
0
        std::ostringstream w;
282
0
        w << "CMAKE_OBJECT_PATH_MAX is set to " << pmax
283
0
          << ", which is less than the minimum of 128.  "
284
0
             "The value will be ignored.";
285
0
        this->IssueDiagnostic(cmDiagnostics::CMD_AUTHOR, w.str());
286
0
      }
287
0
    } else {
288
0
      std::ostringstream w;
289
0
      w << "CMAKE_OBJECT_PATH_MAX is set to \"" << *plen
290
0
        << "\", which fails to parse as a positive integer.  "
291
0
           "The value will be ignored.";
292
0
      this->IssueDiagnostic(cmDiagnostics::CMD_AUTHOR, w.str());
293
0
    }
294
0
  }
295
0
  this->ObjectMaxPathViolations.clear();
296
0
}
297
298
static void MoveSystemIncludesToEnd(std::vector<std::string>& includeDirs,
299
                                    std::string const& config,
300
                                    std::string const& lang,
301
                                    cmGeneratorTarget const* target)
302
0
{
303
0
  if (!target) {
304
0
    return;
305
0
  }
306
307
0
  std::stable_sort(
308
0
    includeDirs.begin(), includeDirs.end(),
309
0
    [&target, &config, &lang](std::string const& a, std::string const& b) {
310
0
      return !target->IsSystemIncludeDirectory(a, config, lang) &&
311
0
        target->IsSystemIncludeDirectory(b, config, lang);
312
0
    });
313
0
}
314
315
static void MoveSystemIncludesToEnd(std::vector<BT<std::string>>& includeDirs,
316
                                    std::string const& config,
317
                                    std::string const& lang,
318
                                    cmGeneratorTarget const* target)
319
0
{
320
0
  if (!target) {
321
0
    return;
322
0
  }
323
324
0
  std::stable_sort(includeDirs.begin(), includeDirs.end(),
325
0
                   [target, &config, &lang](BT<std::string> const& a,
326
0
                                            BT<std::string> const& b) {
327
0
                     return !target->IsSystemIncludeDirectory(a.Value, config,
328
0
                                                              lang) &&
329
0
                       target->IsSystemIncludeDirectory(b.Value, config, lang);
330
0
                   });
331
0
}
332
333
void cmLocalGenerator::TraceDependencies() const
334
0
{
335
  // Generate the rule files for each target.
336
0
  auto const& targets = this->GetGeneratorTargets();
337
0
  for (auto const& target : targets) {
338
0
    if (!target->IsInBuildSystem()) {
339
0
      continue;
340
0
    }
341
0
    target->TraceDependencies();
342
0
  }
343
0
}
344
345
#ifndef CMAKE_BOOTSTRAP
346
void cmLocalGenerator::ResolveSourceGroupGenex()
347
0
{
348
0
  this->Makefile->ResolveSourceGroupGenex(this);
349
0
}
350
#endif
351
352
void cmLocalGenerator::GenerateTestFiles()
353
0
{
354
0
  if (!this->Makefile->IsOn("CMAKE_TESTING_ENABLED")) {
355
0
    return;
356
0
  }
357
358
  // Compute the set of configurations.
359
0
  std::vector<std::string> configurationTypes =
360
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::OnlyMultiConfig);
361
0
  std::string config = this->Makefile->GetDefaultConfiguration();
362
363
0
  std::string file =
364
0
    cmStrCat(this->StateSnapshot.GetDirectory().GetCurrentBinary(),
365
0
             "/CTestTestfile.cmake");
366
0
  this->GlobalGenerator->AddTestFile(file);
367
368
0
  cmGeneratedFileStream fout(file);
369
370
0
  fout << "# CMake generated Testfile for \n"
371
0
          "# Source directory: "
372
0
       << this->StateSnapshot.GetDirectory().GetCurrentSource()
373
0
       << "\n"
374
0
          "# Build directory: "
375
0
       << this->StateSnapshot.GetDirectory().GetCurrentBinary()
376
0
       << "\n"
377
0
          "# \n"
378
0
          "# This file includes the relevant testing commands "
379
0
          "required for \n"
380
0
          "# testing this directory and lists subdirectories to "
381
0
          "be tested as well.\n";
382
383
0
  std::string resourceSpecFile =
384
0
    this->Makefile->GetSafeDefinition("CTEST_RESOURCE_SPEC_FILE");
385
0
  if (!resourceSpecFile.empty()) {
386
0
    fout << "set(CTEST_RESOURCE_SPEC_FILE \"" << resourceSpecFile << "\")\n";
387
0
  }
388
389
0
  cmValue testIncludeFile = this->Makefile->GetProperty("TEST_INCLUDE_FILE");
390
0
  if (testIncludeFile) {
391
0
    fout << "include(\"" << *testIncludeFile << "\")\n";
392
0
  }
393
394
0
  cmValue testIncludeFiles = this->Makefile->GetProperty("TEST_INCLUDE_FILES");
395
0
  if (testIncludeFiles) {
396
0
    cmList includesList{ *testIncludeFiles };
397
0
    for (std::string const& i : includesList) {
398
0
      fout << "include(\"" << i << "\")\n";
399
0
    }
400
0
  }
401
402
  // Ask each test generator to write its code.
403
0
  for (auto const& tester : this->Makefile->GetTestGenerators()) {
404
0
    tester->Compute(this);
405
0
    tester->Generate(fout, config, configurationTypes);
406
0
  }
407
0
  using vec_t = std::vector<cmStateSnapshot>;
408
0
  vec_t const& children = this->Makefile->GetStateSnapshot().GetChildren();
409
0
  for (cmStateSnapshot const& i : children) {
410
    // TODO: Use add_subdirectory instead?
411
0
    std::string outP = i.GetDirectory().GetCurrentBinary();
412
0
    outP = this->MaybeRelativeToCurBinDir(outP);
413
0
    fout << "subdirs(" << cmScriptGenerator::Quote(outP) << ")\n";
414
0
  }
415
416
  // Add directory labels property
417
0
  cmValue directoryLabels =
418
0
    this->Makefile->GetDefinition("CMAKE_DIRECTORY_LABELS");
419
0
  cmValue labels = this->Makefile->GetProperty("LABELS");
420
421
0
  if (labels || directoryLabels) {
422
0
    fout << "set_directory_properties(PROPERTIES LABELS ";
423
0
    if (labels) {
424
0
      fout << cmScriptGenerator::Quote(*labels);
425
0
    }
426
0
    if (labels && directoryLabels) {
427
0
      fout << ";";
428
0
    }
429
0
    if (directoryLabels) {
430
0
      fout << cmScriptGenerator::Quote(*directoryLabels);
431
0
    }
432
0
    fout << ")\n";
433
0
  }
434
0
}
435
436
void cmLocalGenerator::CreateEvaluationFileOutputs()
437
0
{
438
0
  std::vector<std::string> const& configs =
439
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
440
0
  for (std::string const& c : configs) {
441
0
    this->CreateEvaluationFileOutputs(c);
442
0
  }
443
0
}
444
445
void cmLocalGenerator::CreateEvaluationFileOutputs(std::string const& config)
446
0
{
447
0
  for (auto const& geef : this->Makefile->GetEvaluationFiles()) {
448
0
    geef->CreateOutputFile(this, config);
449
0
  }
450
0
}
451
452
void cmLocalGenerator::ProcessEvaluationFiles(
453
  std::vector<std::string>& generatedFiles)
454
0
{
455
0
  for (auto const& geef : this->Makefile->GetEvaluationFiles()) {
456
0
    geef->Generate(this);
457
0
    if (cmSystemTools::GetFatalErrorOccurred()) {
458
0
      return;
459
0
    }
460
0
    std::vector<std::string> files = geef->GetFiles();
461
0
    std::sort(files.begin(), files.end());
462
463
0
    std::vector<std::string> intersection;
464
0
    std::set_intersection(files.begin(), files.end(), generatedFiles.begin(),
465
0
                          generatedFiles.end(),
466
0
                          std::back_inserter(intersection));
467
0
    if (!intersection.empty()) {
468
0
      cmSystemTools::Error("Files to be generated by multiple different "
469
0
                           "commands: " +
470
0
                           cmWrap('"', intersection, '"', " "));
471
0
      return;
472
0
    }
473
474
0
    cm::append(generatedFiles, files);
475
0
    std::inplace_merge(generatedFiles.begin(),
476
0
                       generatedFiles.end() - files.size(),
477
0
                       generatedFiles.end());
478
0
  }
479
0
}
480
481
void cmLocalGenerator::GenerateInstallRules()
482
0
{
483
  // Compute the install prefix.
484
0
  cmValue installPrefix =
485
0
    this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
486
0
  std::string prefix = *installPrefix;
487
488
#if defined(_WIN32) && !defined(__CYGWIN__)
489
  if (!installPrefix) {
490
    if (!cmSystemTools::GetEnv("SystemDrive", prefix)) {
491
      prefix = "C:";
492
    }
493
    cmValue project_name = this->Makefile->GetDefinition("PROJECT_NAME");
494
    if (cmNonempty(project_name)) {
495
      prefix += "/Program Files/";
496
      prefix += *project_name;
497
    } else {
498
      prefix += "/InstalledCMakeProject";
499
    }
500
  }
501
#elif defined(__HAIKU__)
502
  char dir[B_PATH_NAME_LENGTH];
503
  if (!installPrefix) {
504
    if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
505
        B_OK) {
506
      prefix = dir;
507
    } else {
508
      prefix = "/boot/system";
509
    }
510
  }
511
#else
512
0
  if (!installPrefix) {
513
0
    prefix = "/usr/local";
514
0
  }
515
0
#endif
516
0
  if (cmValue stagingPrefix =
517
0
        this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX")) {
518
0
    prefix = *stagingPrefix;
519
0
  }
520
521
  // Compute the set of configurations.
522
0
  std::vector<std::string> configurationTypes =
523
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::OnlyMultiConfig);
524
0
  std::string config = this->Makefile->GetDefaultConfiguration();
525
526
  // Choose a default install configuration.
527
0
  std::string default_config = config;
528
0
  char const* default_order[] = { "RELEASE", "MINSIZEREL", "RELWITHDEBINFO",
529
0
                                  "DEBUG", nullptr };
530
0
  for (char const** c = default_order; *c && default_config.empty(); ++c) {
531
0
    for (std::string const& configurationType : configurationTypes) {
532
0
      if (cmSystemTools::UpperCase(configurationType) == *c) {
533
0
        default_config = configurationType;
534
0
      }
535
0
    }
536
0
  }
537
0
  if (default_config.empty() && !configurationTypes.empty()) {
538
0
    default_config = configurationTypes[0];
539
0
  }
540
541
  // Create the install script file.
542
0
  std::string file = this->StateSnapshot.GetDirectory().GetCurrentBinary();
543
0
  std::string homedir = this->GetState()->GetBinaryDirectory();
544
0
  int toplevel_install = 0;
545
0
  if (file == homedir) {
546
0
    toplevel_install = 1;
547
0
  }
548
0
  file += "/cmake_install.cmake";
549
0
  this->GetGlobalGenerator()->AddInstallScript(file);
550
0
  cmGeneratedFileStream fout(file);
551
552
  // Write the header.
553
  /* clang-format off */
554
0
  fout << "# Install script for directory: "
555
0
       << this->StateSnapshot.GetDirectory().GetCurrentSource()
556
0
       << "\n\n"
557
0
          "# Set the install prefix\n"
558
0
          "if(NOT DEFINED CMAKE_INSTALL_PREFIX)\n"
559
0
          "  set(CMAKE_INSTALL_PREFIX \"" << prefix << "\")\n"
560
0
          "endif()\n"
561
0
       << R"(string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX )"
562
0
       << "\"${CMAKE_INSTALL_PREFIX}\")\n\n";
563
  /* clang-format on */
564
565
  // Write support code for generating per-configuration install rules.
566
  /* clang-format off */
567
0
  fout <<
568
0
    "# Set the install configuration name.\n"
569
0
    "if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
570
0
    "  if(BUILD_TYPE)\n"
571
0
    "    string(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
572
0
    "           CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
573
0
    "  else()\n"
574
0
    "    set(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
575
0
    "  endif()\n"
576
0
    "  message(STATUS \"Install configuration: "
577
0
    "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
578
0
    "endif()\n"
579
0
    "\n";
580
  /* clang-format on */
581
582
  // Write support code for dealing with component-specific installs.
583
  /* clang-format off */
584
0
  fout <<
585
0
    "# Set the component getting installed.\n"
586
0
    "if(NOT CMAKE_INSTALL_COMPONENT)\n"
587
0
    "  if(COMPONENT)\n"
588
0
    "    message(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
589
0
    "    set(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
590
0
    "  else()\n"
591
0
    "    set(CMAKE_INSTALL_COMPONENT)\n"
592
0
    "  endif()\n"
593
0
    "endif()\n"
594
0
    "\n";
595
  /* clang-format on */
596
597
  // Copy user-specified install options to the install code.
598
0
  if (cmValue so_no_exe =
599
0
        this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE")) {
600
    /* clang-format off */
601
0
    fout <<
602
0
      "# Install shared libraries without execute permission?\n"
603
0
      "if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
604
0
      "  set(CMAKE_INSTALL_SO_NO_EXE \"" << *so_no_exe << "\")\n"
605
0
      "endif()\n"
606
0
      "\n";
607
    /* clang-format on */
608
0
  }
609
610
  // Copy cmake cross compile state to install code.
611
0
  if (cmValue crosscompiling =
612
0
        this->Makefile->GetDefinition("CMAKE_CROSSCOMPILING")) {
613
    /* clang-format off */
614
0
    fout <<
615
0
      "# Is this installation the result of a crosscompile?\n"
616
0
      "if(NOT DEFINED CMAKE_CROSSCOMPILING)\n"
617
0
      "  set(CMAKE_CROSSCOMPILING \"" << *crosscompiling << "\")\n"
618
0
      "endif()\n"
619
0
      "\n";
620
    /* clang-format on */
621
0
  }
622
623
  // Write default directory permissions.
624
0
  if (cmValue defaultDirPermissions = this->Makefile->GetDefinition(
625
0
        "CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS")) {
626
    /* clang-format off */
627
0
    fout <<
628
0
      "# Set default install directory permissions.\n"
629
0
      "if(NOT DEFINED CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS)\n"
630
0
      "  set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS \""
631
0
         << *defaultDirPermissions << "\")\n"
632
0
      "endif()\n"
633
0
      "\n";
634
    /* clang-format on */
635
0
  }
636
637
  // Write out CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM so that
638
  // installed code that uses `file(GET_RUNTIME_DEPENDENCIES)`
639
  // has same platform variable as when running cmake
640
0
  if (cmValue platform = this->Makefile->GetDefinition(
641
0
        "CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM")) {
642
    /* clang-format off */
643
0
    fout <<
644
0
      "# Set OS and executable format for runtime-dependencies.\n"
645
0
      "if(NOT DEFINED CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM)\n"
646
0
      "  set(CMAKE_GET_RUNTIME_DEPENDENCIES_PLATFORM \""
647
0
         << *platform << "\")\n"
648
0
      "endif()\n"
649
0
      "\n";
650
    /* clang-format on */
651
0
  }
652
653
  // Write out CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL so that
654
  // installed code that uses `file(GET_RUNTIME_DEPENDENCIES)`
655
  // has same tool selected as when running cmake
656
0
  if (cmValue command =
657
0
        this->Makefile->GetDefinition("CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL")) {
658
    /* clang-format off */
659
0
    fout <<
660
0
      "# Set tool for dependency-resolution of runtime-dependencies.\n"
661
0
      "if(NOT DEFINED CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL)\n"
662
0
      "  set(CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL \""
663
0
         << *command << "\")\n"
664
0
      "endif()\n"
665
0
      "\n";
666
    /* clang-format on */
667
0
  }
668
669
  // Write out CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND so that
670
  // installed code that uses `file(GET_RUNTIME_DEPENDENCIES)`
671
  // has same path to the tool as when running cmake
672
0
  if (cmValue command = this->Makefile->GetDefinition(
673
0
        "CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND")) {
674
    /* clang-format off */
675
0
    fout <<
676
0
      "# Set path to tool for dependency-resolution of runtime-dependencies.\n"
677
0
      "if(NOT DEFINED CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND)\n"
678
0
      "  set(CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND \""
679
0
         << *command << "\")\n"
680
0
      "endif()\n"
681
0
      "\n";
682
    /* clang-format on */
683
0
  }
684
685
  // Write out CMAKE_OBJDUMP so that installed code that uses
686
  // `file(GET_RUNTIME_DEPENDENCIES)` and hasn't specified
687
  // CMAKE_GET_RUNTIME_DEPENDENCIES_COMMAND has consistent
688
  // logic to fallback to CMAKE_OBJDUMP when `objdump` is
689
  // not on the path
690
0
  if (cmValue command = this->Makefile->GetDefinition("CMAKE_OBJDUMP")) {
691
    /* clang-format off */
692
0
    fout <<
693
0
      "# Set path to fallback-tool for dependency-resolution.\n"
694
0
      "if(NOT DEFINED CMAKE_OBJDUMP)\n"
695
0
      "  set(CMAKE_OBJDUMP \""
696
0
         << *command << "\")\n"
697
0
      "endif()\n"
698
0
      "\n";
699
    /* clang-format on */
700
0
  }
701
702
0
  this->AddGeneratorSpecificInstallSetup(fout);
703
704
  // Ask each install generator to write its code.
705
0
  cmPolicies::PolicyStatus status = this->GetPolicyStatus(cmPolicies::CMP0082);
706
0
  auto const& installers = this->Makefile->GetInstallGenerators();
707
0
  bool haveSubdirectoryInstall = false;
708
0
  bool haveInstallAfterSubdirectory = false;
709
0
  if (status == cmPolicies::WARN) {
710
0
    for (auto const& installer : installers) {
711
0
      installer->CheckCMP0082(haveSubdirectoryInstall,
712
0
                              haveInstallAfterSubdirectory);
713
0
      installer->Generate(fout, config, configurationTypes);
714
0
    }
715
0
  } else {
716
0
    for (auto const& installer : installers) {
717
0
      installer->Generate(fout, config, configurationTypes);
718
0
    }
719
0
  }
720
721
  // Write rules from old-style specification stored in targets.
722
0
  this->GenerateTargetInstallRules(fout, config, configurationTypes);
723
724
  // Include install scripts from subdirectories.
725
0
  switch (status) {
726
0
    case cmPolicies::WARN:
727
0
      if (haveInstallAfterSubdirectory &&
728
0
          this->Makefile->PolicyOptionalWarningEnabled(
729
0
            "CMAKE_POLICY_WARNING_CMP0082")) {
730
0
        this->IssuePolicyWarning(cmPolicies::CMP0082);
731
0
      }
732
0
      CM_FALLTHROUGH;
733
0
    case cmPolicies::OLD: {
734
0
      std::vector<cmStateSnapshot> children =
735
0
        this->Makefile->GetStateSnapshot().GetChildren();
736
0
      if (!children.empty()) {
737
0
        fout << "if(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
738
0
        fout << "  # Include the install script for each subdirectory.\n";
739
0
        for (cmStateSnapshot const& c : children) {
740
0
          if (!c.GetDirectory().GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
741
0
            std::string odir = c.GetDirectory().GetCurrentBinary();
742
0
            cmSystemTools::ConvertToUnixSlashes(odir);
743
0
            fout << "  include(\"" << odir << "/cmake_install.cmake\")\n";
744
0
          }
745
0
        }
746
0
        fout << "\n";
747
0
        fout << "endif()\n\n";
748
0
      }
749
0
    } break;
750
751
0
    case cmPolicies::NEW:
752
      // NEW behavior is handled in
753
      // cmInstallSubdirectoryGenerator::GenerateScript()
754
0
      break;
755
0
  }
756
757
  /* clang-format off */
758
759
0
    fout <<
760
0
      "string(REPLACE \";\" \"\\n\" CMAKE_INSTALL_MANIFEST_CONTENT\n"
761
0
      "       \"${CMAKE_INSTALL_MANIFEST_FILES}\")\n"
762
0
      "if(CMAKE_INSTALL_LOCAL_ONLY)\n"
763
0
      "  file(WRITE \"" <<
764
0
      this->StateSnapshot.GetDirectory().GetCurrentBinary() <<
765
0
      "/install_local_manifest.txt\"\n"
766
0
      "     \"${CMAKE_INSTALL_MANIFEST_CONTENT}\")\n"
767
0
      "endif()\n";
768
769
0
    if (toplevel_install) {
770
0
      fout <<
771
0
        "if(CMAKE_INSTALL_COMPONENT)\n"
772
0
        "  if(CMAKE_INSTALL_COMPONENT MATCHES \"^[a-zA-Z0-9_.+-]+$\")\n"
773
0
        "    set(CMAKE_INSTALL_MANIFEST \"install_manifest_"
774
0
        "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
775
0
        "  else()\n"
776
0
        "    string(MD5 CMAKE_INST_COMP_HASH \"${CMAKE_INSTALL_COMPONENT}\")\n"
777
0
        "    set(CMAKE_INSTALL_MANIFEST \"install_manifest_"
778
0
        "${CMAKE_INST_COMP_HASH}.txt\")\n"
779
0
        "    unset(CMAKE_INST_COMP_HASH)\n"
780
0
        "  endif()\n"
781
0
        "else()\n"
782
0
        "  set(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
783
0
        "endif()\n"
784
0
        "\n"
785
0
        "if(NOT CMAKE_INSTALL_LOCAL_ONLY)\n"
786
0
        "  file(WRITE \"" << homedir << "/${CMAKE_INSTALL_MANIFEST}\"\n"
787
0
        "     \"${CMAKE_INSTALL_MANIFEST_CONTENT}\")\n"
788
0
        "endif()\n";
789
0
    }
790
  /* clang-format on */
791
0
}
792
793
void cmLocalGenerator::AddGeneratorTarget(
794
  std::unique_ptr<cmGeneratorTarget> gt)
795
0
{
796
0
  cmGeneratorTarget* gt_ptr = gt.get();
797
798
0
  this->GeneratorTargets.push_back(std::move(gt));
799
0
  this->GeneratorTargetSearchIndex.emplace(gt_ptr->GetName(), gt_ptr);
800
0
  this->GlobalGenerator->IndexGeneratorTarget(gt_ptr);
801
0
}
802
803
void cmLocalGenerator::AddImportedGeneratorTarget(cmGeneratorTarget* gt)
804
0
{
805
0
  this->ImportedGeneratorTargets.emplace(gt->GetName(), gt);
806
0
  this->GlobalGenerator->IndexGeneratorTarget(gt);
807
0
}
808
809
void cmLocalGenerator::AddOwnedImportedGeneratorTarget(
810
  std::unique_ptr<cmGeneratorTarget> gt)
811
0
{
812
0
  this->OwnedImportedGeneratorTargets.push_back(std::move(gt));
813
0
}
814
815
cmGeneratorTarget* cmLocalGenerator::FindLocalNonAliasGeneratorTarget(
816
  std::string const& name) const
817
0
{
818
0
  auto ti = this->GeneratorTargetSearchIndex.find(name);
819
0
  if (ti != this->GeneratorTargetSearchIndex.end()) {
820
0
    return ti->second;
821
0
  }
822
0
  return nullptr;
823
0
}
824
825
void cmLocalGenerator::ComputeTargetManifest()
826
0
{
827
  // Collect the set of configuration types.
828
0
  std::vector<std::string> configNames =
829
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
830
831
  // Add our targets to the manifest for each configuration.
832
0
  auto const& targets = this->GetGeneratorTargets();
833
0
  for (auto const& target : targets) {
834
0
    if (!target->IsInBuildSystem()) {
835
0
      continue;
836
0
    }
837
0
    for (std::string const& c : configNames) {
838
0
      target->ComputeTargetManifest(c);
839
0
    }
840
0
  }
841
0
}
842
843
bool cmLocalGenerator::ComputeTargetCompileFeatures()
844
0
{
845
  // Collect the set of configuration types.
846
0
  std::vector<std::string> configNames =
847
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
848
849
0
  using LanguagePair = std::pair<std::string, std::string>;
850
0
  std::vector<LanguagePair> pairedLanguages{
851
0
    { "OBJC", "C" }, { "OBJCXX", "CXX" }, { "CUDA", "CXX" }, { "HIP", "CXX" }
852
0
  };
853
0
  std::set<LanguagePair> inferredEnabledLanguages;
854
0
  for (auto const& lang : pairedLanguages) {
855
0
    if (this->Makefile->GetState()->GetLanguageEnabled(lang.first)) {
856
0
      inferredEnabledLanguages.insert(lang);
857
0
    }
858
0
  }
859
860
  // Process compile features of all targets.
861
0
  auto const& targets = this->GetGeneratorTargets();
862
0
  for (auto const& target : targets) {
863
0
    for (std::string const& c : configNames) {
864
0
      if (!target->ComputeCompileFeatures(c)) {
865
0
        return false;
866
0
      }
867
0
    }
868
869
    // Now that C/C++ _STANDARD values have been computed
870
    // set the values to ObjC/ObjCXX _STANDARD variables
871
0
    if (target->CanCompileSources()) {
872
0
      for (std::string const& c : configNames) {
873
0
        target->ComputeCompileFeatures(c, inferredEnabledLanguages);
874
0
      }
875
0
    }
876
0
  }
877
878
0
  return true;
879
0
}
880
881
bool cmLocalGenerator::IsRootMakefile() const
882
0
{
883
0
  return !this->StateSnapshot.GetBuildsystemDirectoryParent().IsValid();
884
0
}
885
886
cmState* cmLocalGenerator::GetState() const
887
0
{
888
0
  return this->GlobalGenerator->GetCMakeInstance()->GetState();
889
0
}
890
891
cmStateSnapshot cmLocalGenerator::GetStateSnapshot() const
892
0
{
893
0
  return this->Makefile->GetStateSnapshot();
894
0
}
895
896
std::string cmLocalGenerator::GetRuleLauncher(cmGeneratorTarget* target,
897
                                              std::string const& prop,
898
                                              std::string const& config)
899
0
{
900
0
  cmValue value = this->Makefile->GetProperty(prop);
901
0
  if (target) {
902
0
    value = target->GetProperty(prop);
903
0
  }
904
0
  if (value) {
905
0
    return cmGeneratorExpression::Evaluate(*value, this, config, target);
906
0
  }
907
0
  return "";
908
0
}
909
910
std::string cmLocalGenerator::ConvertToIncludeReference(
911
  std::string const& path, OutputFormat format)
912
0
{
913
0
  return this->ConvertToOutputForExisting(path, format);
914
0
}
915
916
std::string cmLocalGenerator::GetIncludeFlags(
917
  std::vector<std::string> const& includeDirs, cmGeneratorTarget* target,
918
  std::string const& lang, std::string const& config, bool forResponseFile)
919
0
{
920
0
  if (lang.empty()) {
921
0
    return "";
922
0
  }
923
924
0
  std::vector<std::string> includes = includeDirs;
925
0
  MoveSystemIncludesToEnd(includes, config, lang, target);
926
927
0
  OutputFormat shellFormat = forResponseFile ? RESPONSE : SHELL;
928
0
  std::ostringstream includeFlags;
929
930
0
  std::string const& includeFlag =
931
0
    this->Makefile->GetSafeDefinition(cmStrCat("CMAKE_INCLUDE_FLAG_", lang));
932
0
  bool quotePaths = false;
933
0
  if (this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS")) {
934
0
    quotePaths = true;
935
0
  }
936
0
  std::string sep = " ";
937
0
  bool repeatFlag = true;
938
  // should the include flag be repeated like ie. -IA -IB
939
0
  if (cmValue incSep = this->Makefile->GetDefinition(
940
0
        cmStrCat("CMAKE_INCLUDE_FLAG_SEP_", lang))) {
941
    // if there is a separator then the flag is not repeated but is only
942
    // given once i.e.  -classpath a:b:c
943
0
    sep = *incSep;
944
0
    repeatFlag = false;
945
0
  }
946
947
  // Support special system include flag if it is available and the
948
  // normal flag is repeated for each directory.
949
0
  cmValue sysIncludeFlag = nullptr;
950
0
  cmValue sysIncludeFlagWarning = nullptr;
951
0
  if (repeatFlag) {
952
0
    sysIncludeFlag = this->Makefile->GetDefinition(
953
0
      cmStrCat("CMAKE_INCLUDE_SYSTEM_FLAG_", lang));
954
0
    sysIncludeFlagWarning = this->Makefile->GetDefinition(
955
0
      cmStrCat("CMAKE_INCLUDE_SYSTEM_FLAG_", lang, "_WARNING"));
956
0
  }
957
958
0
  cmValue fwSearchFlag = this->Makefile->GetDefinition(
959
0
    cmStrCat("CMAKE_", lang, "_FRAMEWORK_SEARCH_FLAG"));
960
0
  cmValue sysFwSearchFlag = this->Makefile->GetDefinition(
961
0
    cmStrCat("CMAKE_", lang, "_SYSTEM_FRAMEWORK_SEARCH_FLAG"));
962
963
0
  bool flagUsed = false;
964
0
  bool sysIncludeFlagUsed = false;
965
0
  std::set<std::string> emitted;
966
#ifdef __APPLE__
967
  emitted.insert("/System/Library/Frameworks");
968
#endif
969
0
  for (std::string const& i : includes) {
970
0
    if (cmNonempty(fwSearchFlag) && this->Makefile->IsOn("APPLE") &&
971
0
        cmSystemTools::IsPathToFramework(i)) {
972
0
      std::string const frameworkDir = cmSystemTools::GetFilenamePath(i);
973
0
      if (emitted.insert(frameworkDir).second) {
974
0
        if (sysFwSearchFlag && target &&
975
0
            target->IsSystemIncludeDirectory(frameworkDir, config, lang)) {
976
0
          includeFlags << *sysFwSearchFlag;
977
0
        } else {
978
0
          includeFlags << *fwSearchFlag;
979
0
        }
980
0
        includeFlags << this->ConvertToOutputFormat(frameworkDir, shellFormat)
981
0
                     << " ";
982
0
      }
983
0
      continue;
984
0
    }
985
986
0
    if (!flagUsed || repeatFlag) {
987
0
      if (sysIncludeFlag && target &&
988
0
          target->IsSystemIncludeDirectory(i, config, lang)) {
989
0
        includeFlags << *sysIncludeFlag;
990
0
        sysIncludeFlagUsed = true;
991
0
      } else {
992
0
        includeFlags << includeFlag;
993
0
      }
994
0
      flagUsed = true;
995
0
    }
996
0
    std::string includePath = this->ConvertToIncludeReference(i, shellFormat);
997
0
    if (quotePaths && !includePath.empty() && includePath.front() != '\"') {
998
0
      includeFlags << "\"";
999
0
    }
1000
0
    includeFlags << includePath;
1001
0
    if (quotePaths && !includePath.empty() && includePath.front() != '\"') {
1002
0
      includeFlags << "\"";
1003
0
    }
1004
0
    includeFlags << sep;
1005
0
  }
1006
0
  if (sysIncludeFlagUsed && sysIncludeFlagWarning) {
1007
0
    includeFlags << *sysIncludeFlagWarning;
1008
0
  }
1009
0
  std::string flags = includeFlags.str();
1010
  // remove trailing separators
1011
0
  if ((sep[0] != ' ') && !flags.empty() && flags.back() == sep[0]) {
1012
0
    flags.back() = ' ';
1013
0
  }
1014
0
  return cmTrimWhitespace(flags);
1015
0
}
1016
1017
void cmLocalGenerator::AddCompileOptions(std::string& flags,
1018
                                         cmGeneratorTarget* target,
1019
                                         std::string const& lang,
1020
                                         std::string const& config)
1021
0
{
1022
0
  std::vector<BT<std::string>> tmpFlags;
1023
0
  this->AddCompileOptions(tmpFlags, target, lang, config);
1024
0
  this->AppendFlags(flags, tmpFlags);
1025
0
}
1026
1027
void cmLocalGenerator::AddCompileOptions(std::vector<BT<std::string>>& flags,
1028
                                         cmGeneratorTarget* target,
1029
                                         std::string const& lang,
1030
                                         std::string const& config)
1031
0
{
1032
0
  std::string langFlagRegexVar = cmStrCat("CMAKE_", lang, "_FLAG_REGEX");
1033
1034
0
  if (cmValue langFlagRegexStr =
1035
0
        this->Makefile->GetDefinition(langFlagRegexVar)) {
1036
    // Filter flags acceptable to this language.
1037
0
    if (cmValue targetFlags = target->GetProperty("COMPILE_FLAGS")) {
1038
0
      std::vector<std::string> opts;
1039
0
      cmSystemTools::ParseWindowsCommandLine(targetFlags->c_str(), opts);
1040
      // Re-escape these flags since COMPILE_FLAGS were already parsed
1041
      // as a command line above.
1042
0
      std::string compileOpts;
1043
0
      this->AppendCompileOptions(compileOpts, opts, langFlagRegexStr->c_str());
1044
0
      if (!compileOpts.empty()) {
1045
0
        flags.emplace_back(std::move(compileOpts));
1046
0
      }
1047
0
    }
1048
0
    std::vector<BT<std::string>> targetCompileOpts =
1049
0
      target->GetCompileOptions(config, lang);
1050
    // COMPILE_OPTIONS are escaped.
1051
0
    this->AppendCompileOptions(flags, targetCompileOpts,
1052
0
                               langFlagRegexStr->c_str());
1053
0
  } else {
1054
    // Use all flags.
1055
0
    if (cmValue targetFlags = target->GetProperty("COMPILE_FLAGS")) {
1056
      // COMPILE_FLAGS are not escaped for historical reasons.
1057
0
      std::string compileFlags;
1058
0
      this->AppendFlags(compileFlags, *targetFlags);
1059
0
      if (!compileFlags.empty()) {
1060
0
        flags.emplace_back(std::move(compileFlags));
1061
0
      }
1062
0
    }
1063
0
    std::vector<BT<std::string>> targetCompileOpts =
1064
0
      target->GetCompileOptions(config, lang);
1065
    // COMPILE_OPTIONS are escaped.
1066
0
    this->AppendCompileOptions(flags, targetCompileOpts);
1067
0
  }
1068
1069
0
  cmStandardLevelResolver standardResolver(this->Makefile);
1070
0
  for (auto const& it : target->GetMaxLanguageStandards()) {
1071
0
    cmValue standard = target->GetLanguageStandard(it.first, config);
1072
0
    if (!standard) {
1073
0
      continue;
1074
0
    }
1075
0
    if (standardResolver.IsLaterStandard(it.first, *standard, it.second)) {
1076
0
      std::ostringstream e;
1077
0
      e << "The COMPILE_FEATURES property of target \"" << target->GetName()
1078
0
        << "\" was evaluated when computing the link "
1079
0
           "implementation, and the \""
1080
0
        << it.first << "_STANDARD\" was \"" << it.second
1081
0
        << "\" for that computation.  Computing the "
1082
0
           "COMPILE_FEATURES based on the link implementation resulted in a "
1083
0
           "higher \""
1084
0
        << it.first << "_STANDARD\" \"" << *standard
1085
0
        << "\".  "
1086
0
           "This is not permitted. The COMPILE_FEATURES may not both depend "
1087
0
           "on "
1088
0
           "and be depended on by the link implementation.\n";
1089
0
      this->IssueMessage(MessageType::FATAL_ERROR, e.str());
1090
0
      return;
1091
0
    }
1092
0
  }
1093
1094
  // Add Warning as errors flags
1095
0
  if (!this->GetCMakeInstance()->GetIgnoreCompileWarningAsError()) {
1096
0
    cmValue const wError = target->GetProperty("COMPILE_WARNING_AS_ERROR");
1097
0
    cmValue const wErrorOpts = this->Makefile->GetDefinition(
1098
0
      cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_WARNING_AS_ERROR"));
1099
0
    if (wError.IsOn() && wErrorOpts.IsSet()) {
1100
0
      std::string wErrorFlags;
1101
0
      this->AppendCompileOptions(wErrorFlags, *wErrorOpts);
1102
0
      if (!wErrorFlags.empty()) {
1103
0
        flags.emplace_back(std::move(wErrorFlags));
1104
0
      }
1105
0
    }
1106
0
  }
1107
1108
  // Add compile flag for the MSVC compiler only.
1109
0
  cmMakefile* mf = this->GetMakefile();
1110
0
  if (cmValue jmc =
1111
0
        mf->GetDefinition("CMAKE_" + lang + "_COMPILE_OPTIONS_JMC")) {
1112
1113
    // Handle Just My Code debugging flags, /JMC.
1114
    // If the target is a Managed C++ one, /JMC is not compatible.
1115
0
    if (target->GetManagedType(config) !=
1116
0
        cmGeneratorTarget::ManagedType::Managed) {
1117
      // add /JMC flags if target property VS_JUST_MY_CODE_DEBUGGING is set
1118
      // to ON
1119
0
      if (cmValue jmcExprGen =
1120
0
            target->GetProperty("VS_JUST_MY_CODE_DEBUGGING")) {
1121
0
        std::string isJMCEnabled =
1122
0
          cmGeneratorExpression::Evaluate(*jmcExprGen, this, config);
1123
0
        if (cmIsOn(isJMCEnabled)) {
1124
0
          cmList optList{ *jmc };
1125
0
          std::string jmcFlags;
1126
0
          this->AppendCompileOptions(jmcFlags, optList);
1127
0
          if (!jmcFlags.empty()) {
1128
0
            flags.emplace_back(std::move(jmcFlags));
1129
0
          }
1130
0
        }
1131
0
      }
1132
0
    }
1133
0
  }
1134
0
}
1135
1136
cmTarget* cmLocalGenerator::AddCustomCommandToTarget(
1137
  std::string const& target, cmCustomCommandType type,
1138
  std::unique_ptr<cmCustomCommand> cc, cmObjectLibraryCommands objLibCommands)
1139
0
{
1140
0
  cmTarget* t = this->Makefile->GetCustomCommandTarget(
1141
0
    target, objLibCommands, this->DirectoryBacktrace);
1142
0
  if (!t) {
1143
0
    return nullptr;
1144
0
  }
1145
1146
0
  cc->SetBacktrace(this->DirectoryBacktrace);
1147
1148
0
  detail::AddCustomCommandToTarget(*this, cmCommandOrigin::Generator, t, type,
1149
0
                                   std::move(cc));
1150
1151
0
  return t;
1152
0
}
1153
1154
cmSourceFile* cmLocalGenerator::AddCustomCommandToOutput(
1155
  std::unique_ptr<cmCustomCommand> cc, bool replace)
1156
0
{
1157
  // Make sure there is at least one output.
1158
0
  if (cc->GetOutputs().empty()) {
1159
0
    cmSystemTools::Error("Attempt to add a custom rule with no output!");
1160
0
    return nullptr;
1161
0
  }
1162
1163
0
  cc->SetBacktrace(this->DirectoryBacktrace);
1164
0
  return detail::AddCustomCommandToOutput(*this, cmCommandOrigin::Generator,
1165
0
                                          std::move(cc), replace);
1166
0
}
1167
1168
cmTarget* cmLocalGenerator::AddUtilityCommand(
1169
  std::string const& utilityName, bool excludeFromAll,
1170
  std::unique_ptr<cmCustomCommand> cc)
1171
0
{
1172
0
  cmTarget* target =
1173
0
    this->Makefile->AddNewUtilityTarget(utilityName, excludeFromAll);
1174
0
  target->SetIsGeneratorProvided(true);
1175
1176
0
  if (cc->GetCommandLines().empty() && cc->GetDepends().empty()) {
1177
0
    return target;
1178
0
  }
1179
1180
0
  cc->SetBacktrace(this->DirectoryBacktrace);
1181
0
  detail::AddUtilityCommand(*this, cmCommandOrigin::Generator, target,
1182
0
                            std::move(cc));
1183
1184
0
  return target;
1185
0
}
1186
1187
std::vector<BT<std::string>> cmLocalGenerator::GetIncludeDirectoriesImplicit(
1188
  cmGeneratorTarget const* target, std::string const& lang,
1189
  std::string const& config, bool stripImplicitDirs,
1190
  bool appendAllImplicitDirs) const
1191
0
{
1192
0
  std::vector<BT<std::string>> result;
1193
  // Do not repeat an include path.
1194
0
  std::set<std::string> emitted;
1195
1196
0
  auto emitDir = [&result, &emitted](std::string const& dir) {
1197
0
    if (emitted.insert(dir).second) {
1198
0
      result.emplace_back(dir);
1199
0
    }
1200
0
  };
1201
1202
0
  auto emitBT = [&result, &emitted](BT<std::string> const& dir) {
1203
0
    if (emitted.insert(dir.Value).second) {
1204
0
      result.emplace_back(dir);
1205
0
    }
1206
0
  };
1207
1208
  // When automatic include directories are requested for a build then
1209
  // include the source and binary directories at the beginning of the
1210
  // include path to approximate include file behavior for an
1211
  // in-source build.  This does not account for the case of a source
1212
  // file in a subdirectory of the current source directory but we
1213
  // cannot fix this because not all native build tools support
1214
  // per-source-file include paths.
1215
0
  if (this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR")) {
1216
    // Current binary directory
1217
0
    emitDir(this->StateSnapshot.GetDirectory().GetCurrentBinary());
1218
    // Current source directory
1219
0
    emitDir(this->StateSnapshot.GetDirectory().GetCurrentSource());
1220
0
  }
1221
1222
0
  if (!target) {
1223
0
    return result;
1224
0
  }
1225
1226
  // Standard include directories to be added unconditionally at the end.
1227
  // These are intended to simulate additional implicit include directories.
1228
0
  cmList userStandardDirs;
1229
0
  {
1230
0
    std::string const value = this->Makefile->GetSafeDefinition(
1231
0
      cmStrCat("CMAKE_", lang, "_STANDARD_INCLUDE_DIRECTORIES"));
1232
0
    userStandardDirs.assign(value);
1233
0
    for (std::string& usd : userStandardDirs) {
1234
0
      cmSystemTools::ConvertToUnixSlashes(usd);
1235
0
    }
1236
0
  }
1237
1238
  // Implicit include directories
1239
0
  std::vector<std::string> implicitDirs;
1240
0
  std::set<std::string> implicitSet;
1241
  // Include directories to be excluded as if they were implicit.
1242
0
  std::set<std::string> implicitExclude;
1243
0
  {
1244
    // Raw list of implicit include directories
1245
    // Start with "standard" directories that we unconditionally add below.
1246
0
    std::vector<std::string> impDirVec = userStandardDirs;
1247
1248
    // Load implicit include directories for this language.
1249
    // We ignore this for Fortran because:
1250
    // * There are no standard library headers to avoid overriding.
1251
    // * Compilers like gfortran do not search their own implicit include
1252
    //   directories for modules ('.mod' files).
1253
0
    if (lang != "Fortran") {
1254
0
      size_t const impDirVecOldSize = impDirVec.size();
1255
0
      cmList::append(impDirVec,
1256
0
                     this->Makefile->GetDefinition(cmStrCat(
1257
0
                       "CMAKE_", lang, "_IMPLICIT_INCLUDE_DIRECTORIES")));
1258
      // FIXME: Use cmRange with 'advance()' when it supports non-const.
1259
0
      for (size_t i = impDirVecOldSize; i < impDirVec.size(); ++i) {
1260
0
        cmSystemTools::ConvertToUnixSlashes(impDirVec[i]);
1261
0
      }
1262
1263
      // The CMAKE_<LANG>_IMPLICIT_INCLUDE_DIRECTORIES are computed using
1264
      // try_compile in CMAKE_DETERMINE_COMPILER_ABI, but the implicit include
1265
      // directories are not known during that try_compile.  This can be a
1266
      // problem when the HIP runtime include path is /usr/include because the
1267
      // runtime include path is always added to the userDirs and the compiler
1268
      // includes standard library headers via "__clang_hip_runtime_wrapper.h".
1269
0
      if (lang == "HIP" && impDirVec.size() == impDirVecOldSize &&
1270
0
          !cm::contains(impDirVec, "/usr/include")) {
1271
0
        implicitExclude.emplace("/usr/include");
1272
0
      }
1273
0
    }
1274
1275
    // The Platform/UnixPaths module used to hard-code /usr/include for C, CXX,
1276
    // and CUDA in CMAKE_<LANG>_IMPLICIT_INCLUDE_DIRECTORIES, but those
1277
    // variables are now computed.  On macOS the /usr/include directory is
1278
    // inside the platform SDK so the computed value does not contain it
1279
    // directly.  In this case adding -I/usr/include can hide SDK headers so we
1280
    // must still exclude it.
1281
0
    if ((lang == "C" || lang == "CXX" || lang == "CUDA") &&
1282
0
        !cm::contains(impDirVec, "/usr/include") &&
1283
0
        std::find_if(impDirVec.begin(), impDirVec.end(),
1284
0
                     [](std::string const& d) {
1285
0
                       return cmHasLiteralSuffix(d, "/usr/include");
1286
0
                     }) != impDirVec.end()) {
1287
      // Only exclude this hard coded path for backwards compatibility.
1288
0
      implicitExclude.emplace("/usr/include");
1289
0
    }
1290
1291
0
    for (std::string const& i : impDirVec) {
1292
0
      if (implicitSet.insert(this->GlobalGenerator->GetRealPath(i)).second) {
1293
0
        implicitDirs.emplace_back(i);
1294
0
      }
1295
0
    }
1296
0
  }
1297
1298
0
  bool const isCorCxx = (lang == "C" || lang == "CXX");
1299
1300
  // Resolve symlinks in CPATH for comparison with resolved include paths.
1301
  // We do this here instead of when EnvCPATH is populated in case symlinks
1302
  // on disk have changed in the meantime.
1303
0
  std::set<std::string> resolvedEnvCPATH;
1304
0
  if (isCorCxx) {
1305
0
    for (std::string const& i : this->EnvCPATH) {
1306
0
      resolvedEnvCPATH.emplace(this->GlobalGenerator->GetRealPath(i));
1307
0
    }
1308
0
  }
1309
1310
  // Checks if this is not an excluded (implicit) include directory.
1311
0
  auto notExcluded = [this, &implicitSet, &implicitExclude, &resolvedEnvCPATH,
1312
0
                      isCorCxx](std::string const& dir) -> bool {
1313
0
    std::string const& real_dir = this->GlobalGenerator->GetRealPath(dir);
1314
0
    return
1315
      // Do not exclude directories that are not in any excluded set.
1316
0
      !(cm::contains(implicitSet, real_dir) ||
1317
0
        cm::contains(implicitExclude, dir))
1318
      // Do not exclude entries of the CPATH environment variable even though
1319
      // they are implicitly searched by the compiler.  They are meant to be
1320
      // user-specified directories that can be re-ordered or converted to
1321
      // -isystem without breaking real compiler builtin headers.
1322
0
      || (isCorCxx && cm::contains(resolvedEnvCPATH, real_dir));
1323
0
  };
1324
1325
  // Get the target-specific include directories.
1326
0
  std::vector<BT<std::string>> userDirs =
1327
0
    target->GetIncludeDirectories(config, lang);
1328
1329
  // Support putting all the in-project include directories first if
1330
  // it is requested by the project.
1331
0
  if (this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE")) {
1332
0
    std::string const& topSourceDir = this->GetState()->GetSourceDirectory();
1333
0
    std::string const& topBinaryDir = this->GetState()->GetBinaryDirectory();
1334
0
    for (BT<std::string> const& udr : userDirs) {
1335
      // Emit this directory only if it is a subdirectory of the
1336
      // top-level source or binary tree.
1337
0
      if (cmSystemTools::ComparePath(udr.Value, topSourceDir) ||
1338
0
          cmSystemTools::ComparePath(udr.Value, topBinaryDir) ||
1339
0
          cmSystemTools::IsSubDirectory(udr.Value, topSourceDir) ||
1340
0
          cmSystemTools::IsSubDirectory(udr.Value, topBinaryDir)) {
1341
0
        if (notExcluded(udr.Value)) {
1342
0
          emitBT(udr);
1343
0
        }
1344
0
      }
1345
0
    }
1346
0
  }
1347
1348
  // Emit remaining non implicit user directories.
1349
0
  for (BT<std::string> const& udr : userDirs) {
1350
0
    if (notExcluded(udr.Value)) {
1351
0
      emitBT(udr);
1352
0
    }
1353
0
  }
1354
1355
  // Sort result
1356
0
  MoveSystemIncludesToEnd(result, config, lang, target);
1357
1358
  // Append standard include directories for this language.
1359
0
  userDirs.reserve(userDirs.size() + userStandardDirs.size());
1360
0
  for (std::string& usd : userStandardDirs) {
1361
0
    emitDir(usd);
1362
0
    userDirs.emplace_back(std::move(usd));
1363
0
  }
1364
1365
  // Append compiler implicit include directories
1366
0
  if (!stripImplicitDirs) {
1367
    // Append implicit directories that were requested by the user only
1368
0
    for (BT<std::string> const& udr : userDirs) {
1369
0
      if (cm::contains(implicitSet, cmSystemTools::GetRealPath(udr.Value))) {
1370
0
        emitBT(udr);
1371
0
      }
1372
0
    }
1373
    // Append remaining implicit directories (on demand)
1374
0
    if (appendAllImplicitDirs) {
1375
0
      for (std::string& imd : implicitDirs) {
1376
0
        emitDir(imd);
1377
0
      }
1378
0
    }
1379
0
  }
1380
1381
0
  return result;
1382
0
}
1383
1384
void cmLocalGenerator::GetIncludeDirectoriesImplicit(
1385
  std::vector<std::string>& dirs, cmGeneratorTarget const* target,
1386
  std::string const& lang, std::string const& config, bool stripImplicitDirs,
1387
  bool appendAllImplicitDirs) const
1388
0
{
1389
0
  std::vector<BT<std::string>> tmp = this->GetIncludeDirectoriesImplicit(
1390
0
    target, lang, config, stripImplicitDirs, appendAllImplicitDirs);
1391
0
  dirs.reserve(dirs.size() + tmp.size());
1392
0
  for (BT<std::string>& v : tmp) {
1393
0
    dirs.emplace_back(std::move(v.Value));
1394
0
  }
1395
0
}
1396
1397
std::vector<BT<std::string>> cmLocalGenerator::GetIncludeDirectories(
1398
  cmGeneratorTarget const* target, std::string const& lang,
1399
  std::string const& config) const
1400
0
{
1401
0
  return this->GetIncludeDirectoriesImplicit(target, lang, config);
1402
0
}
1403
1404
void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
1405
                                             cmGeneratorTarget const* target,
1406
                                             std::string const& lang,
1407
                                             std::string const& config) const
1408
0
{
1409
0
  this->GetIncludeDirectoriesImplicit(dirs, target, lang, config);
1410
0
}
1411
1412
void cmLocalGenerator::GetStaticLibraryFlags(std::string& flags,
1413
                                             std::string const& config,
1414
                                             std::string const& linkLanguage,
1415
                                             cmGeneratorTarget* target)
1416
0
{
1417
0
  std::vector<BT<std::string>> tmpFlags =
1418
0
    this->GetStaticLibraryFlags(config, linkLanguage, target);
1419
0
  this->AppendFlags(flags, tmpFlags);
1420
0
}
1421
1422
std::vector<BT<std::string>> cmLocalGenerator::GetStaticLibraryFlags(
1423
  std::string const& config, std::string const& linkLanguage,
1424
  cmGeneratorTarget* target)
1425
0
{
1426
0
  std::vector<BT<std::string>> flags;
1427
0
  if (linkLanguage != "Swift" && !this->IsSplitSwiftBuild()) {
1428
0
    std::string staticLibFlags;
1429
0
    this->AddConfigVariableFlags(staticLibFlags, "CMAKE_STATIC_LINKER_FLAGS",
1430
0
                                 config);
1431
0
    if (!staticLibFlags.empty()) {
1432
0
      flags.emplace_back(std::move(staticLibFlags));
1433
0
    }
1434
0
  }
1435
1436
0
  std::string staticLibFlags;
1437
0
  std::string const configUpper = cmSystemTools::UpperCase(config);
1438
0
  this->AppendFlags(staticLibFlags,
1439
0
                    target->GetSafeProperty("STATIC_LIBRARY_FLAGS"));
1440
0
  if (!configUpper.empty()) {
1441
0
    std::string name = "STATIC_LIBRARY_FLAGS_" + configUpper;
1442
0
    this->AppendFlags(staticLibFlags, target->GetSafeProperty(name));
1443
0
  }
1444
1445
0
  if (!staticLibFlags.empty()) {
1446
0
    flags.emplace_back(std::move(staticLibFlags));
1447
0
  }
1448
1449
0
  std::vector<BT<std::string>> staticLibOpts =
1450
0
    target->GetStaticLibraryLinkOptions(config, linkLanguage);
1451
  // STATIC_LIBRARY_OPTIONS are escaped.
1452
0
  this->AppendCompileOptions(flags, staticLibOpts);
1453
1454
0
  return flags;
1455
0
}
1456
1457
void cmLocalGenerator::GetDeviceLinkFlags(
1458
  cmLinkLineDeviceComputer& linkLineComputer, std::string const& config,
1459
  std::string& linkLibs, std::string& linkFlags, std::string& frameworkPath,
1460
  std::string& linkPath, cmGeneratorTarget* target)
1461
0
{
1462
0
  cmGeneratorTarget::DeviceLinkSetter setter(*target);
1463
1464
0
  cmComputeLinkInformation* pcli = target->GetLinkInformation(config);
1465
1466
0
  auto linklang = linkLineComputer.GetLinkerLanguage(target, config);
1467
0
  auto ipoEnabled = target->IsIPOEnabled(linklang, config);
1468
0
  if (!ipoEnabled && pcli) {
1469
0
    ipoEnabled = linkLineComputer.ComputeRequiresDeviceLinkingIPOFlag(*pcli);
1470
0
  }
1471
0
  if (ipoEnabled) {
1472
0
    if (cmValue cudaIPOFlags = this->Makefile->GetDefinition(
1473
0
          "CMAKE_CUDA_DEVICE_LINK_OPTIONS_IPO")) {
1474
0
      linkFlags += *cudaIPOFlags;
1475
0
    }
1476
0
  }
1477
1478
0
  if (pcli) {
1479
    // Compute the required device link libraries when
1480
    // resolving gpu lang device symbols
1481
0
    this->OutputLinkLibraries(pcli, &linkLineComputer, linkLibs, frameworkPath,
1482
0
                              linkPath);
1483
0
  }
1484
1485
0
  this->AddVisibilityPresetFlags(linkFlags, target, "CUDA");
1486
0
  this->GetGlobalGenerator()->EncodeLiteral(linkFlags);
1487
1488
0
  std::vector<std::string> linkOpts;
1489
0
  target->GetLinkOptions(linkOpts, config, "CUDA");
1490
0
  this->SetLinkScriptShell(this->GetGlobalGenerator()->GetUseLinkScript());
1491
  // LINK_OPTIONS are escaped.
1492
0
  this->AppendCompileOptions(linkFlags, linkOpts);
1493
0
  this->SetLinkScriptShell(false);
1494
0
}
1495
1496
void cmLocalGenerator::GetTargetFlags(
1497
  cmLinkLineComputer* linkLineComputer, std::string const& config,
1498
  std::string& linkLibs, std::string& flags, std::string& linkFlags,
1499
  std::string& frameworkPath, std::string& linkPath, cmGeneratorTarget* target)
1500
0
{
1501
0
  std::vector<BT<std::string>> linkFlagsList;
1502
0
  std::vector<BT<std::string>> linkPathList;
1503
0
  std::vector<BT<std::string>> linkLibsList;
1504
0
  this->GetTargetFlags(linkLineComputer, config, linkLibsList, flags,
1505
0
                       linkFlagsList, frameworkPath, linkPathList, target);
1506
0
  this->AppendFlags(linkFlags, linkFlagsList);
1507
0
  this->AppendFlags(linkPath, linkPathList);
1508
0
  this->AppendFlags(linkLibs, linkLibsList);
1509
0
}
1510
1511
void cmLocalGenerator::GetTargetFlags(
1512
  cmLinkLineComputer* linkLineComputer, std::string const& config,
1513
  std::vector<BT<std::string>>& linkLibs, std::string& flags,
1514
  std::vector<BT<std::string>>& linkFlags, std::string& frameworkPath,
1515
  std::vector<BT<std::string>>& linkPath, cmGeneratorTarget* target)
1516
0
{
1517
0
  std::string const configUpper = cmSystemTools::UpperCase(config);
1518
0
  cmComputeLinkInformation* pcli = target->GetLinkInformation(config);
1519
1520
0
  std::string const linkLanguage =
1521
0
    linkLineComputer->GetLinkerLanguage(target, config);
1522
1523
0
  {
1524
0
    std::string createFlags;
1525
0
    this->AppendTargetCreationLinkFlags(createFlags, target, linkLanguage);
1526
0
    if (!createFlags.empty()) {
1527
0
      linkFlags.emplace_back(std::move(createFlags));
1528
0
    }
1529
0
  }
1530
1531
0
  switch (target->GetType()) {
1532
0
    case cmStateEnums::STATIC_LIBRARY:
1533
0
      linkFlags = this->GetStaticLibraryFlags(config, linkLanguage, target);
1534
0
      break;
1535
0
    case cmStateEnums::MODULE_LIBRARY:
1536
0
      CM_FALLTHROUGH;
1537
0
    case cmStateEnums::SHARED_LIBRARY: {
1538
0
      if (this->IsSplitSwiftBuild() || linkLanguage != "Swift") {
1539
0
        std::string libFlags;
1540
0
        this->AddTargetTypeLinkerFlags(libFlags, target, linkLanguage, config);
1541
0
        if (!libFlags.empty()) {
1542
0
          linkFlags.emplace_back(std::move(libFlags));
1543
0
        }
1544
0
      }
1545
1546
0
      std::string langLinkFlags;
1547
0
      this->AddPerLanguageLinkFlags(langLinkFlags, target, linkLanguage,
1548
0
                                    config);
1549
0
      if (!langLinkFlags.empty()) {
1550
0
        linkFlags.emplace_back(std::move(langLinkFlags));
1551
0
      }
1552
1553
0
      std::string sharedLibFlags;
1554
0
      this->AddTargetPropertyLinkFlags(sharedLibFlags, target, config);
1555
0
      if (!sharedLibFlags.empty()) {
1556
0
        this->GetGlobalGenerator()->EncodeLiteral(sharedLibFlags);
1557
0
        linkFlags.emplace_back(std::move(sharedLibFlags));
1558
0
      }
1559
1560
0
      std::vector<BT<std::string>> linkOpts =
1561
0
        target->GetLinkOptions(config, linkLanguage);
1562
0
      this->SetLinkScriptShell(this->GetGlobalGenerator()->GetUseLinkScript());
1563
      // LINK_OPTIONS are escaped.
1564
0
      this->AppendCompileOptions(linkFlags, linkOpts);
1565
0
      this->SetLinkScriptShell(false);
1566
1567
0
      if (pcli) {
1568
0
        this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
1569
0
                                  frameworkPath, linkPath);
1570
0
      }
1571
0
    } break;
1572
0
    case cmStateEnums::EXECUTABLE: {
1573
0
      if (linkLanguage != "Swift" ||
1574
0
          (this->IsSplitSwiftBuild() &&
1575
0
           target->GetPolicyStatusCMP0214() == cmPolicies::NEW)) {
1576
0
        std::string exeFlags;
1577
0
        this->AddTargetTypeLinkerFlags(exeFlags, target, linkLanguage, config);
1578
0
        if (!exeFlags.empty()) {
1579
0
          linkFlags.emplace_back(std::move(exeFlags));
1580
0
        }
1581
0
      }
1582
1583
0
      std::string langLinkFlags;
1584
0
      this->AddPerLanguageLinkFlags(langLinkFlags, target, linkLanguage,
1585
0
                                    config);
1586
0
      if (!langLinkFlags.empty()) {
1587
0
        linkFlags.emplace_back(std::move(langLinkFlags));
1588
0
      }
1589
1590
0
      {
1591
0
        auto exeType = cmStrCat(
1592
0
          "CMAKE_", linkLanguage, "_CREATE_",
1593
0
          (target->IsWin32Executable(config) ? "WIN32" : "CONSOLE"), "_EXE");
1594
0
        std::string exeFlags;
1595
0
        this->AppendFlags(exeFlags, this->Makefile->GetDefinition(exeType),
1596
0
                          exeType, target, cmBuildStep::Link, linkLanguage);
1597
0
        if (!exeFlags.empty()) {
1598
0
          linkFlags.emplace_back(std::move(exeFlags));
1599
0
        }
1600
0
      }
1601
1602
0
      std::string exeFlags;
1603
0
      if (target->IsExecutableWithExports()) {
1604
0
        exeFlags += this->Makefile->GetSafeDefinition(
1605
0
          cmStrCat("CMAKE_EXE_EXPORTS_", linkLanguage, "_FLAG"));
1606
0
        exeFlags += " ";
1607
0
      }
1608
1609
0
      this->AddLanguageFlagsForLinking(flags, target, linkLanguage, config);
1610
0
      if (pcli) {
1611
0
        this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
1612
0
                                  frameworkPath, linkPath);
1613
0
      }
1614
1615
0
      if (this->Makefile->IsOn("BUILD_SHARED_LIBS")) {
1616
0
        std::string sFlagVar = "CMAKE_SHARED_BUILD_" + linkLanguage + "_FLAGS";
1617
0
        exeFlags += this->Makefile->GetSafeDefinition(sFlagVar);
1618
0
        exeFlags += " ";
1619
0
      }
1620
1621
0
      std::string exeExportFlags =
1622
0
        this->GetExeExportFlags(linkLanguage, *target);
1623
0
      if (!exeExportFlags.empty()) {
1624
0
        exeFlags += exeExportFlags;
1625
0
        exeFlags += " ";
1626
0
      }
1627
1628
0
      this->AddTargetPropertyLinkFlags(exeFlags, target, config);
1629
1630
0
      if (!exeFlags.empty()) {
1631
0
        this->GetGlobalGenerator()->EncodeLiteral(exeFlags);
1632
0
        linkFlags.emplace_back(std::move(exeFlags));
1633
0
      }
1634
1635
0
      std::vector<BT<std::string>> linkOpts =
1636
0
        target->GetLinkOptions(config, linkLanguage);
1637
0
      this->SetLinkScriptShell(this->GetGlobalGenerator()->GetUseLinkScript());
1638
      // LINK_OPTIONS are escaped.
1639
0
      this->AppendCompileOptions(linkFlags, linkOpts);
1640
0
      this->SetLinkScriptShell(false);
1641
0
    } break;
1642
0
    default:
1643
0
      break;
1644
0
  }
1645
1646
0
  std::string extraLinkFlags;
1647
0
  this->AppendLinkerTypeFlags(extraLinkFlags, target, config, linkLanguage);
1648
0
  this->AppendPositionIndependentLinkerFlags(extraLinkFlags, target, config,
1649
0
                                             linkLanguage);
1650
0
  this->AppendWarningAsErrorLinkerFlags(extraLinkFlags, target, linkLanguage);
1651
0
  this->AppendIPOLinkerFlags(extraLinkFlags, target, config, linkLanguage);
1652
0
  this->AppendModuleDefinitionFlag(extraLinkFlags, target, linkLineComputer,
1653
0
                                   config, linkLanguage);
1654
1655
0
  if (!extraLinkFlags.empty()) {
1656
0
    this->GetGlobalGenerator()->EncodeLiteral(extraLinkFlags);
1657
0
    linkFlags.emplace_back(std::move(extraLinkFlags));
1658
0
  }
1659
0
}
1660
1661
void cmLocalGenerator::GetTargetCompileFlags(cmGeneratorTarget* target,
1662
                                             std::string const& config,
1663
                                             std::string const& lang,
1664
                                             std::string& flags,
1665
                                             std::string const& arch)
1666
0
{
1667
0
  std::vector<BT<std::string>> tmpFlags =
1668
0
    this->GetTargetCompileFlags(target, config, lang, arch);
1669
0
  this->AppendFlags(flags, tmpFlags);
1670
0
}
1671
1672
std::vector<BT<std::string>> cmLocalGenerator::GetTargetCompileFlags(
1673
  cmGeneratorTarget* target, std::string const& config,
1674
  std::string const& lang, std::string const& arch)
1675
0
{
1676
0
  std::vector<BT<std::string>> flags;
1677
0
  std::string compileFlags;
1678
1679
0
  cmMakefile* mf = this->GetMakefile();
1680
1681
  // Add language-specific flags.
1682
0
  this->AddLanguageFlags(compileFlags, target, cmBuildStep::Compile, lang,
1683
0
                         config);
1684
1685
0
  if (target->IsIPOEnabled(lang, config)) {
1686
0
    this->AppendFeatureOptions(compileFlags, lang, "IPO");
1687
0
  }
1688
1689
0
  this->AddArchitectureFlags(compileFlags, target, lang, config, arch);
1690
1691
0
  if (lang == "Fortran") {
1692
0
    this->AppendFlags(compileFlags,
1693
0
                      this->GetTargetFortranFlags(target, config));
1694
0
  } else if (lang == "Swift") {
1695
    // Only set the compile mode if CMP0157 is set
1696
0
    if (cm::optional<cmSwiftCompileMode> swiftCompileMode =
1697
0
          this->GetSwiftCompileMode(target, config)) {
1698
0
      std::string swiftCompileModeFlag;
1699
0
      switch (*swiftCompileMode) {
1700
0
        case cmSwiftCompileMode::Incremental: {
1701
0
          swiftCompileModeFlag = "-incremental";
1702
0
          if (cmValue flag =
1703
0
                mf->GetDefinition("CMAKE_Swift_COMPILE_OPTIONS_INCREMENTAL")) {
1704
0
            swiftCompileModeFlag = *flag;
1705
0
          }
1706
0
          break;
1707
0
        }
1708
0
        case cmSwiftCompileMode::Wholemodule: {
1709
0
          swiftCompileModeFlag = "-wmo";
1710
0
          if (cmValue flag =
1711
0
                mf->GetDefinition("CMAKE_Swift_COMPILE_OPTIONS_WMO")) {
1712
0
            swiftCompileModeFlag = *flag;
1713
0
          }
1714
0
          break;
1715
0
        }
1716
0
        case cmSwiftCompileMode::Singlefile:
1717
0
          break;
1718
0
        case cmSwiftCompileMode::Unknown: {
1719
0
          this->IssueDiagnostic(
1720
0
            cmDiagnostics::CMD_AUTHOR,
1721
0
            cmStrCat("Unknown Swift_COMPILATION_MODE on target '",
1722
0
                     target->GetName(), '\''));
1723
0
        }
1724
0
      }
1725
0
      this->AppendFlags(compileFlags, swiftCompileModeFlag);
1726
0
    }
1727
0
  }
1728
1729
0
  this->AddFeatureFlags(compileFlags, target, lang, config);
1730
0
  this->AddVisibilityPresetFlags(compileFlags, target, lang);
1731
0
  this->AddColorDiagnosticsFlags(compileFlags, lang);
1732
0
  this->AppendFlags(compileFlags, mf->GetDefineFlags());
1733
0
  this->AppendFlags(compileFlags,
1734
0
                    this->GetFrameworkFlags(lang, config, target));
1735
0
  this->AppendFlags(compileFlags,
1736
0
                    this->GetXcFrameworkFlags(lang, config, target));
1737
1738
0
  if (!compileFlags.empty()) {
1739
0
    flags.emplace_back(std::move(compileFlags));
1740
0
  }
1741
0
  this->AddCompileOptions(flags, target, lang, config);
1742
0
  return flags;
1743
0
}
1744
1745
std::string cmLocalGenerator::GetFrameworkFlags(std::string const& lang,
1746
                                                std::string const& config,
1747
                                                cmGeneratorTarget* target)
1748
0
{
1749
0
  cmLocalGenerator* lg = target->GetLocalGenerator();
1750
0
  cmMakefile* mf = lg->GetMakefile();
1751
1752
0
  if (!target->IsApple()) {
1753
0
    return std::string();
1754
0
  }
1755
1756
0
  cmValue fwSearchFlag =
1757
0
    mf->GetDefinition(cmStrCat("CMAKE_", lang, "_FRAMEWORK_SEARCH_FLAG"));
1758
0
  cmValue sysFwSearchFlag = mf->GetDefinition(
1759
0
    cmStrCat("CMAKE_", lang, "_SYSTEM_FRAMEWORK_SEARCH_FLAG"));
1760
1761
0
  if (!fwSearchFlag && !sysFwSearchFlag) {
1762
0
    return std::string{};
1763
0
  }
1764
1765
0
  std::set<std::string> emitted;
1766
#ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
1767
  emitted.insert("/System/Library/Frameworks");
1768
#endif
1769
0
  std::vector<std::string> includes;
1770
1771
0
  lg->GetIncludeDirectories(includes, target, "C", config);
1772
  // check all include directories for frameworks as this
1773
  // will already have added a -F for the framework
1774
0
  for (std::string const& include : includes) {
1775
0
    if (lg->GetGlobalGenerator()->NameResolvesToFramework(include)) {
1776
0
      std::string frameworkDir = cmStrCat(include, "/../");
1777
0
      frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
1778
0
      emitted.insert(frameworkDir);
1779
0
    }
1780
0
  }
1781
1782
0
  std::string flags;
1783
0
  if (cmComputeLinkInformation* cli = target->GetLinkInformation(config)) {
1784
0
    std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
1785
0
    for (std::string const& framework : frameworks) {
1786
0
      if (emitted.insert(framework).second) {
1787
0
        if (sysFwSearchFlag &&
1788
0
            target->IsSystemIncludeDirectory(framework, config, lang)) {
1789
0
          flags += *sysFwSearchFlag;
1790
0
        } else {
1791
0
          flags += *fwSearchFlag;
1792
0
        }
1793
0
        flags +=
1794
0
          lg->ConvertToOutputFormat(framework, cmOutputConverter::SHELL);
1795
0
        flags += " ";
1796
0
      }
1797
0
    }
1798
0
  }
1799
0
  return flags;
1800
0
}
1801
1802
std::string cmLocalGenerator::GetXcFrameworkFlags(std::string const& lang,
1803
                                                  std::string const& config,
1804
                                                  cmGeneratorTarget* target)
1805
0
{
1806
0
  cmLocalGenerator* lg = target->GetLocalGenerator();
1807
0
  cmMakefile* mf = lg->GetMakefile();
1808
1809
0
  if (!target->IsApple()) {
1810
0
    return std::string();
1811
0
  }
1812
1813
0
  cmValue includeSearchFlag =
1814
0
    mf->GetDefinition(cmStrCat("CMAKE_INCLUDE_FLAG_", lang));
1815
0
  cmValue sysIncludeSearchFlag =
1816
0
    mf->GetDefinition(cmStrCat("CMAKE_INCLUDE_SYSTEM_FLAG_", lang));
1817
1818
0
  if (!includeSearchFlag && !sysIncludeSearchFlag) {
1819
0
    return std::string{};
1820
0
  }
1821
1822
0
  std::string flags;
1823
0
  if (cmComputeLinkInformation* cli = target->GetLinkInformation(config)) {
1824
0
    std::vector<std::string> const& paths = cli->GetXcFrameworkHeaderPaths();
1825
0
    for (std::string const& path : paths) {
1826
0
      if (sysIncludeSearchFlag &&
1827
0
          target->IsSystemIncludeDirectory(path, config, lang)) {
1828
0
        flags += *sysIncludeSearchFlag;
1829
0
      } else {
1830
0
        flags += *includeSearchFlag;
1831
0
      }
1832
0
      flags += lg->ConvertToOutputFormat(path, cmOutputConverter::SHELL);
1833
0
      flags += " ";
1834
0
    }
1835
0
  }
1836
0
  return flags;
1837
0
}
1838
1839
void cmLocalGenerator::GetTargetDefines(cmGeneratorTarget const* target,
1840
                                        std::string const& config,
1841
                                        std::string const& lang,
1842
                                        std::set<std::string>& defines) const
1843
0
{
1844
0
  std::set<BT<std::string>> tmp = this->GetTargetDefines(target, config, lang);
1845
0
  for (BT<std::string> const& v : tmp) {
1846
0
    defines.emplace(v.Value);
1847
0
  }
1848
0
}
1849
1850
std::set<BT<std::string>> cmLocalGenerator::GetTargetDefines(
1851
  cmGeneratorTarget const* target, std::string const& config,
1852
  std::string const& lang) const
1853
0
{
1854
0
  std::set<BT<std::string>> defines;
1855
1856
  // Add the export symbol definition for shared library objects.
1857
0
  if (std::string const* exportMacro = target->GetExportMacro()) {
1858
0
    this->AppendDefines(defines, *exportMacro);
1859
0
  }
1860
0
  for (auto const& sharedLibCompileDef :
1861
0
       target->GetSharedLibraryCompileDefs(config)) {
1862
0
    this->AppendDefines(defines, sharedLibCompileDef);
1863
0
  }
1864
1865
  // Add preprocessor definitions for this target and configuration.
1866
0
  std::vector<BT<std::string>> targetDefines =
1867
0
    target->GetCompileDefinitions(config, lang);
1868
0
  this->AppendDefines(defines, targetDefines);
1869
1870
0
  return defines;
1871
0
}
1872
1873
std::string cmLocalGenerator::GetTargetFortranFlags(
1874
  cmGeneratorTarget const* /*unused*/, std::string const& /*unused*/)
1875
0
{
1876
  // Implemented by specific generators that override this.
1877
0
  return std::string();
1878
0
}
1879
1880
/**
1881
 * Output the linking rules on a command line.  For executables,
1882
 * targetLibrary should be a NULL pointer.  For libraries, it should point
1883
 * to the name of the library.  This will not link a library against itself.
1884
 */
1885
void cmLocalGenerator::OutputLinkLibraries(
1886
  cmComputeLinkInformation* pcli, cmLinkLineComputer* linkLineComputer,
1887
  std::string& linkLibraries, std::string& frameworkPath,
1888
  std::string& linkPath)
1889
0
{
1890
0
  std::vector<BT<std::string>> linkLibrariesList;
1891
0
  std::vector<BT<std::string>> linkPathList;
1892
0
  this->OutputLinkLibraries(pcli, linkLineComputer, linkLibrariesList,
1893
0
                            frameworkPath, linkPathList);
1894
0
  pcli->AppendValues(linkLibraries, linkLibrariesList);
1895
0
  pcli->AppendValues(linkPath, linkPathList);
1896
0
}
1897
1898
void cmLocalGenerator::OutputLinkLibraries(
1899
  cmComputeLinkInformation* pcli, cmLinkLineComputer* linkLineComputer,
1900
  std::vector<BT<std::string>>& linkLibraries, std::string& frameworkPath,
1901
  std::vector<BT<std::string>>& linkPath)
1902
0
{
1903
0
  cmComputeLinkInformation& cli = *pcli;
1904
1905
0
  std::string linkLanguage = cli.GetLinkLanguage();
1906
1907
0
  std::string libPathFlag;
1908
0
  if (cmValue value = this->Makefile->GetDefinition(
1909
0
        "CMAKE_" + cli.GetLinkLanguage() + "_LIBRARY_PATH_FLAG")) {
1910
0
    libPathFlag = *value;
1911
0
  } else {
1912
0
    libPathFlag =
1913
0
      this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
1914
0
  }
1915
1916
0
  std::string libPathTerminator;
1917
0
  if (cmValue value = this->Makefile->GetDefinition(
1918
0
        "CMAKE_" + cli.GetLinkLanguage() + "_LIBRARY_PATH_TERMINATOR")) {
1919
0
    libPathTerminator = *value;
1920
0
  } else {
1921
0
    libPathTerminator =
1922
0
      this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
1923
0
  }
1924
1925
  // Add standard link directories for this language
1926
0
  std::string stdLinkDirString = this->Makefile->GetSafeDefinition(
1927
0
    cmStrCat("CMAKE_", cli.GetLinkLanguage(), "_STANDARD_LINK_DIRECTORIES"));
1928
1929
  // Add standard libraries for this language.
1930
0
  std::string stdLibString = this->Makefile->GetSafeDefinition(
1931
0
    cmStrCat("CMAKE_", cli.GetLinkLanguage(), "_STANDARD_LIBRARIES"));
1932
1933
  // Append the framework search path flags.
1934
0
  cmValue fwSearchFlag = this->Makefile->GetDefinition(
1935
0
    cmStrCat("CMAKE_", linkLanguage, "_FRAMEWORK_SEARCH_FLAG"));
1936
1937
0
  frameworkPath = linkLineComputer->ComputeFrameworkPath(cli, fwSearchFlag);
1938
0
  linkLineComputer->ComputeLinkPath(cli, libPathFlag, libPathTerminator,
1939
0
                                    stdLinkDirString, linkPath);
1940
0
  linkLineComputer->ComputeLinkLibraries(cli, stdLibString, linkLibraries);
1941
0
}
1942
1943
std::string cmLocalGenerator::GetExeExportFlags(
1944
  std::string const& linkLanguage, cmGeneratorTarget& tgt) const
1945
0
{
1946
0
  std::string linkFlags;
1947
1948
  // Flags to export symbols from an executable.
1949
0
  if (tgt.GetType() == cmStateEnums::EXECUTABLE &&
1950
0
      this->StateSnapshot.GetState()->GetGlobalPropertyAsBool(
1951
0
        "TARGET_SUPPORTS_SHARED_LIBS")) {
1952
    // Only add the flags if ENABLE_EXPORTS is on,
1953
    // except on AIX where we compute symbol exports.
1954
0
    if (!tgt.IsAIX() && tgt.GetPropertyAsBool("ENABLE_EXPORTS")) {
1955
0
      linkFlags = this->Makefile->GetSafeDefinition(
1956
0
        cmStrCat("CMAKE_SHARED_LIBRARY_LINK_", linkLanguage, "_FLAGS"));
1957
0
    }
1958
0
  }
1959
0
  return linkFlags;
1960
0
}
1961
1962
bool cmLocalGenerator::AllAppleArchSysrootsAreTheSame(
1963
  std::vector<std::string> const& archs, cmValue sysroot)
1964
0
{
1965
0
  if (!sysroot) {
1966
0
    return false;
1967
0
  }
1968
1969
0
  return std::all_of(archs.begin(), archs.end(),
1970
0
                     [this, sysroot](std::string const& arch) -> bool {
1971
0
                       std::string const& archSysroot =
1972
0
                         this->AppleArchSysroots[arch];
1973
0
                       return cmIsOff(archSysroot) || *sysroot == archSysroot;
1974
0
                     });
1975
0
}
1976
1977
void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
1978
                                            cmGeneratorTarget const* target,
1979
                                            std::string const& lang,
1980
                                            std::string const& config,
1981
                                            std::string const& filterArch)
1982
0
{
1983
  // Only add Apple specific flags on Apple platforms
1984
0
  if (target->IsApple() && this->EmitUniversalBinaryFlags) {
1985
0
    std::vector<std::string> archs = target->GetAppleArchs(config, lang);
1986
0
    if (!archs.empty() &&
1987
0
        (lang == "C" || lang == "CXX" || lang == "OBJC" || lang == "OBJCXX" ||
1988
0
         lang == "ASM")) {
1989
0
      for (std::string const& arch : archs) {
1990
0
        if (filterArch.empty() || filterArch == arch) {
1991
0
          flags += " -arch ";
1992
0
          flags += arch;
1993
0
        }
1994
0
      }
1995
0
    }
1996
1997
0
    cmValue sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
1998
0
    if (sysroot.IsEmpty() &&
1999
0
        this->Makefile->IsOn(
2000
0
          cmStrCat("CMAKE_", lang, "_COMPILER_APPLE_SYSROOT_REQUIRED"))) {
2001
0
      sysroot = this->Makefile->GetDefinition("_CMAKE_OSX_SYSROOT_PATH");
2002
0
    }
2003
0
    if (sysroot && *sysroot == "/") {
2004
0
      sysroot = nullptr;
2005
0
    }
2006
0
    std::string sysrootFlagVar = "CMAKE_" + lang + "_SYSROOT_FLAG";
2007
0
    cmValue sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar);
2008
0
    if (cmNonempty(sysrootFlag)) {
2009
0
      if (!this->AppleArchSysroots.empty() &&
2010
0
          !this->AllAppleArchSysrootsAreTheSame(archs, sysroot)) {
2011
0
        for (std::string const& arch : archs) {
2012
0
          std::string const& archSysroot = this->AppleArchSysroots[arch];
2013
0
          if (cmIsOff(archSysroot)) {
2014
0
            continue;
2015
0
          }
2016
0
          if (filterArch.empty() || filterArch == arch) {
2017
0
            flags += " -Xarch_" + arch + " ";
2018
            // Combine sysroot flag and path to work with -Xarch
2019
0
            std::string arch_sysroot = *sysrootFlag + archSysroot;
2020
0
            flags += this->ConvertToOutputFormat(arch_sysroot, SHELL);
2021
0
          }
2022
0
        }
2023
0
      } else if (cmNonempty(sysroot)) {
2024
0
        flags += " ";
2025
0
        flags += *sysrootFlag;
2026
0
        flags += " ";
2027
0
        flags += this->ConvertToOutputFormat(*sysroot, SHELL);
2028
0
      }
2029
0
    }
2030
2031
0
    cmValue deploymentTarget =
2032
0
      this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
2033
0
    if (cmNonempty(deploymentTarget)) {
2034
0
      std::string deploymentTargetFlagVar =
2035
0
        "CMAKE_" + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
2036
0
      cmValue deploymentTargetFlag =
2037
0
        this->Makefile->GetDefinition(deploymentTargetFlagVar);
2038
0
      if (cmNonempty(deploymentTargetFlag) &&
2039
          // CMAKE_<LANG>_COMPILER_TARGET overrides a --target= for
2040
          // CMAKE_OSX_DEPLOYMENT_TARGET, e.g., for visionOS.
2041
0
          (!cmHasLiteralPrefix(*deploymentTarget, "--target=") ||
2042
0
           this->Makefile
2043
0
             ->GetDefinition(cmStrCat("CMAKE_", lang, "_COMPILER_TARGET"))
2044
0
             .IsEmpty())) {
2045
0
        std::string flag = *deploymentTargetFlag;
2046
2047
        // Add the deployment target architecture to the flag, if needed.
2048
0
        static std::string const kARCH = "<ARCH>";
2049
0
        std::string::size_type archPos = flag.find(kARCH);
2050
0
        if (archPos != std::string::npos) {
2051
          // This placeholder is meant for visionOS, so default to arm64
2052
          // unless only non-arm64 archs are given.
2053
0
          std::string const arch =
2054
0
            (archs.empty() || cm::contains(archs, "arm64")) ? "arm64"
2055
0
                                                            : archs[0];
2056
          // Replace the placeholder with its value.
2057
0
          flag = cmStrCat(flag.substr(0, archPos), arch,
2058
0
                          flag.substr(archPos + kARCH.size()));
2059
0
        }
2060
2061
        // Add the deployment target version to the flag.
2062
0
        static std::string const kVERSION_MIN = "<VERSION_MIN>";
2063
0
        std::string::size_type verPos = flag.find(kVERSION_MIN);
2064
0
        if (verPos != std::string::npos) {
2065
          // Replace the placeholder with its value.
2066
0
          flag = cmStrCat(flag.substr(0, verPos), *deploymentTarget,
2067
0
                          flag.substr(verPos + kVERSION_MIN.size()));
2068
0
        } else {
2069
          // There is no placeholder, so append the value.
2070
0
          flag = cmStrCat(flag, *deploymentTarget);
2071
0
        }
2072
2073
0
        flags += " ";
2074
0
        flags += flag;
2075
0
      }
2076
0
    }
2077
0
  }
2078
0
}
2079
2080
void cmLocalGenerator::AddLanguageFlags(std::string& flags,
2081
                                        cmGeneratorTarget const* target,
2082
                                        cmBuildStep compileOrLink,
2083
                                        std::string const& lang,
2084
                                        std::string const& config)
2085
0
{
2086
  // Add language-specific flags.
2087
0
  this->AddConfigVariableFlags(flags, cmStrCat("CMAKE_", lang, "_FLAGS"),
2088
0
                               config);
2089
2090
  // Add the language standard flag for compiling, and sometimes linking.
2091
0
  if (compileOrLink == cmBuildStep::Compile ||
2092
0
      (compileOrLink == cmBuildStep::Link &&
2093
       // Some toolchains require use of the language standard flag
2094
       // when linking in order to use the matching standard library.
2095
       // FIXME: If CMake gains an abstraction for standard library
2096
       // selection, this will have to be reconciled with it.
2097
0
       this->Makefile->IsOn(
2098
0
         cmStrCat("CMAKE_", lang, "_LINK_WITH_STANDARD_COMPILE_OPTION")))) {
2099
0
    cmStandardLevelResolver standardResolver(this->Makefile);
2100
0
    std::string const& optionFlagDef =
2101
0
      standardResolver.GetCompileOptionDef(target, lang, config);
2102
0
    if (!optionFlagDef.empty()) {
2103
0
      cmValue opt =
2104
0
        target->Target->GetMakefile()->GetDefinition(optionFlagDef);
2105
0
      if (opt) {
2106
0
        cmList optList{ *opt };
2107
0
        for (std::string const& i : optList) {
2108
0
          this->AppendFlagEscape(flags, i);
2109
0
        }
2110
0
      }
2111
0
    }
2112
0
  }
2113
2114
0
  std::string compilerId = this->Makefile->GetSafeDefinition(
2115
0
    cmStrCat("CMAKE_", lang, "_COMPILER_ID"));
2116
2117
0
  std::string compilerSimulateId = this->Makefile->GetSafeDefinition(
2118
0
    cmStrCat("CMAKE_", lang, "_SIMULATE_ID"));
2119
2120
0
  bool const compilerTargetsMsvcABI =
2121
0
    (compilerId == "MSVC" || compilerSimulateId == "MSVC");
2122
0
  bool const compilerTargetsWatcomABI =
2123
0
    (compilerId == "OpenWatcom" || compilerSimulateId == "OpenWatcom");
2124
2125
0
  if (lang == "Swift") {
2126
0
    target->AddSwiftTargetFlags(flags);
2127
0
  } else if (lang == "CUDA") {
2128
0
    target->AddCUDAArchitectureFlags(compileOrLink, config, flags);
2129
0
    target->AddCUDAToolkitFlags(flags);
2130
0
  } else if (lang == "ISPC") {
2131
0
    target->AddISPCTargetFlags(flags);
2132
0
  } else if (lang == "RC" &&
2133
0
             this->Makefile->GetSafeDefinition("CMAKE_RC_COMPILER")
2134
0
                 .find("llvm-rc") != std::string::npos) {
2135
0
    compilerId = this->Makefile->GetSafeDefinition("CMAKE_C_COMPILER_ID");
2136
0
    if (!compilerId.empty()) {
2137
0
      compilerSimulateId =
2138
0
        this->Makefile->GetSafeDefinition("CMAKE_C_SIMULATE_ID");
2139
0
    } else {
2140
0
      compilerId = this->Makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
2141
0
      compilerSimulateId =
2142
0
        this->Makefile->GetSafeDefinition("CMAKE_CXX_SIMULATE_ID");
2143
0
    }
2144
0
  } else if (lang == "HIP") {
2145
0
    target->AddHIPArchitectureFlags(compileOrLink, config, flags);
2146
0
  } else if (lang == "Rust") {
2147
0
    target->AddRustTargetFlags(flags);
2148
0
  }
2149
2150
  // Add VFS Overlay for Clang compilers
2151
0
  if (compilerId == "Clang") {
2152
0
    if (cmValue vfsOverlay =
2153
0
          this->Makefile->GetDefinition("CMAKE_CLANG_VFS_OVERLAY")) {
2154
0
      if (compilerSimulateId == "MSVC") {
2155
0
        this->AppendCompileOptions(
2156
0
          flags,
2157
0
          std::vector<std::string>{ "-Xclang", "-ivfsoverlay", "-Xclang",
2158
0
                                    *vfsOverlay });
2159
0
      } else {
2160
0
        this->AppendCompileOptions(
2161
0
          flags, std::vector<std::string>{ "-ivfsoverlay", *vfsOverlay });
2162
0
      }
2163
0
    }
2164
0
  }
2165
  // Add MSVC runtime library flags.  This is activated by the presence
2166
  // of a default selection whether or not it is overridden by a property.
2167
0
  cmValue msvcRuntimeLibraryDefault =
2168
0
    this->Makefile->GetDefinition("CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT");
2169
0
  if (cmNonempty(msvcRuntimeLibraryDefault)) {
2170
0
    cmValue msvcRuntimeLibraryValue =
2171
0
      target->GetProperty("MSVC_RUNTIME_LIBRARY");
2172
0
    if (!msvcRuntimeLibraryValue) {
2173
0
      msvcRuntimeLibraryValue = msvcRuntimeLibraryDefault;
2174
0
    }
2175
0
    std::string const msvcRuntimeLibrary = cmGeneratorExpression::Evaluate(
2176
0
      *msvcRuntimeLibraryValue, this, config, target);
2177
0
    if (!msvcRuntimeLibrary.empty()) {
2178
0
      if (cmValue msvcRuntimeLibraryOptions = this->Makefile->GetDefinition(
2179
0
            "CMAKE_" + lang + "_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_" +
2180
0
            msvcRuntimeLibrary)) {
2181
0
        this->AppendCompileOptions(flags, *msvcRuntimeLibraryOptions);
2182
0
      } else if (compilerTargetsMsvcABI &&
2183
0
                 !cmSystemTools::GetErrorOccurredFlag()) {
2184
        // The compiler uses the MSVC ABI so it needs a known runtime library.
2185
0
        this->IssueMessage(MessageType::FATAL_ERROR,
2186
0
                           "MSVC_RUNTIME_LIBRARY value '" +
2187
0
                             msvcRuntimeLibrary + "' not known for this " +
2188
0
                             lang + " compiler.");
2189
0
      }
2190
0
    }
2191
0
  }
2192
2193
  // Add Watcom runtime library flags.  This is activated by the presence
2194
  // of a default selection whether or not it is overridden by a property.
2195
0
  cmValue watcomRuntimeLibraryDefault =
2196
0
    this->Makefile->GetDefinition("CMAKE_WATCOM_RUNTIME_LIBRARY_DEFAULT");
2197
0
  if (cmNonempty(watcomRuntimeLibraryDefault)) {
2198
0
    cmValue watcomRuntimeLibraryValue =
2199
0
      target->GetProperty("WATCOM_RUNTIME_LIBRARY");
2200
0
    if (!watcomRuntimeLibraryValue) {
2201
0
      watcomRuntimeLibraryValue = watcomRuntimeLibraryDefault;
2202
0
    }
2203
0
    std::string const watcomRuntimeLibrary = cmGeneratorExpression::Evaluate(
2204
0
      *watcomRuntimeLibraryValue, this, config, target);
2205
0
    if (!watcomRuntimeLibrary.empty()) {
2206
0
      if (cmValue watcomRuntimeLibraryOptions = this->Makefile->GetDefinition(
2207
0
            "CMAKE_" + lang + "_COMPILE_OPTIONS_WATCOM_RUNTIME_LIBRARY_" +
2208
0
            watcomRuntimeLibrary)) {
2209
0
        this->AppendCompileOptions(flags, *watcomRuntimeLibraryOptions);
2210
0
      } else if (compilerTargetsWatcomABI &&
2211
0
                 !cmSystemTools::GetErrorOccurredFlag()) {
2212
        // The compiler uses the Watcom ABI so it needs a known runtime
2213
        // library.
2214
0
        this->IssueMessage(MessageType::FATAL_ERROR,
2215
0
                           "WATCOM_RUNTIME_LIBRARY value '" +
2216
0
                             watcomRuntimeLibrary + "' not known for this " +
2217
0
                             lang + " compiler.");
2218
0
      }
2219
0
    }
2220
0
  }
2221
2222
  // Add MSVC runtime checks flags. This is activated by the presence
2223
  // of a default selection whether or not it is overridden by a property.
2224
0
  cmValue msvcRuntimeChecksDefault =
2225
0
    this->Makefile->GetDefinition("CMAKE_MSVC_RUNTIME_CHECKS_DEFAULT");
2226
0
  if (cmNonempty(msvcRuntimeChecksDefault)) {
2227
0
    cmValue msvcRuntimeChecksValue =
2228
0
      target->GetProperty("MSVC_RUNTIME_CHECKS");
2229
0
    if (!msvcRuntimeChecksValue) {
2230
0
      msvcRuntimeChecksValue = msvcRuntimeChecksDefault;
2231
0
    }
2232
0
    cmList msvcRuntimeChecksList = cmGeneratorExpression::Evaluate(
2233
0
      *msvcRuntimeChecksValue, this, config, target);
2234
0
    msvcRuntimeChecksList.remove_duplicates();
2235
2236
    // RTC1/RTCsu VS GUI workaround
2237
0
    std::string const stackFrameErrorCheck = "StackFrameErrorCheck";
2238
0
    std::string const unitinitializedVariable = "UninitializedVariable";
2239
0
    std::string const rtcSU = "RTCsu";
2240
0
    if ((cm::contains(msvcRuntimeChecksList, stackFrameErrorCheck) &&
2241
0
         cm::contains(msvcRuntimeChecksList, unitinitializedVariable)) ||
2242
0
        cm::contains(msvcRuntimeChecksList, rtcSU)) {
2243
0
      msvcRuntimeChecksList.remove_items(
2244
0
        { stackFrameErrorCheck, unitinitializedVariable, rtcSU });
2245
0
      msvcRuntimeChecksList.append(rtcSU);
2246
0
    }
2247
2248
0
    for (std::string const& msvcRuntimeChecks : msvcRuntimeChecksList) {
2249
0
      if (cmValue msvcRuntimeChecksOptions =
2250
0
            this->Makefile->GetDefinition(cmStrCat(
2251
0
              "CMAKE_", lang,
2252
0
              "_COMPILE_OPTIONS_MSVC_RUNTIME_CHECKS_" + msvcRuntimeChecks))) {
2253
0
        this->AppendCompileOptions(flags, *msvcRuntimeChecksOptions);
2254
0
      } else if (compilerTargetsMsvcABI &&
2255
0
                 !cmSystemTools::GetErrorOccurredFlag()) {
2256
        // The compiler uses the MSVC ABI so it needs a known runtime checks.
2257
0
        this->IssueMessage(MessageType::FATAL_ERROR,
2258
0
                           cmStrCat("MSVC_RUNTIME_CHECKS value '",
2259
0
                                    msvcRuntimeChecks, "' not known for this ",
2260
0
                                    lang, " compiler."));
2261
0
      }
2262
0
    }
2263
0
  }
2264
2265
  // Add MSVC debug information format flags if CMP0141 is NEW.
2266
0
  if (cm::optional<std::string> msvcDebugInformationFormat =
2267
0
        this->GetMSVCDebugFormatName(config, target)) {
2268
0
    if (!msvcDebugInformationFormat->empty()) {
2269
0
      if (cmValue msvcDebugInformationFormatOptions =
2270
0
            this->Makefile->GetDefinition(
2271
0
              cmStrCat("CMAKE_", lang,
2272
0
                       "_COMPILE_OPTIONS_MSVC_DEBUG_INFORMATION_FORMAT_",
2273
0
                       *msvcDebugInformationFormat))) {
2274
0
        this->AppendCompileOptions(flags, *msvcDebugInformationFormatOptions);
2275
0
      } else if (compilerTargetsMsvcABI &&
2276
0
                 !cmSystemTools::GetErrorOccurredFlag()) {
2277
        // The compiler uses the MSVC ABI so it needs a known runtime library.
2278
0
        this->IssueMessage(MessageType::FATAL_ERROR,
2279
0
                           cmStrCat("MSVC_DEBUG_INFORMATION_FORMAT value '",
2280
0
                                    *msvcDebugInformationFormat,
2281
0
                                    "' not known for this ", lang,
2282
0
                                    " compiler."));
2283
0
      }
2284
0
    }
2285
0
  }
2286
0
}
2287
2288
void cmLocalGenerator::AddLanguageFlagsForLinking(
2289
  std::string& flags, cmGeneratorTarget const* target, std::string const& lang,
2290
  std::string const& config)
2291
0
{
2292
0
  this->AddLanguageFlags(flags, target, cmBuildStep::Link, lang, config);
2293
2294
0
  if (target->IsIPOEnabled(lang, config)) {
2295
0
    this->AppendFeatureOptions(flags, lang, "IPO");
2296
0
  }
2297
0
}
2298
2299
cmGeneratorTarget* cmLocalGenerator::FindGeneratorTargetToUse(
2300
  std::string const& name) const
2301
0
{
2302
0
  auto imported = this->ImportedGeneratorTargets.find(name);
2303
0
  if (imported != this->ImportedGeneratorTargets.end()) {
2304
0
    return imported->second;
2305
0
  }
2306
2307
  // find local alias to imported target
2308
0
  auto aliased = this->AliasTargets.find(name);
2309
0
  if (aliased != this->AliasTargets.end()) {
2310
0
    imported = this->ImportedGeneratorTargets.find(aliased->second);
2311
0
    if (imported != this->ImportedGeneratorTargets.end()) {
2312
0
      return imported->second;
2313
0
    }
2314
0
  }
2315
2316
0
  if (cmGeneratorTarget* t = this->FindLocalNonAliasGeneratorTarget(name)) {
2317
0
    return t;
2318
0
  }
2319
2320
0
  return this->GetGlobalGenerator()->FindGeneratorTarget(name);
2321
0
}
2322
2323
bool cmLocalGenerator::GetRealDependency(std::string const& inName,
2324
                                         std::string const& config,
2325
                                         std::string& dep,
2326
                                         cmPolicies::PolicyStatus cmp0212)
2327
0
{
2328
  // Older CMake code may specify the dependency using the target
2329
  // output file rather than the target name.  Such code would have
2330
  // been written before there was support for target properties that
2331
  // modify the name so stripping down to just the file name should
2332
  // produce the target name in this case.
2333
0
  std::string name = cmSystemTools::GetFilenameName(inName);
2334
2335
  // If the input name is the empty string, there is no real
2336
  // dependency. Short-circuit the other checks:
2337
0
  if (name.empty()) {
2338
0
    return false;
2339
0
  }
2340
2341
  // Look for a CMake target with the given name.
2342
0
  cmGeneratorTarget* target = this->FindGeneratorTargetToUse(name);
2343
0
  if (!target && cmHasSuffix(name, ".exe"_s) && cmp0212 != cmPolicies::NEW) {
2344
    // If it doesn't exist, try to strip the `.exe` suffix per CMP0212.
2345
0
    std::string strippedName =
2346
0
      cmSystemTools::GetFilenameWithoutLastExtension(name);
2347
0
    if (cmGeneratorTarget* strippedTarget =
2348
0
          this->FindGeneratorTargetToUse(strippedName)) {
2349
0
      name = strippedName;
2350
0
      target = strippedTarget;
2351
0
    }
2352
0
  }
2353
2354
0
  if (target) {
2355
    // make sure it is not just a coincidence that the target name
2356
    // found is part of the inName
2357
0
    if (cmSystemTools::FileIsFullPath(inName)) {
2358
0
      std::string tLocation;
2359
0
      if (target->GetType() >= cmStateEnums::EXECUTABLE &&
2360
0
          target->GetType() <= cmStateEnums::MODULE_LIBRARY) {
2361
0
        tLocation = target->GetLocation(config);
2362
0
        tLocation = cmSystemTools::GetFilenamePath(tLocation);
2363
0
        tLocation = cmSystemTools::CollapseFullPath(tLocation);
2364
0
      }
2365
0
      std::string depLocation =
2366
0
        cmSystemTools::GetFilenamePath(std::string(inName));
2367
0
      depLocation = cmSystemTools::CollapseFullPath(depLocation);
2368
0
      if (depLocation != tLocation) {
2369
        // it is a full path to a depend that has the same name
2370
        // as a target but is in a different location so do not use
2371
        // the target as the depend
2372
0
        dep = inName;
2373
0
        return true;
2374
0
      }
2375
0
    }
2376
0
    switch (target->GetType()) {
2377
0
      case cmStateEnums::EXECUTABLE:
2378
0
      case cmStateEnums::STATIC_LIBRARY:
2379
0
      case cmStateEnums::SHARED_LIBRARY:
2380
0
      case cmStateEnums::MODULE_LIBRARY:
2381
0
      case cmStateEnums::UNKNOWN_LIBRARY:
2382
0
        dep = target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact,
2383
0
                                  /*realname=*/true);
2384
0
        return true;
2385
0
      case cmStateEnums::OBJECT_LIBRARY:
2386
        // An object library has no single file on which to depend.
2387
        // This was listed to get the target-level dependency.
2388
0
      case cmStateEnums::INTERFACE_LIBRARY:
2389
        // An interface library has no file on which to depend.
2390
        // This was listed to get the target-level dependency.
2391
0
      case cmStateEnums::UTILITY:
2392
0
      case cmStateEnums::GLOBAL_TARGET:
2393
        // A utility target has no file on which to depend.  This was listed
2394
        // only to get the target-level dependency.
2395
0
        return false;
2396
0
    }
2397
0
  }
2398
2399
  // The name was not that of a CMake target.  It must name a file.
2400
0
  if (cmSystemTools::FileIsFullPath(inName)) {
2401
    // This is a full path.  Return it as given.
2402
0
    dep = inName;
2403
0
    return true;
2404
0
  }
2405
2406
  // Check for a source file in this directory that matches the
2407
  // dependency.
2408
0
  if (cmSourceFile* sf = this->Makefile->GetSource(inName)) {
2409
0
    dep = sf->ResolveFullPath();
2410
0
    return true;
2411
0
  }
2412
2413
  // Treat the name as relative to the source directory in which it
2414
  // was given.
2415
0
  dep = cmStrCat(this->GetCurrentSourceDirectory(), '/', inName);
2416
2417
  // If the in-source path does not exist, assume it instead lives in the
2418
  // binary directory.
2419
0
  if (!cmSystemTools::FileExists(dep)) {
2420
0
    dep = cmStrCat(this->GetCurrentBinaryDirectory(), '/', inName);
2421
0
  }
2422
2423
0
  dep = cmSystemTools::CollapseFullPath(dep);
2424
2425
0
  return true;
2426
0
}
2427
2428
static void AddVisibilityCompileOption(std::string& flags,
2429
                                       cmGeneratorTarget const* target,
2430
                                       cmLocalGenerator* lg,
2431
                                       std::string const& lang)
2432
0
{
2433
0
  std::string compileOption = "CMAKE_" + lang + "_COMPILE_OPTIONS_VISIBILITY";
2434
0
  cmValue opt = lg->GetMakefile()->GetDefinition(compileOption);
2435
0
  if (!opt) {
2436
0
    return;
2437
0
  }
2438
0
  std::string flagDefine = lang + "_VISIBILITY_PRESET";
2439
2440
0
  cmValue prop = target->GetProperty(flagDefine);
2441
0
  if (!prop) {
2442
0
    return;
2443
0
  }
2444
0
  if ((*prop != "hidden") && (*prop != "default") && (*prop != "protected") &&
2445
0
      (*prop != "internal")) {
2446
0
    std::ostringstream e;
2447
0
    e << "Target " << target->GetName() << " uses unsupported value \""
2448
0
      << *prop << "\" for " << flagDefine << "."
2449
0
      << " The supported values are: default, hidden, protected, and "
2450
0
         "internal.";
2451
0
    cmSystemTools::Error(e.str());
2452
0
    return;
2453
0
  }
2454
0
  std::string option = *opt + *prop;
2455
0
  lg->AppendFlags(flags, option);
2456
0
}
2457
2458
static void AddInlineVisibilityCompileOption(std::string& flags,
2459
                                             cmGeneratorTarget const* target,
2460
                                             cmLocalGenerator* lg,
2461
                                             std::string const& lang)
2462
0
{
2463
0
  std::string compileOption =
2464
0
    cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN");
2465
0
  cmValue opt = lg->GetMakefile()->GetDefinition(compileOption);
2466
0
  if (!opt) {
2467
0
    return;
2468
0
  }
2469
2470
0
  bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
2471
0
  if (!prop) {
2472
0
    return;
2473
0
  }
2474
0
  lg->AppendFlags(flags, *opt);
2475
0
}
2476
2477
void cmLocalGenerator::AddVisibilityPresetFlags(
2478
  std::string& flags, cmGeneratorTarget const* target, std::string const& lang)
2479
0
{
2480
0
  if (lang.empty()) {
2481
0
    return;
2482
0
  }
2483
2484
0
  AddVisibilityCompileOption(flags, target, this, lang);
2485
2486
0
  if (lang == "CXX" || lang == "OBJCXX") {
2487
0
    AddInlineVisibilityCompileOption(flags, target, this, lang);
2488
0
  }
2489
0
}
2490
2491
void cmLocalGenerator::AddFeatureFlags(std::string& flags,
2492
                                       cmGeneratorTarget const* target,
2493
                                       std::string const& lang,
2494
                                       std::string const& config)
2495
0
{
2496
0
  int targetType = target->GetType();
2497
2498
0
  bool shared = ((targetType == cmStateEnums::SHARED_LIBRARY) ||
2499
0
                 (targetType == cmStateEnums::MODULE_LIBRARY));
2500
2501
0
  if (target->GetLinkInterfaceDependentBoolProperty(
2502
0
        "POSITION_INDEPENDENT_CODE", config)) {
2503
0
    this->AddPositionIndependentFlags(flags, lang, targetType);
2504
0
  }
2505
0
  if (shared) {
2506
0
    this->AppendFeatureOptions(flags, lang, "DLL");
2507
0
  }
2508
0
}
2509
2510
void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
2511
                                                   std::string const& lang,
2512
                                                   int targetType)
2513
0
{
2514
0
  std::string picFlags;
2515
2516
0
  if (targetType == cmStateEnums::EXECUTABLE) {
2517
0
    picFlags = this->Makefile->GetSafeDefinition(
2518
0
      cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_PIE"));
2519
0
  }
2520
0
  if (picFlags.empty()) {
2521
0
    picFlags = this->Makefile->GetSafeDefinition(
2522
0
      cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_PIC"));
2523
0
  }
2524
0
  if (!picFlags.empty()) {
2525
0
    cmList options{ picFlags };
2526
0
    for (std::string const& o : options) {
2527
0
      this->AppendFlagEscape(flags, o);
2528
0
    }
2529
0
  }
2530
0
}
2531
2532
void cmLocalGenerator::AddColorDiagnosticsFlags(std::string& flags,
2533
                                                std::string const& lang)
2534
0
{
2535
0
  cmValue diag = this->Makefile->GetDefinition("CMAKE_COLOR_DIAGNOSTICS");
2536
0
  if (diag.IsSet()) {
2537
0
    std::string colorFlagName;
2538
0
    if (diag.IsOn()) {
2539
0
      colorFlagName =
2540
0
        cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_COLOR_DIAGNOSTICS");
2541
0
    } else {
2542
0
      colorFlagName =
2543
0
        cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_COLOR_DIAGNOSTICS_OFF");
2544
0
    }
2545
2546
0
    cmList options{ this->Makefile->GetDefinition(colorFlagName) };
2547
2548
0
    for (auto const& option : options) {
2549
0
      this->AppendFlagEscape(flags, option);
2550
0
    }
2551
0
  }
2552
0
}
2553
2554
void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
2555
                                              std::string const& var,
2556
                                              std::string const& config)
2557
0
{
2558
  // Add the flags from the variable itself.
2559
0
  this->AppendFlags(flags, this->Makefile->GetSafeDefinition(var));
2560
  // Add the flags from the build-type specific variable.
2561
0
  if (!config.empty()) {
2562
0
    std::string const flagsVar =
2563
0
      cmStrCat(var, '_', cmSystemTools::UpperCase(config));
2564
0
    this->AppendFlags(flags, this->Makefile->GetSafeDefinition(flagsVar));
2565
0
  }
2566
0
}
2567
void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
2568
                                              std::string const& var,
2569
                                              cmGeneratorTarget const* target,
2570
                                              cmBuildStep compileOrLink,
2571
                                              std::string const& lang,
2572
                                              std::string const& config)
2573
0
{
2574
0
  std::string newFlags;
2575
0
  this->AddConfigVariableFlags(newFlags, var, config);
2576
0
  if (!newFlags.empty()) {
2577
0
    this->AppendFlags(flags, newFlags, var, target, compileOrLink, lang);
2578
0
  }
2579
0
}
2580
2581
void cmLocalGenerator::AppendFlags(std::string& flags,
2582
                                   std::string const& newFlags) const
2583
0
{
2584
0
  bool allSpaces =
2585
0
    std::all_of(newFlags.begin(), newFlags.end(), cmsysString_isspace);
2586
2587
0
  if (!newFlags.empty() && !allSpaces) {
2588
0
    if (!flags.empty()) {
2589
0
      flags += " ";
2590
0
    }
2591
0
    flags += newFlags;
2592
0
  }
2593
0
}
2594
2595
void cmLocalGenerator::AppendFlags(
2596
  std::string& flags, std::vector<BT<std::string>> const& newFlags) const
2597
0
{
2598
0
  for (BT<std::string> const& flag : newFlags) {
2599
0
    this->AppendFlags(flags, flag.Value);
2600
0
  }
2601
0
}
2602
2603
void cmLocalGenerator::AppendFlagEscape(std::string& flags,
2604
                                        std::string const& rawFlag) const
2605
0
{
2606
0
  this->AppendFlags(
2607
0
    flags,
2608
0
    this->EscapeForShell(rawFlag, false, false, false, this->IsNinjaMulti()));
2609
0
}
2610
2611
void cmLocalGenerator::AppendLinkFlagsWithParsing(
2612
  std::string& flags, std::string const& newFlags,
2613
  cmGeneratorTarget const* target, std::string const& language)
2614
0
{
2615
0
  std::vector<std::string> options;
2616
0
  cmSystemTools::ParseUnixCommandLine(newFlags.c_str(), options);
2617
0
  this->SetLinkScriptShell(this->GlobalGenerator->GetUseLinkScript());
2618
0
  std::vector<BT<std::string>> optionsWithBT{ options.size() };
2619
0
  std::transform(options.cbegin(), options.cend(), optionsWithBT.begin(),
2620
0
                 [](std::string const& item) -> BT<std::string> {
2621
0
                   return BT<std::string>{ item };
2622
0
                 });
2623
0
  target->ResolveLinkerWrapper(optionsWithBT, language);
2624
0
  for (auto const& item : optionsWithBT) {
2625
0
    this->AppendFlagEscape(flags, item.Value);
2626
0
  }
2627
0
  this->SetLinkScriptShell(false);
2628
0
}
2629
2630
void cmLocalGenerator::AppendFlags(std::string& flags,
2631
                                   std::string const& newFlags,
2632
                                   std::string const& name,
2633
                                   cmGeneratorTarget const* target,
2634
                                   cmBuildStep compileOrLink,
2635
                                   std::string const& language)
2636
0
{
2637
0
  switch (target->GetPolicyStatusCMP0181()) {
2638
0
    case cmPolicies::WARN:
2639
0
      if (!this->Makefile->GetCMakeInstance()->GetIsInTryCompile() &&
2640
0
          this->Makefile->PolicyOptionalWarningEnabled(
2641
0
            "CMAKE_POLICY_WARNING_CMP0181")) {
2642
0
        this->Makefile->IssuePolicyWarning(
2643
0
          cmPolicies::CMP0181, {},
2644
0
          cmStrCat("Since the policy is not set, the contents of variable '"_s,
2645
0
                   name, "' will be used as is."_s),
2646
0
          target->GetBacktrace());
2647
0
      }
2648
0
      CM_FALLTHROUGH;
2649
0
    case cmPolicies::OLD:
2650
0
      this->AppendFlags(
2651
0
        flags, this->GetGlobalGenerator()->GetEncodedLiteral(newFlags));
2652
0
      break;
2653
0
    case cmPolicies::NEW:
2654
0
      if (compileOrLink == cmBuildStep::Link) {
2655
0
        this->AppendLinkFlagsWithParsing(flags, newFlags, target, language);
2656
0
      } else {
2657
0
        this->AppendFlags(flags, newFlags);
2658
0
      }
2659
0
  }
2660
0
}
2661
2662
void cmLocalGenerator::AddISPCDependencies(cmGeneratorTarget* target)
2663
0
{
2664
0
  std::vector<std::string> enabledLanguages =
2665
0
    this->GetState()->GetEnabledLanguages();
2666
0
  if (std::find(enabledLanguages.begin(), enabledLanguages.end(), "ISPC") ==
2667
0
      enabledLanguages.end()) {
2668
0
    return;
2669
0
  }
2670
2671
0
  cmValue ispcHeaderSuffixProp = target->GetProperty("ISPC_HEADER_SUFFIX");
2672
0
  assert(ispcHeaderSuffixProp);
2673
2674
0
  std::vector<std::string> ispcArchSuffixes =
2675
0
    detail::ComputeISPCObjectSuffixes(target);
2676
0
  bool const extra_objects = (ispcArchSuffixes.size() > 1);
2677
2678
0
  std::vector<std::string> configsList =
2679
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
2680
0
  for (std::string const& config : configsList) {
2681
2682
0
    std::string rootObjectDir = target->GetObjectDirectory(config);
2683
0
    std::string headerDir = rootObjectDir;
2684
0
    if (cmValue prop = target->GetProperty("ISPC_HEADER_DIRECTORY")) {
2685
0
      headerDir = cmSystemTools::CollapseFullPath(
2686
0
        cmStrCat(this->GetBinaryDirectory(), '/', *prop));
2687
0
    }
2688
2689
0
    std::vector<cmSourceFile*> sources;
2690
0
    target->GetSourceFiles(sources, config);
2691
2692
    // build up the list of ispc headers and extra objects that this target is
2693
    // generating
2694
0
    for (cmSourceFile const* sf : sources) {
2695
      // Generate this object file's rule file.
2696
0
      std::string const& lang = sf->GetLanguage();
2697
0
      if (lang == "ISPC") {
2698
0
        std::string const& objectName = target->GetObjectName(sf);
2699
2700
        // Drop both ".obj" and the source file extension
2701
0
        std::string ispcSource =
2702
0
          cmSystemTools::GetFilenameWithoutLastExtension(objectName);
2703
0
        ispcSource =
2704
0
          cmSystemTools::GetFilenameWithoutLastExtension(ispcSource);
2705
2706
0
        auto headerPath =
2707
0
          cmStrCat(headerDir, '/', ispcSource, *ispcHeaderSuffixProp);
2708
0
        target->AddISPCGeneratedHeader(headerPath, config);
2709
0
        if (extra_objects) {
2710
0
          std::vector<std::pair<cmSourceFile const*, std::string>> objs;
2711
0
          for (auto& obj : detail::ComputeISPCExtraObjects(
2712
0
                 objectName, rootObjectDir, ispcArchSuffixes)) {
2713
0
            objs.push_back({ sf, std::move(obj) });
2714
0
          }
2715
0
          target->AddISPCGeneratedObject(std::move(objs), config);
2716
0
        }
2717
0
      }
2718
0
    }
2719
0
  }
2720
0
}
2721
2722
void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target)
2723
0
{
2724
0
  std::vector<std::string> configsList =
2725
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
2726
2727
0
  for (std::string const& config : configsList) {
2728
    // FIXME: Refactor collection of sources to not evaluate object
2729
    // libraries.
2730
0
    std::vector<cmSourceFile*> sources;
2731
0
    target->GetSourceFiles(sources, config);
2732
2733
0
    std::string const configUpper = cmSystemTools::UpperCase(config);
2734
0
    static std::array<std::string, 4> const langs = { { "C", "CXX", "OBJC",
2735
0
                                                        "OBJCXX" } };
2736
2737
0
    std::set<std::string> pchLangSet;
2738
0
    if (this->GetGlobalGenerator()->IsXcode()) {
2739
0
      for (std::string const& lang : langs) {
2740
0
        std::string const pchHeader = target->GetPchHeader(config, lang, "");
2741
0
        if (!pchHeader.empty()) {
2742
0
          pchLangSet.emplace(lang);
2743
0
        }
2744
0
      }
2745
0
    }
2746
2747
0
    for (std::string const& lang : langs) {
2748
0
      auto langSources = std::count_if(
2749
0
        sources.begin(), sources.end(),
2750
0
        [&target, &config, &lang](cmSourceFile* sf) {
2751
0
          auto const* const fileSet = target->GetFileSetForSource(config, sf);
2752
0
          return lang == sf->GetLanguage() &&
2753
0
            !((fileSet && fileSet->GetProperty("SKIP_PRECOMPILE_HEADERS")) ||
2754
0
              sf->GetProperty("SKIP_PRECOMPILE_HEADERS"));
2755
0
        });
2756
0
      if (langSources == 0) {
2757
0
        continue;
2758
0
      }
2759
2760
0
      std::vector<std::string> pchArchs = target->GetPchArchs(config, lang);
2761
0
      if (pchArchs.size() > 1) {
2762
0
        std::string useMultiArchPch;
2763
0
        for (std::string const& arch : pchArchs) {
2764
0
          std::string const pchHeader =
2765
0
            target->GetPchHeader(config, lang, arch);
2766
0
          if (!pchHeader.empty()) {
2767
0
            useMultiArchPch = cmStrCat(useMultiArchPch, ";-Xarch_", arch,
2768
0
                                       ";-include", pchHeader);
2769
0
          }
2770
0
        }
2771
2772
0
        if (!useMultiArchPch.empty()) {
2773
2774
0
          target->Target->AppendProperty(
2775
0
            cmStrCat(lang, "_COMPILE_OPTIONS_USE_PCH"),
2776
0
            cmStrCat("$<$<CONFIG:", config, ">:", useMultiArchPch, '>'));
2777
0
        }
2778
0
      }
2779
2780
0
      for (std::string const& arch : pchArchs) {
2781
0
        std::string const pchSource = target->GetPchSource(config, lang, arch);
2782
0
        std::string const pchHeader = target->GetPchHeader(config, lang, arch);
2783
2784
0
        if (pchSource.empty() || pchHeader.empty()) {
2785
0
          if (this->GetGlobalGenerator()->IsXcode() && !pchLangSet.empty()) {
2786
0
            for (auto* sf : sources) {
2787
0
              auto const sourceLanguage = sf->GetLanguage();
2788
0
              if (!sourceLanguage.empty() &&
2789
0
                  pchLangSet.find(sourceLanguage) == pchLangSet.end()) {
2790
0
                sf->SetProperty("SKIP_PRECOMPILE_HEADERS", "ON");
2791
0
              }
2792
0
            }
2793
0
          }
2794
0
          continue;
2795
0
        }
2796
2797
0
        cmValue pchExtension =
2798
0
          this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
2799
2800
0
        if (pchExtension.IsEmpty()) {
2801
0
          continue;
2802
0
        }
2803
2804
0
        auto* reuseTarget = target->GetPchReuseTarget();
2805
0
        bool const haveReuseTarget = reuseTarget && reuseTarget != target;
2806
2807
0
        auto* pch_sf = this->Makefile->GetOrCreateSource(
2808
0
          pchSource, false, cmSourceFileLocationKind::Known);
2809
0
        pch_sf->SetSpecialSourceType(
2810
0
          cmSourceFile::SpecialSourceType::PchSource);
2811
        // PCH sources should never be scanned as they cannot contain C++
2812
        // module references.
2813
0
        pch_sf->SetProperty("CXX_SCAN_FOR_MODULES", "0");
2814
2815
0
        if (!this->GetGlobalGenerator()->IsXcode()) {
2816
0
          if (!haveReuseTarget) {
2817
0
            target->AddSource(pchSource, true);
2818
0
          }
2819
2820
0
          std::string const pchFile = target->GetPchFile(config, lang, arch);
2821
2822
          // Exclude the pch files from linking
2823
0
          if (this->Makefile->IsOn("CMAKE_LINK_PCH")) {
2824
0
            if (!haveReuseTarget) {
2825
0
              pch_sf->AppendProperty(
2826
0
                "OBJECT_OUTPUTS",
2827
0
                cmStrCat("$<$<CONFIG:", config, ">:", pchFile, '>'));
2828
0
            } else {
2829
0
              if (this->Makefile->IsOn("CMAKE_PCH_COPY_COMPILE_PDB")) {
2830
2831
0
                std::string const compilerId =
2832
0
                  this->Makefile->GetSafeDefinition(
2833
0
                    cmStrCat("CMAKE_", lang, "_COMPILER_ID"));
2834
2835
0
                std::string const compilerVersion =
2836
0
                  this->Makefile->GetSafeDefinition(
2837
0
                    cmStrCat("CMAKE_", lang, "_COMPILER_VERSION"));
2838
2839
0
                std::string const langFlags =
2840
0
                  this->Makefile->GetSafeDefinition(
2841
0
                    cmStrCat("CMAKE_", lang, "_FLAGS_", configUpper));
2842
2843
0
                bool editAndContinueDebugInfo = false;
2844
0
                bool programDatabaseDebugInfo = false;
2845
0
                cm::optional<std::string> msvcDebugInformationFormat =
2846
0
                  this->GetMSVCDebugFormatName(config, target);
2847
0
                if (msvcDebugInformationFormat &&
2848
0
                    !msvcDebugInformationFormat->empty()) {
2849
0
                  editAndContinueDebugInfo =
2850
0
                    *msvcDebugInformationFormat == "EditAndContinue";
2851
0
                  programDatabaseDebugInfo =
2852
0
                    *msvcDebugInformationFormat == "ProgramDatabase";
2853
0
                } else {
2854
0
                  editAndContinueDebugInfo =
2855
0
                    langFlags.find("/ZI") != std::string::npos ||
2856
0
                    langFlags.find("-ZI") != std::string::npos;
2857
0
                  programDatabaseDebugInfo =
2858
0
                    langFlags.find("/Zi") != std::string::npos ||
2859
0
                    langFlags.find("-Zi") != std::string::npos;
2860
0
                }
2861
2862
0
                if (editAndContinueDebugInfo) {
2863
0
                  this->CopyPchCompilePdb(config, lang, target, reuseTarget,
2864
0
                                          { ".pdb", ".idb" });
2865
0
                } else if (programDatabaseDebugInfo) {
2866
0
                  this->CopyPchCompilePdb(config, lang, target, reuseTarget,
2867
0
                                          { ".pdb" });
2868
0
                }
2869
0
              }
2870
2871
              // Link to the pch object file
2872
0
              std::string pchSourceObj;
2873
              // Fastbuild will propagate pch.obj for us, no need to link to it
2874
              // explicitly.
2875
0
              if (!this->GetGlobalGenerator()->IsFastbuild()) {
2876
0
                pchSourceObj =
2877
0
                  reuseTarget->GetPchFileObject(config, lang, arch);
2878
0
              }
2879
2880
0
              if (target->GetType() != cmStateEnums::OBJECT_LIBRARY) {
2881
0
                std::string linkerProperty = "LINK_FLAGS_";
2882
0
                if (target->GetType() == cmStateEnums::STATIC_LIBRARY) {
2883
0
                  linkerProperty = "STATIC_LIBRARY_FLAGS_";
2884
0
                }
2885
0
                target->Target->AppendProperty(
2886
0
                  cmStrCat(linkerProperty, configUpper),
2887
0
                  cmStrCat(' ',
2888
0
                           this->ConvertToOutputFormat(pchSourceObj, SHELL)),
2889
0
                  cm::nullopt, true);
2890
0
              } else if (reuseTarget->GetType() ==
2891
0
                         cmStateEnums::OBJECT_LIBRARY) {
2892
0
                target->Target->AppendProperty(
2893
0
                  "INTERFACE_LINK_LIBRARIES",
2894
0
                  cmStrCat("$<$<CONFIG:", config,
2895
0
                           ">:$<LINK_ONLY:", pchSourceObj, ">>"));
2896
0
              }
2897
0
            }
2898
0
          } else {
2899
0
            pch_sf->SetProperty("PCH_EXTENSION", pchExtension);
2900
0
          }
2901
2902
          // Add pchHeader to source files, which will
2903
          // be grouped as "Precompile Header File"
2904
0
          auto* pchHeader_sf = this->Makefile->GetOrCreateSource(
2905
0
            pchHeader, false, cmSourceFileLocationKind::Known);
2906
0
          pchHeader_sf->SetSpecialSourceType(
2907
0
            cmSourceFile::SpecialSourceType::PchHeader);
2908
0
          std::string err;
2909
0
          pchHeader_sf->ResolveFullPath(&err);
2910
0
          if (!err.empty()) {
2911
0
            std::ostringstream msg;
2912
0
            msg << "Unable to resolve full path of PCH-header '" << pchHeader
2913
0
                << "' assigned to target " << target->GetName()
2914
0
                << ", although its path is supposed to be known!";
2915
0
            this->IssueMessage(MessageType::FATAL_ERROR, msg.str());
2916
0
          }
2917
0
          target->AddSource(pchHeader);
2918
0
        }
2919
0
      }
2920
0
    }
2921
0
  }
2922
0
}
2923
2924
void cmLocalGenerator::CopyPchCompilePdb(
2925
  std::string const& config, std::string const& language,
2926
  cmGeneratorTarget* target, cmGeneratorTarget* reuseTarget,
2927
  std::vector<std::string> const& extensions)
2928
0
{
2929
0
  std::string const copy_script = cmStrCat(target->GetSupportDirectory(),
2930
0
                                           "/copy_idb_pdb_", config, ".cmake");
2931
0
  cmGeneratedFileStream file(copy_script);
2932
0
  file.SetCopyIfDifferent(true);
2933
2934
0
  file << "# CMake generated file\n";
2935
2936
0
  file << "# The compiler generated pdb file needs to be written to disk\n"
2937
0
       << "# by mspdbsrv. The foreach retry loop is needed to make sure\n"
2938
0
       << "# the pdb file is ready to be copied.\n\n";
2939
2940
0
  auto configGenex = [&](cm::string_view expr) -> std::string {
2941
0
    if (this->GetGlobalGenerator()->IsMultiConfig()) {
2942
0
      return cmStrCat("$<$<CONFIG:", config, ">:", expr, '>');
2943
0
    }
2944
0
    return std::string(expr);
2945
0
  };
2946
2947
0
  std::vector<std::string> outputs;
2948
0
  auto replaceExtension = [](std::string const& path,
2949
0
                             std::string const& ext) -> std::string {
2950
0
    auto const dir = cmSystemTools::GetFilenamePath(path);
2951
0
    auto const base = cmSystemTools::GetFilenameWithoutLastExtension(path);
2952
0
    if (dir.empty()) {
2953
0
      return cmStrCat(base, ext);
2954
0
    }
2955
0
    return cmStrCat(dir, '/', base, ext);
2956
0
  };
2957
0
  for (auto const& extension : extensions) {
2958
0
    std::string const from_file =
2959
0
      replaceExtension(reuseTarget->GetCompilePDBPath(config), extension);
2960
0
    std::string const to_dir = target->GetCompilePDBDirectory(config);
2961
0
    std::string const to_file =
2962
0
      replaceExtension(reuseTarget->GetCompilePDBName(config), extension);
2963
0
    std::string const dest_file = cmStrCat(to_dir, '/', to_file);
2964
2965
0
    file << "foreach(retry RANGE 1 30)\n";
2966
0
    file << "  if (EXISTS \"" << from_file << "\" AND (NOT EXISTS \""
2967
0
         << dest_file << "\" OR NOT \"" << dest_file << "\" IS_NEWER_THAN \""
2968
0
         << from_file << "\"))\n";
2969
0
    file << "    file(MAKE_DIRECTORY \"" << to_dir << "\")\n";
2970
0
    file << "    execute_process(COMMAND ${CMAKE_COMMAND} -E copy";
2971
0
    file << " \"" << from_file << "\"" << " \"" << to_dir
2972
0
         << "\" RESULT_VARIABLE result " << " ERROR_QUIET)\n";
2973
0
    file << "    if (NOT result EQUAL 0)\n"
2974
0
         << "      execute_process(COMMAND ${CMAKE_COMMAND}"
2975
0
         << " -E sleep 1)\n"
2976
0
         << "    else()\n";
2977
0
    file << "      break()\n"
2978
0
         << "    endif()\n";
2979
0
    file << "  elseif(NOT EXISTS \"" << from_file << "\")\n"
2980
0
         << "    execute_process(COMMAND ${CMAKE_COMMAND}" << " -E sleep 1)\n"
2981
0
         << "  endif()\n";
2982
0
    file << "endforeach()\n";
2983
0
    outputs.push_back(configGenex(dest_file));
2984
0
  }
2985
2986
0
  cmCustomCommandLines commandLines =
2987
0
    cmMakeSingleCommandLine({ configGenex(cmSystemTools::GetCMakeCommand()),
2988
0
                              configGenex("-P"), configGenex(copy_script) });
2989
2990
0
  auto const comment =
2991
0
    cmStrCat("Copying PDB for PCH reuse from ", reuseTarget->GetName(),
2992
0
             " for ", target->GetName());
2993
0
  ;
2994
2995
0
  auto cc = cm::make_unique<cmCustomCommand>();
2996
0
  cc->SetCommandLines(commandLines);
2997
0
  cc->SetComment(comment.c_str());
2998
0
  cc->SetStdPipesUTF8(true);
2999
0
  cc->AppendDepends(
3000
0
    { reuseTarget->GetPchFile(config, language), copy_script });
3001
  // Fastbuild needs to know that this custom command actually depends on a
3002
  // target that produces PCH, so it can sort by dependencies correctly.
3003
0
  if (this->GetGlobalGenerator()->IsFastbuild()) {
3004
0
    cc->AppendDepends({ reuseTarget->GetName() });
3005
0
  }
3006
3007
0
  if (this->GetGlobalGenerator()->IsVisualStudio()) {
3008
0
    cc->SetByproducts(outputs);
3009
0
    this->AddCustomCommandToTarget(
3010
0
      target->GetName(), cmCustomCommandType::PRE_BUILD, std::move(cc),
3011
0
      cmObjectLibraryCommands::Accept);
3012
0
  } else {
3013
0
    cc->SetOutputs(outputs);
3014
0
    cmSourceFile* copy_rule = this->AddCustomCommandToOutput(std::move(cc));
3015
0
    if (copy_rule) {
3016
0
      copy_rule->SetProperty("CXX_SCAN_FOR_MODULES", "0");
3017
0
      auto* pch_pdb_sf = target->AddSource(copy_rule->ResolveFullPath());
3018
0
      pch_pdb_sf->SetSpecialSourceType(
3019
0
        cmSourceFile::SpecialSourceType::PchPdbReuseSource);
3020
0
    }
3021
0
  }
3022
0
}
3023
3024
cm::optional<std::string> cmLocalGenerator::GetMSVCDebugFormatName(
3025
  std::string const& config, cmGeneratorTarget const* target)
3026
0
{
3027
  // MSVC debug information format selection is activated by the presence
3028
  // of a default whether or not it is overridden by a property.
3029
0
  cm::optional<std::string> msvcDebugInformationFormat;
3030
0
  cmValue msvcDebugInformationFormatDefault = this->Makefile->GetDefinition(
3031
0
    "CMAKE_MSVC_DEBUG_INFORMATION_FORMAT_DEFAULT");
3032
0
  if (cmNonempty(msvcDebugInformationFormatDefault)) {
3033
0
    cmValue msvcDebugInformationFormatValue =
3034
0
      target->GetProperty("MSVC_DEBUG_INFORMATION_FORMAT");
3035
0
    if (!msvcDebugInformationFormatValue) {
3036
0
      msvcDebugInformationFormatValue = msvcDebugInformationFormatDefault;
3037
0
    }
3038
0
    msvcDebugInformationFormat = cmGeneratorExpression::Evaluate(
3039
0
      *msvcDebugInformationFormatValue, this, config, target);
3040
0
  }
3041
0
  return msvcDebugInformationFormat;
3042
0
}
3043
3044
cm::optional<cmSwiftCompileMode> cmLocalGenerator::GetSwiftCompileMode(
3045
  cmGeneratorTarget const* target, std::string const& config)
3046
0
{
3047
0
  cmMakefile const* mf = this->GetMakefile();
3048
0
  cmValue const swiftCompileModeDefault =
3049
0
    mf->GetDefinition("CMAKE_Swift_COMPILATION_MODE_DEFAULT");
3050
0
  if (!cmNonempty(swiftCompileModeDefault)) {
3051
0
    return {};
3052
0
  }
3053
0
  cmValue swiftCompileMode = target->GetProperty("Swift_COMPILATION_MODE");
3054
0
  if (!swiftCompileMode) {
3055
0
    swiftCompileMode = swiftCompileModeDefault;
3056
0
  }
3057
3058
0
  std::string const expandedCompileMode =
3059
0
    cmGeneratorExpression::Evaluate(*swiftCompileMode, this, config, target);
3060
0
  if (expandedCompileMode == "wholemodule") {
3061
0
    return cmSwiftCompileMode::Wholemodule;
3062
0
  }
3063
0
  if (expandedCompileMode == "singlefile") {
3064
0
    return cmSwiftCompileMode::Singlefile;
3065
0
  }
3066
0
  if (expandedCompileMode == "incremental") {
3067
0
    return cmSwiftCompileMode::Incremental;
3068
0
  }
3069
0
  return cmSwiftCompileMode::Unknown;
3070
0
}
3071
3072
bool cmLocalGenerator::IsSplitSwiftBuild() const
3073
0
{
3074
0
  return cmNonempty(this->GetMakefile()->GetDefinition(
3075
0
    "CMAKE_Swift_COMPILATION_MODE_DEFAULT"));
3076
0
}
3077
3078
namespace {
3079
3080
inline void RegisterUnitySources(cmGeneratorTarget* target, cmSourceFile* sf,
3081
                                 std::string const& filename)
3082
0
{
3083
0
  target->AddSourceFileToUnityBatch(sf->ResolveFullPath());
3084
0
  sf->SetProperty("UNITY_SOURCE_FILE", filename);
3085
0
}
3086
}
3087
3088
cmLocalGenerator::UnitySource cmLocalGenerator::WriteUnitySource(
3089
  cmGeneratorTarget* target, std::vector<std::string> const& configs,
3090
  cmRange<std::vector<UnityBatchedSource>::const_iterator> sources,
3091
  cmValue beforeInclude, cmValue afterInclude, std::string filename,
3092
  std::string const& unityFileDirectory, UnityPathMode pathMode) const
3093
0
{
3094
0
  cmValue uniqueIdName = target->GetProperty("UNITY_BUILD_UNIQUE_ID");
3095
0
  cmGeneratedFileStream file(
3096
0
    filename, false, target->GetGlobalGenerator()->GetMakefileEncoding());
3097
0
  file.SetCopyIfDifferent(true);
3098
0
  file << "/* generated by CMake */\n\n";
3099
3100
0
  bool perConfig = false;
3101
0
  for (UnityBatchedSource const& ubs : sources) {
3102
0
    cm::optional<std::string> cond;
3103
0
    if (ubs.Configs.size() != configs.size()) {
3104
0
      perConfig = true;
3105
0
      cond = std::string();
3106
0
      cm::string_view sep;
3107
0
      for (size_t ci : ubs.Configs) {
3108
0
        cond = cmStrCat(*cond, sep, "defined(CMAKE_UNITY_CONFIG_",
3109
0
                        cmSystemTools::UpperCase(configs[ci]), ')');
3110
0
        sep = " || "_s;
3111
0
      }
3112
0
    }
3113
0
    RegisterUnitySources(target, ubs.Source, filename);
3114
0
    WriteUnitySourceInclude(file, cond, ubs.Source->ResolveFullPath(),
3115
0
                            beforeInclude, afterInclude, uniqueIdName,
3116
0
                            pathMode, unityFileDirectory);
3117
0
  }
3118
3119
0
  return UnitySource(std::move(filename), perConfig);
3120
0
}
3121
3122
void cmLocalGenerator::WriteUnitySourceInclude(
3123
  std::ostream& unity_file, cm::optional<std::string> const& cond,
3124
  std::string const& sf_full_path, cmValue beforeInclude, cmValue afterInclude,
3125
  cmValue uniqueIdName, UnityPathMode pathMode,
3126
  std::string const& unityFileDirectory) const
3127
0
{
3128
0
  if (cond) {
3129
0
    unity_file << "#if " << *cond << "\n";
3130
0
  }
3131
3132
0
  std::string pathToHash;
3133
0
  std::string relocatableIncludePath;
3134
0
  auto PathEqOrSubDir = [](std::string const& a, std::string const& b) {
3135
0
    return (cmSystemTools::ComparePath(a, b) ||
3136
0
            cmSystemTools::IsSubDirectory(a, b));
3137
0
  };
3138
0
  auto const path = cmSystemTools::GetFilenamePath(sf_full_path);
3139
0
  if (PathEqOrSubDir(path, this->GetBinaryDirectory())) {
3140
0
    relocatableIncludePath =
3141
0
      cmSystemTools::RelativePath(unityFileDirectory, sf_full_path);
3142
0
    pathToHash = "BLD_" +
3143
0
      cmSystemTools::RelativePath(this->GetBinaryDirectory(), sf_full_path);
3144
0
  } else if (PathEqOrSubDir(path, this->GetSourceDirectory())) {
3145
0
    relocatableIncludePath =
3146
0
      cmSystemTools::RelativePath(this->GetSourceDirectory(), sf_full_path);
3147
0
    pathToHash = "SRC_" + relocatableIncludePath;
3148
0
  } else {
3149
0
    relocatableIncludePath = sf_full_path;
3150
0
    pathToHash = "ABS_" + sf_full_path;
3151
0
  }
3152
3153
0
  if (cmNonempty(uniqueIdName)) {
3154
0
    cmCryptoHash hasher(cmCryptoHash::AlgoMD5);
3155
0
    unity_file << "/* " << pathToHash << " */\n"
3156
0
               << "#undef " << *uniqueIdName << "\n"
3157
0
               << "#define " << *uniqueIdName << " unity_"
3158
0
               << hasher.HashString(pathToHash) << "\n";
3159
0
  }
3160
3161
0
  if (beforeInclude) {
3162
0
    unity_file << *beforeInclude << "\n";
3163
0
  }
3164
3165
  // clang-tidy-17 has new include checks that needs NOLINT too.
3166
0
  unity_file
3167
0
    << "/* NOLINTNEXTLINE(bugprone-suspicious-include,misc-include-cleaner) "
3168
0
       "*/\n";
3169
0
  if (pathMode == UnityPathMode::Relative) {
3170
0
    unity_file << "#include \"" << relocatableIncludePath << "\"\n";
3171
0
  } else {
3172
0
    unity_file << "#include \"" << sf_full_path << "\"\n";
3173
0
  }
3174
3175
0
  if (afterInclude) {
3176
0
    unity_file << *afterInclude << "\n";
3177
0
  }
3178
0
  if (cond) {
3179
0
    unity_file << "#endif\n";
3180
0
  }
3181
0
  unity_file << "\n";
3182
0
}
3183
3184
namespace {
3185
std::string unity_file_extension(std::string const& lang)
3186
0
{
3187
0
  std::string extension;
3188
0
  if (lang == "C") {
3189
0
    extension = "_c.c";
3190
0
  } else if (lang == "CXX") {
3191
0
    extension = "_cxx.cxx";
3192
0
  } else if (lang == "CUDA") {
3193
0
    extension = "_cu.cu";
3194
0
  } else if (lang == "OBJC") {
3195
0
    extension = "_m.m";
3196
0
  } else if (lang == "OBJCXX") {
3197
0
    extension = "_mm.mm";
3198
0
  }
3199
0
  return extension;
3200
0
}
3201
3202
char const* unity_file_prefix(cmGeneratorTarget* target)
3203
0
{
3204
0
  if (cmValue val = target->GetProperty("UNITY_BUILD_FILENAME_PREFIX")) {
3205
0
    return val->c_str();
3206
0
  }
3207
0
  return "unity_";
3208
0
}
3209
}
3210
3211
std::vector<cmLocalGenerator::UnitySource>
3212
cmLocalGenerator::AddUnityFilesModeAuto(
3213
  cmGeneratorTarget* target, std::string const& lang,
3214
  std::vector<std::string> const& configs,
3215
  std::vector<UnityBatchedSource> const& filtered_sources,
3216
  cmValue beforeInclude, cmValue afterInclude,
3217
  std::string const& filename_base, UnityPathMode pathMode, size_t batchSize)
3218
0
{
3219
0
  if (batchSize == 0) {
3220
0
    batchSize = filtered_sources.size();
3221
0
  }
3222
0
  char const* filename_prefix = unity_file_prefix(target);
3223
0
  std::vector<UnitySource> unity_files;
3224
0
  for (size_t itemsLeft = filtered_sources.size(), chunk, batch = 0;
3225
0
       itemsLeft > 0; itemsLeft -= chunk, ++batch) {
3226
3227
0
    chunk = std::min(itemsLeft, batchSize);
3228
3229
0
    std::string filename = cmStrCat(filename_base, filename_prefix, batch,
3230
0
                                    unity_file_extension(lang));
3231
0
    auto const begin = filtered_sources.begin() + batch * batchSize;
3232
0
    auto const end = begin + chunk;
3233
0
    unity_files.emplace_back(this->WriteUnitySource(
3234
0
      target, configs, cmMakeRange(begin, end), beforeInclude, afterInclude,
3235
0
      std::move(filename), filename_base, pathMode));
3236
0
  }
3237
0
  return unity_files;
3238
0
}
3239
3240
std::vector<cmLocalGenerator::UnitySource>
3241
cmLocalGenerator::AddUnityFilesModeGroup(
3242
  cmGeneratorTarget* target, std::string const& lang,
3243
  std::vector<std::string> const& configs,
3244
  std::vector<UnityBatchedSource> const& filtered_sources,
3245
  cmValue beforeInclude, cmValue afterInclude,
3246
  std::string const& filename_base, UnityPathMode pathMode)
3247
0
{
3248
0
  std::vector<UnitySource> unity_files;
3249
3250
  // sources organized by group name. Drop any source
3251
  // without a group
3252
0
  std::unordered_map<std::string, std::vector<UnityBatchedSource>>
3253
0
    explicit_mapping;
3254
0
  for (UnityBatchedSource const& ubs : filtered_sources) {
3255
0
    if (cmValue value = ubs.Source->GetProperty("UNITY_GROUP")) {
3256
0
      auto i = explicit_mapping.find(*value);
3257
0
      if (i == explicit_mapping.end()) {
3258
0
        std::vector<UnityBatchedSource> sources{ ubs };
3259
0
        explicit_mapping.emplace(*value, std::move(sources));
3260
0
      } else {
3261
0
        i->second.emplace_back(ubs);
3262
0
      }
3263
0
    }
3264
0
  }
3265
3266
0
  char const* filename_prefix = unity_file_prefix(target);
3267
0
  for (auto const& item : explicit_mapping) {
3268
0
    auto const& name = item.first;
3269
0
    std::string filename = cmStrCat(filename_base, filename_prefix, name,
3270
0
                                    unity_file_extension(lang));
3271
0
    unity_files.emplace_back(this->WriteUnitySource(
3272
0
      target, configs, cmMakeRange(item.second), beforeInclude, afterInclude,
3273
0
      std::move(filename), filename_base, pathMode));
3274
0
  }
3275
3276
0
  return unity_files;
3277
0
}
3278
3279
void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
3280
0
{
3281
  // cmFastbuildNormalTargetGenerator handles unity build.
3282
0
  if (this->GetGlobalGenerator()->IsFastbuild() ||
3283
0
      !target->GetPropertyAsBool("UNITY_BUILD")) {
3284
0
    return;
3285
0
  }
3286
3287
0
  std::vector<UnityBatchedSource> unitySources;
3288
3289
0
  std::vector<std::string> configs =
3290
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
3291
3292
0
  std::map<cmSourceFile const*, size_t> index;
3293
3294
0
  for (size_t ci = 0; ci < configs.size(); ++ci) {
3295
    // FIXME: Refactor collection of sources to not evaluate object libraries.
3296
    // Their final set of object files might be transformed by unity builds.
3297
0
    std::vector<cmSourceFile*> sources;
3298
0
    target->GetSourceFiles(sources, configs[ci]);
3299
0
    for (cmSourceFile* sf : sources) {
3300
0
      cmGeneratorFileSet const* fileSet =
3301
0
        target->GetFileSetForSource(configs[ci], sf);
3302
0
      if (fileSet &&
3303
0
          !cm::FileSetMetadata::GetAttributes(fileSet->GetType())
3304
0
             .contains(cm::FileSetMetadata::FileSetAttributes::UnityBuild)) {
3305
0
        continue;
3306
0
      }
3307
      // Files which need C++ scanning cannot participate in unity builds as
3308
      // there is a single place in TUs that may perform module-dependency bits
3309
      // and a unity source cannot `#include` them in-order and represent a
3310
      // valid TU.
3311
0
      if (sf->GetLanguage() == "CXX"_s &&
3312
0
          target->NeedDyndepForSource("CXX", configs[ci], sf)) {
3313
0
        continue;
3314
0
      }
3315
3316
0
      auto mi = index.find(sf);
3317
0
      if (mi == index.end()) {
3318
0
        unitySources.emplace_back(sf);
3319
0
        std::map<cmSourceFile const*, size_t>::value_type entry(
3320
0
          sf, unitySources.size() - 1);
3321
0
        mi = index.insert(entry).first;
3322
0
      }
3323
0
      unitySources[mi->second].Configs.emplace_back(ci);
3324
0
    }
3325
0
  }
3326
3327
0
  std::string filename_base =
3328
0
    cmStrCat(target->GetCMFSupportDirectory(), "/Unity/");
3329
3330
0
  cmValue batchSizeString = target->GetProperty("UNITY_BUILD_BATCH_SIZE");
3331
0
  size_t const unityBatchSize = batchSizeString
3332
0
    ? static_cast<size_t>(std::atoi(batchSizeString->c_str()))
3333
0
    : 0;
3334
3335
0
  cmValue beforeInclude =
3336
0
    target->GetProperty("UNITY_BUILD_CODE_BEFORE_INCLUDE");
3337
0
  cmValue afterInclude = target->GetProperty("UNITY_BUILD_CODE_AFTER_INCLUDE");
3338
0
  cmValue unityMode = target->GetProperty("UNITY_BUILD_MODE");
3339
0
  UnityPathMode pathMode = target->GetPropertyAsBool("UNITY_BUILD_RELOCATABLE")
3340
0
    ? UnityPathMode::Relative
3341
0
    : UnityPathMode::Absolute;
3342
3343
0
  for (std::string lang : { "C", "CXX", "OBJC", "OBJCXX", "CUDA" }) {
3344
0
    std::vector<UnityBatchedSource> filtered_sources;
3345
0
    std::copy_if(
3346
0
      unitySources.begin(), unitySources.end(),
3347
0
      std::back_inserter(filtered_sources),
3348
0
      [&](UnityBatchedSource const& ubs) -> bool {
3349
0
        cmSourceFile* sf = ubs.Source;
3350
0
        if (sf->GetLanguage() != lang) {
3351
0
          return false;
3352
0
        }
3353
0
        for (auto idx : ubs.Configs) {
3354
0
          cmGeneratorFileSet const* fileSet =
3355
0
            target->GetFileSetForSource(configs[idx], sf);
3356
0
          if (fileSet &&
3357
0
              (fileSet->GetProperty("SKIP_UNITY_BUILD_INCLUSION").IsOn() ||
3358
0
               fileSet->GetProperty(fileSet->BelongsTo(target)
3359
0
                                      ? "COMPILE_OPTIONS"
3360
0
                                      : "INTERFACE_COMPILE_OPTIONS") ||
3361
0
               fileSet->GetProperty(fileSet->BelongsTo(target)
3362
0
                                      ? "COMPILE_DEFINITIONS"
3363
0
                                      : "INTERFACE_COMPILE_DEFINITIONS") ||
3364
0
               fileSet->GetProperty(fileSet->BelongsTo(target)
3365
0
                                      ? "INCLUDE_DIRECTORIES"
3366
0
                                      : "INTERFACE_INCLUDE_DIRECTORIES"))) {
3367
0
            return false;
3368
0
          }
3369
0
        }
3370
0
        return !sf->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION") &&
3371
0
          !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
3372
0
          !sf->GetProperty("COMPILE_OPTIONS") &&
3373
0
          !sf->GetProperty("COMPILE_DEFINITIONS") &&
3374
0
          !sf->GetProperty("COMPILE_FLAGS") &&
3375
0
          !sf->GetProperty("INCLUDE_DIRECTORIES");
3376
0
      });
3377
3378
0
    std::vector<UnitySource> unity_files;
3379
0
    if (!unityMode || *unityMode == "BATCH") {
3380
0
      unity_files = AddUnityFilesModeAuto(
3381
0
        target, lang, configs, filtered_sources, beforeInclude, afterInclude,
3382
0
        filename_base, pathMode, unityBatchSize);
3383
0
    } else if (unityMode && *unityMode == "GROUP") {
3384
0
      unity_files = AddUnityFilesModeGroup(
3385
0
        target, lang, configs, filtered_sources, beforeInclude, afterInclude,
3386
0
        filename_base, pathMode);
3387
0
    } else {
3388
      // unity mode is set to an unsupported value
3389
0
      std::string e("Invalid UNITY_BUILD_MODE value of " + *unityMode +
3390
0
                    " assigned to target " + target->GetName() +
3391
0
                    ". Acceptable values are BATCH and GROUP.");
3392
0
      this->IssueMessage(MessageType::FATAL_ERROR, e);
3393
0
    }
3394
3395
0
    for (UnitySource const& file : unity_files) {
3396
0
      auto* unity = this->GetMakefile()->GetOrCreateSource(file.Path);
3397
0
      unity->SetSpecialSourceType(
3398
0
        cmSourceFile::SpecialSourceType::UnitySource);
3399
0
      target->AddSource(file.Path, true);
3400
0
      unity->SetProperty("SKIP_UNITY_BUILD_INCLUSION", "ON");
3401
0
      unity->SetProperty("UNITY_SOURCE_FILE", file.Path);
3402
0
      unity->SetProperty("CXX_SCAN_FOR_MODULES", "0");
3403
0
      if (file.PerConfig) {
3404
0
        unity->SetProperty("COMPILE_DEFINITIONS",
3405
0
                           "CMAKE_UNITY_CONFIG_$<UPPER_CASE:$<CONFIG>>");
3406
0
      }
3407
3408
0
      if (pathMode == UnityPathMode::Relative) {
3409
0
        unity->AppendProperty("INCLUDE_DIRECTORIES",
3410
0
                              this->GetSourceDirectory(), false);
3411
0
      }
3412
0
    }
3413
0
  }
3414
0
}
3415
3416
void cmLocalGenerator::AddPerLanguageLinkFlags(std::string& flags,
3417
                                               cmGeneratorTarget const* target,
3418
                                               std::string const& lang,
3419
                                               std::string const& config)
3420
0
{
3421
0
  switch (target->GetType()) {
3422
0
    case cmStateEnums::MODULE_LIBRARY:
3423
0
    case cmStateEnums::SHARED_LIBRARY:
3424
0
    case cmStateEnums::EXECUTABLE:
3425
0
      break;
3426
0
    default:
3427
0
      return;
3428
0
  }
3429
3430
0
  std::string langLinkFlags =
3431
0
    this->Makefile->GetSafeDefinition(cmStrCat("CMAKE_", lang, "_LINK_FLAGS"));
3432
3433
0
  switch (target->GetPolicyStatusCMP0210()) {
3434
0
    case cmPolicies::WARN:
3435
      // WARN only when CMAKE_<LANG>_LINK_FLAGS is set, and when the current
3436
      // target is not an executable, and CMAKE_<LANG>_LINK_FLAGS is not equal
3437
      // to CMAKE_EXECUTABLE_CREATE_<LANG>_FLAGS. This warns users trying to
3438
      // use the NEW behavior on old projects (since CMake will be ignoring
3439
      // their wishes), while also exempting cases when the latter variable
3440
      // (substituted for the former spelling under the NEW behavior) is being
3441
      // used legitimately by CMake.
3442
      // Additionally, WARN at most once per language, instead of on every
3443
      // target.
3444
0
      if (!langLinkFlags.empty() &&
3445
0
          target->GetType() != cmStateEnums::EXECUTABLE &&
3446
0
          langLinkFlags !=
3447
0
            this->Makefile->GetSafeDefinition(
3448
0
              cmStrCat("CMAKE_EXECUTABLE_CREATE_", lang, "_FLAGS")) &&
3449
0
          this->GlobalGenerator->ShouldWarnCMP0210(lang)) {
3450
0
        this->IssuePolicyWarning(
3451
0
          cmPolicies::CMP0210, {},
3452
0
          cmStrCat("For compatibility with older versions of CMake, CMAKE_"_s,
3453
0
                   lang,
3454
0
                   "_LINK_FLAGS will be ignored for all non-EXECUTABLE "
3455
0
                   "targets which use these flags."_s));
3456
0
      }
3457
0
      CM_FALLTHROUGH;
3458
0
    case cmPolicies::OLD:
3459
      // OLD behavior is to do nothing here, since the use of
3460
      // CMAKE_<LANG>_LINK_FLAGS for EXECUTABLEs is handled elsewhere.
3461
0
      break;
3462
0
    case cmPolicies::NEW:
3463
      // NEW behavior is to support per-language link flags for all target
3464
      // types.
3465
0
      this->AppendLinkFlagsWithParsing(flags, langLinkFlags, target, lang);
3466
0
      if (!config.empty()) {
3467
0
        std::string lankLinkFlagsConfig =
3468
0
          this->Makefile->GetSafeDefinition(cmStrCat(
3469
0
            "CMAKE_", lang, "_LINK_FLAGS_", cmSystemTools::UpperCase(config)));
3470
0
        this->AppendLinkFlagsWithParsing(flags, lankLinkFlagsConfig, target,
3471
0
                                         lang);
3472
0
      }
3473
0
      break;
3474
0
  }
3475
0
}
3476
3477
void cmLocalGenerator::AppendTargetCreationLinkFlags(
3478
  std::string& flags, cmGeneratorTarget const* target,
3479
  std::string const& linkLanguage)
3480
0
{
3481
0
  std::string createFlagsVar;
3482
0
  cmValue createFlagsVal;
3483
0
  switch (target->GetType()) {
3484
0
    case cmStateEnums::STATIC_LIBRARY:
3485
0
      break;
3486
0
    case cmStateEnums::MODULE_LIBRARY:
3487
0
      createFlagsVar =
3488
0
        cmStrCat("CMAKE_SHARED_MODULE_CREATE_", linkLanguage, "_FLAGS");
3489
0
      createFlagsVal = this->Makefile->GetDefinition(createFlagsVar);
3490
      // On some platforms we use shared library creation flags for modules.
3491
0
      CM_FALLTHROUGH;
3492
0
    case cmStateEnums::SHARED_LIBRARY:
3493
0
      if (!createFlagsVal) {
3494
0
        createFlagsVar =
3495
0
          cmStrCat("CMAKE_SHARED_LIBRARY_CREATE_", linkLanguage, "_FLAGS");
3496
0
        createFlagsVal = this->Makefile->GetDefinition(createFlagsVar);
3497
0
      }
3498
0
      break;
3499
0
    case cmStateEnums::EXECUTABLE:
3500
0
      createFlagsVar = target->GetPolicyStatusCMP0210() == cmPolicies::NEW
3501
0
        ? cmStrCat("CMAKE_EXECUTABLE_CREATE_", linkLanguage, "_FLAGS")
3502
0
        : cmStrCat("CMAKE_", linkLanguage, "_LINK_FLAGS");
3503
0
      createFlagsVal = this->Makefile->GetDefinition(createFlagsVar);
3504
0
      break;
3505
0
    default:
3506
0
      break;
3507
0
  }
3508
0
  if (createFlagsVal) {
3509
0
    this->AppendFlags(flags, *createFlagsVal, createFlagsVar, target,
3510
0
                      cmBuildStep::Link, linkLanguage);
3511
0
  }
3512
0
}
3513
3514
void cmLocalGenerator::AppendLinkerTypeFlags(std::string& flags,
3515
                                             cmGeneratorTarget* target,
3516
                                             std::string const& config,
3517
                                             std::string const& linkLanguage)
3518
0
{
3519
0
  switch (target->GetType()) {
3520
0
    case cmStateEnums::EXECUTABLE:
3521
0
    case cmStateEnums::SHARED_LIBRARY:
3522
0
    case cmStateEnums::MODULE_LIBRARY:
3523
0
      break;
3524
0
    default:
3525
0
      return;
3526
0
  }
3527
3528
0
  auto linkMode =
3529
0
    cmStrCat("CMAKE_", linkLanguage, target->IsDeviceLink() ? "_DEVICE_" : "_",
3530
0
             "LINK_MODE");
3531
0
  auto mode = this->Makefile->GetDefinition(linkMode);
3532
0
  if (mode && mode != "DRIVER"_s) {
3533
0
    return;
3534
0
  }
3535
3536
0
  auto linkerType = target->GetLinkerTypeProperty(linkLanguage, config);
3537
0
  if (linkerType.empty()) {
3538
0
    linkerType = "DEFAULT";
3539
0
  }
3540
0
  auto usingLinker =
3541
0
    cmStrCat("CMAKE_", linkLanguage, "_USING_",
3542
0
             target->IsDeviceLink() ? "DEVICE_" : "", "LINKER_", linkerType);
3543
0
  auto linkerTypeFlags = this->Makefile->GetDefinition(usingLinker);
3544
0
  if (linkerTypeFlags) {
3545
0
    if (!linkerTypeFlags.IsEmpty()) {
3546
0
      auto linkerFlags = cmExpandListWithBacktrace(linkerTypeFlags);
3547
0
      target->ResolveLinkerWrapper(linkerFlags, linkLanguage);
3548
0
      this->AppendFlags(flags, linkerFlags);
3549
0
    }
3550
0
  } else if (linkerType != "DEFAULT"_s) {
3551
0
    auto isCMakeLinkerType = [](std::string const& type) -> bool {
3552
0
      return std::all_of(type.cbegin(), type.cend(), cmsysString_isupper);
3553
0
    };
3554
0
    if (isCMakeLinkerType(linkerType)) {
3555
0
      this->IssueMessage(
3556
0
        MessageType::FATAL_ERROR,
3557
0
        cmStrCat("LINKER_TYPE '", linkerType,
3558
0
                 "' is unknown or not supported by this toolchain."));
3559
0
    } else {
3560
0
      this->IssueMessage(
3561
0
        MessageType::FATAL_ERROR,
3562
0
        cmStrCat("LINKER_TYPE '", linkerType,
3563
0
                 "' is unknown. Did you forget to define the '", usingLinker,
3564
0
                 "' variable?"));
3565
0
    }
3566
0
  }
3567
0
}
3568
3569
void cmLocalGenerator::AddTargetTypeLinkerFlags(
3570
  std::string& flags, cmGeneratorTarget const* target, std::string const& lang,
3571
  std::string const& config)
3572
0
{
3573
0
  std::string linkerFlagsVar;
3574
0
  switch (target->GetType()) {
3575
0
    case cmStateEnums::EXECUTABLE:
3576
0
      linkerFlagsVar = "CMAKE_EXE_LINKER_FLAGS";
3577
0
      break;
3578
0
    case cmStateEnums::SHARED_LIBRARY:
3579
0
      linkerFlagsVar = "CMAKE_SHARED_LINKER_FLAGS";
3580
0
      break;
3581
0
    case cmStateEnums::MODULE_LIBRARY:
3582
0
      linkerFlagsVar = "CMAKE_MODULE_LINKER_FLAGS";
3583
0
      break;
3584
0
    default:
3585
0
      return;
3586
0
  }
3587
0
  this->AddConfigVariableFlags(flags, linkerFlagsVar, target,
3588
0
                               cmBuildStep::Link, lang, config);
3589
0
}
3590
3591
void cmLocalGenerator::AddTargetPropertyLinkFlags(
3592
  std::string& flags, cmGeneratorTarget const* target,
3593
  std::string const& config)
3594
0
{
3595
0
  cmValue targetLinkFlags = target->GetProperty("LINK_FLAGS");
3596
0
  if (targetLinkFlags) {
3597
0
    this->AppendFlags(flags, *targetLinkFlags);
3598
0
  }
3599
0
  if (!config.empty()) {
3600
0
    cmValue targetLinkFlagsConfig = target->GetProperty(
3601
0
      cmStrCat("LINK_FLAGS_", cmSystemTools::UpperCase(config)));
3602
0
    if (targetLinkFlagsConfig) {
3603
0
      this->AppendFlags(flags, *targetLinkFlagsConfig);
3604
0
    }
3605
0
  }
3606
0
}
3607
3608
void cmLocalGenerator::AppendIPOLinkerFlags(std::string& flags,
3609
                                            cmGeneratorTarget* target,
3610
                                            std::string const& config,
3611
                                            std::string const& lang)
3612
0
{
3613
0
  if (!target->IsIPOEnabled(lang, config)) {
3614
0
    return;
3615
0
  }
3616
3617
0
  switch (target->GetType()) {
3618
0
    case cmStateEnums::EXECUTABLE:
3619
0
    case cmStateEnums::SHARED_LIBRARY:
3620
0
    case cmStateEnums::MODULE_LIBRARY:
3621
0
      break;
3622
0
    default:
3623
0
      return;
3624
0
  }
3625
3626
0
  std::string const name = "CMAKE_" + lang + "_LINK_OPTIONS_IPO";
3627
0
  cmValue rawFlagsList = this->Makefile->GetDefinition(name);
3628
0
  if (!rawFlagsList) {
3629
0
    return;
3630
0
  }
3631
3632
0
  cmList flagsList{ *rawFlagsList };
3633
0
  for (std::string const& o : flagsList) {
3634
0
    this->AppendFlagEscape(flags, o);
3635
0
  }
3636
0
}
3637
3638
void cmLocalGenerator::AppendPositionIndependentLinkerFlags(
3639
  std::string& flags, cmGeneratorTarget* target, std::string const& config,
3640
  std::string const& lang)
3641
0
{
3642
  // For now, only EXECUTABLE is concerned
3643
0
  if (target->GetType() != cmStateEnums::EXECUTABLE) {
3644
0
    return;
3645
0
  }
3646
3647
0
  char const* PICValue = target->GetLinkPIEProperty(config);
3648
0
  if (!PICValue && lang != "Rust") {
3649
    // POSITION_INDEPENDENT_CODE is not set, note that for Rust we do not
3650
    // return as the compiler tends to enable PIE all the time, which is the
3651
    // opposite of what C & C++ compilers do. So instead of letting the rust
3652
    // compiler decide on its own whether PIE should be enabled, we explicit
3653
    // set it.
3654
0
    return;
3655
0
  }
3656
3657
0
  std::string const mode = cmIsOn(PICValue) ? "PIE" : "NO_PIE";
3658
3659
0
  std::string supported = "CMAKE_" + lang + "_LINK_" + mode + "_SUPPORTED";
3660
0
  if (this->Makefile->GetDefinition(supported).IsOff()) {
3661
0
    return;
3662
0
  }
3663
3664
0
  std::string name = "CMAKE_" + lang + "_LINK_OPTIONS_" + mode;
3665
3666
0
  auto pieFlags = this->Makefile->GetSafeDefinition(name);
3667
0
  if (pieFlags.empty()) {
3668
0
    return;
3669
0
  }
3670
3671
0
  cmList flagsList{ pieFlags };
3672
0
  for (auto const& flag : flagsList) {
3673
0
    this->AppendFlagEscape(flags, flag);
3674
0
  }
3675
0
}
3676
3677
void cmLocalGenerator::AppendWarningAsErrorLinkerFlags(
3678
  std::string& flags, cmGeneratorTarget* target, std::string const& lang)
3679
0
{
3680
0
  if (this->GetCMakeInstance()->GetIgnoreLinkWarningAsError()) {
3681
0
    return;
3682
0
  }
3683
3684
0
  switch (target->GetType()) {
3685
0
    case cmStateEnums::EXECUTABLE:
3686
0
    case cmStateEnums::SHARED_LIBRARY:
3687
0
    case cmStateEnums::MODULE_LIBRARY:
3688
0
      break;
3689
0
    default:
3690
0
      return;
3691
0
  }
3692
3693
0
  auto const wError = target->GetProperty("LINK_WARNING_AS_ERROR");
3694
0
  if (wError.IsOff()) {
3695
0
    return;
3696
0
  }
3697
0
  cmList wErrorOptions;
3698
0
  if (wError.IsOn()) {
3699
0
    wErrorOptions = { "DRIVER", "LINKER" };
3700
0
  } else {
3701
0
    wErrorOptions = wError;
3702
0
    std::sort(wErrorOptions.begin(), wErrorOptions.end());
3703
0
    wErrorOptions.erase(
3704
0
      std::unique(wErrorOptions.begin(), wErrorOptions.end()),
3705
0
      wErrorOptions.end());
3706
0
  }
3707
3708
0
  auto linkModeIsDriver =
3709
0
    this->Makefile->GetDefinition(cmStrCat("CMAKE_", lang, "_LINK_MODE")) ==
3710
0
    "DRIVER"_s;
3711
0
  std::string errorMessage;
3712
0
  for (auto const& option : wErrorOptions) {
3713
0
    if (option != "DRIVER"_s && option != "LINKER"_s) {
3714
0
      errorMessage += cmStrCat("  ", option, '\n');
3715
0
      continue;
3716
0
    }
3717
3718
0
    if (option == "DRIVER"_s && !linkModeIsDriver) {
3719
0
      continue;
3720
0
    }
3721
3722
0
    auto const wErrorOpts = this->Makefile->GetDefinition(cmStrCat(
3723
0
      "CMAKE_", lang, '_', (option == "DRIVER"_s ? "COMPILE" : "LINK"),
3724
0
      "_OPTIONS_WARNING_AS_ERROR"));
3725
0
    if (wErrorOpts.IsSet()) {
3726
0
      auto items =
3727
0
        cmExpandListWithBacktrace(wErrorOpts, target->GetBacktrace());
3728
0
      if (option == "LINKER"_s) {
3729
0
        target->ResolveLinkerWrapper(items, lang);
3730
0
      }
3731
0
      for (auto const& item : items) {
3732
0
        this->AppendFlagEscape(flags, item.Value);
3733
0
      }
3734
0
    }
3735
0
  }
3736
0
  if (!errorMessage.empty()) {
3737
0
    this->Makefile->GetCMakeInstance()->IssueMessage(
3738
0
      MessageType::FATAL_ERROR,
3739
0
      cmStrCat(
3740
0
        "Erroneous value(s) for 'LINK_WARNING_AS_ERROR' property of target '",
3741
0
        target->GetName(), "':\n", errorMessage));
3742
0
  }
3743
0
}
3744
3745
void cmLocalGenerator::AppendDependencyInfoLinkerFlags(
3746
  std::string& flags, cmGeneratorTarget* target, std::string const& config,
3747
  std::string const& linkLanguage)
3748
0
{
3749
0
  if (!this->GetGlobalGenerator()->SupportsLinkerDependencyFile() ||
3750
0
      !target->HasLinkDependencyFile(config)) {
3751
0
    return;
3752
0
  }
3753
3754
0
  auto depFlag = *this->Makefile->GetDefinition(
3755
0
    cmStrCat("CMAKE_", linkLanguage, "_LINKER_DEPFILE_FLAGS"));
3756
0
  if (depFlag.empty()) {
3757
0
    return;
3758
0
  }
3759
3760
0
  auto depFile = this->ConvertToOutputFormat(
3761
0
    this->MaybeRelativeToWorkDir(this->GetLinkDependencyFile(target, config)),
3762
0
    cmOutputConverter::SHELL);
3763
0
  auto rulePlaceholderExpander = this->CreateRulePlaceholderExpander();
3764
0
  cmRulePlaceholderExpander::RuleVariables linkDepsVariables;
3765
0
  linkDepsVariables.DependencyFile = depFile.c_str();
3766
0
  rulePlaceholderExpander->ExpandRuleVariables(this, depFlag,
3767
0
                                               linkDepsVariables);
3768
0
  auto depFlags = cmExpandListWithBacktrace(depFlag);
3769
0
  target->ResolveLinkerWrapper(depFlags, linkLanguage);
3770
3771
0
  this->AppendFlags(flags, depFlags);
3772
0
}
3773
3774
std::string cmLocalGenerator::GetLinkDependencyFile(
3775
  cmGeneratorTarget* /*target*/, std::string const& /*config*/) const
3776
0
{
3777
0
  return "link.d";
3778
0
}
3779
3780
void cmLocalGenerator::AppendModuleDefinitionFlag(
3781
  std::string& flags, cmGeneratorTarget const* target,
3782
  cmLinkLineComputer* linkLineComputer, std::string const& config,
3783
  std::string const& lang)
3784
0
{
3785
0
  cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
3786
0
    target->GetModuleDefinitionInfo(config);
3787
0
  if (!mdi || mdi->DefFile.empty()) {
3788
0
    return;
3789
0
  }
3790
3791
0
  cmValue defFileFlag = this->Makefile->GetDefinition(
3792
0
    cmStrCat("CMAKE_", lang, "_LINK_DEF_FILE_FLAG"));
3793
0
  if (!defFileFlag) {
3794
0
    defFileFlag = this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
3795
0
  }
3796
0
  if (!defFileFlag) {
3797
0
    return;
3798
0
  }
3799
3800
  // Append the flag and value.  Use ConvertToLinkReference to help
3801
  // vs6's "cl -link" pass it to the linker.
3802
0
  std::string flag =
3803
0
    cmStrCat(*defFileFlag,
3804
0
             this->ConvertToOutputFormat(
3805
0
               linkLineComputer->ConvertToLinkReference(mdi->DefFile),
3806
0
               cmOutputConverter::SHELL));
3807
0
  this->AppendFlags(flags, flag);
3808
0
}
3809
3810
bool cmLocalGenerator::AppendLWYUFlags(std::string& flags,
3811
                                       cmGeneratorTarget const* target,
3812
                                       std::string const& lang)
3813
0
{
3814
0
  auto useLWYU = target->GetPropertyAsBool("LINK_WHAT_YOU_USE") &&
3815
0
    (target->GetType() == cmStateEnums::TargetType::EXECUTABLE ||
3816
0
     target->GetType() == cmStateEnums::TargetType::SHARED_LIBRARY ||
3817
0
     target->GetType() == cmStateEnums::TargetType::MODULE_LIBRARY);
3818
3819
0
  if (useLWYU) {
3820
0
    auto const& lwyuFlag = this->GetMakefile()->GetSafeDefinition(
3821
0
      cmStrCat("CMAKE_", lang, "_LINK_WHAT_YOU_USE_FLAG"));
3822
0
    useLWYU = !lwyuFlag.empty();
3823
3824
0
    if (useLWYU) {
3825
0
      std::vector<BT<std::string>> lwyuOpts;
3826
0
      lwyuOpts.emplace_back(lwyuFlag);
3827
0
      this->AppendFlags(flags, target->ResolveLinkerWrapper(lwyuOpts, lang));
3828
0
    }
3829
0
  }
3830
3831
0
  return useLWYU;
3832
0
}
3833
3834
void cmLocalGenerator::AppendCompileOptions(std::string& options,
3835
                                            std::string const& options_list,
3836
                                            char const* regex) const
3837
0
{
3838
  // Short-circuit if there are no options.
3839
0
  if (options_list.empty()) {
3840
0
    return;
3841
0
  }
3842
3843
  // Expand the list of options.
3844
0
  cmList options_vec{ options_list };
3845
0
  this->AppendCompileOptions(options, options_vec, regex);
3846
0
}
3847
3848
void cmLocalGenerator::AppendCompileOptions(
3849
  std::string& options, std::vector<std::string> const& options_vec,
3850
  char const* regex) const
3851
0
{
3852
0
  if (regex) {
3853
    // Filter flags upon specified reges.
3854
0
    cmsys::RegularExpression r(regex);
3855
3856
0
    for (std::string const& opt : options_vec) {
3857
0
      if (r.find(opt)) {
3858
0
        this->AppendFlagEscape(options, opt);
3859
0
      }
3860
0
    }
3861
0
  } else {
3862
0
    for (std::string const& opt : options_vec) {
3863
0
      this->AppendFlagEscape(options, opt);
3864
0
    }
3865
0
  }
3866
0
}
3867
3868
void cmLocalGenerator::AppendCompileOptions(
3869
  std::vector<BT<std::string>>& options,
3870
  std::vector<BT<std::string>> const& options_vec, char const* regex) const
3871
0
{
3872
0
  if (regex) {
3873
    // Filter flags upon specified regular expressions.
3874
0
    cmsys::RegularExpression r(regex);
3875
3876
0
    for (BT<std::string> const& opt : options_vec) {
3877
0
      if (r.find(opt.Value)) {
3878
0
        std::string flag;
3879
0
        this->AppendFlagEscape(flag, opt.Value);
3880
0
        options.emplace_back(std::move(flag), opt.Backtrace);
3881
0
      }
3882
0
    }
3883
0
  } else {
3884
0
    for (BT<std::string> const& opt : options_vec) {
3885
0
      std::string flag;
3886
0
      this->AppendFlagEscape(flag, opt.Value);
3887
0
      options.emplace_back(std::move(flag), opt.Backtrace);
3888
0
    }
3889
0
  }
3890
0
}
3891
3892
void cmLocalGenerator::AppendIncludeDirectories(
3893
  std::vector<std::string>& includes, std::string const& includes_list,
3894
  cmSourceFile const& sourceFile) const
3895
0
{
3896
  // Short-circuit if there are no includes.
3897
0
  if (includes_list.empty()) {
3898
0
    return;
3899
0
  }
3900
3901
  // Expand the list of includes.
3902
0
  cmList includes_vec{ includes_list };
3903
0
  this->AppendIncludeDirectories(includes, includes_vec, sourceFile);
3904
0
}
3905
3906
void cmLocalGenerator::AppendIncludeDirectories(
3907
  std::vector<std::string>& includes,
3908
  std::vector<std::string> const& includes_vec,
3909
  cmSourceFile const& sourceFile) const
3910
0
{
3911
0
  std::unordered_set<std::string> uniqueIncludes;
3912
3913
0
  for (std::string const& include : includes_vec) {
3914
0
    if (!cmSystemTools::FileIsFullPath(include)) {
3915
0
      std::ostringstream e;
3916
0
      e << "Found relative path while evaluating include directories of "
3917
0
           "\""
3918
0
        << sourceFile.GetLocation().GetName() << "\":\n  \"" << include
3919
0
        << "\"\n";
3920
3921
0
      this->IssueMessage(MessageType::FATAL_ERROR, e.str());
3922
0
      return;
3923
0
    }
3924
3925
0
    std::string inc = include;
3926
3927
0
    if (!cmIsOff(inc)) {
3928
0
      cmSystemTools::ConvertToUnixSlashes(inc);
3929
0
    }
3930
3931
0
    if (uniqueIncludes.insert(inc).second) {
3932
0
      includes.push_back(std::move(inc));
3933
0
    }
3934
0
  }
3935
0
}
3936
3937
void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
3938
                                     std::string const& defines_list) const
3939
0
{
3940
0
  std::set<BT<std::string>> tmp;
3941
0
  this->AppendDefines(tmp, cmExpandListWithBacktrace(defines_list));
3942
0
  for (BT<std::string> const& i : tmp) {
3943
0
    defines.emplace(i.Value);
3944
0
  }
3945
0
}
3946
3947
void cmLocalGenerator::AppendDefines(
3948
  std::set<std::string>& defines,
3949
  std::vector<BT<std::string>> const& defines_vec) const
3950
0
{
3951
0
  std::set<BT<std::string>> tmp;
3952
0
  this->AppendDefines(tmp, defines_vec);
3953
0
  for (BT<std::string> const& i : tmp) {
3954
0
    defines.emplace(i.Value);
3955
0
  }
3956
0
}
3957
3958
void cmLocalGenerator::AppendDefines(std::set<BT<std::string>>& defines,
3959
                                     std::string const& defines_list) const
3960
0
{
3961
  // Short-circuit if there are no definitions.
3962
0
  if (defines_list.empty()) {
3963
0
    return;
3964
0
  }
3965
3966
  // Expand the list of definitions.
3967
0
  this->AppendDefines(defines, cmExpandListWithBacktrace(defines_list));
3968
0
}
3969
3970
void cmLocalGenerator::AppendDefines(
3971
  std::set<BT<std::string>>& defines,
3972
  std::vector<BT<std::string>> const& defines_vec) const
3973
0
{
3974
0
  for (BT<std::string> const& d : defines_vec) {
3975
    // Skip unsupported definitions.
3976
0
    if (!this->CheckDefinition(d.Value)) {
3977
0
      continue;
3978
0
    }
3979
    // remove any leading -D
3980
0
    if (cmHasLiteralPrefix(d.Value, "-D")) {
3981
0
      defines.emplace(d.Value.substr(2), d.Backtrace);
3982
0
    } else {
3983
0
      defines.insert(d);
3984
0
    }
3985
0
  }
3986
0
}
3987
3988
void cmLocalGenerator::JoinDefines(std::set<std::string> const& defines,
3989
                                   std::string& definesString,
3990
                                   std::string const& lang)
3991
0
{
3992
  // Lookup the define flag for the current language.
3993
0
  std::string dflag = "-D";
3994
0
  if (!lang.empty()) {
3995
0
    cmValue df =
3996
0
      this->Makefile->GetDefinition(cmStrCat("CMAKE_", lang, "_DEFINE_FLAG"));
3997
0
    if (cmNonempty(df)) {
3998
0
      dflag = *df;
3999
0
    }
4000
0
  }
4001
0
  char const* itemSeparator = definesString.empty() ? "" : " ";
4002
0
  for (std::string const& define : defines) {
4003
    // Append the definition with proper escaping.
4004
0
    std::string def = dflag;
4005
0
    if (this->GetState()->UseWatcomWMake()) {
4006
      // The Watcom compiler does its own command line parsing instead
4007
      // of using the windows shell rules.  Definitions are one of
4008
      //   -DNAME
4009
      //   -DNAME=<cpp-token>
4010
      //   -DNAME="c-string with spaces and other characters(?@#$)"
4011
      //
4012
      // Watcom will properly parse each of these cases from the
4013
      // command line without any escapes.  However we still have to
4014
      // get the '$' and '#' characters through WMake as '$$' and
4015
      // '$#'.
4016
0
      for (char c : define) {
4017
0
        if (c == '$' || c == '#') {
4018
0
          def += '$';
4019
0
        }
4020
0
        def += c;
4021
0
      }
4022
0
    } else {
4023
      // Make the definition appear properly on the command line.  Use
4024
      // -DNAME="value" instead of -D"NAME=value" for historical reasons.
4025
0
      std::string::size_type eq = define.find('=');
4026
0
      def += define.substr(0, eq);
4027
0
      if (eq != std::string::npos) {
4028
0
        def += "=";
4029
0
        def += this->EscapeForShell(define.substr(eq + 1), true);
4030
0
      }
4031
0
    }
4032
0
    definesString += itemSeparator;
4033
0
    itemSeparator = " ";
4034
0
    definesString += def;
4035
0
  }
4036
0
}
4037
4038
void cmLocalGenerator::AppendFeatureOptions(std::string& flags,
4039
                                            std::string const& lang,
4040
                                            char const* feature)
4041
0
{
4042
0
  cmValue optionList = this->Makefile->GetDefinition(
4043
0
    cmStrCat("CMAKE_", lang, "_COMPILE_OPTIONS_", feature));
4044
0
  if (optionList) {
4045
0
    cmList options{ *optionList };
4046
0
    for (std::string const& o : options) {
4047
0
      this->AppendFlagEscape(flags, o);
4048
0
    }
4049
0
  }
4050
0
}
4051
4052
cmValue cmLocalGenerator::GetFeature(std::string const& feature,
4053
                                     std::string const& config)
4054
0
{
4055
0
  std::string featureName = feature;
4056
  // TODO: Define accumulation policy for features (prepend, append,
4057
  // replace). Currently we always replace.
4058
0
  if (!config.empty()) {
4059
0
    featureName += "_";
4060
0
    featureName += cmSystemTools::UpperCase(config);
4061
0
  }
4062
0
  cmStateSnapshot snp = this->StateSnapshot;
4063
0
  while (snp.IsValid()) {
4064
0
    if (cmValue value = snp.GetDirectory().GetProperty(featureName)) {
4065
0
      return value;
4066
0
    }
4067
0
    snp = snp.GetBuildsystemDirectoryParent();
4068
0
  }
4069
0
  return nullptr;
4070
0
}
4071
4072
std::string cmLocalGenerator::GetProjectName() const
4073
0
{
4074
0
  return this->StateSnapshot.GetProjectName();
4075
0
}
4076
4077
std::string cmLocalGenerator::ConstructComment(
4078
  cmCustomCommandGenerator const& ccg, char const* default_comment) const
4079
0
{
4080
  // Check for a comment provided with the command.
4081
0
  if (cm::optional<std::string> comment = ccg.GetComment()) {
4082
0
    return *comment;
4083
0
  }
4084
4085
  // Construct a reasonable default comment if possible.
4086
0
  if (!ccg.GetOutputs().empty()) {
4087
0
    std::string comment;
4088
0
    comment = "Generating ";
4089
0
    char const* sep = "";
4090
0
    for (std::string const& o : ccg.GetOutputs()) {
4091
0
      comment += sep;
4092
0
      comment += this->MaybeRelativeToCurBinDir(o);
4093
0
      sep = ", ";
4094
0
    }
4095
0
    return comment;
4096
0
  }
4097
4098
  // Otherwise use the provided default.
4099
0
  return default_comment;
4100
0
}
4101
4102
class cmInstallTargetGeneratorLocal : public cmInstallTargetGenerator
4103
{
4104
public:
4105
  cmInstallTargetGeneratorLocal(cmLocalGenerator* lg, std::string const& t,
4106
                                std::string const& dest, bool implib)
4107
0
    : cmInstallTargetGenerator(
4108
0
        t, dest, implib, "", std::vector<std::string>(), "Unspecified",
4109
0
        cmInstallGenerator::SelectMessageLevel(lg->GetMakefile()), false,
4110
0
        false)
4111
0
  {
4112
0
    this->Compute(lg);
4113
0
  }
4114
};
4115
4116
void cmLocalGenerator::GenerateTargetInstallRules(
4117
  std::ostream& os, std::string const& config,
4118
  std::vector<std::string> const& configurationTypes)
4119
0
{
4120
  // Convert the old-style install specification from each target to
4121
  // an install generator and run it.
4122
0
  auto const& tgts = this->GetGeneratorTargets();
4123
0
  for (auto const& l : tgts) {
4124
0
    if (l->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
4125
0
      continue;
4126
0
    }
4127
4128
    // Include the user-specified pre-install script for this target.
4129
0
    if (cmValue preinstall = l->GetProperty("PRE_INSTALL_SCRIPT")) {
4130
0
      cmInstallScriptGenerator g(*preinstall, false, "", false, false);
4131
0
      g.Generate(os, config, configurationTypes);
4132
0
    }
4133
4134
    // Install this target if a destination is given.
4135
0
    if (!l->Target->GetInstallPath().empty()) {
4136
      // Compute the full install destination.  Note that converting
4137
      // to unix slashes also removes any trailing slash.
4138
      // We also skip over the leading slash given by the user.
4139
0
      std::string destination = l->Target->GetInstallPath().substr(1);
4140
0
      cmSystemTools::ConvertToUnixSlashes(destination);
4141
0
      if (destination.empty()) {
4142
0
        destination = ".";
4143
0
      }
4144
4145
      // Generate the proper install generator for this target type.
4146
0
      switch (l->GetType()) {
4147
0
        case cmStateEnums::EXECUTABLE:
4148
0
        case cmStateEnums::STATIC_LIBRARY:
4149
0
        case cmStateEnums::MODULE_LIBRARY: {
4150
          // Use a target install generator.
4151
0
          cmInstallTargetGeneratorLocal g(this, l->GetName(), destination,
4152
0
                                          false);
4153
0
          g.Generate(os, config, configurationTypes);
4154
0
        } break;
4155
0
        case cmStateEnums::SHARED_LIBRARY: {
4156
#if defined(_WIN32) || defined(__CYGWIN__)
4157
          // Special code to handle DLL.  Install the import library
4158
          // to the normal destination and the DLL to the runtime
4159
          // destination.
4160
          cmInstallTargetGeneratorLocal g1(this, l->GetName(), destination,
4161
                                           true);
4162
          g1.Generate(os, config, configurationTypes);
4163
          // We also skip over the leading slash given by the user.
4164
          destination = l->Target->GetRuntimeInstallPath().substr(1);
4165
          cmSystemTools::ConvertToUnixSlashes(destination);
4166
          cmInstallTargetGeneratorLocal g2(this, l->GetName(), destination,
4167
                                           false);
4168
          g2.Generate(os, config, configurationTypes);
4169
#else
4170
          // Use a target install generator.
4171
0
          cmInstallTargetGeneratorLocal g(this, l->GetName(), destination,
4172
0
                                          false);
4173
0
          g.Generate(os, config, configurationTypes);
4174
0
#endif
4175
0
        } break;
4176
0
        default:
4177
0
          break;
4178
0
      }
4179
0
    }
4180
4181
    // Include the user-specified post-install script for this target.
4182
0
    if (cmValue postinstall = l->GetProperty("POST_INSTALL_SCRIPT")) {
4183
0
      cmInstallScriptGenerator g(*postinstall, false, "", false, false);
4184
0
      g.Generate(os, config, configurationTypes);
4185
0
    }
4186
0
  }
4187
0
}
4188
4189
namespace {
4190
bool cmLocalGeneratorShortenObjectName(std::string& objName,
4191
                                       std::string::size_type max_len)
4192
0
{
4193
  // Check if the path can be shortened using an md5 sum replacement for
4194
  // a portion of the path.
4195
0
  std::string::size_type md5Len = 32;
4196
0
  std::string::size_type numExtraChars = objName.size() - max_len + md5Len;
4197
0
  std::string::size_type pos = objName.find('/', numExtraChars);
4198
0
  if (pos == std::string::npos) {
4199
0
    pos = objName.rfind('/', numExtraChars);
4200
0
    if (pos == std::string::npos || pos <= md5Len) {
4201
0
      return false;
4202
0
    }
4203
0
  }
4204
4205
  // Replace the beginning of the path portion of the object name with
4206
  // its own md5 sum.
4207
0
  cmCryptoHash md5(cmCryptoHash::AlgoMD5);
4208
0
  std::string md5name = cmStrCat(md5.HashString(objName.substr(0, pos)),
4209
0
                                 cm::string_view(objName).substr(pos));
4210
0
  objName = md5name;
4211
4212
  // The object name is now shorter, check if it is short enough.
4213
0
  return pos >= numExtraChars;
4214
0
}
4215
4216
bool cmLocalGeneratorCheckObjectName(std::string& objName,
4217
                                     std::string::size_type dir_len,
4218
                                     std::string::size_type max_total_len)
4219
0
{
4220
  // Enforce the maximum file name length if possible.
4221
0
  std::string::size_type max_obj_len = max_total_len;
4222
0
  if (dir_len < max_total_len) {
4223
0
    max_obj_len = max_total_len - dir_len;
4224
0
    if (objName.size() > max_obj_len) {
4225
      // The current object file name is too long.  Try to shorten it.
4226
0
      return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
4227
0
    }
4228
    // The object file name is short enough.
4229
0
    return true;
4230
0
  }
4231
  // The build directory in which the object will be stored is
4232
  // already too deep.
4233
0
  return false;
4234
0
}
4235
}
4236
4237
std::string cmLocalGenerator::CreateSafeObjectFileName(
4238
  std::string const& sin) const
4239
0
{
4240
  // Start with the original name.
4241
0
  std::string ssin = sin;
4242
4243
  // Avoid full paths by removing leading slashes.
4244
0
  ssin.erase(0, ssin.find_first_not_of('/'));
4245
4246
  // Avoid full paths by removing colons.
4247
0
  std::replace(ssin.begin(), ssin.end(), ':', '_');
4248
4249
  // Avoid relative paths that go up the tree.
4250
0
  cmSystemTools::ReplaceString(ssin, "../", "__/");
4251
4252
  // Avoid spaces.
4253
0
  std::replace(ssin.begin(), ssin.end(), ' ', '_');
4254
4255
0
  return ssin;
4256
0
}
4257
4258
void cmLocalGenerator::ComputeSourceGroupSearchIndex()
4259
0
{
4260
0
#if !defined(CMAKE_BOOTSTRAP)
4261
0
  SourceGroupVector const& sourceGroups = this->Makefile->GetSourceGroups();
4262
4263
  // Build lookup index from sources to source groups
4264
0
  std::queue<cmSourceGroup*> sgToVisit;
4265
0
  for (auto const& group : sourceGroups) {
4266
0
    cmSourceGroup* cmSourceGroup = group.get();
4267
0
    sgToVisit.emplace(cmSourceGroup);
4268
0
  }
4269
4270
0
  while (!sgToVisit.empty()) {
4271
0
    cmSourceGroup* sourceGroup = sgToVisit.front();
4272
0
    sgToVisit.pop();
4273
0
    for (auto const& sgChild : sourceGroup->GetGroupChildren()) {
4274
0
      sgToVisit.emplace(sgChild.get());
4275
0
    }
4276
0
    for (std::string const& source : sourceGroup->GetGroupFiles()) {
4277
0
      this->SourceGroupSearchIndex.emplace(source, sourceGroup);
4278
0
    }
4279
0
  }
4280
0
#endif
4281
0
}
4282
4283
cmSourceGroup* cmLocalGenerator::FindSourceGroup(std::string const& source)
4284
0
{
4285
0
#if !defined(CMAKE_BOOTSTRAP)
4286
0
  auto const indexIt = SourceGroupSearchIndex.find(source);
4287
0
  if (indexIt != SourceGroupSearchIndex.cend()) {
4288
0
    if (cmSourceGroup* result = indexIt->second) {
4289
0
      return result;
4290
0
    }
4291
0
  }
4292
4293
0
  cmSourceGroup* sourceGroup =
4294
0
    cmSourceGroup::FindSourceGroup(source, this->Makefile->GetSourceGroups());
4295
0
  if (sourceGroup) {
4296
    // Update index if we have a miss
4297
0
    SourceGroupSearchIndex.emplace(source, sourceGroup);
4298
0
  }
4299
0
  return sourceGroup;
4300
#else
4301
  static_cast<void>(source);
4302
  return nullptr;
4303
#endif
4304
0
}
4305
4306
std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
4307
  std::string const& sin, std::string const& dir_max)
4308
0
{
4309
  // Look for an existing mapped name for this object file.
4310
0
  auto it = this->UniqueObjectNamesMap.find(sin);
4311
4312
  // If no entry exists create one.
4313
0
  if (it == this->UniqueObjectNamesMap.end()) {
4314
0
    auto ssin = this->CreateSafeObjectFileName(sin);
4315
4316
    // Mangle the name if necessary.
4317
0
    if (this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES")) {
4318
0
      bool done;
4319
0
      int cc = 0;
4320
0
      char rpstr[100];
4321
0
      snprintf(rpstr, sizeof(rpstr), "_p_");
4322
0
      cmSystemTools::ReplaceString(ssin, "+", rpstr);
4323
0
      std::string sssin = sin;
4324
0
      do {
4325
0
        done = true;
4326
0
        for (it = this->UniqueObjectNamesMap.begin();
4327
0
             it != this->UniqueObjectNamesMap.end(); ++it) {
4328
0
          if (it->second == ssin) {
4329
0
            done = false;
4330
0
          }
4331
0
        }
4332
0
        if (done) {
4333
0
          break;
4334
0
        }
4335
0
        sssin = ssin;
4336
0
        cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
4337
0
        snprintf(rpstr, sizeof(rpstr), "_p%d_", cc++);
4338
0
      } while (!done);
4339
0
    }
4340
4341
0
    if (!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
4342
0
                                         this->ObjectPathMax)) {
4343
      // Warn if this is the first time the path has been seen.
4344
0
      if (this->ObjectMaxPathViolations.insert(dir_max).second) {
4345
0
        std::ostringstream m;
4346
        /* clang-format off */
4347
0
        m << "The object file directory\n"
4348
0
          << "  " << dir_max << "\n"
4349
0
          << "has " << dir_max.size() << " characters.  "
4350
0
          << "The maximum full path to an object file is "
4351
0
          << this->ObjectPathMax << " characters "
4352
0
          << "(see CMAKE_OBJECT_PATH_MAX).  "
4353
0
          << "Object file\n"
4354
0
          << "  " << ssin << "\n"
4355
0
          << "cannot be safely placed under this directory.  "
4356
0
          << "The build may not work correctly.";
4357
        /* clang-format on */
4358
0
        this->IssueMessage(MessageType::WARNING, m.str());
4359
0
      }
4360
0
    }
4361
4362
    // Insert the newly mapped object file name.
4363
0
    std::map<std::string, std::string>::value_type e(sin, ssin);
4364
0
    it = this->UniqueObjectNamesMap.insert(e).first;
4365
0
  }
4366
4367
  // Return the map entry.
4368
0
  return it->second;
4369
0
}
4370
4371
void cmLocalGenerator::ComputeObjectFilenames(
4372
  std::map<cmSourceFile const*, cmObjectLocations>& /*unused*/,
4373
  std::string const& /*unused*/, cmGeneratorTarget const* /*unused*/)
4374
0
{
4375
0
}
4376
4377
bool cmLocalGenerator::IsWindowsShell() const
4378
0
{
4379
0
  return this->GetState()->UseWindowsShell();
4380
0
}
4381
4382
bool cmLocalGenerator::IsWatcomWMake() const
4383
0
{
4384
0
  return this->GetState()->UseWatcomWMake();
4385
0
}
4386
4387
bool cmLocalGenerator::IsMinGWMake() const
4388
0
{
4389
0
  return this->GetState()->UseMinGWMake();
4390
0
}
4391
4392
bool cmLocalGenerator::IsNMake() const
4393
0
{
4394
0
  return this->GetState()->UseNMake();
4395
0
}
4396
4397
bool cmLocalGenerator::IsNinjaMulti() const
4398
0
{
4399
0
  return this->GetState()->UseNinjaMulti();
4400
0
}
4401
4402
bool cmLocalGenerator::IsWindowsVSIDE() const
4403
0
{
4404
0
  return this->GetState()->UseWindowsVSIDE();
4405
0
}
4406
4407
namespace {
4408
std::string relativeIfUnder(std::string const& top, std::string const& cur,
4409
                            std::string const& path)
4410
0
{
4411
  // Use a path relative to 'cur' if it can be expressed without
4412
  // a `../` sequence that leaves 'top'.
4413
0
  if (cmSystemTools::IsSubDirectory(path, cur) ||
4414
0
      (cmSystemTools::IsSubDirectory(cur, top) &&
4415
0
       cmSystemTools::IsSubDirectory(path, top))) {
4416
0
    return cmSystemTools::ForceToRelativePath(cur, path);
4417
0
  }
4418
0
  return path;
4419
0
}
4420
}
4421
4422
std::string cmLocalGenerator::GetRelativeSourceFileName(
4423
  cmSourceFile const& source) const
4424
0
{
4425
  // Construct the object file name using the full path to the source
4426
  // file which is its only unique identification.
4427
0
  std::string const& fullPath = source.GetFullPath();
4428
4429
  // Try referencing the source relative to the source tree.
4430
0
  std::string relFromSource = relativeIfUnder(
4431
0
    this->GetSourceDirectory(), this->GetCurrentSourceDirectory(), fullPath);
4432
0
  assert(!relFromSource.empty());
4433
0
  bool relSource = !cmSystemTools::FileIsFullPath(relFromSource);
4434
0
  bool subSource = relSource && relFromSource[0] != '.';
4435
4436
  // Try referencing the source relative to the binary tree.
4437
0
  std::string relFromBinary = relativeIfUnder(
4438
0
    this->GetBinaryDirectory(), this->GetCurrentBinaryDirectory(), fullPath);
4439
0
  assert(!relFromBinary.empty());
4440
0
  bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary);
4441
0
  bool subBinary = relBinary && relFromBinary[0] != '.';
4442
4443
  // Select a nice-looking reference to the source file to construct
4444
  // the object file name.
4445
0
  std::string objectName;
4446
  // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
4447
  // NOLINTNEXTLINE(bugprone-branch-clone)
4448
0
  if ((relSource && !relBinary) || (subSource && !subBinary)) {
4449
0
    objectName = relFromSource;
4450
0
  } else if ((relBinary && !relSource) || (subBinary && !subSource) ||
4451
0
             relFromBinary.length() < relFromSource.length()) {
4452
0
    objectName = relFromBinary;
4453
0
  } else {
4454
0
    objectName = relFromSource;
4455
0
  }
4456
0
  return objectName;
4457
0
}
4458
4459
std::string cmLocalGenerator::GetCustomObjectFileName(
4460
  cmSourceFile const& source) const
4461
0
{
4462
0
  if (!this->GetGlobalGenerator()->SupportsCustomObjectNames()) {
4463
0
    return std::string{};
4464
0
  }
4465
4466
0
  if (auto objName = source.GetProperty("OBJECT_NAME")) {
4467
0
    cmGeneratorExpression ge(*this->GetCMakeInstance());
4468
0
    auto cge = ge.Parse(objName);
4469
0
    static std::string const INVALID_GENEX =
4470
0
      "_cmake_invalid_object_name_genex";
4471
0
    static std::string const INVALID_VALUE =
4472
0
      "_cmake_invalid_object_name_value";
4473
4474
0
    if (!cge) {
4475
0
      this->Makefile->IssueMessage(
4476
0
        MessageType::FATAL_ERROR,
4477
0
        cmStrCat("The  \"OBJECT_NAME\" property for\n  ", source.GetFullPath(),
4478
0
                 "\nis not a valid generator expression (", objName, ")."));
4479
0
      return INVALID_GENEX;
4480
0
    }
4481
0
    if (cge->GetHadHeadSensitiveCondition()) {
4482
      // Not reachable; all target-sensitive  genexes actually fail to parse.
4483
0
      this->Makefile->IssueMessage(
4484
0
        MessageType::FATAL_ERROR,
4485
0
        cmStrCat("The  \"OBJECT_NAME\" property for\n  ", source.GetFullPath(),
4486
0
                 "\ncontains a condition that queries the consuming target "
4487
0
                 "which is not supported (",
4488
0
                 objName, ")."));
4489
0
      return INVALID_GENEX;
4490
0
    }
4491
0
    if (cge->GetHadLinkLanguageSensitiveCondition()) {
4492
      // Not reachable; all target-sensitive  genexes actually fail to parse.
4493
0
      this->Makefile->IssueMessage(
4494
0
        MessageType::FATAL_ERROR,
4495
0
        cmStrCat("The  \"OBJECT_NAME\" property for\n  ", source.GetFullPath(),
4496
0
                 "\ncontains a condition that queries the link language "
4497
0
                 "which is not supported (",
4498
0
                 objName, ")."));
4499
0
      return INVALID_GENEX;
4500
0
    }
4501
4502
0
    auto objNameValue = cge->Evaluate(this, "");
4503
0
    if (cge->GetHadContextSensitiveCondition()) {
4504
0
      this->Makefile->IssueMessage(
4505
0
        MessageType::FATAL_ERROR,
4506
0
        cmStrCat("The  \"OBJECT_NAME\" property for\n  ", source.GetFullPath(),
4507
0
                 "\ncontains a context-sensitive condition which is not "
4508
0
                 "supported (",
4509
0
                 objName, ")."));
4510
0
      return INVALID_GENEX;
4511
0
    }
4512
4513
    // Skip if it evaluates to empty.
4514
0
    if (!objNameValue.empty()) {
4515
0
      cmCMakePath objNamePath = objNameValue;
4516
      // Verify that it is a relative path.
4517
0
      if (objNamePath.IsAbsolute()) {
4518
0
        this->Makefile->IssueMessage(
4519
0
          MessageType::FATAL_ERROR,
4520
0
          cmStrCat(
4521
0
            "The  \"OBJECT_NAME\" property for\n  ", source.GetFullPath(),
4522
0
            "\nresolves to an absolute path which is not supported:\n  ",
4523
0
            objNameValue));
4524
0
        return INVALID_VALUE;
4525
0
      }
4526
0
      auto isInvalidComponent = [](cmCMakePath const& component) -> bool {
4527
0
        return component == ".."_s;
4528
0
      };
4529
      // Verify that it contains no `..` components.
4530
0
      if (std::any_of(objNamePath.begin(), objNamePath.end(),
4531
0
                      isInvalidComponent)) {
4532
0
        this->Makefile->IssueMessage(
4533
0
          MessageType::FATAL_ERROR,
4534
0
          cmStrCat("The  \"OBJECT_NAME\" property for\n  ",
4535
0
                   source.GetFullPath(), "\ncontains an invalid component (",
4536
0
                   objNameValue, ")."));
4537
0
        return INVALID_VALUE;
4538
0
      }
4539
4540
0
      return objNameValue;
4541
0
    }
4542
0
  }
4543
4544
0
  return std::string{};
4545
0
}
4546
4547
std::string cmLocalGenerator::GetCustomInstallObjectFileName(
4548
  cmSourceFile const& source, std::string const& config,
4549
  char const* custom_ext) const
4550
0
{
4551
0
  if (auto objName = source.GetProperty("INSTALL_OBJECT_NAME")) {
4552
0
    cmGeneratorExpression ge(*this->GetCMakeInstance());
4553
0
    auto cge = ge.Parse(objName);
4554
0
    static std::string const INVALID_GENEX =
4555
0
      "_cmake_invalid_object_name_genex";
4556
0
    static std::string const INVALID_VALUE =
4557
0
      "_cmake_invalid_object_name_value";
4558
4559
0
    if (!cge) {
4560
0
      this->Makefile->IssueMessage(
4561
0
        MessageType::FATAL_ERROR,
4562
0
        cmStrCat("The  \"INSTALL_OBJECT_NAME\" property for\n  ",
4563
0
                 source.GetFullPath(),
4564
0
                 "\nis not a valid generator expression (", objName, ")."));
4565
0
      return INVALID_GENEX;
4566
0
    }
4567
0
    if (cge->GetHadHeadSensitiveCondition()) {
4568
      // Not reachable; all target-sensitive  genexes actually fail to parse.
4569
0
      this->Makefile->IssueMessage(
4570
0
        MessageType::FATAL_ERROR,
4571
0
        cmStrCat("The  \"INSTALL_OBJECT_NAME\" property for\n  ",
4572
0
                 source.GetFullPath(),
4573
0
                 "\ncontains a condition that queries the consuming target "
4574
0
                 "which is not supported (",
4575
0
                 objName, ")."));
4576
0
      return INVALID_GENEX;
4577
0
    }
4578
0
    if (cge->GetHadLinkLanguageSensitiveCondition()) {
4579
      // Not reachable; all target-sensitive  genexes actually fail to parse.
4580
0
      this->Makefile->IssueMessage(
4581
0
        MessageType::FATAL_ERROR,
4582
0
        cmStrCat("The  \"INSTALL_OBJECT_NAME\" property for\n  ",
4583
0
                 source.GetFullPath(),
4584
0
                 "\ncontains a condition that queries the link language "
4585
0
                 "which is not supported (",
4586
0
                 objName, ")."));
4587
0
      return INVALID_GENEX;
4588
0
    }
4589
4590
0
    auto objNameValue = cge->Evaluate(this, config);
4591
4592
    // Skip if it evaluates to empty.
4593
0
    if (!objNameValue.empty()) {
4594
0
      cmCMakePath objNamePath = objNameValue;
4595
      // Verify that it is a relative path.
4596
0
      if (objNamePath.IsAbsolute()) {
4597
0
        this->Makefile->IssueMessage(
4598
0
          MessageType::FATAL_ERROR,
4599
0
          cmStrCat(
4600
0
            "The  \"INSTALL_OBJECT_NAME\" property for\n  ",
4601
0
            source.GetFullPath(),
4602
0
            "\nresolves to an absolute path which is not supported:\n  ",
4603
0
            objNameValue));
4604
0
        return INVALID_VALUE;
4605
0
      }
4606
0
      auto isInvalidComponent = [](cmCMakePath const& component) -> bool {
4607
0
        return component == ".."_s;
4608
0
      };
4609
      // Verify that it contains no `..` components.
4610
0
      if (std::any_of(objNamePath.begin(), objNamePath.end(),
4611
0
                      isInvalidComponent)) {
4612
0
        this->Makefile->IssueMessage(
4613
0
          MessageType::FATAL_ERROR,
4614
0
          cmStrCat("The  \"INSTALL_OBJECT_NAME\" property for\n  ",
4615
0
                   source.GetFullPath(), "\ncontains an invalid component (",
4616
0
                   objNameValue, ")."));
4617
0
        return INVALID_VALUE;
4618
0
      }
4619
4620
0
      if (custom_ext) {
4621
0
        objNameValue += custom_ext;
4622
0
      } else {
4623
0
        objNameValue +=
4624
0
          this->GetGlobalGenerator()->GetLanguageOutputExtension(source);
4625
0
      }
4626
4627
0
      return objNameValue;
4628
0
    }
4629
0
  }
4630
4631
0
  return std::string{};
4632
0
}
4633
4634
void cmLocalGenerator::FillCustomInstallObjectLocations(
4635
  cmSourceFile const& source, std::string const& config,
4636
  char const* custom_ext,
4637
  std::map<std::string, cmObjectLocation>& mapping) const
4638
0
{
4639
0
  auto installLoc =
4640
0
    this->GetCustomInstallObjectFileName(source, config, custom_ext);
4641
0
  if (!installLoc.empty()) {
4642
0
    mapping[config] = installLoc;
4643
0
  }
4644
0
}
4645
4646
std::string cmLocalGenerator::GetObjectFileNameWithoutTarget(
4647
  cmSourceFile const& source, std::string const& dir_max,
4648
  bool* hasSourceExtension, char const* customOutputExtension,
4649
  bool const* forceShortObjectName)
4650
0
{
4651
0
  bool useShortObjectNames = this->UseShortObjectNames();
4652
0
  if (forceShortObjectName) {
4653
0
    useShortObjectNames = *forceShortObjectName;
4654
0
  }
4655
4656
0
  if (!useShortObjectNames &&
4657
0
      this->GetGlobalGenerator()->SupportsCustomObjectNames()) {
4658
0
    auto customName = this->GetCustomObjectFileName(source);
4659
0
    if (!customName.empty()) {
4660
0
      auto ext = this->GlobalGenerator->GetLanguageOutputExtension(source);
4661
0
      if (customOutputExtension) {
4662
0
        ext = *customOutputExtension;
4663
0
      }
4664
0
      return cmStrCat(customName, ext);
4665
0
    }
4666
0
  }
4667
4668
  // This can return an absolute path in the case where source is
4669
  // not relative to the current source or binary directories
4670
0
  std::string objectName = this->GetRelativeSourceFileName(source);
4671
  // if it is still a full path check for the try compile case
4672
  // try compile never have in source sources, and should not
4673
  // have conflicting source file names in the same target
4674
0
  if (cmSystemTools::FileIsFullPath(objectName)) {
4675
0
    if (this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile()) {
4676
0
      objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
4677
0
    }
4678
0
  }
4679
0
  bool const isPchObject = source.IsPchHeader() || source.IsPchSource();
4680
4681
  // Short object path policy selected, use as little info as necessary to
4682
  // select an object name
4683
0
  bool keptSourceExtension = true;
4684
0
  if (useShortObjectNames) {
4685
0
    objectName = this->GetShortObjectFileName(source);
4686
0
    keptSourceExtension = false;
4687
0
  }
4688
4689
  // Ensure that for the CMakeFiles/<target>.dir/generated_source_file
4690
  // we don't end up having:
4691
  // CMakeFiles/<target>.dir/CMakeFiles/<target>.dir/generated_source_file.obj
4692
0
  cmValue unitySourceFile = source.GetProperty("UNITY_SOURCE_FILE");
4693
0
  cmValue pchExtension = source.GetProperty("PCH_EXTENSION");
4694
0
  if (unitySourceFile || pchExtension || isPchObject) {
4695
0
    if (pchExtension) {
4696
0
      customOutputExtension = pchExtension->c_str();
4697
0
    }
4698
4699
0
    cmsys::RegularExpression var("(CMakeFiles/[^/]+.dir/)");
4700
0
    if (var.find(objectName)) {
4701
0
      objectName.erase(var.start(), var.end() - var.start());
4702
0
    }
4703
0
  }
4704
4705
  // Replace the original source file extension with the object file
4706
  // extension.
4707
0
  if (!source.GetPropertyAsBool("KEEP_EXTENSION")) {
4708
    // Decide whether this language wants to replace the source
4709
    // extension with the object extension.
4710
0
    bool replaceExt = false;
4711
0
    std::string lang = source.GetLanguage();
4712
0
    if (!lang.empty()) {
4713
0
      replaceExt = this->Makefile->IsOn(
4714
0
        cmStrCat("CMAKE_", lang, "_OUTPUT_EXTENSION_REPLACE"));
4715
0
    }
4716
4717
    // Remove the source extension if it is to be replaced.
4718
0
    if (replaceExt || customOutputExtension) {
4719
0
      keptSourceExtension = false;
4720
0
      std::string::size_type dot_pos = objectName.rfind('.');
4721
0
      if (dot_pos != std::string::npos) {
4722
0
        objectName = objectName.substr(0, dot_pos);
4723
0
      }
4724
0
    }
4725
4726
    // Strip source file extension when shortening object file paths
4727
0
    if (useShortObjectNames) {
4728
0
      objectName = cmSystemTools::GetFilenameWithoutExtension(objectName);
4729
0
    }
4730
    // Store the new extension.
4731
0
    if (customOutputExtension) {
4732
0
      objectName += customOutputExtension;
4733
0
    } else {
4734
0
      objectName += this->GlobalGenerator->GetLanguageOutputExtension(source);
4735
0
    }
4736
0
  }
4737
0
  if (hasSourceExtension) {
4738
0
    *hasSourceExtension = keptSourceExtension;
4739
0
  }
4740
4741
0
  if (source.GetLanguage() == "Rust") {
4742
0
    cmValue const rustEmit = source.GetRustEmitProperty();
4743
    // Rust requires any rlib to start with lib prefix on all platforms to
4744
    // allow linking to them as crate. So we enforce having lib prefix for rust
4745
    // "object" files.
4746
0
    if (rustEmit == "link") {
4747
0
      cmCMakePath objectPath(objectName);
4748
0
      std::string const objectFileName =
4749
0
        "lib" + objectPath.GetFileName().String();
4750
0
      objectPath.ReplaceFileName(objectFileName);
4751
0
      objectName = objectPath.String();
4752
0
    }
4753
0
  }
4754
4755
  // Convert to a safe name.
4756
0
  return this->CreateSafeUniqueObjectFileName(objectName, dir_max);
4757
0
}
4758
4759
bool cmLocalGenerator::UseShortObjectNames(
4760
  cmStateEnums::IntermediateDirKind kind) const
4761
0
{
4762
0
  return this->GlobalGenerator->UseShortObjectNames(kind);
4763
0
}
4764
4765
std::string cmLocalGenerator::GetObjectOutputRoot(
4766
  cmStateEnums::IntermediateDirKind kind) const
4767
0
{
4768
0
  if (this->UseShortObjectNames(kind)) {
4769
0
    return cmStrCat(this->GetCurrentBinaryDirectory(), '/',
4770
0
                    this->GlobalGenerator->GetShortBinaryOutputDir());
4771
0
  }
4772
0
  return cmStrCat(this->GetCurrentBinaryDirectory(), "/CMakeFiles");
4773
0
}
4774
4775
bool cmLocalGenerator::AlwaysUsesCMFPaths() const
4776
0
{
4777
0
  return true;
4778
0
}
4779
4780
std::string cmLocalGenerator::GetShortObjectFileName(
4781
  cmSourceFile const& source) const
4782
0
{
4783
0
  std::string objectName = this->GetRelativeSourceFileName(source);
4784
0
  cmCryptoHash objNameHasher(cmCryptoHash::AlgoSHA3_512);
4785
0
  std::string terseObjectName =
4786
0
    objNameHasher.HashString(objectName).substr(0, 8);
4787
0
  return terseObjectName;
4788
0
}
4789
4790
std::string cmLocalGenerator::ComputeShortTargetDirectory(
4791
  cmGeneratorTarget const* target) const
4792
0
{
4793
0
  auto const& tgtName = target->GetName();
4794
0
  return this->GetGlobalGenerator()->ComputeTargetShortName(
4795
0
    this->GetCurrentBinaryDirectory(), tgtName);
4796
0
}
4797
4798
std::string cmLocalGenerator::GetSourceFileLanguage(cmSourceFile const& source)
4799
0
{
4800
0
  return source.GetLanguage();
4801
0
}
4802
4803
cmake* cmLocalGenerator::GetCMakeInstance() const
4804
0
{
4805
0
  return this->GlobalGenerator->GetCMakeInstance();
4806
0
}
4807
4808
std::string const& cmLocalGenerator::GetSourceDirectory() const
4809
0
{
4810
0
  return this->GetCMakeInstance()->GetHomeDirectory();
4811
0
}
4812
4813
std::string const& cmLocalGenerator::GetBinaryDirectory() const
4814
0
{
4815
0
  return this->GetCMakeInstance()->GetHomeOutputDirectory();
4816
0
}
4817
4818
std::string const& cmLocalGenerator::GetCurrentBinaryDirectory() const
4819
0
{
4820
0
  return this->StateSnapshot.GetDirectory().GetCurrentBinary();
4821
0
}
4822
4823
std::string const& cmLocalGenerator::GetCurrentSourceDirectory() const
4824
0
{
4825
0
  return this->StateSnapshot.GetDirectory().GetCurrentSource();
4826
0
}
4827
4828
std::string cmLocalGenerator::GetTargetDirectory(
4829
  cmGeneratorTarget const* /*unused*/,
4830
  cmStateEnums::IntermediateDirKind /*kind*/) const
4831
0
{
4832
0
  cmSystemTools::Error("GetTargetDirectory"
4833
0
                       " called on cmLocalGenerator");
4834
0
  return "";
4835
0
}
4836
4837
cmPolicies::PolicyStatus cmLocalGenerator::GetPolicyStatus(
4838
  cmPolicies::PolicyID id) const
4839
0
{
4840
0
  return this->Makefile->GetPolicyStatus(id);
4841
0
}
4842
4843
bool cmLocalGenerator::CheckDefinition(std::string const& define) const
4844
0
{
4845
  // Many compilers do not support -DNAME(arg)=sdf so we disable it.
4846
0
  std::string::size_type pos = define.find_first_of("(=");
4847
0
  if (pos != std::string::npos) {
4848
0
    if (define[pos] == '(') {
4849
0
      std::ostringstream e;
4850
      /* clang-format off */
4851
0
      e << "WARNING: Function-style preprocessor definitions may not be "
4852
0
           "passed on the compiler command line because many compilers "
4853
0
           "do not support it.\n"
4854
0
           "CMake is dropping a preprocessor definition: " << define << "\n"
4855
0
           "Consider defining the macro in a (configured) header file.\n";
4856
      /* clang-format on */
4857
0
      cmSystemTools::Message(e.str());
4858
0
      return false;
4859
0
    }
4860
0
  }
4861
4862
  // Many compilers do not support # in the value so we disable it.
4863
0
  if (define.find_first_of('#') != std::string::npos) {
4864
0
    std::ostringstream e;
4865
    /* clang-format off */
4866
0
    e << "WARNING: Preprocessor definitions containing '#' may not be "
4867
0
         "passed on the compiler command line because many compilers "
4868
0
         "do not support it.\n"
4869
0
         "CMake is dropping a preprocessor definition: " << define << "\n"
4870
0
         "Consider defining the macro in a (configured) header file.\n";
4871
    /* clang-format on */
4872
0
    cmSystemTools::Message(e.str());
4873
0
    return false;
4874
0
  }
4875
4876
  // Assume it is supported.
4877
0
  return true;
4878
0
}
4879
4880
static void cmLGInfoProp(cmMakefile* mf, cmGeneratorTarget* target,
4881
                         std::string const& prop)
4882
0
{
4883
0
  if (cmValue val = target->GetProperty(prop)) {
4884
0
    mf->AddDefinition(prop, *val);
4885
0
  }
4886
0
}
4887
4888
void cmLocalGenerator::GenerateAppleInfoPList(cmGeneratorTarget* target,
4889
                                              std::string const& targetName,
4890
                                              std::string const& fname)
4891
0
{
4892
  // Find the Info.plist template.
4893
0
  cmValue in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
4894
0
  std::string inFile = cmNonempty(in) ? *in : "MacOSXBundleInfo.plist.in";
4895
0
  if (!cmSystemTools::FileIsFullPath(inFile)) {
4896
0
    std::string inMod = this->Makefile->GetModulesFile(inFile);
4897
0
    if (!inMod.empty()) {
4898
0
      inFile = inMod;
4899
0
    }
4900
0
  }
4901
0
  if (!cmSystemTools::FileExists(inFile, true)) {
4902
0
    std::ostringstream e;
4903
0
    e << "Target " << target->GetName() << " Info.plist template \"" << inFile
4904
0
      << "\" could not be found.";
4905
0
    cmSystemTools::Error(e.str());
4906
0
    return;
4907
0
  }
4908
4909
  // Convert target properties to variables in an isolated makefile
4910
  // scope to configure the file.  If properties are set they will
4911
  // override user make variables.  If not the configuration will fall
4912
  // back to the directory-level values set by the user.
4913
0
  cmMakefile* mf = this->Makefile;
4914
0
  cmMakefile::ScopePushPop varScope(mf);
4915
0
  mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName);
4916
0
  cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
4917
0
  cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
4918
0
  cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
4919
0
  cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
4920
0
  cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
4921
0
  cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
4922
0
  cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
4923
0
  cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
4924
0
  mf->ConfigureFile(inFile, fname, false, false, false);
4925
0
}
4926
4927
void cmLocalGenerator::GenerateFrameworkInfoPList(
4928
  cmGeneratorTarget* target, std::string const& targetName,
4929
  std::string const& fname)
4930
0
{
4931
  // Find the Info.plist template.
4932
0
  cmValue in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
4933
0
  std::string inFile = cmNonempty(in) ? *in : "MacOSXFrameworkInfo.plist.in";
4934
0
  if (!cmSystemTools::FileIsFullPath(inFile)) {
4935
0
    std::string inMod = this->Makefile->GetModulesFile(inFile);
4936
0
    if (!inMod.empty()) {
4937
0
      inFile = inMod;
4938
0
    }
4939
0
  }
4940
0
  if (!cmSystemTools::FileExists(inFile, true)) {
4941
0
    std::ostringstream e;
4942
0
    e << "Target " << target->GetName() << " Info.plist template \"" << inFile
4943
0
      << "\" could not be found.";
4944
0
    cmSystemTools::Error(e.str());
4945
0
    return;
4946
0
  }
4947
4948
  // Convert target properties to variables in an isolated makefile
4949
  // scope to configure the file.  If properties are set they will
4950
  // override user make variables.  If not the configuration will fall
4951
  // back to the directory-level values set by the user.
4952
0
  cmMakefile* mf = this->Makefile;
4953
0
  cmMakefile::ScopePushPop varScope(mf);
4954
0
  mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName);
4955
0
  cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
4956
0
  cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
4957
0
  cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
4958
0
  cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_NAME");
4959
0
  cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
4960
0
  mf->ConfigureFile(inFile, fname, false, false, false);
4961
0
}
4962
4963
namespace {
4964
cm::string_view CustomOutputRoleKeyword(cmLocalGenerator::OutputRole role)
4965
0
{
4966
0
  return (role == cmLocalGenerator::OutputRole::Primary ? "OUTPUT"_s
4967
0
                                                        : "BYPRODUCTS"_s);
4968
0
}
4969
4970
void CreateGeneratedSource(cmLocalGenerator& lg, std::string const& output,
4971
                           cmLocalGenerator::OutputRole role,
4972
                           cmCommandOrigin origin,
4973
                           cmListFileBacktrace const& lfbt)
4974
0
{
4975
0
  if (cmGeneratorExpression::Find(output) != std::string::npos) {
4976
0
    lg.GetCMakeInstance()->IssueMessage(
4977
0
      MessageType::FATAL_ERROR,
4978
0
      "Generator expressions in custom command outputs are not implemented!",
4979
0
      lfbt);
4980
0
    return;
4981
0
  }
4982
4983
  // Make sure the file will not be generated into the source
4984
  // directory during an out of source build.
4985
0
  if (!lg.GetMakefile()->CanIWriteThisFile(output)) {
4986
0
    lg.GetCMakeInstance()->IssueMessage(
4987
0
      MessageType::FATAL_ERROR,
4988
0
      cmStrCat(CustomOutputRoleKeyword(role), " path\n  ", output,
4989
0
               "\nin a source directory as an output of custom command."),
4990
0
      lfbt);
4991
0
    return;
4992
0
  }
4993
4994
  // Make sure the output file name has no invalid characters.
4995
0
  bool const hashNotAllowed = lg.GetState()->UseBorlandMake();
4996
0
  std::string::size_type pos = output.find_first_of("<>");
4997
0
  if (pos == std::string::npos && hashNotAllowed) {
4998
0
    pos = output.find_first_of('#');
4999
0
  }
5000
5001
0
  if (pos != std::string::npos) {
5002
0
    lg.GetCMakeInstance()->IssueMessage(
5003
0
      MessageType::FATAL_ERROR,
5004
0
      cmStrCat(CustomOutputRoleKeyword(role), " containing a \"", output[pos],
5005
0
               "\" is not allowed."),
5006
0
      lfbt);
5007
0
    return;
5008
0
  }
5009
5010
  // Outputs without generator expressions from the project are already
5011
  // created and marked as generated.  Do not mark them again, because
5012
  // other commands might have overwritten the property.
5013
0
  if (origin == cmCommandOrigin::Generator) {
5014
0
    lg.GetMakefile()->GetOrCreateGeneratedSource(output);
5015
0
  }
5016
0
}
5017
5018
std::string ComputeCustomCommandRuleFileName(cmLocalGenerator& lg,
5019
                                             cmListFileBacktrace const& bt,
5020
                                             std::string const& output)
5021
0
{
5022
  // If the output path has no generator expressions, use it directly.
5023
0
  if (cmGeneratorExpression::Find(output) == std::string::npos) {
5024
0
    return output;
5025
0
  }
5026
5027
  // The output path contains a generator expression, but we must choose
5028
  // a single source file path to which to attach the custom command.
5029
  // Use some heuristics to provide a nice-looking name when possible.
5030
5031
  // If the only genex is $<CONFIG>, replace that gracefully.
5032
0
  {
5033
0
    std::string simple = output;
5034
0
    cmSystemTools::ReplaceString(simple, "$<CONFIG>", "(CONFIG)");
5035
0
    if (cmGeneratorExpression::Find(simple) == std::string::npos) {
5036
0
      return simple;
5037
0
    }
5038
0
  }
5039
5040
  // If the genex evaluates to the same value in all configurations, use that.
5041
0
  {
5042
0
    std::vector<std::string> allConfigOutputs =
5043
0
      lg.ExpandCustomCommandOutputGenex(output, bt);
5044
0
    if (allConfigOutputs.size() == 1) {
5045
0
      return allConfigOutputs.front();
5046
0
    }
5047
0
  }
5048
5049
  // Fall back to a deterministic unique name.
5050
0
  cmCryptoHash h(cmCryptoHash::AlgoSHA256);
5051
0
  return cmStrCat(lg.GetCurrentBinaryDirectory(), "/CMakeFiles/",
5052
0
                  h.HashString(output).substr(0, 16));
5053
0
}
5054
5055
cmSourceFile* AddCustomCommand(cmLocalGenerator& lg, cmCommandOrigin origin,
5056
                               std::unique_ptr<cmCustomCommand> cc,
5057
                               bool replace)
5058
0
{
5059
0
  cmMakefile* mf = lg.GetMakefile();
5060
0
  auto const& lfbt = cc->GetBacktrace();
5061
0
  auto const& outputs = cc->GetOutputs();
5062
0
  auto const& byproducts = cc->GetByproducts();
5063
0
  auto const& commandLines = cc->GetCommandLines();
5064
5065
  // Choose a source file on which to store the custom command.
5066
0
  cmSourceFile* file = nullptr;
5067
0
  if (!commandLines.empty() && cc->HasMainDependency()) {
5068
0
    auto const& main_dependency = cc->GetMainDependency();
5069
    // The main dependency was specified.  Use it unless a different
5070
    // custom command already used it.
5071
0
    file = mf->GetSource(main_dependency);
5072
0
    if (file && file->GetCustomCommand() && !replace) {
5073
      // The main dependency already has a custom command.
5074
0
      if (commandLines == file->GetCustomCommand()->GetCommandLines()) {
5075
        // The existing custom command is identical.  Silently ignore
5076
        // the duplicate.
5077
0
        return file;
5078
0
      }
5079
      // The existing custom command is different.  We need to
5080
      // generate a rule file for this new command.
5081
0
      file = nullptr;
5082
0
    } else if (!file) {
5083
0
      file = mf->CreateSource(main_dependency);
5084
0
    }
5085
0
  }
5086
5087
  // Generate a rule file if the main dependency is not available.
5088
0
  if (!file) {
5089
0
    cmGlobalGenerator* gg = lg.GetGlobalGenerator();
5090
5091
    // Construct a rule file associated with the first output produced.
5092
0
    std::string outName = gg->GenerateRuleFile(
5093
0
      ComputeCustomCommandRuleFileName(lg, lfbt, outputs[0]));
5094
5095
    // Check if the rule file already exists.
5096
0
    file = mf->GetSource(outName, cmSourceFileLocationKind::Known);
5097
0
    if (file && file->GetCustomCommand() && !replace) {
5098
      // The rule file already exists.
5099
0
      if (commandLines != file->GetCustomCommand()->GetCommandLines()) {
5100
0
        lg.GetCMakeInstance()->IssueMessage(
5101
0
          MessageType::FATAL_ERROR,
5102
0
          cmStrCat("Attempt to add a custom rule to output\n  ", outName,
5103
0
                   "\nwhich already has a custom rule."),
5104
0
          lfbt);
5105
0
      }
5106
0
      return file;
5107
0
    }
5108
5109
    // Create a cmSourceFile for the rule file.
5110
0
    if (!file) {
5111
0
      file = mf->CreateSource(outName, true, cmSourceFileLocationKind::Known);
5112
0
    }
5113
0
    file->SetProperty("__CMAKE_RULE", "1");
5114
0
  }
5115
5116
  // Attach the custom command to the file.
5117
0
  if (file) {
5118
0
    cc->SetEscapeAllowMakeVars(true);
5119
5120
0
    lg.AddSourceOutputs(file, outputs, cmLocalGenerator::OutputRole::Primary,
5121
0
                        lfbt, origin);
5122
0
    lg.AddSourceOutputs(file, byproducts,
5123
0
                        cmLocalGenerator::OutputRole::Byproduct, lfbt, origin);
5124
5125
0
    file->SetCustomCommand(std::move(cc));
5126
0
  }
5127
0
  return file;
5128
0
}
5129
5130
bool AnyOutputMatches(std::string const& name,
5131
                      std::vector<std::string> const& outputs)
5132
0
{
5133
0
  return std::any_of(outputs.begin(), outputs.end(),
5134
0
                     [&name](std::string const& output) -> bool {
5135
0
                       std::string::size_type pos = output.rfind(name);
5136
                       // If the output matches exactly
5137
0
                       return (pos != std::string::npos &&
5138
0
                               pos == output.size() - name.size() &&
5139
0
                               (pos == 0 || output[pos - 1] == '/'));
5140
0
                     });
5141
0
}
5142
5143
bool AnyTargetCommandOutputMatches(
5144
  std::string const& name, std::vector<cmCustomCommand> const& commands)
5145
0
{
5146
0
  return std::any_of(commands.begin(), commands.end(),
5147
0
                     [&name](cmCustomCommand const& command) -> bool {
5148
0
                       return AnyOutputMatches(name, command.GetByproducts());
5149
0
                     });
5150
0
}
5151
}
5152
5153
namespace detail {
5154
void AddCustomCommandToTarget(cmLocalGenerator& lg, cmCommandOrigin origin,
5155
                              cmTarget* target, cmCustomCommandType type,
5156
                              std::unique_ptr<cmCustomCommand> cc)
5157
0
{
5158
  // Add the command to the appropriate build step for the target.
5159
0
  cc->SetEscapeAllowMakeVars(true);
5160
0
  cc->SetTarget(target->GetName());
5161
5162
0
  lg.AddTargetByproducts(target, cc->GetByproducts(), cc->GetBacktrace(),
5163
0
                         origin);
5164
5165
0
  switch (type) {
5166
0
    case cmCustomCommandType::PRE_BUILD:
5167
0
      target->AddPreBuildCommand(std::move(*cc));
5168
0
      break;
5169
0
    case cmCustomCommandType::PRE_LINK:
5170
0
      target->AddPreLinkCommand(std::move(*cc));
5171
0
      break;
5172
0
    case cmCustomCommandType::POST_BUILD:
5173
0
      target->AddPostBuildCommand(std::move(*cc));
5174
0
      break;
5175
0
  }
5176
5177
0
  cc.reset();
5178
0
}
5179
5180
cmSourceFile* AddCustomCommandToOutput(cmLocalGenerator& lg,
5181
                                       cmCommandOrigin origin,
5182
                                       std::unique_ptr<cmCustomCommand> cc,
5183
                                       bool replace)
5184
0
{
5185
0
  return AddCustomCommand(lg, origin, std::move(cc), replace);
5186
0
}
5187
5188
void AppendCustomCommandToOutput(cmLocalGenerator& lg,
5189
                                 cmListFileBacktrace const& lfbt,
5190
                                 std::string const& output,
5191
                                 std::vector<std::string> const& depends,
5192
                                 cmImplicitDependsList const& implicit_depends,
5193
                                 cmCustomCommandLines const& commandLines)
5194
0
{
5195
  // Lookup an existing command.
5196
0
  cmSourceFile* sf = nullptr;
5197
0
  if (cmGeneratorExpression::Find(output) == std::string::npos) {
5198
0
    sf = lg.GetSourceFileWithOutput(output);
5199
0
  } else {
5200
    // This output path has a generator expression.  Evaluate it to
5201
    // find the output for any configurations.
5202
0
    for (std::string const& out :
5203
0
         lg.ExpandCustomCommandOutputGenex(output, lfbt)) {
5204
0
      sf = lg.GetSourceFileWithOutput(out);
5205
0
      if (sf) {
5206
0
        break;
5207
0
      }
5208
0
    }
5209
0
  }
5210
5211
0
  if (sf) {
5212
0
    if (cmCustomCommand* cc = sf->GetCustomCommand()) {
5213
0
      cc->AppendCommands(commandLines);
5214
0
      cc->AppendDepends(depends);
5215
0
      if (cc->GetCodegen() && !implicit_depends.empty()) {
5216
0
        lg.GetCMakeInstance()->IssueMessage(
5217
0
          MessageType::FATAL_ERROR,
5218
0
          "Cannot append IMPLICIT_DEPENDS to existing CODEGEN custom "
5219
0
          "command.");
5220
0
      }
5221
0
      cc->AppendImplicitDepends(implicit_depends);
5222
0
      return;
5223
0
    }
5224
0
  }
5225
5226
  // No existing command found.
5227
0
  lg.GetCMakeInstance()->IssueMessage(
5228
0
    MessageType::FATAL_ERROR,
5229
0
    cmStrCat("Attempt to APPEND to custom command with output\n  ", output,
5230
0
             "\nwhich is not already a custom command output."),
5231
0
    lfbt);
5232
0
}
5233
5234
void AddUtilityCommand(cmLocalGenerator& lg, cmCommandOrigin origin,
5235
                       cmTarget* target, std::unique_ptr<cmCustomCommand> cc)
5236
0
{
5237
  // They might be moved away
5238
0
  auto byproducts = cc->GetByproducts();
5239
0
  auto lfbt = cc->GetBacktrace();
5240
5241
  // Use an empty comment to avoid generation of default comment.
5242
0
  if (!cc->GetComment()) {
5243
0
    cc->SetComment("");
5244
0
  }
5245
5246
  // Create the generated symbolic output name of the utility target.
5247
0
  std::string output =
5248
0
    lg.CreateUtilityOutput(target->GetName(), byproducts, lfbt);
5249
0
  cc->SetOutputs(output);
5250
5251
0
  cmSourceFile* rule = AddCustomCommand(lg, origin, std::move(cc),
5252
0
                                        /*replace=*/false);
5253
0
  if (rule) {
5254
0
    lg.AddTargetByproducts(target, byproducts, lfbt, origin);
5255
0
  }
5256
5257
0
  target->AddSource(output);
5258
0
}
5259
5260
std::vector<std::string> ComputeISPCObjectSuffixes(cmGeneratorTarget* target)
5261
0
{
5262
0
  cmValue const targetProperty = target->GetProperty("ISPC_INSTRUCTION_SETS");
5263
0
  cmList ispcTargets;
5264
5265
0
  if (!targetProperty.IsOff()) {
5266
0
    ispcTargets.assign(targetProperty);
5267
0
    for (auto& ispcTarget : ispcTargets) {
5268
      // transform targets into the suffixes
5269
0
      auto pos = ispcTarget.find('-');
5270
0
      auto target_suffix = ispcTarget.substr(0, pos);
5271
      // ISPC uses underscores in output file suffixes where the target name
5272
      // has dots (e.g. "avx10.2dmr" produces files with "_avx10_2dmr" suffix)
5273
0
      std::replace(target_suffix.begin(), target_suffix.end(), '.', '_');
5274
0
      if (target_suffix ==
5275
0
          "avx1") { // when targeting avx1 ISPC uses the 'avx' output string
5276
0
        target_suffix = "avx";
5277
0
      } else if (target_suffix == "sse4_1" || target_suffix == "sse4_2") {
5278
        // when targeting sse4.1 or sse4.2 ISPC uses the 'sse4' output string
5279
0
        target_suffix = "sse4";
5280
0
      }
5281
0
      ispcTarget = target_suffix;
5282
0
    }
5283
0
  }
5284
0
  return std::move(ispcTargets.data());
5285
0
}
5286
5287
std::vector<std::string> ComputeISPCExtraObjects(
5288
  std::string const& objectName, std::string const& buildDirectory,
5289
  std::vector<std::string> const& ispcSuffixes)
5290
0
{
5291
0
  auto normalizedDir = cmSystemTools::CollapseFullPath(buildDirectory);
5292
0
  std::vector<std::string> computedObjects;
5293
0
  computedObjects.reserve(ispcSuffixes.size());
5294
5295
0
  auto extension = cmSystemTools::GetFilenameLastExtensionView(objectName);
5296
5297
  // We can't use cmSystemTools::GetFilenameWithoutLastExtension as it
5298
  // drops any directories in objectName
5299
0
  auto objNameNoExt = objectName;
5300
0
  std::string::size_type dot_pos = objectName.rfind('.');
5301
0
  if (dot_pos != std::string::npos) {
5302
0
    objNameNoExt.resize(dot_pos);
5303
0
  }
5304
5305
0
  for (auto const& ispcTarget : ispcSuffixes) {
5306
0
    computedObjects.emplace_back(
5307
0
      cmStrCat(normalizedDir, '/', objNameNoExt, '_', ispcTarget, extension));
5308
0
  }
5309
5310
0
  return computedObjects;
5311
0
}
5312
}
5313
5314
cmSourcesWithOutput cmLocalGenerator::GetSourcesWithOutput(
5315
  std::string const& name) const
5316
0
{
5317
  // Linear search?  Also see GetSourceFileWithOutput for detail.
5318
0
  if (!cmSystemTools::FileIsFullPath(name)) {
5319
0
    cmSourcesWithOutput sources;
5320
0
    sources.Target = this->LinearGetTargetWithOutput(name);
5321
0
    sources.Source = this->LinearGetSourceFileWithOutput(
5322
0
      name, cmSourceOutputKind::OutputOrByproduct, sources.SourceIsByproduct);
5323
0
    return sources;
5324
0
  }
5325
  // Otherwise we use an efficient lookup map.
5326
0
  auto o = this->OutputToSource.find(name);
5327
0
  if (o != this->OutputToSource.end()) {
5328
0
    return o->second.Sources;
5329
0
  }
5330
0
  return {};
5331
0
}
5332
5333
cmSourceFile* cmLocalGenerator::GetSourceFileWithOutput(
5334
  std::string const& name, cmSourceOutputKind kind) const
5335
0
{
5336
  // If the queried path is not absolute we use the backward compatible
5337
  // linear-time search for an output with a matching suffix.
5338
0
  if (!cmSystemTools::FileIsFullPath(name)) {
5339
0
    bool byproduct = false;
5340
0
    return this->LinearGetSourceFileWithOutput(name, kind, byproduct);
5341
0
  }
5342
  // Otherwise we use an efficient lookup map.
5343
0
  auto o = this->OutputToSource.find(name);
5344
0
  if (o != this->OutputToSource.end() &&
5345
0
      (!o->second.Sources.SourceIsByproduct ||
5346
0
       kind == cmSourceOutputKind::OutputOrByproduct)) {
5347
    // Source file could also be null pointer for example if we found the
5348
    // byproduct of a utility target, a PRE_BUILD, PRE_LINK, or POST_BUILD
5349
    // command of a target, or a not yet created custom command.
5350
0
    return o->second.Sources.Source;
5351
0
  }
5352
0
  return nullptr;
5353
0
}
5354
5355
std::string cmLocalGenerator::CreateUtilityOutput(
5356
  std::string const& targetName, std::vector<std::string> const&,
5357
  cmListFileBacktrace const&)
5358
0
{
5359
0
  std::string force =
5360
0
    cmStrCat(this->GetCurrentBinaryDirectory(), "/CMakeFiles/", targetName);
5361
  // The output is not actually created so mark it symbolic.
5362
0
  if (cmSourceFile* sf = this->Makefile->GetOrCreateGeneratedSource(force)) {
5363
0
    sf->SetProperty("SYMBOLIC", "1");
5364
0
  } else {
5365
0
    cmSystemTools::Error("Could not get source file entry for " + force);
5366
0
  }
5367
0
  return force;
5368
0
}
5369
5370
std::vector<cmCustomCommandGenerator>
5371
cmLocalGenerator::MakeCustomCommandGenerators(cmCustomCommand const& cc,
5372
                                              std::string const& config)
5373
0
{
5374
0
  std::vector<cmCustomCommandGenerator> ccgs;
5375
0
  ccgs.emplace_back(cc, config, this);
5376
0
  return ccgs;
5377
0
}
5378
5379
std::vector<std::string> cmLocalGenerator::ExpandCustomCommandOutputPaths(
5380
  cmCompiledGeneratorExpression const& cge, std::string const& config)
5381
0
{
5382
0
  cmList paths{ cge.Evaluate(this, config) };
5383
0
  for (std::string& p : paths) {
5384
0
    p = cmSystemTools::CollapseFullPath(p, this->GetCurrentBinaryDirectory());
5385
0
  }
5386
0
  return std::move(paths.data());
5387
0
}
5388
5389
std::vector<std::string> cmLocalGenerator::ExpandCustomCommandOutputGenex(
5390
  std::string const& o, cmListFileBacktrace const& bt)
5391
0
{
5392
0
  std::vector<std::string> allConfigOutputs;
5393
0
  cmGeneratorExpression ge(*this->GetCMakeInstance(), bt);
5394
0
  std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(o);
5395
0
  std::vector<std::string> configs =
5396
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
5397
0
  for (std::string const& config : configs) {
5398
0
    std::vector<std::string> configOutputs =
5399
0
      this->ExpandCustomCommandOutputPaths(*cge, config);
5400
0
    allConfigOutputs.reserve(allConfigOutputs.size() + configOutputs.size());
5401
0
    std::move(configOutputs.begin(), configOutputs.end(),
5402
0
              std::back_inserter(allConfigOutputs));
5403
0
  }
5404
0
  auto endUnique =
5405
0
    cmRemoveDuplicates(allConfigOutputs.begin(), allConfigOutputs.end());
5406
0
  allConfigOutputs.erase(endUnique, allConfigOutputs.end());
5407
0
  return allConfigOutputs;
5408
0
}
5409
5410
void cmLocalGenerator::AddTargetByproducts(
5411
  cmTarget* target, std::vector<std::string> const& byproducts,
5412
  cmListFileBacktrace const& bt, cmCommandOrigin origin)
5413
0
{
5414
0
  for (std::string const& o : byproducts) {
5415
0
    if (cmGeneratorExpression::Find(o) == std::string::npos) {
5416
0
      this->UpdateOutputToSourceMap(o, target, bt, origin);
5417
0
      continue;
5418
0
    }
5419
5420
    // This byproduct path has a generator expression.  Evaluate it to
5421
    // register the byproducts for all configurations.
5422
0
    for (std::string const& b : this->ExpandCustomCommandOutputGenex(o, bt)) {
5423
0
      this->UpdateOutputToSourceMap(b, target, bt, cmCommandOrigin::Generator);
5424
0
    }
5425
0
  }
5426
0
}
5427
5428
void cmLocalGenerator::AddSourceOutputs(
5429
  cmSourceFile* source, std::vector<std::string> const& outputs,
5430
  OutputRole role, cmListFileBacktrace const& bt, cmCommandOrigin origin)
5431
0
{
5432
0
  for (std::string const& o : outputs) {
5433
0
    if (cmGeneratorExpression::Find(o) == std::string::npos) {
5434
0
      this->UpdateOutputToSourceMap(o, source, role, bt, origin);
5435
0
      continue;
5436
0
    }
5437
5438
    // This output path has a generator expression.  Evaluate it to
5439
    // register the outputs for all configurations.
5440
0
    for (std::string const& out :
5441
0
         this->ExpandCustomCommandOutputGenex(o, bt)) {
5442
0
      this->UpdateOutputToSourceMap(out, source, role, bt,
5443
0
                                    cmCommandOrigin::Generator);
5444
0
    }
5445
0
  }
5446
0
}
5447
5448
void cmLocalGenerator::UpdateOutputToSourceMap(std::string const& byproduct,
5449
                                               cmTarget* target,
5450
                                               cmListFileBacktrace const& bt,
5451
                                               cmCommandOrigin origin)
5452
0
{
5453
0
  SourceEntry entry;
5454
0
  entry.Sources.Target = target;
5455
5456
0
  auto pr = this->OutputToSource.emplace(byproduct, entry);
5457
0
  if (pr.second) {
5458
0
    CreateGeneratedSource(*this, byproduct, OutputRole::Byproduct, origin, bt);
5459
0
  } else {
5460
0
    SourceEntry& current = pr.first->second;
5461
    // Has the target already been set?
5462
0
    if (!current.Sources.Target) {
5463
0
      current.Sources.Target = target;
5464
0
    } else {
5465
      // Multiple custom commands/targets produce the same output (source file
5466
      // or target).  See also comment in other UpdateOutputToSourceMap
5467
      // overload.
5468
      //
5469
      // TODO: Warn the user about this case.
5470
0
    }
5471
0
  }
5472
0
}
5473
5474
void cmLocalGenerator::UpdateOutputToSourceMap(std::string const& output,
5475
                                               cmSourceFile* source,
5476
                                               OutputRole role,
5477
                                               cmListFileBacktrace const& bt,
5478
                                               cmCommandOrigin origin)
5479
0
{
5480
0
  SourceEntry entry;
5481
0
  entry.Sources.Source = source;
5482
0
  entry.Sources.SourceIsByproduct = role == OutputRole::Byproduct;
5483
5484
0
  auto pr = this->OutputToSource.emplace(output, entry);
5485
0
  if (pr.second) {
5486
0
    CreateGeneratedSource(*this, output, role, origin, bt);
5487
0
  } else {
5488
0
    SourceEntry& current = pr.first->second;
5489
    // Outputs take precedence over byproducts
5490
0
    if (!current.Sources.Source ||
5491
0
        (current.Sources.SourceIsByproduct && role == OutputRole::Primary)) {
5492
0
      current.Sources.Source = source;
5493
0
      current.Sources.SourceIsByproduct = false;
5494
0
    } else {
5495
      // Multiple custom commands produce the same output but may
5496
      // be attached to a different source file (MAIN_DEPENDENCY).
5497
      // LinearGetSourceFileWithOutput would return the first one,
5498
      // so keep the mapping for the first one.
5499
      //
5500
      // TODO: Warn the user about this case.  However, the VS 8 generator
5501
      // triggers it for separate generate.stamp rules in ZERO_CHECK and
5502
      // individual targets.
5503
0
    }
5504
0
  }
5505
0
}
5506
5507
cmTarget* cmLocalGenerator::LinearGetTargetWithOutput(
5508
  std::string const& name) const
5509
0
{
5510
  // We go through the ordered vector of targets to get reproducible results
5511
  // should multiple names match.
5512
0
  for (cmTarget* t : this->Makefile->GetOrderedTargets()) {
5513
    // Does the output of any command match the source file name?
5514
0
    if (AnyTargetCommandOutputMatches(name, t->GetPreBuildCommands())) {
5515
0
      return t;
5516
0
    }
5517
0
    if (AnyTargetCommandOutputMatches(name, t->GetPreLinkCommands())) {
5518
0
      return t;
5519
0
    }
5520
0
    if (AnyTargetCommandOutputMatches(name, t->GetPostBuildCommands())) {
5521
0
      return t;
5522
0
    }
5523
0
  }
5524
0
  return nullptr;
5525
0
}
5526
5527
cmSourceFile* cmLocalGenerator::LinearGetSourceFileWithOutput(
5528
  std::string const& name, cmSourceOutputKind kind, bool& byproduct) const
5529
0
{
5530
  // Outputs take precedence over byproducts.
5531
0
  byproduct = false;
5532
0
  cmSourceFile* fallback = nullptr;
5533
5534
  // Look through all the source files that have custom commands and see if the
5535
  // custom command has the passed source file as an output.
5536
0
  for (auto const& src : this->Makefile->GetSourceFiles()) {
5537
    // Does this source file have a custom command?
5538
0
    if (src->GetCustomCommand()) {
5539
      // Does the output of the custom command match the source file name?
5540
0
      if (AnyOutputMatches(name, src->GetCustomCommand()->GetOutputs())) {
5541
        // Return the first matching output.
5542
0
        return src.get();
5543
0
      }
5544
0
      if (kind == cmSourceOutputKind::OutputOrByproduct) {
5545
0
        if (AnyOutputMatches(name, src->GetCustomCommand()->GetByproducts())) {
5546
          // Do not return the source yet as there might be a matching output.
5547
0
          fallback = src.get();
5548
0
        }
5549
0
      }
5550
0
    }
5551
0
  }
5552
5553
  // Did we find a byproduct?
5554
0
  byproduct = fallback != nullptr;
5555
0
  return fallback;
5556
0
}