Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmGlobalFastbuildGenerator.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
4
#include "cmGlobalFastbuildGenerator.h"
5
6
#include <algorithm>
7
#include <cstdlib>
8
#include <initializer_list>
9
#include <iterator>
10
#include <queue>
11
#include <sstream>
12
13
#include <cm/memory>
14
15
#include "cmsys/FStream.hxx"
16
#include "cmsys/RegularExpression.hxx"
17
18
#include "cmFastbuildLinkLineComputer.h"
19
#include "cmFastbuildTargetGenerator.h" // IWYU pragma: keep
20
#include "cmGeneratedFileStream.h"
21
#include "cmGeneratorTarget.h"
22
#include "cmGlobCacheEntry.h"
23
#include "cmGlobalGenerator.h"
24
#include "cmGlobalGeneratorFactory.h"
25
#include "cmList.h"
26
#include "cmLocalFastbuildGenerator.h"
27
#include "cmLocalGenerator.h"
28
#include "cmMakefile.h"
29
#include "cmMessageType.h"
30
#include "cmState.h"
31
#include "cmStateDirectory.h"
32
#include "cmStateSnapshot.h"
33
#include "cmStringAlgorithms.h"
34
#include "cmSystemTools.h"
35
#include "cmValue.h"
36
#include "cmVersion.h"
37
#include "cmake.h"
38
39
#if defined(_WIN32)
40
#  include <future>
41
42
#  include <objbase.h>
43
#  include <shellapi.h>
44
45
#endif
46
47
0
#define FASTBUILD_REBUILD_BFF_TARGET_NAME "rebuild-bff"
48
0
#define FASTBUILD_GLOB_CHECK_TARGET "glob-check"
49
0
#define FASTBUILD_ENV_VAR_NAME "LocalEnv"
50
51
// IDE support
52
0
#define FASTBUILD_XCODE_BASE_PATH "XCode/Projects"
53
#define FASTBUILD_VS_BASE_PATH "VisualStudio/Projects"
54
0
#define FASTBUILD_VS_PROJECT_SUFFIX "-vcxproj"
55
56
0
#define FASTBUILD_IDE_VS_COMMAND_PREFIX "cd ^$(SolutionDir).. && "
57
0
#define FASTBUILD_DEFAULT_IDE_BUILD_ARGS " -ide -cache -summary -dist "
58
59
constexpr auto FASTBUILD_CAPTURE_SYSTEM_ENV =
60
  "CMAKE_FASTBUILD_CAPTURE_SYSTEM_ENV";
61
constexpr auto FASTBUILD_ENV_OVERRIDES = "CMAKE_FASTBUILD_ENV_OVERRIDES";
62
63
// Inherits from "CMAKE_FASTBUILD_VERBOSE_GENERATOR" env variable.
64
constexpr auto FASTBUILD_VERBOSE_GENERATOR =
65
  "CMAKE_FASTBUILD_VERBOSE_GENERATOR";
66
constexpr auto FASTBUILD_CACHE_PATH = "CMAKE_FASTBUILD_CACHE_PATH";
67
// Compiler settings.
68
constexpr auto FASTBUILD_COMPILER_EXTRA_FILES =
69
  "CMAKE_FASTBUILD_COMPILER_EXTRA_FILES";
70
constexpr auto FASTBUILD_USE_LIGHTCACHE = "CMAKE_FASTBUILD_USE_LIGHTCACHE";
71
constexpr auto FASTBUILD_USE_RELATIVE_PATHS =
72
  "CMAKE_FASTBUILD_USE_RELATIVE_PATHS";
73
constexpr auto FASTBUILD_USE_DETERMINISTIC_PATHS =
74
  "CMAKE_FASTBUILD_USE_DETERMINISTIC_PATHS";
75
constexpr auto FASTBUILD_SOURCE_MAPPING = "CMAKE_FASTBUILD_SOURCE_MAPPING";
76
constexpr auto FASTBUILD_CLANG_REWRITE_INCLUDES =
77
  "CMAKE_FASTBUILD_CLANG_REWRITE_INCLUDES";
78
constexpr auto FASTBUILD_CLANG_GCC_UPDATE_XLANG_ARG =
79
  "CMAKE_FASTBUILD_CLANG_GCC_UPDATE_XLANG_ARG";
80
constexpr auto FASTBUILD_ALLOW_RESPONSE_FILE =
81
  "CMAKE_FASTBUILD_ALLOW_RESPONSE_FILE";
82
constexpr auto FASTBUILD_FORCE_RESPONSE_FILE =
83
  "CMAKE_FASTBUILD_FORCE_RESPONSE_FILE";
84
85
constexpr auto FASTBUILD_IDE_ARGS = "CMAKE_FASTBUILD_IDE_ARGS";
86
87
static std::map<std::string, std::string> const compilerIdToFastbuildFamily = {
88
  { "MSVC", "msvc" }, { "Clang", "clang" },      { "AppleClang", "clang" },
89
  { "GNU", "gcc" },   { "NVIDIA", "cuda-nvcc" }, { "Clang-cl", "clang-cl" },
90
};
91
92
static std::set<std::string> const supportedLanguages = { "C", "CXX", "CUDA",
93
                                                          "OBJC", "OBJCXX" };
94
95
template <class T>
96
FastbuildAliasNode generateAlias(std::string const& name, char const* postfix,
97
                                 T const& nodes)
98
0
{
99
0
  FastbuildAliasNode alias;
100
0
  alias.Name = name + postfix;
101
0
  for (auto const& node : nodes) {
102
0
    alias.PreBuildDependencies.emplace(node.Name);
103
0
  }
104
0
  return alias;
105
0
}
Unexecuted instantiation: FastbuildAliasNode generateAlias<std::__1::vector<FastbuildExecNode, std::__1::allocator<FastbuildExecNode> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*, std::__1::vector<FastbuildExecNode, std::__1::allocator<FastbuildExecNode> > const&)
Unexecuted instantiation: FastbuildAliasNode generateAlias<std::__1::vector<FastbuildObjectListNode, std::__1::allocator<FastbuildObjectListNode> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*, std::__1::vector<FastbuildObjectListNode, std::__1::allocator<FastbuildObjectListNode> > const&)
106
107
void FastbuildTarget::GenerateAliases()
108
0
{
109
  // -deps
110
0
  this->DependenciesAlias.Name =
111
0
    this->Name + FASTBUILD_DEPS_ARTIFACTS_ALIAS_POSTFIX;
112
0
  for (auto const& dep : this->PreBuildDependencies) {
113
0
    if (dep.Type != FastbuildTargetDepType::ORDER_ONLY) {
114
0
      this->DependenciesAlias.PreBuildDependencies.emplace(dep);
115
0
    }
116
0
  }
117
118
  // PRE/POST/REST
119
0
  if (!this->PreBuildExecNodes.PreBuildDependencies.empty()) {
120
0
    this->PreBuildExecNodes.Name =
121
0
      this->Name + FASTBUILD_PRE_BUILD_ALIAS_POSTFIX;
122
0
  }
123
0
  if (!this->PreLinkExecNodes.Nodes.empty()) {
124
0
    this->PreLinkExecNodes.Alias =
125
0
      generateAlias(this->Name, FASTBUILD_PRE_LINK_ALIAS_POSTFIX,
126
0
                    this->PreLinkExecNodes.Nodes);
127
0
  }
128
0
  if (!this->PostBuildExecNodes.Alias.PreBuildDependencies.empty()) {
129
0
    this->PostBuildExecNodes.Alias.Name =
130
0
      this->Name + FASTBUILD_POST_BUILD_ALIAS_POSTFIX;
131
0
  }
132
0
  if (!this->ExecNodes.PreBuildDependencies.empty()) {
133
0
    this->ExecNodes.Name = this->Name + FASTBUILD_CUSTOM_COMMAND_ALIAS_POSTFIX;
134
0
  }
135
136
  // If we don't have any node that we can build by name (e.g. no static /
137
  // dynamic lib or executable) -> create an alias so that we can build this
138
  // target by name.
139
0
  if (LinkerNode.empty()) {
140
0
    FastbuildAliasNode alias;
141
0
    alias.Name = this->Name;
142
0
    if (LinkerNode.empty()) {
143
0
      for (FastbuildObjectListNode const& objListNode : ObjectListNodes) {
144
0
        alias.PreBuildDependencies.emplace(objListNode.Name);
145
0
      }
146
0
    } else {
147
0
      for (FastbuildLinkerNode const& linkerNode : LinkerNode) {
148
0
        alias.PreBuildDependencies.emplace(linkerNode.Name);
149
0
      }
150
0
    }
151
0
    AliasNodes.emplace_back(std::move(alias));
152
0
  }
153
154
  // Link artifacts (should not be added to all
155
  // since on Windows it might contain Import Lib and FASTBuild doesn't know
156
  // how to create it, so "-all" will fail).
157
0
  AliasNodes.emplace_back(generateAlias(
158
0
    this->Name, FASTBUILD_OBJECTS_ALIAS_POSTFIX, this->ObjectListNodes));
159
160
0
  for (auto const& linkerNode : this->LinkerNode) {
161
0
    if (linkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY ||
162
0
        linkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY ||
163
0
        linkerNode.Type == FastbuildLinkerNode::EXECUTABLE) {
164
0
      std::string postfix = FASTBUILD_LINK_ARTIFACTS_ALIAS_POSTFIX;
165
0
      if (!linkerNode.Arch.empty()) {
166
0
        postfix += cmStrCat('-', linkerNode.Arch);
167
0
      }
168
#ifdef _WIN32
169
      // On Windows DLL and Executables must be linked via Import Lib file
170
      // (.lib).
171
      if (linkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY ||
172
          linkerNode.Type == FastbuildLinkerNode::EXECUTABLE) {
173
        FastbuildAliasNode linkAlias;
174
        linkAlias.Name = this->Name + FASTBUILD_LINK_ARTIFACTS_ALIAS_POSTFIX;
175
        linkAlias.PreBuildDependencies.emplace(
176
          FASTBUILD_DOLLAR_TAG "TargetOutputImplib" FASTBUILD_DOLLAR_TAG);
177
        AliasNodes.emplace_back(std::move(linkAlias));
178
        continue;
179
      }
180
#endif
181
0
      FastbuildAliasNode alias;
182
0
      alias.Name = this->Name + postfix;
183
0
      alias.PreBuildDependencies.emplace(linkerNode.LinkerOutput);
184
0
      AliasNodes.emplace_back(std::move(alias));
185
0
    }
186
0
  }
187
0
}
188
189
cmGlobalFastbuildGenerator::cmGlobalFastbuildGenerator(cmake* cm)
190
0
  : cmGlobalCommonGenerator(cm)
191
0
  , BuildFileStream(nullptr)
192
0
{
193
#ifdef _WIN32
194
  cm->GetState()->SetWindowsShell(true);
195
#endif
196
0
  this->FindMakeProgramFile = "CMakeFastbuildFindMake.cmake";
197
0
  cm->GetState()->SetFastbuildMake(true);
198
0
  cm->GetState()->SetIsGeneratorMultiConfig(false);
199
0
}
200
201
void cmGlobalFastbuildGenerator::ReadCompilerOptions(
202
  FastbuildCompiler& compiler, cmMakefile* mf)
203
0
{
204
0
  if (compiler.CompilerFamily == "custom") {
205
0
    return;
206
0
  }
207
208
0
  if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_USE_LIGHTCACHE))) {
209
0
    compiler.UseLightCache = true;
210
0
  }
211
0
  if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_USE_RELATIVE_PATHS))) {
212
0
    compiler.UseRelativePaths = true;
213
0
    UsingRelativePaths = true;
214
0
  }
215
0
  if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_USE_DETERMINISTIC_PATHS))) {
216
0
    compiler.UseDeterministicPaths = true;
217
0
  }
218
0
  std::string sourceMapping = mf->GetSafeDefinition(FASTBUILD_SOURCE_MAPPING);
219
0
  if (!sourceMapping.empty()) {
220
0
    compiler.SourceMapping = std::move(sourceMapping);
221
0
  }
222
0
  auto const clangRewriteIncludesDef =
223
0
    mf->GetDefinition(FASTBUILD_CLANG_REWRITE_INCLUDES);
224
0
  if (clangRewriteIncludesDef.IsSet() && clangRewriteIncludesDef.IsOff()) {
225
0
    compiler.ClangRewriteIncludes = false;
226
0
  }
227
0
  if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_CLANG_GCC_UPDATE_XLANG_ARG))) {
228
0
    compiler.ClangGCCUpdateXLanguageArg = true;
229
0
  }
