Coverage Report

Created: 2026-03-12 06:35

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