230
0
  if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_ALLOW_RESPONSE_FILE))) {
231
0
    compiler.AllowResponseFile = true;
232
0
  }
233
0
  if (cmIsOn(mf->GetSafeDefinition(FASTBUILD_FORCE_RESPONSE_FILE))) {
234
0
    compiler.ForceResponseFile = true;
235
0
  }
236
0
}
237
238
void cmGlobalFastbuildGenerator::ProcessEnvironment()
239
0
{
240
0
  bool const CaptureSystemEnv =
241
0
    !this->GetGlobalSetting(FASTBUILD_CAPTURE_SYSTEM_ENV).IsSet() ||
242
0
    this->GetGlobalSetting(FASTBUILD_CAPTURE_SYSTEM_ENV).IsOn();
243
  // On Windows environment is needed for MSVC, but preserve ability to discard
244
  // it from the generated file if requested.
245
0
  if (CaptureSystemEnv) {
246
0
    LocalEnvironment = cmSystemTools::GetEnvironmentVariables();
247
0
  }
248
  // FASTBuild strips off "-isysroot" command line option (see :
249
  // https://github.com/fastbuild/fastbuild/issues/1066).
250
  // If 'SDK_ROOT' is not set via env and '-isysroot' is absent, AppleClang
251
  // seems to use MacOS SDK by default (even though FBuild flattens includes
252
  // before compiling). It breaks cross-compilation for iOS. Tested in
253
  // "RunCMake.Framework" test.
254
0
  std::string const osxRoot = this->GetSafeGlobalSetting("CMAKE_OSX_SYSROOT");
255
0
  if (!osxRoot.empty()) {
256
0
    LocalEnvironment.emplace_back("SDKROOT=" + osxRoot);
257
0
  }
258
259
0
  auto const EnvOverrides =
260
0
    this->GetSafeGlobalSetting(FASTBUILD_ENV_OVERRIDES);
261
262
0
  if (!EnvOverrides.empty()) {
263
0
    auto const overrideEnvVar = [this](std::string const& prefix,
264
0
                                       std::string val) {
265
0
      auto const iter =
266
0
        std::find_if(LocalEnvironment.begin(), LocalEnvironment.end(),
267
0
                     [&prefix](std::string const& value) {
268
0
                       return cmSystemTools::StringStartsWith(value.c_str(),
269
0
                                                              prefix.c_str());
270
0
                     });
271
0
      if (iter != LocalEnvironment.end()) {
272
0
        *iter = std::move(val);
273
0
      } else {
274
0
        LocalEnvironment.emplace_back(std::move(val));
275
0
      }
276
0
    };
277
0
    for (auto const& val : cmList{ EnvOverrides }) {
278
0
      auto const pos = val.find('=');
279
0
      if (pos != std::string::npos && ((pos + 1) < val.size())) {
280
0
        overrideEnvVar(val.substr(0, pos + 1), val);
281
0
      }
282
0
    }
283
0
  }
284
285
  // Empty strings are not allowed.
286
0
  LocalEnvironment.erase(
287
0
    std::remove_if(LocalEnvironment.begin(), LocalEnvironment.end(),
288
0
                   [](std::string const& s) { return s.empty(); }),
289
0
    LocalEnvironment.end());
290
0
}
291
292
std::unique_ptr<cmGlobalGeneratorFactory>
293
cmGlobalFastbuildGenerator::NewFactory()
294
35
{
295
35
  return std::unique_ptr<cmGlobalGeneratorFactory>(
296
35
    new cmGlobalGeneratorSimpleFactory<cmGlobalFastbuildGenerator>());
297
35
}
298
299
void cmGlobalFastbuildGenerator::EnableLanguage(
300
  std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
301
0
{
302
0
  this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
303
0
  for (std::string const& l : lang) {
304
0
    if (l == "NONE") {
305
0
      continue;
306
0
    }
307
0
    this->ResolveLanguageCompiler(l, mf, optional);
308
0
  }
309
0
}
310
311
bool cmGlobalFastbuildGenerator::FindMakeProgram(cmMakefile* mf)
312
0
{
313
0
  if (!cmGlobalGenerator::FindMakeProgram(mf)) {
314
0
    return false;
315
0
  }
316
0
  if (auto fastbuildCommand = mf->GetDefinition("CMAKE_MAKE_PROGRAM")) {
317
0
    this->FastbuildCommand = *fastbuildCommand;
318
0
    std::vector<std::string> command;
319
0
    command.push_back(this->FastbuildCommand);
320
0
    command.emplace_back("-version");
321
0
    std::string version;
322
0
    std::string error;
323
0
    if (!cmSystemTools::RunSingleCommand(command, &version, &error, nullptr,
324
0
                                         nullptr,
325
0
                                         cmSystemTools::OUTPUT_NONE)) {
326
0
      mf->IssueMessage(MessageType::FATAL_ERROR,
327
0
                       "Running\n '" + cmJoin(command, "' '") +
328
0
                         "'\n"
329
0
                         "failed with:\n " +
330
0
                         error);
331
0
      cmSystemTools::SetFatalErrorOccurred();
332
0
      return false;
333
0
    }
334
0
    cmsys::RegularExpression versionRegex(R"(^FASTBuild v([0-9]+\.[0-9]+))");
335
0
    versionRegex.find(version);
336
0
    this->FastbuildVersion = versionRegex.match(1);
337
0
  }
338
0
  return true;
339
0
}
340
341
std::unique_ptr<cmLocalGenerator>
342
cmGlobalFastbuildGenerator::CreateLocalGenerator(cmMakefile* makefile)
343
0
{
344
0
  return std::unique_ptr<cmLocalGenerator>(
345
0
    cm::make_unique<cmLocalFastbuildGenerator>(this, makefile));
346
0
}
347
348
std::vector<cmGlobalGenerator::GeneratedMakeCommand>
349
cmGlobalFastbuildGenerator::GenerateBuildCommand(
350
  std::string const& makeProgram, std::string const& /*projectName*/,
351
  std::string const& projectDir, std::vector<std::string> const& targetNames,
352
  std::string const& /*config*/, int /*jobs*/, bool verbose,
353
  cmBuildOptions /*buildOptions*/, std::vector<std::string> const& makeOptions,
354
  BuildTryCompile isInTryCompile)
355
0
{
356
0
  GeneratedMakeCommand makeCommand;
357
0
  this->FastbuildCommand = this->SelectMakeProgram(makeProgram);
358
0
  makeCommand.Add(this->FastbuildCommand);
359
  // A build command for fastbuild looks like this:
360
  // fbuild.exe [make-options] [-config projectName.bff] <target>
361
362
0
  std::string configFile = cmStrCat(projectDir, '/', FASTBUILD_BUILD_FILE);
363
364
  // Push in the make options
365
0
  makeCommand.Add(makeOptions.begin(), makeOptions.end());
366
367
0
  if (!configFile.empty()) {
368
0
    makeCommand.Add("-config", configFile);
369
0
  }
370
  // Tested in "RunCMake.SymlinkTrees" test.
371
0
  makeCommand.Add("-continueafterdbmove");
372
373
  // Tested in RunCMake.LinkWhatYouUse on Linux. (We need to see output of
374
  // LinkerStampExe process).
375
  // In general, it might be useful to see output of external processes
376
  // regardless of their outcome.
377
0
  makeCommand.Add("-showcmdoutput");
378
379
  // Add the target-config to the command
380
0
  for (auto const& tname : targetNames) {
381
0
    if (!tname.empty()) {
382
0
      makeCommand.Add(tname);
383
0
    }
384
0
  }
385
0
  if (verbose) {
386
0
    makeCommand.Add("-verbose");
387
0
  }
388
389
  // Don't do extra work during "TryCompile".
390
0
  if (isInTryCompile == BuildTryCompile::Yes) {
391
0
    return { std::move(makeCommand) };
392
0
  }
393
394
  // Make "rebuild-bff" target up-to-date before running the build.
395
0
  std::string output;
396
0
  ExecuteFastbuildTarget(projectDir, FASTBUILD_REBUILD_BFF_TARGET_NAME, output,
397
0
                         { "-why" });
398
399
  // If fbuild.bff was re-generated we need to "restat" it.
400
0
  if (output.find("Need to build") != std::string::npos) {
401
    // Let the user know that re-generation happened (and why it
402
    // happened).
403
0
    cmSystemTools::Stdout(output);
404
    // FASTBuild will consider the target out-of-date in case some of the
405
    // inputs have changes after re-generation which might happen if, for
406
    // example, configuration depends on some files generated during
407
    // the configuration itself.
408
0
    AskCMakeToMakeRebuildBFFUpToDate(projectDir);
409
0
  }
410
411
0
  return { std::move(makeCommand) };
412
0
}
413
414
void cmGlobalFastbuildGenerator::ComputeTargetObjectDirectory(
415
  cmGeneratorTarget* gt) const
416
0
{
417
  // Compute full path to object file directory for this target.
418
0
  std::string dir =
419
0
    cmStrCat(gt->GetSupportDirectory(), '/', this->GetCMakeCFGIntDir(), '/');
420
0
  gt->ObjectDirectory = std::move(dir);
421
0
}
422
423
void cmGlobalFastbuildGenerator::AppendDirectoryForConfig(
424
  std::string const& prefix, std::string const& config,
425
  std::string const& suffix, std::string& dir)
426
0
{
427
0
  if (!config.empty() && this->IsMultiConfig()) {
428
0
    dir += cmStrCat(prefix, config, suffix);
429
0
  }
430
0
}
431
432
cmDocumentationEntry cmGlobalFastbuildGenerator::GetDocumentation()
433
0
{
434
0
  return { cmGlobalFastbuildGenerator::GetActualName(),
435
0
           "Generates fbuild.bff files." };
436
0
}
437
438
void cmGlobalFastbuildGenerator::Generate()
439
0
{
440
  // Check minimum Fastbuild version.
441
0
  if (cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
442
0
                                    this->FastbuildVersion,
443
0
                                    RequiredFastbuildVersion())) {
444
0
    std::ostringstream msg;
445
0
    msg << "The detected version of Fastbuild (" << this->FastbuildVersion;
446
0
    msg << ") is less than the version of Fastbuild required by CMake (";
447
0
    msg << this->RequiredFastbuildVersion() << ").";
448
0
    this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
449
0
                                           msg.str());
450
0
    return;
451
0
  }
452
0
  this->ProcessEnvironment();
453
454
0
  this->OpenBuildFileStream();
455
456
0
  this->WriteSettings();
457
0
  this->WriteEnvironment();
458
459
  // Execute the standard generate process
460
0
  cmGlobalGenerator::Generate();
461
462
  // Write compilers
463
0
  this->WriteCompilers();
464
465
0
  this->WriteTargets();
466
467
0
  this->CloseBuildFileStream();
468
469
0
  if (cmSystemTools::GetErrorOccurredFlag()) {
470
0
    return;
471
0
  }
472
473
0
  this->RemoveUnknownClangTidyExportFixesFiles();
474
475
0
  if (this->GetCMakeInstance()->GetRegenerateDuringBuild() ||
476
0
      this->GetCMakeInstance()->GetIsInTryCompile()) {
477
0
    return;
478
0
  }
479
0
  std::string const workingDir =
480
0
    this->GetCMakeInstance()->GetHomeOutputDirectory();
481
  //  Make "rebuild-bff" target up-to-date after the generation.
482
  //  This is actually a noop, it just asks CMake to touch the generated file
483
  //  so FASTBuild would consider the target as up-to-date.
484
0
  AskCMakeToMakeRebuildBFFUpToDate(workingDir);
485
486
0
  if (this->GlobalSettingIsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
487
0
    std::string output;
488
0
    ExecuteFastbuildTarget(workingDir, FASTBUILD_ALL_TARGET_NAME, output,
489
0
                           { "-compdb" });
490
0
  }
491
0
}
492
493
void cmGlobalFastbuildGenerator::AskCMakeToMakeRebuildBFFUpToDate(
494
  std::string const& workingDir) const
495
0
{
496
  // "restat" the generated build file.
497
  // The idea here is to mimic what Ninja's "restat" command does.
498
  // We need to make the "rebuild.bff" target up-to-date, so the regeneration
499
  // will only be triggered when CMake files have actually changed.
500
  // Tested in "RunCMake.Configure" test.
501
0
  cmsys::ofstream{
502
0
    cmStrCat(workingDir, '/', FASTBUILD_RESTAT_FILE).c_str(),
503
0
    std::ios::out | std::ios::binary
504
0
  } << cmStrCat(workingDir, '/', FASTBUILD_BUILD_FILE);
505
0
  std::string output;
506
0
  ExecuteFastbuildTarget(workingDir, FASTBUILD_REBUILD_BFF_TARGET_NAME,
507
0
                         output);
508
0
}
509
510
void cmGlobalFastbuildGenerator::ExecuteFastbuildTarget(
511
  std::string const& dir, std::string const& target, std::string& output,
512
  std::vector<std::string> const& fbuildOptions) const
513
0
{
514
0
  std::vector<std::string> command;
515
516
0
  command.emplace_back(this->FastbuildCommand);
517
0
  command.emplace_back("-config");
518
0
  std::string const file = cmStrCat(dir, '/', FASTBUILD_BUILD_FILE);
519
0
  command.emplace_back(file);
520
0
  command.emplace_back(target);
521
0
  if (!fbuildOptions.empty()) {
522
0
    command.emplace_back(cmJoin(fbuildOptions, " "));
523
0
  }
524
525
0
  int retVal = 0;
526
0
  if (!cmSystemTools::RunSingleCommand(command, &output, nullptr, &retVal,
527
0
                                       dir.c_str(),
528
0
                                       cmSystemTools::OUTPUT_NONE) ||
529
0
      retVal != 0) {
530
0
    cmSystemTools::Error(cmStrCat("Failed to run FASTBuild command:\n  '",
531
0
                                  cmJoin(command, "' '"), "'\nOutput:\n",
532
0
                                  output));
533
0
    cmSystemTools::Stdout(output);
534
0
    std::exit(retVal);
535
0
  }
536
0
}
537
538
void cmGlobalFastbuildGenerator::WriteSettings()
539
0
{
540
  // Define some placeholder
541
0
  WriteDivider();
542
0
  *this->BuildFileStream << "// Helper variables\n\n";
543
544
0
  WriteVariable("FB_INPUT_1_PLACEHOLDER", Quote("\"%1\""));
545
0
  WriteVariable("FB_INPUT_1_0_PLACEHOLDER", Quote("\"%1[0]\""));
546
0
  WriteVariable("FB_INPUT_1_1_PLACEHOLDER", Quote("\"%1[1]\""));
547
0
  WriteVariable("FB_INPUT_2_PLACEHOLDER", Quote("\"%2\""));
548
0
  WriteVariable("FB_INPUT_3_PLACEHOLDER", Quote("\"%3\""));
549
550
0
  std::string cacheDir;
551
  // If explicitly set from CMake.
552
0
  auto val = this->GetSafeGlobalSetting(FASTBUILD_CACHE_PATH);
553
0
  if (!val.empty()) {
554
0
    cacheDir = std::move(val);
555
0
    cmSystemTools::ConvertToOutputSlashes(cacheDir);
556
0
  }
557
558
0
  WriteDivider();
559
0
  *this->BuildFileStream << "// Settings\n\n";
560
561
0
  WriteCommand("Settings");
562
0
  *this->BuildFileStream << "{\n";
563
0
  if (!cacheDir.empty()) {
564
0
    WriteVariable("CachePath", Quote(cacheDir), 1);
565
0
  }
566
  // Concurrency groups.
567
0
  WriteStruct(
568
0
    FASTBUILD_UTIL_CONCURRENCY_GROUP_NAME,
569
0
    { { "ConcurrencyGroupName", Quote(FASTBUILD_UTIL_CONCURRENCY_GROUP_NAME) },
570
0
      { "ConcurrencyLimit", "1" } },
571
0
    1);
572
573
0
  WriteArray("ConcurrencyGroups",
574
0
             { "." FASTBUILD_UTIL_CONCURRENCY_GROUP_NAME }, 1);
575
576
0
  *this->BuildFileStream << "}\n";
577
0
}
578
579
void cmGlobalFastbuildGenerator::WriteEnvironment()
580
0
{
581
0
  if (!LocalEnvironment.empty()) {
582
0
    WriteArray(FASTBUILD_ENV_VAR_NAME, Wrap(LocalEnvironment), 0);
583
0
  }
584
0
}
585
586
void cmGlobalFastbuildGenerator::WriteDivider()
587
0
{
588
0
  *this->BuildFileStream << "// ======================================"
589
0
                            "=======================================\n";
590
0
}
591
592
void cmGlobalFastbuildGenerator::Indent(int count)
593
0
{
594
0
  for (int i = 0; i < count; ++i) {
595
0
    *this->BuildFileStream << "  ";
596
0
  }
597
0
}
598
599
void cmGlobalFastbuildGenerator::WriteComment(std::string const& comment,
600
                                              int indent)
601
0
{
602
0
  if (comment.empty()) {
603
0
    return;
604
0
  }
605
606
0
  std::string::size_type lpos = 0;
607
0
  std::string::size_type rpos;
608
0
  *this->BuildFileStream << "\n";
609
0
  Indent(indent);
610
0
  *this->BuildFileStream << "/////////////////////////////////////////////\n";
611
0
  while ((rpos = comment.find('\n', lpos)) != std::string::npos) {
612
0
    Indent(indent);
613
0
    *this->BuildFileStream << "// " << comment.substr(lpos, rpos - lpos)
614
0
                           << "\n";
615
0
    lpos = rpos + 1;
616
0
  }
617
0
  Indent(indent);
618
0
  *this->BuildFileStream << "// " << comment.substr(lpos) << "\n\n";
619
0
}
620
621
void cmGlobalFastbuildGenerator::WriteVariable(std::string const& key,
622
                                               std::string const& value,
623
                                               int indent)
624
0
{
625
0
  WriteVariable(key, value, "=", indent);
626
0
}
627
628
void cmGlobalFastbuildGenerator::WriteVariable(std::string const& key,
629
                                               std::string const& value,
630
                                               std::string const& op,
631
                                               int indent)
632
0
{
633
0
  Indent(indent);
634
0
  *this->BuildFileStream << "." << key << " " + op + (value.empty() ? "" : " ")
635
0
                         << value << "\n";
636
0
}
637
638
void cmGlobalFastbuildGenerator::WriteCommand(std::string const& command,
639
                                              std::string const& value,
640
                                              int indent)
641
0
{
642
0
  Indent(indent);
643
0
  *this->BuildFileStream << command;
644
0
  if (!value.empty()) {
645
0
    *this->BuildFileStream << "(" << value << ")";
646
0
  }
647
0
  *this->BuildFileStream << "\n";
648
0
}
649
650
void cmGlobalFastbuildGenerator::WriteArray(
651
  std::string const& key, std::vector<std::string> const& values, int indent)
652
0
{
653
0
  WriteArray(key, values, "=", indent);
654
0
}
655
656
void cmGlobalFastbuildGenerator::WriteArray(
657
  std::string const& key, std::vector<std::string> const& values,
658
  std::string const& op, int indent)
659
0
{
660
0
  WriteVariable(key, "", op, indent);
661
0
  Indent(indent);
662
0
  *this->BuildFileStream << "{\n";
663
0
  char const* sep = "";
664
0
  for (std::string const& value : values) {
665
0
    *this->BuildFileStream << sep;
666
0
    sep = ",\n";
667
0
    Indent(indent + 1);
668
0
    *this->BuildFileStream << value;
669
0
  }
670
0
  *this->BuildFileStream << "\n";
671
0
  Indent(indent);
672
0
  *this->BuildFileStream << "}\n";
673
0
}
674
675
void cmGlobalFastbuildGenerator::WriteStruct(
676
  std::string const& name,
677
  std::vector<std::pair<std::string, std::string>> const& variables,
678
  int indent)
679
0
{
680
0
  WriteVariable(name, "", "=", indent);
681
0
  Indent(indent);
682
0
  *this->BuildFileStream << "[\n";
683
0
  for (auto const& val : variables) {
684
0
    auto const& key = val.first;
685
0
    auto const& value = val.second;
686
0
    WriteVariable(key, value, "=", indent + 1);
687
0
  }
688
0
  Indent(indent);
689
0
  *this->BuildFileStream << "]\n";
690
0
}
691
692
std::string cmGlobalFastbuildGenerator::Quote(std::string const& str,
693
                                              std::string const& quotation)
694
0
{
695
0
  std::string result = str;
696
0
  cmSystemTools::ReplaceString(result, quotation, "^" + quotation);
697
0
  cmSystemTools::ReplaceString(result, FASTBUILD_DOLLAR_TAG, "$");
698
0
  return quotation + result + quotation;
699
0
}
700
std::string cmGlobalFastbuildGenerator::QuoteIfHasSpaces(std::string str)
701
0
{
702
0
  if (str.find(' ') != std::string::npos) {
703
0
    return cmStrCat('"', str, '"');
704
0
  }
705
0
  return str;
706
0
}
707
708
struct WrapHelper
709
{
710
  std::string Prefix;
711
  std::string Suffix;
712
  bool EscapeDollar;
713
714
  std::string operator()(std::string in)
715
0
  {
716
    // If we have ^ in env variable - need to escape it.
717
0
    cmSystemTools::ReplaceString(in, "^", "^^");
718
    // Those all are considered as line ends by FASTBuild.
719
0
    cmSystemTools::ReplaceString(in, "\n", "\\n");
720
0
    cmSystemTools::ReplaceString(in, "\r", "\\r");
721
    // Escaping of single quotes tested in "RunCMake.CompilerArgs" test.
722
0
    cmSystemTools::ReplaceString(in, "'", "^'");
723
0
    std::string result = Prefix + in + Suffix;
724
0
    if (EscapeDollar) {
725
0
      cmSystemTools::ReplaceString(result, "$", "^$");
726
0
      cmSystemTools::ReplaceString(result, FASTBUILD_DOLLAR_TAG, "$");
727
0
    }
728
0
    return result;
729
0
  }
730
  std::string operator()(FastbuildTargetDep const& in)
731
0
  {
732
0
    return (*this)(in.Name);
733
0
  }
734
};
735
template <class T>
736
std::vector<std::string> cmGlobalFastbuildGenerator::Wrap(
737
  T const& in, std::string const& prefix, std::string const& suffix,
738
  bool const escape_dollar)
739
0
{
740
741
0
  std::vector<std::string> result;
742
743
0
  WrapHelper helper = { prefix, suffix, escape_dollar };
744
745
0
  std::transform(in.begin(), in.end(), std::back_inserter(result), helper);
746
747
0
  return result;
748
0
}
Unexecuted instantiation: std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > cmGlobalFastbuildGenerator::Wrap<std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)
Unexecuted instantiation: std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > cmGlobalFastbuildGenerator::Wrap<std::__1::set<FastbuildTargetDep, std::__1::less<FastbuildTargetDep>, std::__1::allocator<FastbuildTargetDep> > >(std::__1::set<FastbuildTargetDep, std::__1::less<FastbuildTargetDep>, std::__1::allocator<FastbuildTargetDep> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)
Unexecuted instantiation: std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > cmGlobalFastbuildGenerator::Wrap<std::__1::set<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >(std::__1::set<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)
749
750
void cmGlobalFastbuildGenerator::TopologicalSort(
751
  std::vector<FastbuildTargetPtrT>& nodes)
752
0
{
753
0
  std::unordered_map<std::string, int> inDegree;
754
0
  std::unordered_map<std::string, std::set<std::string>> reverseDeps;
755
0
  std::unordered_map<std::string, std::size_t> originalIndex;
756
757
  // Track original positions
758
0
  for (std::size_t i = 0; i < nodes.size(); ++i) {
759
0
    auto const& node = nodes[i];
760
0
    inDegree[node->Name] = 0;
761
0
    originalIndex[node->Name] = i;
762
0
  }
763
764
  // Build reverse dependency graph and in-degree map
765
0
  for (auto const& node : nodes) {
766
0
    for (auto const& dep : node->PreBuildDependencies) {
767
0
      if (inDegree.count(dep.Name)) {
768
0
        reverseDeps[dep.Name].insert(node->Name);
769
0
        ++inDegree[node->Name];
770
0
      }
771
0
    }
772
0
  }
773
774
  // Min-heap based on original position
775
0
  auto const cmp = [&](std::string const& a, std::string const& b) {
776
0
    return originalIndex[a] > originalIndex[b];
777
0
  };
778
0
  std::priority_queue<std::string, std::vector<std::string>, decltype(cmp)>
779
0
    zeroInDegree(cmp);
780
781
0
  for (auto const& val : inDegree) {
782
0
    auto const& degree = val.second;
783
0
    auto const& name = val.first;
784
0
    if (degree == 0) {
785
0
      zeroInDegree.push(name);
786
0
    }
787
0
  }
788
789
0
  std::vector<std::string> sorted;
790
0
  while (!zeroInDegree.empty()) {
791
0
    std::string node = zeroInDegree.top();
792
0
    zeroInDegree.pop();
793
0
    sorted.push_back(node);
794
0
    for (auto const& dep : reverseDeps[node]) {
795
0
      if (--inDegree[dep] == 0) {
796
0
        zeroInDegree.push(dep);
797
0
      }
798
0
    }
799
0
  }
800
801
0
  if (sorted.size() != nodes.size()) {
802
0
    cmSystemTools::Error("Failed to sort (Cyclic dependency)");
803
0
    cmSystemTools::Error(cmStrCat("Sorted size: ", sorted.size()));
804
0
    cmSystemTools::Error(cmStrCat("nodes size: ", nodes.size()));
805
0
    for (auto const& node : nodes) {
806
0
      cmSystemTools::Error("Node: " + node->Name);
807
0
      for (auto const& dep : reverseDeps[node->Name]) {
808
0
        cmSystemTools::Error("\tReverse dep: " + dep);
809
0
      }
810
0
      for (auto const& child : node->PreBuildDependencies) {
811
0
        cmSystemTools::Error("\tChild: " + child.Name);
812
0
      }
813
0
    }
814
0
    for (auto const& node : sorted) {
815
0
      cmSystemTools::Error("Sorted: " + node);
816
0
    }
817
0
    for (auto const& node : nodes) {
818
0
      cmSystemTools::Error("In node: " + node->Name);
819
0
    }
820
0
  }
821
822
  // Reconstruct sorted nodes
823
0
  std::vector<FastbuildTargetPtrT> result;
824
0
  for (auto const& name : sorted) {
825
0
    auto it = std::find_if(
826
0
      nodes.begin(), nodes.end(), [&name](FastbuildTargetPtrT const& node) {
827
0
        return node /* the node might be in moved-from state*/ &&
828
0
          node->Name == name;
829
0
      });
830
0
    if (it != nodes.end()) {
831
0
      result.emplace_back(std::move(*it));
832
0
    }
833
0
  }
834
835
0
  std::swap(result, nodes);
836
0
}
837
838
void cmGlobalFastbuildGenerator::WriteDisclaimer()
839
0
{
840
0
  *this->BuildFileStream << "// CMAKE generated file: DO NOT EDIT!\n"
841
0
                         << "// Generated by \"" << this->GetName() << "\""
842
0
                         << " Generator, CMake Version "
843
0
                         << cmVersion::GetMajorVersion() << "."
844
0
                         << cmVersion::GetMinorVersion() << "\n\n";
845
0
}
846
847
void cmGlobalFastbuildGenerator::OpenBuildFileStream()
848
0
{
849
  // Compute Fastbuild's build file path.
850
0
  std::string buildFilePath =
851
0
    this->GetCMakeInstance()->GetHomeOutputDirectory();
852
0
  buildFilePath += "/";
853
0
  buildFilePath += FASTBUILD_BUILD_FILE;
854
855
  // Get a stream where to generate things.
856
0
  if (!this->BuildFileStream) {
857
0
    this->BuildFileStream = cm::make_unique<cmGeneratedFileStream>(
858
0
      buildFilePath, false, this->GetMakefileEncoding());
859
0
    if (!this->BuildFileStream) {
860
      // An error message is generated by the constructor if it cannot
861
      // open the file.
862
0
      return;
863
0
    }
864
0
  }
865
866
  // Write the do not edit header.
867
0
  this->WriteDisclaimer();
868
869
  // Write a comment about this file.
870
0
  *this->BuildFileStream
871
0
    << "// This file contains all the build statements\n\n";
872
0
}
873
874
void cmGlobalFastbuildGenerator::CloseBuildFileStream()
875
0
{
876
0
  if (this->BuildFileStream) {
877
0
    this->BuildFileStream.reset();
878
0
  } else {
879
0
    cmSystemTools::Error("Build file stream was not open.");
880
0
  }
881
0
}
882
883
void cmGlobalFastbuildGenerator::WriteCompilers()
884
0
{
885
0
  WriteDivider();
886
0
  *this->BuildFileStream << "// Compilers\n\n";
887
0
  for (auto const& val : Compilers) {
888
0
    auto const& compilerDef = val.second;
889
890
0
    std::string compilerPath = compilerDef.Executable;
891
892
    // Write out the compiler that has been configured
893
0
    WriteCommand("Compiler", Quote(compilerDef.Name));
894
0
    *this->BuildFileStream << "{\n";
895
0
    for (auto const& extra : compilerDef.ExtraVariables) {
896
0
      auto const& extraKey = extra.first;
897
0
      auto const& extraVal = extra.second;
898
0
      WriteVariable(extraKey, Quote(extraVal), 1);
899
0
    }
900
0
    WriteVariable("Executable", Quote(compilerPath), 1);
901
0
    WriteVariable("CompilerFamily", Quote(compilerDef.CompilerFamily), 1);
902
0
    if (this->GetCMakeInstance()->GetIsInTryCompile()) {
903
0
      WriteVariable("AllowCaching", "false", 1);
904
0
      WriteVariable("AllowDistribution", "false", 1);
905
0
    }
906
907
0
    if (compilerDef.UseLightCache && compilerDef.CompilerFamily == "msvc") {
908
0
      WriteVariable("UseLightCache_Experimental", "true", 1);
909
0
    }
910
0
    if (compilerDef.UseRelativePaths) {
911
0
      WriteVariable("UseRelativePaths_Experimental", "true", 1);
912
0
    }
913
0
    if (compilerDef.UseDeterministicPaths) {
914
0
      WriteVariable("UseDeterministicPaths_Experimental", "true", 1);
915
0
    }
916
917
0
    if (!compilerDef.SourceMapping.empty()) {
918
0
      WriteVariable("SourceMapping_Experimental",
919
0
                    Quote(compilerDef.SourceMapping), 1);
920
0
    }
921
922
0
    auto const isClang = [&compilerDef] {
923
0
      return compilerDef.CompilerFamily == "clang" ||
924
0
        compilerDef.CompilerFamily == "clang-cl";
925
0
    };
926
927
0
    if (!compilerDef.ClangRewriteIncludes && isClang()) {
928
0
      WriteVariable("ClangRewriteIncludes", "false", 1);
929
0
    }
930
0
    if (compilerDef.ClangGCCUpdateXLanguageArg &&
931
0
        (isClang() || compilerDef.CompilerFamily == "gcc")) {
932
0
      WriteVariable("ClangGCCUpdateXLanguageArg", "true", 1);
933
0
    }
934
935
0
    if (compilerDef.AllowResponseFile) {
936
0
      WriteVariable("AllowResponseFile", "true", 1);
937
0
    }
938
0
    if (compilerDef.ForceResponseFile) {
939
940
0
      WriteVariable("ForceResponseFile", "true", 1);
941
0
    }
942
943
0
    if (compilerDef.DontUseEnv) {
944
0
      LogMessage("Not using system environment");
945
0
    } else {
946
0
      if (!LocalEnvironment.empty()) {
947
0
        WriteVariable("Environment", "." FASTBUILD_ENV_VAR_NAME, 1);
948
0
      }
949
0
    }
950
0
    if (!compilerDef.ExtraFiles.empty()) {
951
      // Do not escape '$' sign, CMAKE_${LANG}_FASTBUILD_EXTRA_FILES might
952
      // contain FB variables to be expanded (we do use some internally).
953
      // Besides a path cannot contain a '$'
954
0
      WriteArray("ExtraFiles", Wrap(compilerDef.ExtraFiles, "'", "'", false),
955
0
                 1);
956
0
    }
957
0
    *this->BuildFileStream << "}\n";
958
959
0
    auto const compilerId = compilerDef.Name;
960
0
    WriteVariable(compilerId, Quote(compilerDef.Name));
961
0
    *this->BuildFileStream << "\n";
962
0
  }
963
  // We need this because the Library command needs a compiler
964
  // even if don't compile anything
965
0
  if (!this->Compilers.empty()) {
966
0
    WriteVariable("Compiler_dummy",
967
0
                  Quote(this->Compilers.begin()->second.Name));
968
0
  }
969
0
}
970
971
void cmGlobalFastbuildGenerator::AddCompiler(std::string const& language,
972
                                             cmMakefile* mf)
973
0
{
974
0
  if (this->Compilers.find(FASTBUILD_COMPILER_PREFIX + language) !=
975
0
      this->Compilers.end()) {
976
0
    return;
977
0
  }
978
979
  // Calculate the root location of the compiler
980
0
  std::string const variableString = cmStrCat("CMAKE_", language, "_COMPILER");
981
0
  std::string const compilerLocation = mf->GetSafeDefinition(variableString);
982
0
  if (compilerLocation.empty()) {
983
0
    return;
984
0
  }
985
986
  // Calculate the i18n number.
987
0
  std::string i18nNum = "1033";
988
989
  // Add the language to the compiler's name
990
0
  FastbuildCompiler compilerDef;
991
0
  compilerDef.ExtraVariables["Root"] =
992
0
    cmSystemTools::GetFilenamePath(compilerLocation);
993
0
  compilerDef.Name = FASTBUILD_COMPILER_PREFIX + language;
994
0
  compilerDef.Executable = compilerLocation;
995
0
  compilerDef.CmakeCompilerID =
996
0
    mf->GetSafeDefinition(cmStrCat("CMAKE_", language, "_COMPILER_ID"));
997
0
  if (compilerDef.CmakeCompilerID == "Clang" &&
998
0
      mf->GetSafeDefinition(cmStrCat(
999
0
        "CMAKE_", language, "_COMPILER_FRONTEND_VARIANT")) == "MSVC") {
1000
0
    compilerDef.CmakeCompilerID = "Clang-cl";
1001
0
  }
1002
1003
0
  compilerDef.CmakeCompilerVersion =
1004
0
    mf->GetSafeDefinition(cmStrCat("CMAKE_", language, "_COMPILER_VERSION"));
1005
0
  compilerDef.Language = language;
1006
1007
0
  cmExpandList(mf->GetSafeDefinition(FASTBUILD_COMPILER_EXTRA_FILES),
1008
0
               compilerDef.ExtraFiles);
1009
1010
0
  if (supportedLanguages.find(language) != supportedLanguages.end()) {
1011
0
    auto const iter =
1012
0
      compilerIdToFastbuildFamily.find(compilerDef.CmakeCompilerID);
1013
0
    if (iter != compilerIdToFastbuildFamily.end()) {
1014
0
      compilerDef.CompilerFamily = iter->second;
1015
0
    }
1016
0
  }
1017
1018
  // Has to be called after we determined 'CompilerFamily'.
1019
0
  ReadCompilerOptions(compilerDef, mf);
1020
1021
  // If FASTBUILD_COMPILER_EXTRA_FILES is not set - automatically add extra
1022
  // files based on compiler (see
1023
  // https://fastbuild.org/docs/functions/compiler.html)
1024
0
  if (!this->GetCMakeInstance()->GetIsInTryCompile() &&
1025
0
      compilerDef.ExtraFiles.empty() &&
1026
0
      (language == "C" || language == "CXX") &&
1027
0
      compilerDef.CmakeCompilerID == "MSVC") {
1028
    // https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html
1029
1030
    // Visual Studio 17 (19.30 to 19.39)
1031
    // TODO
1032
1033
    // Visual Studio 16 (19.20 to 19.29)
1034
0
    if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
1035
0
                                      compilerDef.CmakeCompilerVersion,
1036
0
                                      "19.20")) {
1037
0
      compilerDef.ExtraFiles.push_back("$Root$/c1.dll");
1038
0
      compilerDef.ExtraFiles.push_back("$Root$/c1xx.dll");
1039
0
      compilerDef.ExtraFiles.push_back("$Root$/c2.dll");
1040
0
      compilerDef.ExtraFiles.push_back(
1041
0
        "$Root$/atlprov.dll"); // Only needed if using ATL
1042
0
      compilerDef.ExtraFiles.push_back("$Root$/msobj140.dll");
1043
0
      compilerDef.ExtraFiles.push_back("$Root$/mspdb140.dll");
1044
0
      compilerDef.ExtraFiles.push_back("$Root$/mspdbcore.dll");
1045
0
      compilerDef.ExtraFiles.push_back("$Root$/mspdbsrv.exe");
1046
0
      compilerDef.ExtraFiles.push_back("$Root$/mspft140.dll");
1047
0
      compilerDef.ExtraFiles.push_back("$Root$/msvcp140.dll");
1048
0
      compilerDef.ExtraFiles.push_back(
1049
0
        "$Root$/msvcp140_atomic_wait.dll"); // Required circa 16.8.3
1050
                                            // (14.28.29333)
1051
0
      compilerDef.ExtraFiles.push_back(
1052
0
        "$Root$/tbbmalloc.dll"); // Required as of 16.2 (14.22.27905)
1053
0
      compilerDef.ExtraFiles.push_back("$Root$/vcruntime140.dll");
1054
0
      compilerDef.ExtraFiles.push_back(
1055
0
        "$Root$/vcruntime140_1.dll"); // Required as of 16.5.1 (14.25.28610)
1056
0
      compilerDef.ExtraFiles.push_back(
1057
0
        cmStrCat("$Root$/", i18nNum, "/clui.dll"));
1058
0
      compilerDef.ExtraFiles.push_back(cmStrCat(
1059
0
        "$Root$/", i18nNum, "/mspft140ui.dll")); // Localized messages for
1060
                                                 // static analysis
1061
0
    }
1062
    // Visual Studio 15 (19.10 to 19.19)
1063
0
    else if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
1064
0
                                           compilerDef.CmakeCompilerVersion,
1065
0
                                           "19.10")) {
1066
0
      compilerDef.ExtraFiles.push_back("$Root$/c1.dll");
1067
0
      compilerDef.ExtraFiles.push_back("$Root$/c1xx.dll");
1068
0
      compilerDef.ExtraFiles.push_back("$Root$/c2.dll");
1069
0
      compilerDef.ExtraFiles.push_back(
1070
0
        "$Root$/atlprov.dll"); // Only needed if using ATL
1071
0
      compilerDef.ExtraFiles.push_back("$Root$/msobj140.dll");
1072
0
      compilerDef.ExtraFiles.push_back("$Root$/mspdb140.dll");
1073
0
      compilerDef.ExtraFiles.push_back("$Root$/mspdbcore.dll");
1074
0
      compilerDef.ExtraFiles.push_back("$Root$/mspdbsrv.exe");
1075
0
      compilerDef.ExtraFiles.push_back("$Root$/mspft140.dll");
1076
0
      compilerDef.ExtraFiles.push_back("$Root$/msvcp140.dll");
1077
0
      compilerDef.ExtraFiles.push_back("$Root$/vcruntime140.dll");
1078
0
      compilerDef.ExtraFiles.push_back("$Root$/" + i18nNum + "/clui.dll");
1079
0
    }
1080
0
  }
1081
  // TODO: Handle Intel compiler
1082
1083
0
  this->Compilers[compilerDef.Name] = std::move(compilerDef);
1084
0
}
1085
1086
void cmGlobalFastbuildGenerator::AddLauncher(std::string const& prefix,
1087
                                             std::string const& launcher,
1088
                                             std::string const& language,
1089
                                             std::string const& args)
1090
0
{
1091
0
  if (this->Compilers.find(prefix + language) != this->Compilers.end()) {
1092
0
    return;
1093
0
  }
1094
0
  LogMessage("Launcher: " + launcher);
1095
0
  LogMessage("Launcher args: " + args);
1096
0
  FastbuildCompiler compilerDef;
1097
0
  compilerDef.Name = prefix + language;
1098
0
  compilerDef.Args = args;
1099
0
  if (cmSystemTools::FileIsFullPath(launcher)) {
1100
0
    compilerDef.Executable = launcher;
1101
0
  } else {
1102
    // FASTBuild needs an absolute path to the executable.
1103
0
    compilerDef.Executable = cmSystemTools::FindProgram(launcher);
1104
0
    if (compilerDef.Executable.empty()) {
1105
0
      cmSystemTools::Error("Failed to find path to " + launcher);
1106
0
      return;
1107
0
    }
1108
0
  }
1109
  // When CTest is used as a launcher, there is an interesting env variable
1110
  // "CTEST_LAUNCH_LOGS" which is set by parent CTest process and is expected
1111
  // to be read from global (sic!) env by the launched CTest process. So we
1112
  // will need to make this global env available for CTest executable used as a
1113
  // "launcher". Tested in RunCMake.ctest_labels_for_subprojects test..
1114
0
  compilerDef.DontUseEnv = true;
1115
0
  this->Compilers[compilerDef.Name] = std::move(compilerDef);
1116
0
}
1117
1118
std::string cmGlobalFastbuildGenerator::ConvertToFastbuildPath(
1119
  std::string const& path) const
1120
0
{
1121
0
  cmLocalGenerator const* root = LocalGenerators[0].get();
1122
0
  return root->MaybeRelativeToWorkDir(cmSystemTools::FileIsFullPath(path)
1123
0
                                        ? cmSystemTools::CollapseFullPath(path)
1124
0
                                        : path);
1125
0
}
1126
1127
std::unique_ptr<cmLinkLineComputer>
1128
cmGlobalFastbuildGenerator::CreateLinkLineComputer(
1129
  cmOutputConverter* outputConverter,
1130
  cmStateDirectory const& /* stateDir */) const
1131
0
{
1132
0
  return cm::make_unique<cmFastbuildLinkLineComputer>(
1133
0
    outputConverter,
1134
0
    this->LocalGenerators[0]->GetStateSnapshot().GetDirectory(), this);
1135
0
}
1136
1137
void cmGlobalFastbuildGenerator::WriteExec(FastbuildExecNode const& Exec,
1138
                                           int indent)
1139
0
{
1140
0
  auto const identPlus1 = indent + 1;
1141
0
  WriteCommand("Exec", Exec.Name.empty() ? std::string{} : Quote(Exec.Name),
1142
0
               indent);
1143
0
  Indent(indent);
1144
0
  *BuildFileStream << "{\n";
1145
0
  {
1146
0
    if (!Exec.PreBuildDependencies.empty()) {
1147
0
      WriteArray("PreBuildDependencies", Wrap(Exec.PreBuildDependencies),
1148
0
                 identPlus1);
1149
0
    }
1150
0
    WriteVariable("ExecExecutable", Quote(Exec.ExecExecutable), identPlus1);
1151
0
    if (!Exec.ExecArguments.empty()) {
1152
0
      WriteVariable("ExecArguments", Quote(Exec.ExecArguments), identPlus1);
1153
0
    }
1154
0
    if (!Exec.ExecWorkingDir.empty()) {
1155
0
      WriteVariable("ExecWorkingDir", Quote(Exec.ExecWorkingDir), identPlus1);
1156
0
    }
1157
0
    if (!Exec.ExecInput.empty()) {
1158
0
      WriteArray("ExecInput", Wrap(Exec.ExecInput), identPlus1);
1159
0
    }
1160
0
    if (Exec.ExecUseStdOutAsOutput) {
1161
0
      WriteVariable("ExecUseStdOutAsOutput", "true", identPlus1);
1162
0
    }
1163
0
    if (!Exec.ExecInputPath.empty()) {
1164
0
      WriteArray("ExecInputPath", Wrap(Exec.ExecInputPath), identPlus1);
1165
0
    }
1166
0
    if (!Exec.ExecInputPattern.empty()) {
1167
0
      WriteArray("ExecInputPattern", Wrap(Exec.ExecInputPattern), identPlus1);
1168
0
    }
1169
0
    WriteVariable("ExecAlwaysShowOutput", "true", identPlus1);
1170
0
    WriteVariable("ExecOutput", Quote(Exec.ExecOutput), identPlus1);
1171
0
    WriteVariable("ExecAlways", Exec.ExecAlways ? "true" : "false",
1172
0
                  identPlus1);
1173
0
    if (!Exec.ConcurrencyGroupName.empty()) {
1174
0
      WriteVariable("ConcurrencyGroupName", Quote(Exec.ConcurrencyGroupName),
1175
0
                    identPlus1);
1176
0
    }
1177
0
  }
1178
0
  Indent(indent);
1179
0
  *BuildFileStream << "}\n";
1180
0
  static bool const verbose = GlobalSettingIsOn(FASTBUILD_VERBOSE_GENERATOR) ||
1181
0
    cmSystemTools::HasEnv(FASTBUILD_VERBOSE_GENERATOR);
1182
  // Those aliases are only used for troubleshooting the generated file.
1183
0
  if (verbose) {
1184
0
    WriteAlias(Exec.OutputsAlias);
1185
0
    WriteAlias(Exec.ByproductsAlias);
1186
0
  }
1187
0
}
1188
1189
void cmGlobalFastbuildGenerator::WriteUnity(FastbuildUnityNode const& Unity)
1190
0
{
1191
0
  WriteCommand("Unity", Quote(Unity.Name), 1);
1192
0
  Indent(1);
1193
0
  *BuildFileStream << "{\n";
1194
0
  {
1195
0
    WriteVariable("UnityOutputPath", Quote(Unity.UnityOutputPath), 2);
1196
0
    WriteVariable("UnityOutputPattern", Quote(Unity.UnityOutputPattern), 2);
1197
0
    WriteArray("UnityInputFiles", Wrap(Unity.UnityInputFiles), 2);
1198
0
    if (!Unity.UnityInputIsolatedFiles.empty()) {
1199
0
      WriteArray("UnityInputIsolatedFiles",
1200
0
                 Wrap(Unity.UnityInputIsolatedFiles), 2);
1201
0
    }
1202
0
    if (UsingRelativePaths) {
1203
0
      WriteVariable("UseRelativePaths_Experimental", "true", 2);
1204
0
    }
1205
0
  }
1206
0
  Indent(1);
1207
0
  *BuildFileStream << "}\n";
1208
0
}
1209
1210
void cmGlobalFastbuildGenerator::WriteObjectList(
1211
  FastbuildObjectListNode const& ObjectList, bool allowDistribution)
1212
0
{
1213
0
  WriteCommand("ObjectList", Quote(ObjectList.Name), 1);
1214
0
  Indent(1);
1215
0
  *BuildFileStream << "{\n";
1216
0
  {
1217
0
    if (!allowDistribution) {
1218
0
      WriteVariable("AllowDistribution", "false", 2);
1219
0
    }
1220
0
    if (!ObjectList.PreBuildDependencies.empty()) {
1221
0
      WriteArray("PreBuildDependencies", Wrap(ObjectList.PreBuildDependencies),
1222
0
                 2);
1223
0
    }
1224
0
    WriteVariable("Compiler", ObjectList.Compiler, 2);
1225
    // If only PCH output is present - this node reuses existing PCH.
1226
0
    if (!ObjectList.PCHOutputFile.empty()) {
1227
0
      WriteVariable("PCHOutputFile", Quote(ObjectList.PCHOutputFile), 2);
1228
0
    }
1229
    // If PCHInputFile and PCHOptions are present  - this node creates PCH.
1230
0
    if (!ObjectList.PCHInputFile.empty() && !ObjectList.PCHOptions.empty()) {
1231
0
      WriteVariable("PCHInputFile", Quote(ObjectList.PCHInputFile), 2);
1232
0
      WriteVariable("PCHOptions", Quote(ObjectList.PCHOptions), 2);
1233
0
    }
1234
0
    WriteVariable("CompilerOptions", Quote(ObjectList.CompilerOptions), 2);
1235
0
    WriteVariable("CompilerOutputPath", Quote(ObjectList.CompilerOutputPath),
1236
0
                  2);
1237
0
    WriteVariable("CompilerOutputExtension",
1238
0
                  Quote(ObjectList.CompilerOutputExtension), 2);
1239
0
    WriteVariable("CompilerOutputKeepBaseExtension", "true", 2);
1240
0
    if (!ObjectList.CompilerInputUnity.empty()) {
1241
0
      WriteArray("CompilerInputUnity", Wrap(ObjectList.CompilerInputUnity), 2);
1242
0
    }
1243
0
    if (!ObjectList.CompilerInputFiles.empty()) {
1244
0
      WriteArray("CompilerInputFiles", Wrap(ObjectList.CompilerInputFiles), 2);
1245
0
    }
1246
0
    if (!ObjectList.AllowCaching) {
1247
0
      WriteVariable("AllowCaching", "false", 2);
1248
0
    }
1249
0
    if (!ObjectList.AllowDistribution) {
1250
0
      WriteVariable("AllowDistribution", "false", 2);
1251
0
    }
1252
0
    if (ObjectList.Hidden) {
1253
0
      WriteVariable("Hidden", "true", 2);
1254
0
    }
1255
0
  }
1256
0
  Indent(1);
1257
0
  *BuildFileStream << "}\n";
1258
0
}
1259
1260
void cmGlobalFastbuildGenerator::WriteLinker(
1261
  FastbuildLinkerNode const& LinkerNode, bool allowDistribution)
1262
0
{
1263
0
  WriteCommand(
1264
0
    LinkerNode.Type == FastbuildLinkerNode::EXECUTABLE         ? "Executable"
1265
0
      : LinkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY ? "DLL"
1266
0
                                                               : "Library",
1267
0
    (!LinkerNode.Name.empty() && LinkerNode.Name != LinkerNode.LinkerOutput)
1268
0
      ? Quote(LinkerNode.Name)
1269
0
      : "",
1270
0
    1);
1271
0
  Indent(1);
1272
0
  *BuildFileStream << "{\n";
1273
0
  {
1274
0
    if (!LinkerNode.PreBuildDependencies.empty()) {
1275
0
      WriteArray("PreBuildDependencies", Wrap(LinkerNode.PreBuildDependencies),
1276
0
                 2);
1277
0
    }
1278
0
    if (!allowDistribution) {
1279
0
      WriteVariable("AllowDistribution", "false", 2);
1280
0
    }
1281
1282
0
    if (!LinkerNode.Compiler.empty() &&
1283
0
        LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY) {
1284
0
      WriteVariable("Compiler", LinkerNode.Compiler, 2);
1285
0
      WriteVariable("CompilerOptions", Quote(LinkerNode.CompilerOptions), 2);
1286
0
      WriteVariable("CompilerOutputPath", Quote("."), 2);
1287
0
    }
1288
0
    if (!LocalEnvironment.empty()) {
1289
0
      WriteVariable("Environment", "." FASTBUILD_ENV_VAR_NAME, 2);
1290
0
    }
1291
1292
0
    WriteVariable(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
1293
0
                    ? "Librarian"
1294
0
                    : "Linker",
1295
0
                  Quote(LinkerNode.Linker), 2);
1296
1297
0
    WriteVariable(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
1298
0
                    ? "LibrarianOptions"
1299
0
                    : "LinkerOptions",
1300
0
                  Quote(LinkerNode.LinkerOptions), 2);
1301
1302
0
    WriteVariable(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
1303
0
                    ? "LibrarianOutput"
1304
0
                    : "LinkerOutput",
1305
0
                  Quote(LinkerNode.LinkerOutput), 2);
1306
1307
0
    if (!LinkerNode.LibrarianAdditionalInputs.empty()) {
1308
0
      WriteArray(LinkerNode.Type == FastbuildLinkerNode::STATIC_LIBRARY
1309
0
                   ? "LibrarianAdditionalInputs"
1310
0
                   : "Libraries",
1311
0
                 Wrap(LinkerNode.LibrarianAdditionalInputs), 2);
1312
0
    }
1313
0
    if (!LinkerNode.Libraries2.empty()) {
1314
0
      WriteArray("Libraries2", Wrap(LinkerNode.Libraries2), 2);
1315
0
    }
1316
0
    if (!LinkerNode.LibrarianAdditionalInputs.empty()) {
1317
1318
0
      if (!LinkerNode.LinkerType.empty()) {
1319
0
        WriteVariable("LinkerType", Quote(LinkerNode.LinkerType), 2);
1320
0
      }
1321
0
    }
1322
0
    if (LinkerNode.Type == FastbuildLinkerNode::EXECUTABLE ||
1323
0
        LinkerNode.Type == FastbuildLinkerNode::SHARED_LIBRARY) {
1324
0
      WriteVariable("LinkerLinkObjects",
1325
0
                    LinkerNode.LinkerLinkObjects ? "true" : "false", 2);
1326
1327
0
      if (!LinkerNode.LinkerStampExe.empty()) {
1328
0
        WriteVariable("LinkerStampExe", Quote(LinkerNode.LinkerStampExe), 2);
1329
0
        if (!LinkerNode.LinkerStampExeArgs.empty()) {
1330
0
          WriteVariable("LinkerStampExeArgs",
1331
0
                        Quote(LinkerNode.LinkerStampExeArgs), 2);
1332
0
        }
1333
0
      }
1334
0
    }
1335
0
    Indent(1);
1336
0
    *BuildFileStream << "}\n";
1337
0
  }
1338
0
}
1339
1340
void cmGlobalFastbuildGenerator::WriteAlias(FastbuildAliasNode const& Alias,
1341
                                            int indent)
1342
0
{
1343
0
  if (Alias.PreBuildDependencies.empty()) {
1344
0
    return;
1345
0
  }
1346
0
  auto const identPlus1 = indent + 1;
1347
0
  WriteCommand("Alias", Quote(Alias.Name), indent);
1348
0
  Indent(indent);
1349
0
  *BuildFileStream << "{\n";
1350
0
  WriteArray("Targets", Wrap(Alias.PreBuildDependencies), identPlus1);
1351
0
  if (Alias.Hidden) {
1352
0
    WriteVariable("Hidden", "true", identPlus1);
1353
0
  }
1354
0
  Indent(indent);
1355
0
  *BuildFileStream << "}\n";
1356
0
}
1357
1358
void cmGlobalFastbuildGenerator::WriteCopy(FastbuildCopyNode const& Copy)
1359
0
{
1360
0
  cmGlobalFastbuildGenerator::WriteCommand(
1361
0
    Copy.CopyDir ? "CopyDir" : "Copy",
1362
0
    cmGlobalFastbuildGenerator::Quote(Copy.Name), 1);
1363
0
  cmGlobalFastbuildGenerator::Indent(1);
1364
1365
0
  *BuildFileStream << "{\n";
1366
0
  WriteVariable("PreBuildDependencies",
1367
0
                cmGlobalFastbuildGenerator::Quote(Copy.PreBuildDependencies),
1368
0
                2);
1369
0
  WriteVariable(Copy.CopyDir ? "SourcePaths" : "Source",
1370
0
                cmGlobalFastbuildGenerator::Quote(Copy.Source), 2);
1371
0
  WriteVariable("Dest", cmGlobalFastbuildGenerator::Quote(Copy.Dest), 2);
1372
0
  cmGlobalFastbuildGenerator::Indent(1);
1373
0
  *BuildFileStream << "}\n";
1374
0
}
1375
1376
void cmGlobalFastbuildGenerator::WriteTarget(FastbuildTarget const& target)
1377
0
{
1378
0
  for (auto const& val : target.Variables) {
1379
0
    auto const& key = val.first;
1380
0
    auto const& value = val.second;
1381
0
    WriteVariable(key, cmGlobalFastbuildGenerator::Quote(value), 1);
1382
0
  }
1383
  // add_custom_commands(...)
1384
0
  for (auto const& alias : { target.ExecNodes }) {
1385
0
    this->WriteAlias(alias);
1386
0
  }
1387
1388
  // -deps Alias.
1389
0
  this->WriteAlias(target.DependenciesAlias);
1390
1391
  // PRE_BUILD.
1392
0
  for (auto const& alias : { target.PreBuildExecNodes }) {
1393
0
    this->WriteAlias(alias);
1394
0
  }
1395
1396
  // Copy commands.
1397
1398
0
  for (FastbuildCopyNode const& node : target.CopyNodes) {
1399
0
    this->WriteCopy(node);
1400
0
  }
1401
1402
  // Unity.
1403
0
  for (FastbuildUnityNode const& unity : target.UnityNodes) {
1404
0
    this->WriteUnity(unity);
1405
0
  }
1406
1407
  // Objects.
1408
0
  for (FastbuildObjectListNode const& objectList : target.ObjectListNodes) {
1409
0
    this->WriteObjectList(objectList, target.AllowDistribution);
1410
0
  }
1411
1412
0
  if (!target.PreLinkExecNodes.Nodes.empty()) {
1413
0
    for (auto const& exec : target.PreLinkExecNodes.Nodes) {
1414
0
      this->WriteExec(exec);
1415
0
    }
1416
0
    this->WriteAlias(target.PreLinkExecNodes.Alias);
1417
0
  }
1418
1419
  // Libraries / executables.
1420
0
  if (!target.LinkerNode.empty()) {
1421
0
    for (auto const& cudaDeviceLinkNode : target.CudaDeviceLinkNode) {
1422
0
      this->WriteLinker(cudaDeviceLinkNode, target.AllowDistribution);
1423
0
    }
1424
0
    for (auto const& linkerNode : target.LinkerNode) {
1425
0
      this->WriteLinker(linkerNode, target.AllowDistribution);
1426
0
    }
1427
0
  }
1428
1429
0
  if (!target.PostBuildExecNodes.Nodes.empty()) {
1430
0
    for (auto const& exec : target.PostBuildExecNodes.Nodes) {
1431
0
      this->WriteExec(exec);
1432
0
    }
1433
0
    this->WriteAlias(target.PostBuildExecNodes.Alias);
1434
0
  }
1435
1436
  // Aliases (if any).
1437
0
  for (FastbuildAliasNode const& alias : target.AliasNodes) {
1438
0
    this->WriteAlias(alias);
1439
0
  }
1440
0
}
1441
void cmGlobalFastbuildGenerator::WriteIDEProjects()
1442
0
{
1443
#if defined(_WIN32)
1444
  std::string platformToolset;
1445
  std::string const toolset =
1446
    this->GetSafeGlobalSetting("MSVC_TOOLSET_VERSION");
1447
  if (!toolset.empty()) {
1448
    platformToolset = cmStrCat('v', toolset);
1449
  }
1450
#endif
1451
0
  for (auto const& proj : IDEProjects) {
1452
0
    (void)proj;
1453
    // VS
1454
#if defined(_WIN32)
1455
    auto const& VSProj = proj.second.first;
1456
    WriteCommand("VCXProject", Quote(VSProj.Alias));
1457
    *this->BuildFileStream << "{\n";
1458
    WriteVariable("ProjectOutput", Quote(VSProj.ProjectOutput), 1);
1459
    if (!platformToolset.empty()) {
1460
      WriteVariable("PlatformToolset", Quote(platformToolset), 1);
1461
    }
1462
    WriteIDEProjectConfig(VSProj.ProjectConfigs);
1463
    WriteVSBuildCommands();
1464
    WriteIDEProjectCommon(VSProj);
1465
    *this->BuildFileStream << "}\n\n";
1466
1467
    // XCode
1468
#elif defined(__APPLE__)
1469
    auto const& XCodeProj = proj.second.second;
1470
    WriteCommand("XCodeProject", Quote(XCodeProj.Alias), 0);
1471
    *this->BuildFileStream << "{\n";
1472
    WriteVariable("ProjectOutput", Quote(XCodeProj.ProjectOutput), 1);
1473
    WriteIDEProjectConfig(XCodeProj.ProjectConfigs);
1474
    WriteXCodeBuildCommands();
1475
    WriteIDEProjectCommon(XCodeProj);
1476
    *this->BuildFileStream << "}\n\n";
1477
#endif
1478
0
  }
1479
1480
#if defined(_WIN32)
1481
  this->WriteSolution();
1482
#elif defined(__APPLE__)
1483
  this->WriteXCodeTopLevelProject();
1484
#endif
1485
0
}
1486
1487
std::string cmGlobalFastbuildGenerator::GetIDEBuildArgs() const
1488
0
{
1489
0
  cmValue const ideArgs = this->GetGlobalSetting(FASTBUILD_IDE_ARGS);
1490
0
  if (ideArgs) {
1491
0
    return cmStrCat(' ', ideArgs, ' ');
1492
0
  }
1493
0
  return FASTBUILD_DEFAULT_IDE_BUILD_ARGS;
1494
0
}
1495
1496
void cmGlobalFastbuildGenerator::WriteVSBuildCommands()
1497
0
{
1498
0
  std::string const ideArgs = this->GetIDEBuildArgs();
1499
0
  WriteVariable(
1500
0
    "ProjectBuildCommand",
1501
0
    Quote(cmStrCat(FASTBUILD_IDE_VS_COMMAND_PREFIX, this->FastbuildCommand,
1502
0
                   ideArgs, " ^$(ProjectName)")),
1503
0
    1);
1504
0
  WriteVariable(
1505
0
    "ProjectRebuildCommand",
1506
0
    Quote(cmStrCat(FASTBUILD_IDE_VS_COMMAND_PREFIX, this->FastbuildCommand,
1507
0
                   ideArgs, "-clean ^$(ProjectName)")),
1508
0
    1);
1509
0
  WriteVariable("ProjectCleanCommand",
1510
0
                Quote(cmStrCat(FASTBUILD_IDE_VS_COMMAND_PREFIX,
1511
0
                               this->FastbuildCommand, ideArgs, " clean")),
1512
0
                1);
1513
0
}
1514
void cmGlobalFastbuildGenerator::WriteXCodeBuildCommands()
1515
0
{
1516
0
  std::string const ideArgs = this->GetIDEBuildArgs();
1517
0
  WriteVariable("XCodeBuildToolPath", Quote(this->FastbuildCommand), 1);
1518
0
  WriteVariable("XCodeBuildToolArgs",
1519
0
                Quote(cmStrCat(ideArgs, "^$(FASTBUILD_TARGET)")), 1);
1520
0
  WriteVariable("XCodeBuildWorkingDir",
1521
0
                Quote(this->CMakeInstance->GetHomeOutputDirectory()), 1);
1522
0
}
1523
1524
void cmGlobalFastbuildGenerator::WriteIDEProjectCommon(
1525
  IDEProjectCommon const& project)
1526
0
{
1527
0
  WriteVariable("ProjectBasePath", Quote(project.ProjectBasePath), 1);
1528
  // So Fastbuild will pick up files relative to CMakeLists.txt
1529
0
  WriteVariable("ProjectInputPaths", Quote(project.ProjectBasePath), 1);
1530
0
}
1531
1532
void cmGlobalFastbuildGenerator::WriteIDEProjectConfig(
1533
  std::vector<IDEProjectConfig> const& configs, std::string const& keyName)
1534
0
{
1535
0
  std::vector<std::string> allConfigVariables;
1536
0
  for (auto const& config : configs) {
1537
0
    std::string configName = "Config" + config.Config;
1538
0
    WriteVariable(configName, "", 1);
1539
0
    Indent(1);
1540
0
    *this->BuildFileStream << "[\n";
1541
0
    WriteVariable("Config", Quote(config.Config), 2);
1542
0
    if (!config.Target.empty()) {
1543
0
      WriteVariable("Target", Quote(config.Target), 2);
1544
0
    }
1545
0
    if (!config.Platform.empty()) {
1546
0
      WriteVariable("Platform", Quote(config.Platform), 2);
1547
0
    }
1548
0
    Indent(1);
1549
0
    *this->BuildFileStream << "]\n";
1550
0
    allConfigVariables.emplace_back(std::move(configName));
1551
0
  }
1552
0
  WriteArray(keyName, Wrap(allConfigVariables, ".", ""), 1);
1553
0
}
1554
1555
void cmGlobalFastbuildGenerator::AddTargetAll()
1556
0
{
1557
0
  FastbuildAliasNode allAliasNode;
1558
0
  allAliasNode.Name = FASTBUILD_ALL_TARGET_NAME;
1559
1560
0
  for (auto const& targetBase : FastbuildTargets) {
1561
0
    if (targetBase->Type == FastbuildTargetType::LINK) {
1562
0
      auto const& target = static_cast<FastbuildTarget const&>(*targetBase);
1563
      // Add non-global and non-excluded targets to "all"
1564
0
      if (!target.IsGlobal && !target.ExcludeFromAll) {
1565
0
        allAliasNode.PreBuildDependencies.emplace(target.Name);
1566
0
      }
1567
0
    } else if (targetBase->Type == FastbuildTargetType::ALIAS) {
1568
0
      auto const& target = static_cast<FastbuildAliasNode const&>(*targetBase);
1569
0
      if (!target.ExcludeFromAll) {
1570
0
        allAliasNode.PreBuildDependencies.emplace(target.Name);
1571
0
      }
1572
0
    }
1573
0
  }
1574
0
  if (allAliasNode.PreBuildDependencies.empty()) {
1575
0
    allAliasNode.PreBuildDependencies.emplace(FASTBUILD_NOOP_FILE_NAME);
1576
0
  }
1577
0
  this->AddTarget(std::move(allAliasNode));
1578
0
}
1579
1580
void cmGlobalFastbuildGenerator::AddGlobCheckExec()
1581
0
{
1582
  // Tested in "RunCMake.file" test.
1583
0
  std::string const globScript =
1584
0
    this->GetCMakeInstance()->GetGlobVerifyScript();
1585
0
  if (!globScript.empty()) {
1586
1587
0
    FastbuildExecNode globCheck;
1588
0
    globCheck.Name = FASTBUILD_GLOB_CHECK_TARGET;
1589
0
    globCheck.ExecExecutable = cmSystemTools::GetCMakeCommand();
1590
0
    globCheck.ExecArguments =
1591
0
      cmStrCat("-P ", this->ConvertToFastbuildPath(globScript));
1592
0
    globCheck.ExecAlways = false;
1593
0
    globCheck.ExecUseStdOutAsOutput = false;
1594
0
    auto const cache = this->GetCMakeInstance()->GetGlobCacheEntries();
1595
0
    for (auto const& entry : cache) {
1596
0
      auto path = cmSystemTools::GetFilenamePath(entry.Expression);
1597
0
      auto expression = cmSystemTools::GetFilenameName(entry.Expression);
1598
0
      if (std::find(globCheck.ExecInputPath.begin(),
1599
0
                    globCheck.ExecInputPath.end(),
1600
0
                    path) == globCheck.ExecInputPath.end()) {
1601
0
        globCheck.ExecInputPath.emplace_back(std::move(path));
1602
0
      }
1603
0
      if (std::find(globCheck.ExecInputPattern.begin(),
1604
0
                    globCheck.ExecInputPattern.end(),
1605
0
                    expression) == globCheck.ExecInputPattern.end()) {
1606
0
        globCheck.ExecInputPattern.emplace_back(std::move(expression));
1607
0
      }
1608
0
    }
1609
0
    globCheck.ExecOutput = this->ConvertToFastbuildPath(
1610
0
      this->GetCMakeInstance()->GetGlobVerifyStamp());
1611
0
    this->AddTarget(std::move(globCheck));
1612
0
  }
1613
0
}
1614
void cmGlobalFastbuildGenerator::WriteSolution()
1615
0
{
1616
0
  std::string const solutionName = LocalGenerators[0]->GetProjectName();
1617
0
  std::unordered_map<std::string /*folder*/, std::vector<std::string>>
1618
0
    VSProjectFolders;
1619
0
  std::unordered_map<std::string /*project*/,
1620
0
                     std::vector<std::string> /*deps*/>
1621
0
    VSProjectDeps;
1622
0
  std::vector<std::string> VSProjectsWithoutFolder;
1623
1624
0
  for (auto const& IDEProj : IDEProjects) {
1625
0
    auto const VSProj = IDEProj.second.first;
1626
0
    VSProjectFolders[VSProj.folder].emplace_back(VSProj.Alias);
1627
0
    auto& deps = VSProjectDeps[VSProj.Alias];
1628
0
    deps.reserve(VSProj.deps.size());
1629
0
    for (auto const& dep : VSProj.deps) {
1630
0
      if (dep.Type == FastbuildTargetDepType::REGULAR) {
1631
0
        deps.push_back(cmStrCat(dep.Name, FASTBUILD_VS_PROJECT_SUFFIX));
1632
0
      }
1633
0
    }
1634
0
  }
1635
1636
0
  WriteCommand("VSSolution", Quote("solution"));
1637
0
  *this->BuildFileStream << "{\n";
1638
1639
0
  WriteVariable("SolutionOutput",
1640
0
                Quote(cmJoin({ "VisualStudio", solutionName + ".sln" }, "/")),
1641
0
                1);
1642
1643
0
  auto const& configs = IDEProjects.begin()->second.first.ProjectConfigs;
1644
0
  WriteIDEProjectConfig(configs, "SolutionConfigs");
1645
0
  int folderNumber = 0;
1646
0
  std::vector<std::string> folders;
1647
0
  for (auto& item : VSProjectFolders) {
1648
0
    auto const& pathToFolder = item.first;
1649
0
    auto& projectsInFolder = item.second;
1650
0
    if (pathToFolder.empty()) {
1651
0
      std::move(projectsInFolder.begin(), projectsInFolder.end(),
1652
0
                std::back_inserter(VSProjectsWithoutFolder));
1653
0
    } else {
1654
0
      std::string folderName = cmStrCat("Folder_", ++folderNumber);
1655
0
      WriteStruct(
1656
0
        folderName,
1657
0
        { { "Path", Quote(pathToFolder) },
1658
0
          { "Projects",
1659
0
            cmStrCat('{', cmJoin(Wrap(projectsInFolder), ","), '}') } },
1660
0
        1);
1661
0
      folders.emplace_back(std::move(folderName));
1662
0
    }
1663
0
  }
1664
0
  if (!folders.empty()) {
1665
0
    WriteArray("SolutionFolders ", Wrap(folders, ".", ""), 1);
1666
0
  }
1667
1668
0
  int depNumber = 0;
1669
0
  std::vector<std::string> dependencies;
1670
0
  for (auto const& dep : VSProjectDeps) {
1671
0
    std::string const& projectName = dep.first;
1672
0
    std::vector<std::string> const& projectDeps = dep.second;
1673
    // This project has some deps.
1674
0
    if (!projectDeps.empty()) {
1675
0
      std::string depName = cmStrCat("Deps_", ++depNumber);
1676
0
      WriteStruct(depName,
1677
0
                  {
1678
0
                    { "Projects", Quote(projectName) },
1679
0
                    { "Dependencies",
1680
0
                      cmStrCat('{', cmJoin(Wrap(projectDeps), ","), '}') },
1681
0
                  },
1682
0
                  1);
1683
0
      dependencies.emplace_back(std::move(depName));
1684
0
    }
1685
0
  }
1686
1687
0
  if (!dependencies.empty()) {
1688
0
    WriteArray("SolutionDependencies  ", Wrap(dependencies, ".", ""), 1);
1689
0
  }
1690
0
  if (!VSProjectsWithoutFolder.empty()) {
1691
0
    WriteArray("SolutionProjects", Wrap(VSProjectsWithoutFolder), 1);
1692
0
  }
1693
1694
0
  *this->BuildFileStream << "}\n";
1695
0
}
1696
1697
void cmGlobalFastbuildGenerator::WriteXCodeTopLevelProject()
1698
0
{
1699
0
  std::string const projectName = LocalGenerators[0]->GetProjectName();
1700
0
  std::vector<std::string> XCodeProjects;
1701
0
  for (auto const& IDEProj : IDEProjects) {
1702
0
    auto const XCodeProj = IDEProj.second.second;
1703
0
    XCodeProjects.emplace_back(XCodeProj.Alias);
1704
0
  }
1705
1706
0
  WriteCommand("XCodeProject", Quote("xcode"));
1707
0
  *this->BuildFileStream << "{\n";
1708
1709
0
  WriteVariable(
1710
0
    "ProjectOutput",
1711
0
    Quote(
1712
0
      cmJoin({ "XCode", projectName + ".xcodeproj", "project.pbxproj" }, "/")),
1713
0
    1);
1714
0
  WriteVariable("ProjectBasePath", Quote(FASTBUILD_XCODE_BASE_PATH), 1);
1715
1716
0
  auto const& configs = IDEProjects.begin()->second.second.ProjectConfigs;
1717
0
  WriteIDEProjectConfig(configs);
1718
0
  WriteArray("ProjectFiles", Wrap(XCodeProjects), 1);
1719
1720
0
  *this->BuildFileStream << "}\n";
1721
0
}
1722
1723
void cmGlobalFastbuildGenerator::LogMessage(std::string const& m) const
1724
0
{
1725
0
  static bool const verbose = GlobalSettingIsOn(FASTBUILD_VERBOSE_GENERATOR) ||
1726
0
    cmSystemTools::HasEnv(FASTBUILD_VERBOSE_GENERATOR);
1727
0
  if (verbose) {
1728
0
    cmSystemTools::Message(m);
1729
0
  }
1730
0
}
1731
void cmGlobalFastbuildGenerator::AddFileToClean(std::string const& file)
1732
0
{
1733
0
  AllFilesToClean.insert(file);
1734
0
}
1735
1736
std::string cmGlobalFastbuildGenerator::GetExternalShellExecutable()
1737
0
{
1738
  // FindProgram is expensive - touches filesystem and makes syscalls, so cache
1739
  // it.
1740
0
  static std::string const cached =
1741
#ifdef _WIN32
1742
    cmSystemTools::FindProgram(
1743
      "cmd.exe", std::vector<std::string>{ "C:\\Windows\\System32" });
1744
#else
1745
0
    cmSystemTools::FindProgram("sh", std::vector<std::string>{ "/bin" });
1746
0
#endif
1747
0
  return cached;
1748
0
}
1749
1750
void cmGlobalFastbuildGenerator::WriteTargetRebuildBFF()
1751
0
{
1752
0
  std::vector<std::string> implicitDeps;
1753
0
  for (auto& lg : LocalGenerators) {
1754
0
    std::vector<std::string> const& lf = lg->GetMakefile()->GetListFiles();
1755
0
    for (auto const& dep : lf) {
1756
0
      implicitDeps.push_back(this->ConvertToFastbuildPath(dep));
1757
0
    }
1758
0
  }
1759
0
  auto const* cmake = this->GetCMakeInstance();
1760
0
  std::string outDir = cmake->GetHomeOutputDirectory() + '/';
1761
1762
0
  implicitDeps.push_back(outDir + "CMakeCache.txt");
1763
1764
0
  FastbuildExecNode rebuildBFF;
1765
0
  rebuildBFF.Name = FASTBUILD_REBUILD_BFF_TARGET_NAME;
1766
0
  if (!this->GetCMakeInstance()->GetGlobVerifyScript().empty()) {
1767
0
    implicitDeps.emplace_back(this->GetCMakeInstance()->GetGlobVerifyStamp());
1768
0
  }
1769
1770
0
  std::sort(implicitDeps.begin(), implicitDeps.end());
1771
0
  implicitDeps.erase(std::unique(implicitDeps.begin(), implicitDeps.end()),
1772
0
                     implicitDeps.end());
1773
0
  std::string args =
1774
0
    cmStrCat("--regenerate-during-build",
1775
0
             (this->GetCMakeInstance()->GetIgnoreCompileWarningAsError()
1776
0
                ? " --compile-no-warning-as-error"
1777
0
                : ""),
1778
0
             (this->GetCMakeInstance()->GetIgnoreLinkWarningAsError()
1779
0
                ? " --link-no-warning-as-error"
1780
0
                : ""),
1781
0
             " -S", QuoteIfHasSpaces(cmake->GetHomeDirectory()), " -B",
1782
0
             QuoteIfHasSpaces(cmake->GetHomeOutputDirectory()));
1783
1784
0
  rebuildBFF.ExecArguments = std::move(args);
1785
0
  rebuildBFF.ExecInput = implicitDeps;
1786
0
  rebuildBFF.ExecExecutable = cmSystemTools::GetCMakeCommand();
1787
0
  rebuildBFF.ExecWorkingDir = outDir;
1788
0
  rebuildBFF.ExecOutput = outDir + FASTBUILD_BUILD_FILE;
1789
0
  this->WriteExec(rebuildBFF, 0);
1790
0
}
1791
1792
void cmGlobalFastbuildGenerator::WriteCleanScript()
1793
0
{
1794
0
  std::string const path =
1795
0
    cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), '/',
1796
0
             FASTBUILD_CLEAN_SCRIPT_NAME);
1797
0
  cmsys::ofstream scriptFile(path.c_str(), std::ios::out | std::ios::binary);
1798
0
  if (!scriptFile.is_open()) {
1799
0
    cmSystemTools::Error("Failed to open: " FASTBUILD_CLEAN_SCRIPT_NAME);
1800
0
    return;
1801
0
  }
1802
0
  for (std::string const& file : AllFilesToClean) {
1803
#if defined(_WIN32)
1804
    scriptFile << "del /f /q "
1805
               << cmSystemTools::ConvertToWindowsOutputPath(file) << "\n";
1806
#else
1807
0
    scriptFile << "rm -f " << file << '\n';
1808
0
#endif
1809
0
  }
1810
0
}
1811
1812
void cmGlobalFastbuildGenerator::WriteTargetClean()
1813
0
{
1814
0
  if (AllFilesToClean.empty()) {
1815
0
    FastbuildAliasNode clean;
1816
0
    clean.Name = FASTBUILD_CLEAN_TARGET_NAME;
1817
0
    clean.PreBuildDependencies.emplace(FASTBUILD_CLEAN_FILE_NAME);
1818
0
    WriteAlias(clean, 0);
1819
0
    return;
1820
0
  }
1821
0
  WriteCleanScript();
1822
0
  FastbuildExecNode clean;
1823
0
  clean.Name = FASTBUILD_CLEAN_TARGET_NAME;
1824
0
  clean.ExecExecutable = GetExternalShellExecutable();
1825
0
  clean.ExecArguments =
1826
0
    FASTBUILD_SCRIPT_FILE_ARG FASTBUILD_1_INPUT_PLACEHOLDER;
1827
0
  clean.ExecInput = { FASTBUILD_CLEAN_SCRIPT_NAME };
1828
0
  clean.ExecAlways = true;
1829
0
  clean.ExecUseStdOutAsOutput = true;
1830
0
  clean.ExecOutput = FASTBUILD_CLEAN_FILE_NAME;
1831
0
  clean.ExecWorkingDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
1832
0
  WriteExec(clean, 0);
1833
0
}
1834
1835
void cmGlobalFastbuildGenerator::WriteTargets()
1836
0
{
1837
0
  std::string const outputDir = this->CMakeInstance->GetHomeOutputDirectory();
1838
0
  LogMessage("GetHomeOutputDirectory: " + outputDir);
1839
  // Noop file that 'all' can alias to if we don't have any other targets...
1840
  // The exact location of the "noop" file is verified in one of the tests in
1841
  // "RunCMake.CMakePresetsPackage" test suite.
1842
0
  cmSystemTools::Touch(cmStrCat(this->CMakeInstance->GetHomeOutputDirectory(),
1843
0
                                '/', FASTBUILD_NOOP_FILE_NAME),
1844
0
                       true);
1845
0
  cmSystemTools::Touch(cmStrCat(this->CMakeInstance->GetHomeOutputDirectory(),
1846
0
                                '/', FASTBUILD_CLEAN_FILE_NAME),
1847
0
                       true);
1848
  // Add "all" utility target before sorting, so we can correctly sort
1849
  // targets that depend on it
1850
0
  AddTargetAll();
1851
0
  TopologicalSort(FastbuildTargets);
1852
1853
0
  AddGlobCheckExec();
1854
1855
0
  for (auto const& targetBase : FastbuildTargets) {
1856
0
    this->WriteComment("Target definition: " + targetBase->Name);
1857
    // Target start.
1858
0
    *BuildFileStream << "{\n";
1859
1860
0
    if (targetBase->Type == FastbuildTargetType::EXEC) {
1861
0
      this->WriteExec(static_cast<FastbuildExecNode const&>(*targetBase));
1862
0
    } else if (targetBase->Type == FastbuildTargetType::ALIAS) {
1863
0
      this->WriteAlias(static_cast<FastbuildAliasNode const&>(*targetBase));
1864
0
    } else if (targetBase->Type == FastbuildTargetType::LINK) {
1865
0
      auto const& target = static_cast<FastbuildTarget const&>(*targetBase);
1866
0
      this->WriteTarget(target);
1867
0
    }
1868
    // Target end.
1869
0
    *BuildFileStream << "}\n";
1870
0
  }
1871
1872
0
  if (!this->GetCMakeInstance()->GetIsInTryCompile()) {
1873
0
    if (!IDEProjects.empty()) {
1874
0
      this->WriteIDEProjects();
1875
0
    }
1876
0
  }
1877
0
  this->WriteTargetClean();
1878
0
  this->WriteTargetRebuildBFF();
1879
0
}
1880
1881
std::string cmGlobalFastbuildGenerator::GetTargetName(
1882
  cmGeneratorTarget const* GeneratorTarget) const
1883
0
{
1884
0
  std::string targetName =
1885
0
    GeneratorTarget->GetLocalGenerator()->GetCurrentBinaryDirectory();
1886
0
  targetName += "/";
1887
0
  targetName += GeneratorTarget->GetName();
1888
0
  targetName = this->ConvertToFastbuildPath(targetName);
1889
0
  return targetName;
1890
0
}
1891
1892
cm::optional<FastbuildTarget>
1893
cmGlobalFastbuildGenerator::GetTargetByOutputName(
1894
  std::string const& output) const
1895
0
{
1896
0
  for (auto const& targetBase : FastbuildTargets) {
1897
0
    if (targetBase->Type == FastbuildTargetType::LINK) {
1898
0
      auto const& target = static_cast<FastbuildTarget const&>(*targetBase);
1899
0
      if (std::any_of(target.LinkerNode.begin(), target.LinkerNode.end(),
1900
0
                      [&output](FastbuildLinkerNode const& target_) {
1901
0
                        return target_.LinkerOutput == output;
1902
0
                      })) {
1903
0
        return target;
1904
0
      }
1905
0
    }
1906
0
  }
1907
0
  return cm::nullopt;
1908
0
}
1909
1910
void cmGlobalFastbuildGenerator::AddIDEProject(
1911
  FastbuildTargetBase const& target, std::string const& config)
1912
0
{
1913
0
  auto const& configs = GetConfigNames();
1914
0
  if (std::find(configs.begin(), configs.end(), config) == configs.end()) {
1915
0
    LogMessage("Config " + config + " doesn't exist, IDE projest for " +
1916
0
               target.Name + " won't be generated");
1917
0
    return;
1918
0
  }
1919
0
  auto& IDEProject = IDEProjects[target.BaseName];
1920
0
  auto const relativeSubdir = cmSystemTools::RelativePath(
1921
0
    this->GetCMakeInstance()->GetHomeDirectory(), target.BasePath);
1922
  // VS
1923
0
  auto& VSProject = IDEProject.first;
1924
0
  VSProject.Alias = cmStrCat(target.BaseName, FASTBUILD_VS_PROJECT_SUFFIX);
1925
0
  VSProject.ProjectOutput = cmStrCat("VisualStudio/Projects/", relativeSubdir,
1926
0
                                     '/', target.BaseName + ".vcxproj");
1927
0
  VSProject.ProjectBasePath = target.BasePath;
1928
0
  VSProject.folder = relativeSubdir;
1929
0
  VSProject.deps = target.PreBuildDependencies;
1930
  // XCode
1931
0
  auto& XCodeProject = IDEProject.second;
1932
0
  XCodeProject.Alias = target.BaseName + "-xcodeproj";
1933
0
  XCodeProject.ProjectOutput =
1934
0
    cmStrCat("XCode/Projects/", relativeSubdir, '/',
1935
0
             target.BaseName + ".xcodeproj/project.pbxproj");
1936
0
  XCodeProject.ProjectBasePath = target.BasePath;
1937
1938
0
  IDEProjectConfig VSConfig;
1939
0
  VSConfig.Platform = "X64";
1940
0
  IDEProjectConfig XCodeConfig;
1941
0
  VSConfig.Target = XCodeConfig.Target = target.Name;
1942
0
  VSConfig.Config = XCodeConfig.Config = config.empty() ? "DEFAULT" : config;
1943
1944
0
  VSProject.ProjectConfigs.emplace_back(std::move(VSConfig));
1945
0
  XCodeProject.ProjectConfigs.emplace_back(std::move(XCodeConfig));
1946
0
}
1947
1948
bool cmGlobalFastbuildGenerator::IsExcluded(cmGeneratorTarget* target)
1949
0
{
1950
0
  return cmGlobalGenerator::IsExcluded(LocalGenerators[0].get(), target);
1951
0
}
1952
std::vector<std::string> const& cmGlobalFastbuildGenerator::GetConfigNames()
1953
  const
1954
0
{
1955
0
  return static_cast<cmLocalFastbuildGenerator const*>(
1956
0
           this->LocalGenerators.front().get())
1957
0
    ->GetConfigNames();
1958
0
}
1959
1960
bool cmGlobalFastbuildGenerator::Open(std::string const& bindir,
1961
                                      std::string const& projectName,
1962
                                      bool dryRun)
1963
0
{
1964
#ifdef _WIN32
1965
  std::string sln = bindir + "/VisualStudio/" + projectName + ".sln";
1966
1967
  if (dryRun) {
1968
    return cmSystemTools::FileExists(sln, true);
1969
  }
1970
1971
  sln = cmSystemTools::ConvertToOutputPath(sln);
1972
1973
  auto OpenSolution = [](std::string pathToSolution) {
1974
    HRESULT comInitialized =
1975
      CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
1976
    if (FAILED(comInitialized)) {
1977
      return false;
1978
    }
1979
1980
    HINSTANCE hi = ShellExecuteA(NULL, "open", pathToSolution.c_str(), NULL,
1981
                                 NULL, SW_SHOWNORMAL);
1982
1983
    CoUninitialize();
1984
1985
    return reinterpret_cast<intptr_t>(hi) > 32;
1986
  };
1987
1988
  return std::async(std::launch::async, OpenSolution, sln).get();
1989
#else
1990
0
  return cmGlobalCommonGenerator::Open(bindir, projectName, dryRun);
1991
0
#endif
1992
0
}