Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmake.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 "cmake.h"
4
5
#include <algorithm>
6
#include <array>
7
#include <cassert>
8
#include <chrono>
9
#include <climits>
10
#include <cstdio>
11
#include <cstdlib>
12
#include <initializer_list>
13
#include <iomanip>
14
#include <iostream>
15
#include <iterator>
16
#include <sstream>
17
#include <stdexcept>
18
#include <utility>
19
20
#include <cm/memory>
21
#include <cm/optional>
22
#include <cm/string_view>
23
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
24
#  include <cm/iterator>
25
#endif
26
27
#include <cmext/algorithm>
28
#include <cmext/string_view>
29
30
#include <sys/types.h>
31
32
#include "cmsys/FStream.hxx"
33
#include "cmsys/Glob.hxx"
34
#include "cmsys/RegularExpression.hxx"
35
36
#include "cm_sys_stat.h"
37
38
#include "cmBuildOptions.h"
39
#include "cmCMakePath.h"
40
#include "cmCMakePresetsGraph.h"
41
#include "cmCommandLineArgument.h"
42
#include "cmCommands.h"
43
#ifdef CMake_ENABLE_DEBUGGER
44
#  include "cmDebuggerAdapter.h"
45
#  ifdef _WIN32
46
#    include "cmDebuggerWindowsPipeConnection.h"
47
#  else //!_WIN32
48
#    include "cmDebuggerPosixPipeConnection.h"
49
#  endif //_WIN32
50
#endif
51
#include "cmDocumentation.h"
52
#include "cmDocumentationEntry.h"
53
#include "cmDuration.h"
54
#include "cmExternalMakefileProjectGenerator.h"
55
#include "cmFileTimeCache.h"
56
#include "cmGeneratorTarget.h"
57
#include "cmGlobCacheEntry.h" // IWYU pragma: keep
58
#include "cmGlobalGenerator.h"
59
#include "cmGlobalGeneratorFactory.h"
60
#include "cmLinkLineComputer.h"
61
#include "cmLocalGenerator.h"
62
#include "cmMakefile.h"
63
#if !defined(CMAKE_BOOTSTRAP)
64
#  include "cmMakefileProfilingData.h"
65
#endif
66
#include "cmJSONState.h"
67
#include "cmList.h"
68
#include "cmMessenger.h"
69
#ifndef CMAKE_BOOTSTRAP
70
#  include "cmSarifLog.h"
71
#endif
72
#include "cmState.h"
73
#include "cmStateDirectory.h"
74
#include "cmStringAlgorithms.h"
75
#include "cmSystemTools.h"
76
#include "cmTarget.h"
77
#include "cmTargetLinkLibraryType.h"
78
#include "cmUVProcessChain.h"
79
#include "cmUtils.hxx"
80
#include "cmVersionConfig.h"
81
#include "cmWorkingDirectory.h"
82
83
#if !defined(CMAKE_BOOTSTRAP)
84
#  include <unordered_map>
85
86
#  include <cm3p/curl/curl.h>
87
#  include <cm3p/json/writer.h>
88
89
#  include "cmConfigureLog.h"
90
#  include "cmFileAPI.h"
91
#  include "cmGraphVizWriter.h"
92
#  include "cmInstrumentation.h"
93
#  include "cmInstrumentationQuery.h"
94
#  include "cmVariableWatch.h"
95
#endif
96
97
#if defined(__MINGW32__) && defined(CMAKE_BOOTSTRAP)
98
#  define CMAKE_BOOT_MINGW
99
#endif
100
101
// include the generator
102
#if defined(_WIN32) && !defined(__CYGWIN__)
103
#  if !defined(CMAKE_BOOT_MINGW)
104
#    include <cmext/memory>
105
106
#    include "cmGlobalBorlandMakefileGenerator.h"
107
#    include "cmGlobalFastbuildGenerator.h"
108
#    include "cmGlobalJOMMakefileGenerator.h"
109
#    include "cmGlobalNMakeMakefileGenerator.h"
110
#    include "cmGlobalVisualStudio14Generator.h"
111
#    include "cmGlobalVisualStudioVersionedGenerator.h"
112
#    include "cmVSSetupHelper.h"
113
114
#    define CMAKE_HAVE_VS_GENERATORS
115
#  endif
116
#  include "cmGlobalMSYSMakefileGenerator.h"
117
#  include "cmGlobalMinGWMakefileGenerator.h"
118
#else
119
#endif
120
#if defined(CMAKE_USE_WMAKE)
121
#  include "cmGlobalWatcomWMakeGenerator.h"
122
#endif
123
#if !defined(CMAKE_BOOTSTRAP)
124
#  include "cmGlobalNinjaGenerator.h"
125
#  include "cmGlobalUnixMakefileGenerator3.h"
126
#elif defined(CMAKE_BOOTSTRAP_MAKEFILES)
127
#  include "cmGlobalUnixMakefileGenerator3.h"
128
#elif defined(CMAKE_BOOTSTRAP_NINJA)
129
#  include "cmGlobalNinjaGenerator.h"
130
#endif
131
#include "cmGlobalFastbuildGenerator.h"
132
133
#if !defined(CMAKE_BOOTSTRAP)
134
#  include "cmExtraCodeBlocksGenerator.h"
135
#  include "cmExtraCodeLiteGenerator.h"
136
#  include "cmExtraEclipseCDT4Generator.h"
137
#  include "cmExtraKateGenerator.h"
138
#  include "cmExtraSublimeTextGenerator.h"
139
140
// NOTE: the __linux__ macro is predefined on Android host too, but
141
// main CMakeLists.txt filters out this generator by host name.
142
#  if (defined(__linux__) && !defined(__ANDROID__)) || defined(_WIN32)
143
#    include "cmGlobalGhsMultiGenerator.h"
144
#  endif
145
#endif
146
147
#if defined(__APPLE__)
148
#  if !defined(CMAKE_BOOTSTRAP)
149
#    include "cmGlobalXCodeGenerator.h"
150
151
#    define CMAKE_USE_XCODE 1
152
#  endif
153
#  include <sys/resource.h>
154
#  include <sys/time.h>
155
#endif
156
157
namespace {
158
159
#if !defined(CMAKE_BOOTSTRAP)
160
using JsonValueMapType = std::unordered_map<std::string, Json::Value>;
161
#endif
162
163
35
auto IgnoreAndTrueLambda = [](std::string const&, cmake*) -> bool {
164
35
  return true;
165
35
};
166
167
using CommandArgument =
168
  cmCommandLineArgument<bool(std::string const& value, cmake* state)>;
169
170
#ifndef CMAKE_BOOTSTRAP
171
void cmWarnUnusedCliWarning(std::string const& variable, int /*unused*/,
172
                            void* ctx, char const* /*unused*/,
173
                            cmMakefile const* /*unused*/)
174
0
{
175
0
  cmake* cm = reinterpret_cast<cmake*>(ctx);
176
0
  cm->MarkCliAsUsed(variable);
177
0
}
178
#endif
179
180
bool cmakeCheckStampFile(std::string const& stampName)
181
0
{
182
  // The stamp file does not exist.  Use the stamp dependencies to
183
  // determine whether it is really out of date.  This works in
184
  // conjunction with cmLocalVisualStudio7Generator to avoid
185
  // repeatedly re-running CMake when the user rebuilds the entire
186
  // solution.
187
0
  std::string stampDepends = cmStrCat(stampName, ".depend");
188
#if defined(_WIN32) || defined(__CYGWIN__)
189
  cmsys::ifstream fin(stampDepends.c_str(), std::ios::in | std::ios::binary);
190
#else
191
0
  cmsys::ifstream fin(stampDepends.c_str());
192
0
#endif
193
0
  if (!fin) {
194
    // The stamp dependencies file cannot be read.  Just assume the
195
    // build system is really out of date.
196
0
    std::cout << "CMake is re-running because " << stampName
197
0
              << " dependency file is missing.\n";
198
0
    return false;
199
0
  }
200
201
  // Compare the stamp dependencies against the dependency file itself.
202
0
  {
203
0
    cmFileTimeCache ftc;
204
0
    std::string dep;
205
0
    while (cmSystemTools::GetLineFromStream(fin, dep)) {
206
0
      int result;
207
0
      if (!dep.empty() && dep[0] != '#' &&
208
0
          (!ftc.Compare(stampDepends, dep, &result) || result < 0)) {
209
        // The stamp depends file is older than this dependency.  The
210
        // build system is really out of date.
211
        /* clang-format off */
212
0
        std::cout << "CMake is re-running because " << stampName
213
0
                  << " is out-of-date.\n"
214
0
                     "  the file '" << dep << "'\n"
215
0
                     "  is newer than '" << stampDepends << "'\n"
216
0
                     "  result='" << result << "'\n";
217
        /* clang-format on */
218
0
        return false;
219
0
      }
220
0
    }
221
0
  }
222
223
  // The build system is up to date.  The stamp file has been removed
224
  // by the VS IDE due to a "rebuild" request.  Restore it atomically.
225
0
  std::ostringstream stampTempStream;
226
0
  stampTempStream << stampName << ".tmp" << cmSystemTools::RandomNumber();
227
0
  std::string stampTemp = stampTempStream.str();
228
0
  {
229
    // TODO: Teach cmGeneratedFileStream to use a random temp file (with
230
    // multiple tries in unlikely case of conflict) and use that here.
231
0
    cmsys::ofstream stamp(stampTemp.c_str());
232
0
    stamp << "# CMake generation timestamp file for this directory.\n";
233
0
  }
234
0
  std::string err;
235
0
  if (cmSystemTools::RenameFile(stampTemp, stampName,
236
0
                                cmSystemTools::Replace::Yes, &err) ==
237
0
      cmSystemTools::RenameResult::Success) {
238
    // CMake does not need to re-run because the stamp file is up-to-date.
239
0
    return true;
240
0
  }
241
0
  cmSystemTools::RemoveFile(stampTemp);
242
0
  cmSystemTools::Error(
243
0
    cmStrCat("Cannot restore timestamp \"", stampName, "\": ", err));
244
0
  return false;
245
0
}
246
247
bool cmakeCheckStampList(std::string const& stampList)
248
0
{
249
  // If the stamp list does not exist CMake must rerun to generate it.
250
0
  if (!cmSystemTools::FileExists(stampList)) {
251
0
    std::cout << "CMake is re-running because generate.stamp.list "
252
0
                 "is missing.\n";
253
0
    return false;
254
0
  }
255
0
  cmsys::ifstream fin(stampList.c_str());
256
0
  if (!fin) {
257
0
    std::cout << "CMake is re-running because generate.stamp.list "
258
0
                 "could not be read.\n";
259
0
    return false;
260
0
  }
261
262
  // Check each stamp.
263
0
  std::string stampName;
264
0
  while (cmSystemTools::GetLineFromStream(fin, stampName)) {
265
0
    if (!cmakeCheckStampFile(stampName)) {
266
0
      return false;
267
0
    }
268
0
  }
269
0
  return true;
270
0
}
271
272
} // namespace
273
274
cmDocumentationEntry cmake::CMAKE_STANDARD_OPTIONS_TABLE[19] = {
275
  { "-S <path-to-source>", "Explicitly specify a source directory." },
276
  { "-B <path-to-build>", "Explicitly specify a build directory." },
277
  { "-C <initial-cache>", "Pre-load a script to populate the cache." },
278
  { "-D <var>[:<type>]=<value>", "Create or update a cmake cache entry." },
279
  { "-U <globbing_expr>", "Remove matching entries from CMake cache." },
280
  { "-G <generator-name>", "Specify a build system generator." },
281
  { "-T <toolset-name>", "Specify toolset name if supported by generator." },
282
  { "-A <platform-name>", "Specify platform name if supported by generator." },
283
  { "--toolchain <file>", "Specify toolchain file [CMAKE_TOOLCHAIN_FILE]." },
284
  { "--install-prefix <directory>",
285
    "Specify install directory [CMAKE_INSTALL_PREFIX]." },
286
  { "--project-file <project-file-name>",
287
    "Specify an alternate project file name." },
288
  { "-Wdev", "Enable developer warnings." },
289
  { "-Wno-dev", "Suppress developer warnings." },
290
  { "-Werror=dev", "Make developer warnings errors." },
291
  { "-Wno-error=dev", "Make developer warnings not errors." },
292
  { "-Wdeprecated", "Enable deprecation warnings." },
293
  { "-Wno-deprecated", "Suppress deprecation warnings." },
294
  { "-Werror=deprecated",
295
    "Make deprecated macro and function warnings "
296
    "errors." },
297
  { "-Wno-error=deprecated",
298
    "Make deprecated macro and function warnings "
299
    "not errors." }
300
};
301
302
cmake::cmake(cmState::Role role, cmState::TryCompile isTryCompile)
303
35
  : CMakeWorkingDirectory(cmSystemTools::GetLogicalWorkingDirectory())
304
35
  , FileTimeCache(cm::make_unique<cmFileTimeCache>())
305
#ifndef CMAKE_BOOTSTRAP
306
35
  , VariableWatch(cm::make_unique<cmVariableWatch>())
307
#endif
308
35
  , State(cm::make_unique<cmState>(role, isTryCompile))
309
35
  , Messenger(cm::make_unique<cmMessenger>())
310
35
{
311
35
  this->TraceFile.close();
312
35
  this->CurrentSnapshot = this->State->CreateBaseSnapshot();
313
314
#ifdef __APPLE__
315
  struct rlimit rlp;
316
  if (!getrlimit(RLIMIT_STACK, &rlp)) {
317
    if (rlp.rlim_cur != rlp.rlim_max) {
318
      rlp.rlim_cur = rlp.rlim_max;
319
      setrlimit(RLIMIT_STACK, &rlp);
320
    }
321
  }
322
#endif
323
324
35
  this->AddDefaultGenerators();
325
35
  this->AddDefaultExtraGenerators();
326
35
  if (role == cmState::Role::Project || role == cmState::Role::FindPackage ||
327
35
      role == cmState::Role::Script || role == cmState::Role::CTest ||
328
35
      role == cmState::Role::CPack) {
329
35
    this->AddScriptingCommands();
330
35
  }
331
35
  if (role == cmState::Role::Project || role == cmState::Role::FindPackage) {
332
0
    this->AddProjectCommands();
333
0
  }
334
335
35
  if (role == cmState::Role::Project || role == cmState::Role::Help) {
336
0
    this->LoadEnvironmentPresets();
337
0
  }
338
339
  // Make sure we can capture the build tool output.
340
35
  cmSystemTools::EnableVSConsoleOutput();
341
342
  // Set up a list of source and header extensions.
343
  // These are used to find files when the extension is not given.
344
35
  {
345
35
    auto setupExts = [](FileExtensions& exts,
346
210
                        std::initializer_list<cm::string_view> extList) {
347
      // Fill ordered vector
348
210
      exts.ordered.reserve(extList.size());
349
1.19k
      for (cm::string_view ext : extList) {
350
1.19k
        exts.ordered.emplace_back(ext);
351
1.19k
      }
352
      // Fill unordered set
353
210
      exts.unordered.insert(exts.ordered.begin(), exts.ordered.end());
354
210
    };
355
356
    // The "c" extension MUST precede the "C" extension.
357
35
    setupExts(this->CLikeSourceFileExtensions,
358
35
              { "c", "C", "c++", "cc", "cpp", "cxx", "cu", "mpp", "m", "M",
359
35
                "mm", "ixx", "cppm", "ccm", "cxxm", "c++m" });
360
35
    setupExts(this->HeaderFileExtensions,
361
35
              { "h", "hh", "h++", "hm", "hpp", "hxx", "in", "txx" });
362
35
    setupExts(this->CudaFileExtensions, { "cu" });
363
35
    setupExts(this->FortranFileExtensions,
364
35
              { "f", "F", "for", "f77", "f90", "f95", "f03" });
365
35
    setupExts(this->HipFileExtensions, { "hip" });
366
35
    setupExts(this->ISPCFileExtensions, { "ispc" });
367
35
  }
368
35
}
369
370
35
cmake::~cmake() = default;
371
372
#if !defined(CMAKE_BOOTSTRAP)
373
Json::Value cmake::ReportVersionJson() const
374
0
{
375
0
  Json::Value version = Json::objectValue;
376
0
  version["string"] = CMake_VERSION;
377
0
  version["major"] = CMake_VERSION_MAJOR;
378
0
  version["minor"] = CMake_VERSION_MINOR;
379
0
  version["suffix"] = CMake_VERSION_SUFFIX;
380
0
  version["isDirty"] = (CMake_VERSION_IS_DIRTY == 1);
381
0
  version["patch"] = CMake_VERSION_PATCH;
382
0
  return version;
383
0
}
384
385
Json::Value cmake::ReportCapabilitiesJson() const
386
0
{
387
0
  Json::Value obj = Json::objectValue;
388
389
  // Version information:
390
0
  obj["version"] = this->ReportVersionJson();
391
392
  // Generators:
393
0
  std::vector<cmake::GeneratorInfo> generatorInfoList;
394
0
  this->GetRegisteredGenerators(generatorInfoList);
395
396
0
  auto* curlVersion = curl_version_info(CURLVERSION_FIRST);
397
398
0
  JsonValueMapType generatorMap;
399
0
  for (cmake::GeneratorInfo const& gi : generatorInfoList) {
400
0
    if (gi.isAlias) { // skip aliases, they are there for compatibility reasons
401
                      // only
402
0
      continue;
403
0
    }
404
405
0
    if (gi.extraName.empty()) {
406
0
      Json::Value gen = Json::objectValue;
407
0
      gen["name"] = gi.name;
408
0
      gen["toolsetSupport"] = gi.supportsToolset;
409
0
      gen["platformSupport"] = gi.supportsPlatform;
410
0
      if (!gi.supportedPlatforms.empty()) {
411
0
        Json::Value supportedPlatforms = Json::arrayValue;
412
0
        for (std::string const& platform : gi.supportedPlatforms) {
413
0
          supportedPlatforms.append(platform);
414
0
        }
415
0
        gen["supportedPlatforms"] = std::move(supportedPlatforms);
416
0
      }
417
0
      gen["extraGenerators"] = Json::arrayValue;
418
0
      generatorMap[gi.name] = gen;
419
0
    } else {
420
0
      Json::Value& gen = generatorMap[gi.baseName];
421
0
      gen["extraGenerators"].append(gi.extraName);
422
0
    }
423
0
  }
424
425
0
  Json::Value generators = Json::arrayValue;
426
0
  for (auto const& i : generatorMap) {
427
0
    generators.append(i.second);
428
0
  }
429
0
  obj["generators"] = generators;
430
0
  obj["fileApi"] = cmFileAPI::ReportCapabilities();
431
0
  obj["serverMode"] = false;
432
0
  obj["tls"] = static_cast<bool>(curlVersion->features & CURL_VERSION_SSL);
433
0
#  ifdef CMake_ENABLE_DEBUGGER
434
0
  obj["debugger"] = true;
435
#  else
436
  obj["debugger"] = false;
437
#  endif
438
439
0
  return obj;
440
0
}
441
#endif
442
443
std::string cmake::ReportCapabilities() const
444
0
{
445
0
  std::string result;
446
0
#if !defined(CMAKE_BOOTSTRAP)
447
0
  Json::FastWriter writer;
448
0
  result = writer.write(this->ReportCapabilitiesJson());
449
#else
450
  result = "Not supported";
451
#endif
452
0
  return result;
453
0
}
454
455
bool cmake::RoleSupportsExitCode() const
456
0
{
457
0
  cmState::Role const role = this->State->GetRole();
458
0
  return role == cmState::Role::Script || role == cmState::Role::CTest;
459
0
}
460
461
cmake::CommandFailureAction cmake::GetCommandFailureAction() const
462
0
{
463
0
  switch (this->State->GetRole()) {
464
0
    case cmState::Role::Project:
465
0
    case cmState::Role::CTest:
466
0
      return CommandFailureAction::EXIT_CODE;
467
0
    default:
468
0
      return CommandFailureAction::FATAL_ERROR;
469
0
  }
470
0
}
471
472
void cmake::CleanupCommandsAndMacros()
473
0
{
474
0
  this->CurrentSnapshot = this->State->Reset();
475
0
  this->State->RemoveUserDefinedCommands();
476
0
  this->CurrentSnapshot.SetDefaultDefinitions();
477
  // FIXME: InstalledFiles probably belongs in the global generator.
478
0
  this->InstalledFiles.clear();
479
0
}
480
481
#ifndef CMAKE_BOOTSTRAP
482
void cmake::SetWarningFromPreset(std::string const& name,
483
                                 cm::optional<bool> warning,
484
                                 cm::optional<bool> error)
485
0
{
486
0
  if (warning) {
487
0
    if (*warning) {
488
0
      this->DiagLevels[name] = std::max(this->DiagLevels[name], DIAG_WARN);
489
0
    } else {
490
0
      this->DiagLevels[name] = DIAG_IGNORE;
491
0
    }
492
0
  }
493
0
  if (error) {
494
0
    if (*error) {
495
0
      this->DiagLevels[name] = DIAG_ERROR;
496
0
    } else {
497
0
      this->DiagLevels[name] = std::min(this->DiagLevels[name], DIAG_WARN);
498
0
    }
499
0
  }
500
0
}
501
502
void cmake::ProcessPresetVariables()
503
1
{
504
1
  for (auto const& var : this->UnprocessedPresetVariables) {
505
0
    if (!var.second) {
506
0
      continue;
507
0
    }
508
0
    cmStateEnums::CacheEntryType type = cmStateEnums::UNINITIALIZED;
509
0
    if (!var.second->Type.empty()) {
510
0
      type = cmState::StringToCacheEntryType(var.second->Type);
511
0
    }
512
0
    this->ProcessCacheArg(var.first, var.second->Value, type);
513
0
  }
514
1
}
515
516
void cmake::PrintPresetVariables()
517
0
{
518
0
  bool first = true;
519
0
  for (auto const& var : this->UnprocessedPresetVariables) {
520
0
    if (!var.second) {
521
0
      continue;
522
0
    }
523
0
    cmStateEnums::CacheEntryType type = cmStateEnums::UNINITIALIZED;
524
0
    if (!var.second->Type.empty()) {
525
0
      type = cmState::StringToCacheEntryType(var.second->Type);
526
0
    }
527
0
    if (first) {
528
0
      std::cout << "Preset CMake variables:\n\n";
529
0
      first = false;
530
0
    }
531
0
    std::cout << "  " << var.first;
532
0
    if (type != cmStateEnums::UNINITIALIZED) {
533
0
      std::cout << ':' << cmState::CacheEntryTypeToString(type);
534
0
    }
535
0
    std::cout << "=\"" << var.second->Value << "\"\n";
536
0
  }
537
0
  if (!first) {
538
0
    std::cout << '\n';
539
0
  }
540
0
  this->UnprocessedPresetVariables.clear();
541
0
}
542
543
void cmake::ProcessPresetEnvironment()
544
1
{
545
1
  for (auto const& var : this->UnprocessedPresetEnvironment) {
546
0
    if (var.second) {
547
0
      cmSystemTools::PutEnv(cmStrCat(var.first, '=', *var.second));
548
0
    }
549
0
  }
550
1
}
551
552
void cmake::PrintPresetEnvironment()
553
0
{
554
0
  bool first = true;
555
0
  for (auto const& var : this->UnprocessedPresetEnvironment) {
556
0
    if (!var.second) {
557
0
      continue;
558
0
    }
559
0
    if (first) {
560
0
      std::cout << "Preset environment variables:\n\n";
561
0
      first = false;
562
0
    }
563
0
    std::cout << "  " << var.first << "=\"" << *var.second << "\"\n";
564
0
  }
565
0
  if (!first) {
566
0
    std::cout << '\n';
567
0
  }
568
0
  this->UnprocessedPresetEnvironment.clear();
569
0
}
570
#endif
571
572
// Parse the args
573
bool cmake::SetCacheArgs(std::vector<std::string> const& args)
574
1
{
575
1
  static std::string const kCMAKE_POLICY_VERSION_MINIMUM =
576
1
    "CMAKE_POLICY_VERSION_MINIMUM";
577
1
  if (!this->State->GetInitializedCacheValue(kCMAKE_POLICY_VERSION_MINIMUM)) {
578
1
    cm::optional<std::string> policyVersion =
579
1
      cmSystemTools::GetEnvVar(kCMAKE_POLICY_VERSION_MINIMUM);
580
1
    if (policyVersion && !policyVersion->empty()) {
581
0
      this->AddCacheEntry(
582
0
        kCMAKE_POLICY_VERSION_MINIMUM, *policyVersion,
583
0
        "Override policy version for cmake_minimum_required calls.",
584
0
        cmStateEnums::STRING);
585
0
      this->State->SetCacheEntryProperty(kCMAKE_POLICY_VERSION_MINIMUM,
586
0
                                         "ADVANCED", "1");
587
0
    }
588
1
  }
589
590
1
  auto DefineLambda = [](std::string const& entry, cmake* state) -> bool {
591
0
    std::string var;
592
0
    std::string value;
593
0
    cmStateEnums::CacheEntryType type = cmStateEnums::UNINITIALIZED;
594
0
    if (cmState::ParseCacheEntry(entry, var, value, type)) {
595
0
#ifndef CMAKE_BOOTSTRAP
596
0
      state->UnprocessedPresetVariables.erase(var);
597
0
#endif
598
0
      state->ProcessCacheArg(var, value, type);
599
0
    } else {
600
0
      cmSystemTools::Error(cmStrCat("Parse error in command line argument: ",
601
0
                                    entry, "\n Should be: VAR:type=value\n"));
602
0
      return false;
603
0
    }
604
0
    return true;
605
0
  };
606
607
1
  auto WarningLambda = [](cm::string_view entry, cmake* state) -> bool {
608
0
    bool foundNo = false;
609
0
    bool foundError = false;
610
611
0
    if (cmHasLiteralPrefix(entry, "no-")) {
612
0
      foundNo = true;
613
0
      entry.remove_prefix(3);
614
0
    }
615
616
0
    if (cmHasLiteralPrefix(entry, "error=")) {
617
0
      foundError = true;
618
0
      entry.remove_prefix(6);
619
0
    }
620
621
0
    if (entry.empty()) {
622
0
      cmSystemTools::Error("No warning name provided.");
623
0
      return false;
624
0
    }
625
626
0
    std::string const name = std::string(entry);
627
0
    if (!foundNo && !foundError) {
628
      // -W<name>
629
0
      state->DiagLevels[name] = std::max(state->DiagLevels[name], DIAG_WARN);
630
0
    } else if (foundNo && !foundError) {
631
      // -Wno<name>
632
0
      state->DiagLevels[name] = DIAG_IGNORE;
633
0
    } else if (!foundNo && foundError) {
634
      // -Werror=<name>
635
0
      state->DiagLevels[name] = DIAG_ERROR;
636
0
    } else {
637
      // -Wno-error=<name>
638
      // This can downgrade an error to a warning, but should not enable
639
      // or disable a warning in the first place.
640
0
      auto dli = state->DiagLevels.find(name);
641
0
      if (dli != state->DiagLevels.end()) {
642
0
        dli->second = std::min(dli->second, DIAG_WARN);
643
0
      }
644
0
    }
645
0
    return true;
646
0
  };
647
648
1
  auto UnSetLambda = [](std::string const& entryPattern,
649
1
                        cmake* state) -> bool {
650
0
    cmsys::RegularExpression regex(
651
0
      cmsys::Glob::PatternToRegex(entryPattern, true, true));
652
    // go through all cache entries and collect the vars which will be
653
    // removed
654
0
    std::vector<std::string> entriesToDelete;
655
0
    std::vector<std::string> cacheKeys = state->State->GetCacheEntryKeys();
656
0
    for (std::string const& ck : cacheKeys) {
657
0
      cmStateEnums::CacheEntryType t = state->State->GetCacheEntryType(ck);
658
0
      if (t != cmStateEnums::STATIC) {
659
0
        if (regex.find(ck)) {
660
0
          entriesToDelete.push_back(ck);
661
0
        }
662
0
      }
663
0
    }
664
665
    // now remove them from the cache
666
0
    for (std::string const& currentEntry : entriesToDelete) {
667
0
#ifndef CMAKE_BOOTSTRAP
668
0
      state->UnprocessedPresetVariables.erase(currentEntry);
669
0
#endif
670
0
      state->State->RemoveCacheEntry(currentEntry);
671
0
    }
672
0
    return true;
673
0
  };
674
675
1
  auto ScriptLambda = [&](std::string const& path, cmake* state) -> bool {
676
1
    assert(this->State->GetRole() == cmState::Role::Script);
677
1
#ifdef CMake_ENABLE_DEBUGGER
678
    // Script mode doesn't hit the usual code path in cmake::Run() that starts
679
    // the debugger, so start it manually here instead.
680
1
    if (!this->StartDebuggerIfEnabled()) {
681
0
      return false;
682
0
    }
683
1
#endif
684
    // Register fake project commands that hint misuse in script mode.
685
1
    GetProjectCommandsInScriptMode(state->GetState());
686
    // Documented behavior of CMAKE{,_CURRENT}_{SOURCE,BINARY}_DIR is to be
687
    // set to $PWD for -P mode.
688
1
    state->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
689
1
    state->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
690
1
    state->ReadListFile(args, path);
691
1
    return true;
692
1
  };
693
694
1
  auto PrefixLambda = [&](std::string const& path, cmake* state) -> bool {
695
0
    std::string const var = "CMAKE_INSTALL_PREFIX";
696
0
    cmStateEnums::CacheEntryType type = cmStateEnums::PATH;
697
0
    cmCMakePath absolutePath(path);
698
0
    if (absolutePath.IsAbsolute()) {
699
0
#ifndef CMAKE_BOOTSTRAP
700
0
      state->UnprocessedPresetVariables.erase(var);
701
0
#endif
702
0
      state->ProcessCacheArg(var, path, type);
703
0
      return true;
704
0
    }
705
0
    cmSystemTools::Error("Absolute paths are required for --install-prefix");
706
0
    return false;
707
0
  };
708
709
1
  auto ToolchainLambda = [&](std::string const& path, cmake* state) -> bool {
710
0
    std::string const var = "CMAKE_TOOLCHAIN_FILE";
711
0
    cmStateEnums::CacheEntryType type = cmStateEnums::FILEPATH;
712
0
#ifndef CMAKE_BOOTSTRAP
713
0
    state->UnprocessedPresetVariables.erase(var);
714
0
#endif
715
0
    state->ProcessCacheArg(var, path, type);
716
0
    return true;
717
0
  };
718
719
1
  std::vector<CommandArgument> arguments = {
720
1
    CommandArgument{ "-D", "-D must be followed with VAR=VALUE.",
721
1
                     CommandArgument::Values::One,
722
1
                     CommandArgument::RequiresSeparator::No, DefineLambda },
723
1
    CommandArgument{ "-W", "-W must be followed with [no-]<name>.",
724
1
                     CommandArgument::Values::One,
725
1
                     CommandArgument::RequiresSeparator::No, WarningLambda },
726
1
    CommandArgument{ "-U", "-U must be followed with VAR.",
727
1
                     CommandArgument::Values::One,
728
1
                     CommandArgument::RequiresSeparator::No, UnSetLambda },
729
1
    CommandArgument{
730
1
      "-C", "-C must be followed by a file name.",
731
1
      CommandArgument::Values::One, CommandArgument::RequiresSeparator::No,
732
1
      [&](std::string const& value, cmake* state) -> bool {
733
0
        if (value.empty()) {
734
0
          cmSystemTools::Error("No file name specified for -C");
735
0
          return false;
736
0
        }
737
0
        cmSystemTools::Stdout(
738
0
          cmStrCat("loading initial cache file ", value, '\n'));
739
        // Resolve script path specified on command line
740
        // relative to $PWD.
741
0
        auto path = cmSystemTools::ToNormalizedPathOnDisk(value);
742
0
        state->InitializeFileAPI();
743
0
        state->InitializeInstrumentation();
744
0
        state->ReadListFile(args, path);
745
0
        return true;
746
0
      } },
747
748
1
    CommandArgument{ "-P", "-P must be followed by a file name.",
749
1
                     CommandArgument::Values::One,
750
1
                     CommandArgument::RequiresSeparator::No, ScriptLambda },
751
1
    CommandArgument{ "--toolchain", "No file specified for --toolchain",
752
1
                     CommandArgument::Values::One, ToolchainLambda },
753
1
    CommandArgument{ "--install-prefix",
754
1
                     "No install directory specified for --install-prefix",
755
1
                     CommandArgument::Values::One, PrefixLambda },
756
1
    CommandArgument{ "--find-package", CommandArgument::Values::Zero,
757
1
                     IgnoreAndTrueLambda },
758
1
  };
759
2
  for (decltype(args.size()) i = 1; i < args.size(); ++i) {
760
1
    std::string const& arg = args[i];
761
762
1
    if (arg == "--" && this->State->GetRole() == cmState::Role::Script) {
763
      // Stop processing CMake args and avoid possible errors
764
      // when arbitrary args are given to CMake script.
765
0
      break;
766
0
    }
767
8
    for (auto const& m : arguments) {
768
8
      if (m.matches(arg)) {
769
1
        bool const parsedCorrectly = m.parse(arg, i, args, this);
770
1
        if (!parsedCorrectly) {
771
0
          return false;
772
0
        }
773
1
      }
774
8
    }
775
1
  }
776
777
1
  if (this->State->GetRole() == cmState::Role::FindPackage) {
778
0
    return this->FindPackage(args);
779
0
  }
780
781
1
  return true;
782
1
}
783
784
void cmake::ProcessCacheArg(std::string const& var, std::string const& value,
785
                            cmStateEnums::CacheEntryType type)
786
0
{
787
  // The value is transformed if it is a filepath for example, so
788
  // we can't compare whether the value is already in the cache until
789
  // after we call AddCacheEntry.
790
0
  bool haveValue = false;
791
0
  std::string cachedValue;
792
0
  if (this->WarnUnusedCli) {
793
0
    if (cmValue v = this->State->GetInitializedCacheValue(var)) {
794
0
      haveValue = true;
795
0
      cachedValue = *v;
796
0
    }
797
0
  }
798
799
0
  this->AddCacheEntry(
800
0
    var, value, "No help, variable specified on the command line.", type);
801
802
0
  if (this->WarnUnusedCli) {
803
0
    if (!haveValue ||
804
0
        cachedValue != *this->State->GetInitializedCacheValue(var)) {
805
0
      this->WatchUnusedCli(var);
806
0
    }
807
0
  }
808
0
}
809
810
void cmake::ReadListFile(std::vector<std::string> const& args,
811
                         std::string const& path)
812
1
{
813
  // if a generator was not yet created, temporarily create one
814
1
  cmGlobalGenerator* gg = this->GetGlobalGenerator();
815
816
  // if a generator was not specified use a generic one
817
1
  std::unique_ptr<cmGlobalGenerator> gen;
818
1
  if (!gg) {
819
1
    gen = cm::make_unique<cmGlobalGenerator>(this);
820
1
    gg = gen.get();
821
1
  }
822
823
  // read in the list file to fill the cache
824
1
  if (!path.empty()) {
825
1
    this->CurrentSnapshot = this->State->Reset();
826
1
    cmStateSnapshot snapshot = this->GetCurrentSnapshot();
827
1
    snapshot.GetDirectory().SetCurrentBinary(this->GetHomeOutputDirectory());
828
1
    snapshot.GetDirectory().SetCurrentSource(this->GetHomeDirectory());
829
1
    snapshot.SetDefaultDefinitions();
830
1
    cmMakefile mf(gg, snapshot);
831
1
    if (this->State->GetRole() == cmState::Role::Script) {
832
1
      mf.SetScriptModeFile(cmSystemTools::ToNormalizedPathOnDisk(path));
833
1
      mf.SetArgcArgv(args);
834
1
    }
835
1
    if (!cmSystemTools::FileExists(path, true)) {
836
0
      cmSystemTools::Error("Not a file: " + path);
837
0
    }
838
1
    if (!mf.ReadListFile(path)) {
839
1
      cmSystemTools::Error("Error processing file: " + path);
840
1
    }
841
1
  }
842
1
}
843
844
bool cmake::FindPackage(std::vector<std::string> const& args)
845
0
{
846
0
  this->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
847
0
  this->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
848
849
0
  this->SetGlobalGenerator(cm::make_unique<cmGlobalGenerator>(this));
850
851
0
  cmStateSnapshot snapshot = this->GetCurrentSnapshot();
852
0
  snapshot.GetDirectory().SetCurrentBinary(
853
0
    cmSystemTools::GetLogicalWorkingDirectory());
854
0
  snapshot.GetDirectory().SetCurrentSource(
855
0
    cmSystemTools::GetLogicalWorkingDirectory());
856
  // read in the list file to fill the cache
857
0
  snapshot.SetDefaultDefinitions();
858
0
  auto mfu = cm::make_unique<cmMakefile>(this->GetGlobalGenerator(), snapshot);
859
0
  cmMakefile* mf = mfu.get();
860
0
  this->GlobalGenerator->AddMakefile(std::move(mfu));
861
862
0
  mf->SetArgcArgv(args);
863
864
0
  std::string systemFile = mf->GetModulesFile("CMakeFindPackageMode.cmake");
865
0
  mf->ReadListFile(systemFile);
866
867
0
  std::string language = mf->GetSafeDefinition("LANGUAGE");
868
0
  std::string mode = mf->GetSafeDefinition("MODE");
869
0
  std::string packageName = mf->GetSafeDefinition("NAME");
870
0
  bool packageFound = mf->IsOn("PACKAGE_FOUND");
871
0
  bool quiet = mf->IsOn("PACKAGE_QUIET");
872
873
0
  if (!packageFound) {
874
0
    if (!quiet) {
875
0
      printf("%s not found.\n", packageName.c_str());
876
0
    }
877
0
  } else if (mode == "EXIST"_s) {
878
0
    if (!quiet) {
879
0
      printf("%s found.\n", packageName.c_str());
880
0
    }
881
0
  } else if (mode == "COMPILE"_s) {
882
0
    std::string includes = mf->GetSafeDefinition("PACKAGE_INCLUDE_DIRS");
883
0
    cmList includeDirs{ includes };
884
885
0
    this->GlobalGenerator->CreateGenerationObjects();
886
0
    auto const& lg = this->GlobalGenerator->LocalGenerators[0];
887
0
    std::string includeFlags =
888
0
      lg->GetIncludeFlags(includeDirs, nullptr, language, std::string());
889
890
0
    std::string definitions = mf->GetSafeDefinition("PACKAGE_DEFINITIONS");
891
0
    printf("%s %s\n", includeFlags.c_str(), definitions.c_str());
892
0
  } else if (mode == "LINK"_s) {
893
0
    char const* targetName = "dummy";
894
0
    std::vector<std::string> srcs;
895
0
    cmTarget* tgt = mf->AddExecutable(targetName, srcs, true);
896
0
    tgt->SetProperty("LINKER_LANGUAGE", language);
897
898
0
    std::string libs = mf->GetSafeDefinition("PACKAGE_LIBRARIES");
899
0
    cmList libList{ libs };
900
0
    for (std::string const& lib : libList) {
901
0
      tgt->AddLinkLibrary(*mf, lib, GENERAL_LibraryType);
902
0
    }
903
904
0
    std::string buildType = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
905
0
    buildType = cmSystemTools::UpperCase(buildType);
906
907
0
    std::string linkLibs;
908
0
    std::string frameworkPath;
909
0
    std::string linkPath;
910
0
    std::string flags;
911
0
    std::string linkFlags;
912
0
    this->GlobalGenerator->CreateGenerationObjects();
913
0
    cmGeneratorTarget* gtgt =
914
0
      this->GlobalGenerator->FindGeneratorTarget(tgt->GetName());
915
0
    cmLocalGenerator* lg = gtgt->GetLocalGenerator();
916
0
    cmLinkLineComputer linkLineComputer(lg,
917
0
                                        lg->GetStateSnapshot().GetDirectory());
918
0
    lg->GetTargetFlags(&linkLineComputer, buildType, linkLibs, flags,
919
0
                       linkFlags, frameworkPath, linkPath, gtgt);
920
0
    linkLibs = frameworkPath + linkPath + linkLibs;
921
922
0
    printf("%s\n", linkLibs.c_str());
923
924
    /*    if ( use_win32 )
925
          {
926
          tgt->SetProperty("WIN32_EXECUTABLE", "ON");
927
          }
928
        if ( use_macbundle)
929
          {
930
          tgt->SetProperty("MACOSX_BUNDLE", "ON");
931
          }*/
932
0
  }
933
934
0
  return packageFound;
935
0
}
936
937
void cmake::LoadEnvironmentPresets()
938
0
{
939
0
  std::string envGenVar;
940
0
  bool hasEnvironmentGenerator = false;
941
0
  if (cmSystemTools::GetEnv("CMAKE_GENERATOR", envGenVar)) {
942
0
    hasEnvironmentGenerator = true;
943
0
    this->EnvironmentGenerator = envGenVar;
944
0
  }
945
946
0
  auto readGeneratorVar = [&](std::string const& name, std::string& key) {
947
0
    std::string varValue;
948
0
    if (cmSystemTools::GetEnv(name, varValue)) {
949
0
      if (hasEnvironmentGenerator) {
950
0
        key = varValue;
951
0
      } else if (!this->GetIsInTryCompile()) {
952
0
        std::string message =
953
0
          cmStrCat("Warning: Environment variable ", name,
954
0
                   " will be ignored, because CMAKE_GENERATOR is not set.");
955
0
        cmSystemTools::Message(message, "Warning");
956
0
      }
957
0
    }
958
0
  };
959
960
0
  readGeneratorVar("CMAKE_GENERATOR_INSTANCE", this->GeneratorInstance);
961
0
  readGeneratorVar("CMAKE_GENERATOR_PLATFORM", this->GeneratorPlatform);
962
0
  readGeneratorVar("CMAKE_GENERATOR_TOOLSET", this->GeneratorToolset);
963
0
  this->IntermediateDirStrategy =
964
0
    cmSystemTools::GetEnvVar("CMAKE_INTERMEDIATE_DIR_STRATEGY");
965
0
  this->AutogenIntermediateDirStrategy =
966
0
    cmSystemTools::GetEnvVar("CMAKE_AUTOGEN_INTERMEDIATE_DIR_STRATEGY");
967
0
}
968
969
namespace {
970
enum class ListPresets
971
{
972
  None,
973
  Configure,
974
  Build,
975
  Test,
976
  Package,
977
  Workflow,
978
  All,
979
};
980
}
981
982
// Parse the args
983
void cmake::SetArgs(std::vector<std::string> const& args)
984
35
{
985
35
  this->cmdArgs = args;
986
35
  bool haveToolset = false;
987
35
  bool havePlatform = false;
988
35
  bool haveBArg = false;
989
35
  bool haveCMLName = false;
990
35
  std::string possibleUnknownArg;
991
35
  std::string extraProvidedPath;
992
35
#if !defined(CMAKE_BOOTSTRAP)
993
35
  std::string profilingFormat;
994
35
  std::string profilingOutput;
995
35
  std::string presetName;
996
997
35
  ListPresets listPresets = ListPresets::None;
998
35
#endif
999
1000
35
  auto EmptyStringArgLambda = [](std::string const&, cmake* state) -> bool {
1001
0
    state->IssueMessage(
1002
0
      MessageType::WARNING,
1003
0
      "Ignoring empty string (\"\") provided on the command line.");
1004
0
    return true;
1005
0
  };
1006
1007
35
  auto SourceArgLambda = [](std::string const& value, cmake* state) -> bool {
1008
0
    if (value.empty()) {
1009
0
      cmSystemTools::Error("No source directory specified for -S");
1010
0
      return false;
1011
0
    }
1012
0
    state->SetHomeDirectoryViaCommandLine(
1013
0
      cmSystemTools::ToNormalizedPathOnDisk(value));
1014
0
    return true;
1015
0
  };
1016
1017
35
  auto BuildArgLambda = [&](std::string const& value, cmake* state) -> bool {
1018
0
    if (value.empty()) {
1019
0
      cmSystemTools::Error("No build directory specified for -B");
1020
0
      return false;
1021
0
    }
1022
0
    state->SetHomeOutputDirectory(
1023
0
      cmSystemTools::ToNormalizedPathOnDisk(value));
1024
0
    haveBArg = true;
1025
0
    return true;
1026
0
  };
1027
1028
35
  auto PlatformLambda = [&](std::string const& value, cmake* state) -> bool {
1029
0
    if (havePlatform) {
1030
0
      cmSystemTools::Error("Multiple -A options not allowed");
1031
0
      return false;
1032
0
    }
1033
0
    state->SetGeneratorPlatform(value);
1034
0
    havePlatform = true;
1035
0
    return true;
1036
0
  };
1037
1038
35
  auto ToolsetLambda = [&](std::string const& value, cmake* state) -> bool {
1039
0
    if (haveToolset) {
1040
0
      cmSystemTools::Error("Multiple -T options not allowed");
1041
0
      return false;
1042
0
    }
1043
0
    state->SetGeneratorToolset(value);
1044
0
    haveToolset = true;
1045
0
    return true;
1046
0
  };
1047
1048
35
  auto CMakeListsFileLambda = [&](std::string const& value,
1049
35
                                  cmake* state) -> bool {
1050
0
    if (haveCMLName) {
1051
0
      cmSystemTools::Error("Multiple --project-file options not allowed");
1052
0
      return false;
1053
0
    }
1054
0
    state->SetCMakeListName(value);
1055
0
    haveCMLName = true;
1056
0
    return true;
1057
0
  };
1058
1059
35
  std::vector<CommandArgument> arguments = {
1060
35
    CommandArgument{ "", CommandArgument::Values::Zero, EmptyStringArgLambda },
1061
35
    CommandArgument{ "-S", "No source directory specified for -S",
1062
35
                     CommandArgument::Values::One,
1063
35
                     CommandArgument::RequiresSeparator::No, SourceArgLambda },
1064
35
    CommandArgument{ "-H", "No source directory specified for -H",
1065
35
                     CommandArgument::Values::One,
1066
35
                     CommandArgument::RequiresSeparator::No, SourceArgLambda },
1067
35
    CommandArgument{ "-O", CommandArgument::Values::Zero,
1068
35
                     IgnoreAndTrueLambda },
1069
35
    CommandArgument{ "-B", "No build directory specified for -B",
1070
35
                     CommandArgument::Values::One,
1071
35
                     CommandArgument::RequiresSeparator::No, BuildArgLambda },
1072
35
    CommandArgument{ "--fresh", CommandArgument::Values::Zero,
1073
35
                     [](std::string const&, cmake* cm) -> bool {
1074
0
                       cm->FreshCache = true;
1075
0
                       return true;
1076
0
                     } },
1077
35
    CommandArgument{ "-P", "-P must be followed by a file name.",
1078
35
                     CommandArgument::Values::One,
1079
35
                     CommandArgument::RequiresSeparator::No,
1080
35
                     IgnoreAndTrueLambda },
1081
35
    CommandArgument{ "-D", "-D must be followed with VAR=VALUE.",
1082
35
                     CommandArgument::Values::One,
1083
35
                     CommandArgument::RequiresSeparator::No,
1084
35
                     IgnoreAndTrueLambda },
1085
35
    CommandArgument{ "-C", "-C must be followed by a file name.",
1086
35
                     CommandArgument::Values::One,
1087
35
                     CommandArgument::RequiresSeparator::No,
1088
35
                     IgnoreAndTrueLambda },
1089
35
    CommandArgument{
1090
35
      "-U", "-U must be followed with VAR.", CommandArgument::Values::One,
1091
35
      CommandArgument::RequiresSeparator::No, IgnoreAndTrueLambda },
1092
35
    CommandArgument{ "-W", "-W must be followed with [no-]<name>.",
1093
35
                     CommandArgument::Values::One,
1094
35
                     CommandArgument::RequiresSeparator::No,
1095
35
                     IgnoreAndTrueLambda },
1096
35
    CommandArgument{ "-A", "No platform specified for -A",
1097
35
                     CommandArgument::Values::One,
1098
35
                     CommandArgument::RequiresSeparator::No, PlatformLambda },
1099
35
    CommandArgument{ "-T", "No toolset specified for -T",
1100
35
                     CommandArgument::Values::One,
1101
35
                     CommandArgument::RequiresSeparator::No, ToolsetLambda },
1102
35
    CommandArgument{ "--toolchain", "No file specified for --toolchain",
1103
35
                     CommandArgument::Values::One, IgnoreAndTrueLambda },
1104
35
    CommandArgument{ "--install-prefix",
1105
35
                     "No install directory specified for --install-prefix",
1106
35
                     CommandArgument::Values::One, IgnoreAndTrueLambda },
1107
1108
35
    CommandArgument{ "--check-build-system", CommandArgument::Values::Two,
1109
35
                     [](std::string const& value, cmake* state) -> bool {
1110
0
                       cmList values{ value };
1111
0
                       state->CheckBuildSystemArgument = values[0];
1112
0
                       state->ClearBuildSystem = (atoi(values[1].c_str()) > 0);
1113
0
                       return true;
1114
0
                     } },
1115
35
    CommandArgument{ "--check-stamp-file", CommandArgument::Values::One,
1116
35
                     [](std::string const& value, cmake* state) -> bool {
1117
0
                       state->CheckStampFile = value;
1118
0
                       return true;
1119
0
                     } },
1120
35
    CommandArgument{ "--check-stamp-list", CommandArgument::Values::One,
1121
35
                     [](std::string const& value, cmake* state) -> bool {
1122
0
                       state->CheckStampList = value;
1123
0
                       return true;
1124
0
                     } },
1125
35
    CommandArgument{ "--regenerate-during-build",
1126
35
                     CommandArgument::Values::Zero,
1127
35
                     [](std::string const&, cmake* state) -> bool {
1128
0
                       state->RegenerateDuringBuild = true;
1129
0
                       return true;
1130
0
                     } },
1131
1132
35
    CommandArgument{ "--find-package", CommandArgument::Values::Zero,
1133
35
                     IgnoreAndTrueLambda },
1134
1135
35
    CommandArgument{ "--graphviz", "No file specified for --graphviz",
1136
35
                     CommandArgument::Values::One,
1137
35
                     [](std::string const& value, cmake* state) -> bool {
1138
0
                       state->SetGraphVizFile(
1139
0
                         cmSystemTools::ToNormalizedPathOnDisk(value));
1140
0
                       return true;
1141
0
                     } },
1142
1143
35
    CommandArgument{ "--debug-trycompile", CommandArgument::Values::Zero,
1144
35
                     [](std::string const&, cmake* state) -> bool {
1145
0
                       std::cout << "debug trycompile on\n";
1146
0
                       state->DebugTryCompileOn();
1147
0
                       return true;
1148
0
                     } },
1149
35
    CommandArgument{ "--debug-output", CommandArgument::Values::Zero,
1150
35
                     [](std::string const&, cmake* state) -> bool {
1151
0
                       std::cout << "Running with debug output on.\n";
1152
0
                       state->SetDebugOutputOn(true);
1153
0
                       return true;
1154
0
                     } },
1155
1156
35
    CommandArgument{ "--log-level", "Invalid level specified for --log-level",
1157
35
                     CommandArgument::Values::One,
1158
35
                     [](std::string const& value, cmake* state) -> bool {
1159
0
                       auto const logLevel = StringToLogLevel(value);
1160
0
                       if (logLevel == Message::LogLevel::LOG_UNDEFINED) {
1161
0
                         cmSystemTools::Error(
1162
0
                           "Invalid level specified for --log-level");
1163
0
                         return false;
1164
0
                       }
1165
0
                       state->SetLogLevel(logLevel);
1166
0
                       state->LogLevelWasSetViaCLI = true;
1167
0
                       return true;
1168
0
                     } },
1169
    // This is supported for backward compatibility. This option only
1170
    // appeared in the 3.15.x release series and was renamed to
1171
    // --log-level in 3.16.0
1172
35
    CommandArgument{ "--loglevel", "Invalid level specified for --loglevel",
1173
35
                     CommandArgument::Values::One,
1174
35
                     [](std::string const& value, cmake* state) -> bool {
1175
0
                       auto const logLevel = StringToLogLevel(value);
1176
0
                       if (logLevel == Message::LogLevel::LOG_UNDEFINED) {
1177
0
                         cmSystemTools::Error(
1178
0
                           "Invalid level specified for --loglevel");
1179
0
                         return false;
1180
0
                       }
1181
0
                       state->SetLogLevel(logLevel);
1182
0
                       state->LogLevelWasSetViaCLI = true;
1183
0
                       return true;
1184
0
                     } },
1185
1186
35
    CommandArgument{ "--log-context", CommandArgument::Values::Zero,
1187
35
                     [](std::string const&, cmake* state) -> bool {
1188
0
                       state->SetShowLogContext(true);
1189
0
                       return true;
1190
0
                     } },
1191
35
    CommandArgument{ "--project-file",
1192
35
                     "No filename specified for --project-file",
1193
35
                     CommandArgument::Values::One, CMakeListsFileLambda },
1194
35
    CommandArgument{
1195
35
      "--debug-find", CommandArgument::Values::Zero,
1196
35
      [](std::string const&, cmake* state) -> bool {
1197
0
        std::cout << "Running with debug output on for the `find` commands.\n";
1198
0
        state->SetDebugFindOutput(true);
1199
0
        return true;
1200
0
      } },
1201
35
    CommandArgument{
1202
35
      "--debug-find-pkg", "Provide a package argument for --debug-find-pkg",
1203
35
      CommandArgument::Values::One, CommandArgument::RequiresSeparator::Yes,
1204
35
      [](std::string const& value, cmake* state) -> bool {
1205
0
        std::vector<std::string> find_pkgs(cmTokenize(value, ','));
1206
0
        std::cout << "Running with debug output on for the 'find' commands "
1207
0
                     "for package(s)";
1208
0
        for (auto const& v : find_pkgs) {
1209
0
          std::cout << ' ' << v;
1210
0
          state->SetDebugFindOutputPkgs(v);
1211
0
        }
1212
0
        std::cout << ".\n";
1213
0
        return true;
1214
0
      } },
1215
35
    CommandArgument{
1216
35
      "--debug-find-var", CommandArgument::Values::One,
1217
35
      CommandArgument::RequiresSeparator::Yes,
1218
35
      [](std::string const& value, cmake* state) -> bool {
1219
0
        std::vector<std::string> find_vars(cmTokenize(value, ','));
1220
0
        std::cout << "Running with debug output on for the variable(s)";
1221
0
        for (auto const& v : find_vars) {
1222
0
          std::cout << ' ' << v;
1223
0
          state->SetDebugFindOutputVars(v);
1224
0
        }
1225
0
        std::cout << ".\n";
1226
0
        return true;
1227
0
      } },
1228
35
    CommandArgument{ "--trace", CommandArgument::Values::Zero,
1229
35
                     [](std::string const&, cmake* state) -> bool {
1230
0
                       std::cout << "Put cmake in trace mode.\n";
1231
0
                       state->SetTrace(true);
1232
0
                       state->SetTraceExpand(false);
1233
0
                       return true;
1234
0
                     } },
1235
35
    CommandArgument{ "--trace-expand", CommandArgument::Values::Zero,
1236
35
                     [](std::string const&, cmake* state) -> bool {
1237
0
                       std::cout << "Put cmake in trace mode, but with "
1238
0
                                    "variables expanded.\n";
1239
0
                       state->SetTrace(true);
1240
0
                       state->SetTraceExpand(true);
1241
0
                       return true;
1242
0
                     } },
1243
35
    CommandArgument{
1244
35
      "--trace-format", "Invalid format specified for --trace-format",
1245
35
      CommandArgument::Values::One,
1246
35
      [](std::string const& value, cmake* state) -> bool {
1247
0
        std::cout << "Put cmake in trace mode and sets the "
1248
0
                     "trace output format.\n";
1249
0
        state->SetTrace(true);
1250
0
        auto const traceFormat = StringToTraceFormat(value);
1251
0
        if (traceFormat == TraceFormat::Undefined) {
1252
0
          cmSystemTools::Error("Invalid format specified for --trace-format. "
1253
0
                               "Valid formats are human, json-v1.");
1254
0
          return false;
1255
0
        }
1256
0
        state->SetTraceFormat(traceFormat);
1257
0
        return true;
1258
0
      } },
1259
35
    CommandArgument{ "--trace-source", "No file specified for --trace-source",
1260
35
                     CommandArgument::Values::OneOrMore,
1261
35
                     [](std::string const& values, cmake* state) -> bool {
1262
0
                       std::cout << "Put cmake in trace mode, but output only "
1263
0
                                    "lines of a specified file. Multiple "
1264
0
                                    "options are allowed.\n";
1265
0
                       for (auto file :
1266
0
                            cmSystemTools::SplitString(values, ';')) {
1267
0
                         cmSystemTools::ConvertToUnixSlashes(file);
1268
0
                         state->AddTraceSource(file);
1269
0
                       }
1270
0
                       state->SetTrace(true);
1271
0
                       return true;
1272
0
                     } },
1273
35
    CommandArgument{ "--trace-redirect",
1274
35
                     "No file specified for --trace-redirect",
1275
35
                     CommandArgument::Values::One,
1276
35
                     [](std::string const& value, cmake* state) -> bool {
1277
0
                       std::cout
1278
0
                         << "Put cmake in trace mode and redirect trace "
1279
0
                            "output to a file instead of stderr.\n";
1280
0
                       std::string file(value);
1281
0
                       cmSystemTools::ConvertToUnixSlashes(file);
1282
0
                       state->SetTraceFile(file);
1283
0
                       state->SetTrace(true);
1284
0
                       return true;
1285
0
                     } },
1286
35
    CommandArgument{ "--warn-uninitialized", CommandArgument::Values::Zero,
1287
35
                     [](std::string const&, cmake* state) -> bool {
1288
0
                       std::cout << "Warn about uninitialized values.\n";
1289
0
                       state->SetWarnUninitialized(true);
1290
0
                       return true;
1291
0
                     } },
1292
35
    CommandArgument{ "--warn-unused-vars", CommandArgument::Values::Zero,
1293
35
                     IgnoreAndTrueLambda }, // Option was removed.
1294
35
    CommandArgument{ "--no-warn-unused-cli", CommandArgument::Values::Zero,
1295
35
                     [](std::string const&, cmake* state) -> bool {
1296
0
                       std::cout
1297
0
                         << "Not searching for unused variables given on the "
1298
0
                            "command line.\n";
1299
0
                       state->SetWarnUnusedCli(false);
1300
0
                       return true;
1301
0
                     } },
1302
35
    CommandArgument{
1303
35
      "--check-system-vars", CommandArgument::Values::Zero,
1304
35
      [](std::string const&, cmake* state) -> bool {
1305
0
        std::cout << "Also check system files when warning about unused and "
1306
0
                     "uninitialized variables.\n";
1307
0
        state->SetCheckSystemVars(true);
1308
0
        return true;
1309
0
      } },
1310
35
    CommandArgument{
1311
35
      "--compile-no-warning-as-error", CommandArgument::Values::Zero,
1312
35
      [](std::string const&, cmake* state) -> bool {
1313
0
        std::cout << "Ignoring COMPILE_WARNING_AS_ERROR target property and "
1314
0
                     "CMAKE_COMPILE_WARNING_AS_ERROR variable.\n";
1315
0
        state->SetIgnoreCompileWarningAsError(true);
1316
0
        return true;
1317
0
      } },
1318
35
    CommandArgument{
1319
35
      "--link-no-warning-as-error", CommandArgument::Values::Zero,
1320
35
      [](std::string const&, cmake* state) -> bool {
1321
0
        std::cout << "Ignoring LINK_WARNING_AS_ERROR target property and "
1322
0
                     "CMAKE_LINK_WARNING_AS_ERROR variable.\n";
1323
0
        state->SetIgnoreLinkWarningAsError(true);
1324
0
        return true;
1325
0
      } },
1326
35
#ifndef CMAKE_BOOTSTRAP
1327
35
    CommandArgument{ "--sarif-output", "No file specified for --sarif-output",
1328
35
                     CommandArgument::Values::One,
1329
35
                     [](std::string const& value, cmake* state) -> bool {
1330
0
                       state->SarifFilePath =
1331
0
                         cmSystemTools::ToNormalizedPathOnDisk(value);
1332
0
                       state->SarifFileOutput = true;
1333
0
                       return true;
1334
0
                     } },
1335
35
#endif
1336
35
    CommandArgument{ "--debugger", CommandArgument::Values::Zero,
1337
35
                     [](std::string const&, cmake* state) -> bool {
1338
0
#ifdef CMake_ENABLE_DEBUGGER
1339
0
                       std::cout << "Running with debugger on.\n";
1340
0
                       state->SetDebuggerOn(true);
1341
0
                       return true;
1342
#else
1343
                       static_cast<void>(state);
1344
                       cmSystemTools::Error(
1345
                         "CMake was not built with support for --debugger");
1346
                       return false;
1347
#endif
1348
0
                     } },
1349
35
    CommandArgument{ "--debugger-pipe",
1350
35
                     "No path specified for --debugger-pipe",
1351
35
                     CommandArgument::Values::One,
1352
35
                     [](std::string const& value, cmake* state) -> bool {
1353
0
#ifdef CMake_ENABLE_DEBUGGER
1354
0
                       state->DebuggerPipe = value;
1355
0
                       return true;
1356
#else
1357
                       static_cast<void>(value);
1358
                       static_cast<void>(state);
1359
                       cmSystemTools::Error("CMake was not built with support "
1360
                                            "for --debugger-pipe");
1361
                       return false;
1362
#endif
1363
0
                     } },
1364
35
    CommandArgument{ "--debugger-dap-log",
1365
35
                     "No file specified for --debugger-dap-log",
1366
35
                     CommandArgument::Values::One,
1367
35
                     [](std::string const& value, cmake* state) -> bool {
1368
0
#ifdef CMake_ENABLE_DEBUGGER
1369
0
                       state->DebuggerDapLogFile =
1370
0
                         cmSystemTools::ToNormalizedPathOnDisk(value);
1371
0
                       return true;
1372
#else
1373
                       static_cast<void>(value);
1374
                       static_cast<void>(state);
1375
                       cmSystemTools::Error("CMake was not built with support "
1376
                                            "for --debugger-dap-log");
1377
                       return false;
1378
#endif
1379
0
                     } },
1380
35
  };
1381
1382
#if defined(CMAKE_HAVE_VS_GENERATORS)
1383
  arguments.emplace_back("--vs-solution-file", CommandArgument::Values::One,
1384
                         [](std::string const& value, cmake* state) -> bool {
1385
                           state->VSSolutionFile = value;
1386
                           return true;
1387
                         });
1388
#endif
1389
1390
35
#if !defined(CMAKE_BOOTSTRAP)
1391
35
  arguments.emplace_back("--profiling-format",
1392
35
                         "No format specified for --profiling-format",
1393
35
                         CommandArgument::Values::One,
1394
35
                         [&](std::string const& value, cmake*) -> bool {
1395
0
                           profilingFormat = value;
1396
0
                           return true;
1397
0
                         });
1398
35
  arguments.emplace_back(
1399
35
    "--profiling-output", "No path specified for --profiling-output",
1400
35
    CommandArgument::Values::One,
1401
35
    [&profilingOutput](std::string const& value, cmake*) -> bool {
1402
0
      profilingOutput = cmSystemTools::ToNormalizedPathOnDisk(value);
1403
0
      return true;
1404
0
    });
1405
35
  arguments.emplace_back("--preset", "No preset specified for --preset",
1406
35
                         CommandArgument::Values::One,
1407
35
                         [&](std::string const& value, cmake*) -> bool {
1408
0
                           presetName = value;
1409
0
                           return true;
1410
0
                         });
1411
35
  arguments.emplace_back(
1412
35
    "--list-presets", CommandArgument::Values::ZeroOrOne,
1413
35
    [&](std::string const& value, cmake*) -> bool {
1414
0
      if (value.empty() || value == "configure") {
1415
0
        listPresets = ListPresets::Configure;
1416
0
      } else if (value == "build") {
1417
0
        listPresets = ListPresets::Build;
1418
0
      } else if (value == "test") {
1419
0
        listPresets = ListPresets::Test;
1420
0
      } else if (value == "package") {
1421
0
        listPresets = ListPresets::Package;
1422
0
      } else if (value == "workflow") {
1423
0
        listPresets = ListPresets::Workflow;
1424
0
      } else if (value == "all") {
1425
0
        listPresets = ListPresets::All;
1426
0
      } else {
1427
0
        cmSystemTools::Error(
1428
0
          "Invalid value specified for --list-presets.\n"
1429
0
          "Valid values are configure, build, test, package, or all. "
1430
0
          "When no value is passed the default is configure.");
1431
0
        return false;
1432
0
      }
1433
1434
0
      return true;
1435
0
    });
1436
1437
35
#endif
1438
1439
35
  bool badGeneratorName = false;
1440
35
  CommandArgument generatorCommand(
1441
35
    "-G", "No generator specified for -G", CommandArgument::Values::One,
1442
35
    CommandArgument::RequiresSeparator::No,
1443
35
    [&](std::string const& value, cmake* state) -> bool {
1444
0
      bool valid = state->CreateAndSetGlobalGenerator(value);
1445
0
      badGeneratorName = !valid;
1446
0
      return valid;
1447
0
    });
1448
1449
70
  for (decltype(args.size()) i = 1; i < args.size(); ++i) {
1450
    // iterate each argument
1451
35
    std::string const& arg = args[i];
1452
1453
35
    if (this->State->GetRole() == cmState::Role::Script && arg == "--") {
1454
      // Stop processing CMake args and avoid possible errors
1455
      // when arbitrary args are given to CMake script.
1456
0
      break;
1457
0
    }
1458
1459
    // Generator flag has special handling for when to print help
1460
    // so it becomes the exception
1461
35
    if (generatorCommand.matches(arg)) {
1462
0
      bool parsed = generatorCommand.parse(arg, i, args, this);
1463
0
      if (!parsed && !badGeneratorName) {
1464
0
        this->PrintGeneratorList();
1465
0
        return;
1466
0
      }
1467
0
      continue;
1468
0
    }
1469
1470
35
    bool matched = false;
1471
35
    bool parsedCorrectly = true; // needs to be true so we can ignore
1472
                                 // arguments so as -E
1473
245
    for (auto const& m : arguments) {
1474
245
      if (m.matches(arg)) {
1475
35
        matched = true;
1476
35
        parsedCorrectly = m.parse(arg, i, args, this);
1477
35
        break;
1478
35
      }
1479
245
    }
1480
1481
    // We have an issue where arguments to a "-P" script mode
1482
    // can be provided before the "-P" argument. This means
1483
    // that we need to lazily check this argument after checking
1484
    // all args.
1485
    // Additionally it can't be the source/binary tree location
1486
35
    if (!parsedCorrectly) {
1487
0
      cmSystemTools::Error("Run 'cmake --help' for all supported options.");
1488
0
      exit(1);
1489
35
    } else if (!matched && cmHasPrefix(arg, '-')) {
1490
0
      possibleUnknownArg = arg;
1491
35
    } else if (!matched) {
1492
0
      bool parsedDirectory = this->SetDirectoriesFromFile(arg);
1493
0
      if (!parsedDirectory) {
1494
0
        extraProvidedPath = arg;
1495
0
      }
1496
0
    }
1497
35
  }
1498
1499
35
  if (!extraProvidedPath.empty() &&
1500
0
      this->State->GetRole() == cmState::Role::Project) {
1501
0
    this->IssueMessage(MessageType::WARNING,
1502
0
                       cmStrCat("Ignoring extra path from command line:\n \"",
1503
0
                                extraProvidedPath, '"'));
1504
0
  }
1505
35
  if (!possibleUnknownArg.empty() &&
1506
0
      this->State->GetRole() != cmState::Role::Script) {
1507
0
    cmSystemTools::Error(cmStrCat("Unknown argument ", possibleUnknownArg));
1508
0
    cmSystemTools::Error("Run 'cmake --help' for all supported options.");
1509
0
    exit(1);
1510
0
  }
1511
1512
  // Empty instance, platform and toolset if only a generator is specified
1513
35
  if (this->GlobalGenerator) {
1514
0
    this->GeneratorInstance = "";
1515
0
    if (!this->GeneratorPlatformSet) {
1516
0
      this->GeneratorPlatform = "";
1517
0
    }
1518
0
    if (!this->GeneratorToolsetSet) {
1519
0
      this->GeneratorToolset = "";
1520
0
    }
1521
0
  }
1522
1523
35
#if !defined(CMAKE_BOOTSTRAP)
1524
35
  if (!profilingOutput.empty() || !profilingFormat.empty()) {
1525
0
    if (profilingOutput.empty()) {
1526
0
      cmSystemTools::Error(
1527
0
        "--profiling-format specified but no --profiling-output!");
1528
0
      return;
1529
0
    }
1530
0
    if (profilingFormat == "google-trace"_s) {
1531
0
      try {
1532
0
        this->ProfilingOutput =
1533
0
          cm::make_unique<cmMakefileProfilingData>(profilingOutput);
1534
0
      } catch (std::runtime_error& e) {
1535
0
        cmSystemTools::Error(
1536
0
          cmStrCat("Could not start profiling: ", e.what()));
1537
0
        return;
1538
0
      }
1539
0
    } else {
1540
0
      cmSystemTools::Error("Invalid format specified for --profiling-format");
1541
0
      return;
1542
0
    }
1543
0
  }
1544
35
#endif
1545
1546
35
  bool const haveSourceDir = !this->GetHomeDirectory().empty();
1547
35
  bool const haveBinaryDir = !this->GetHomeOutputDirectory().empty();
1548
35
  bool const havePreset =
1549
#ifdef CMAKE_BOOTSTRAP
1550
    false;
1551
#else
1552
35
    !presetName.empty();
1553
35
#endif
1554
1555
35
  if (this->State->GetRole() == cmState::Role::Project && !haveSourceDir &&
1556
0
      !haveBinaryDir && !havePreset) {
1557
0
    this->IssueMessage(
1558
0
      MessageType::WARNING,
1559
0
      "No source or binary directory provided. Both will be assumed to be "
1560
0
      "the same as the current working directory, but note that this "
1561
0
      "warning will become a fatal error in future CMake releases.");
1562
0
  }
1563
1564
35
  if (!haveSourceDir) {
1565
0
    this->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
1566
0
  }
1567
35
  if (!haveBinaryDir) {
1568
0
    this->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
1569
0
  }
1570
1571
35
#if !defined(CMAKE_BOOTSTRAP)
1572
35
  if (listPresets != ListPresets::None || !presetName.empty()) {
1573
0
    cmCMakePresetsGraph presetsGraph;
1574
0
    auto result = presetsGraph.ReadProjectPresets(this->GetHomeDirectory());
1575
0
    if (result != true) {
1576
0
      std::string errorMsg =
1577
0
        cmStrCat("Could not read presets from ", this->GetHomeDirectory(),
1578
0
                 ":\n", presetsGraph.parseState.GetErrorMessage());
1579
0
      cmSystemTools::Error(errorMsg);
1580
0
      return;
1581
0
    }
1582
1583
0
    if (listPresets != ListPresets::None) {
1584
0
      if (listPresets == ListPresets::Configure) {
1585
0
        this->PrintPresetList(presetsGraph);
1586
0
      } else if (listPresets == ListPresets::Build) {
1587
0
        presetsGraph.PrintBuildPresetList();
1588
0
      } else if (listPresets == ListPresets::Test) {
1589
0
        presetsGraph.PrintTestPresetList();
1590
0
      } else if (listPresets == ListPresets::Package) {
1591
0
        presetsGraph.PrintPackagePresetList();
1592
0
      } else if (listPresets == ListPresets::Workflow) {
1593
0
        presetsGraph.PrintWorkflowPresetList();
1594
0
      } else if (listPresets == ListPresets::All) {
1595
0
        presetsGraph.PrintAllPresets();
1596
0
      }
1597
1598
0
      this->State->SetRoleToHelpForListPresets();
1599
0
      return;
1600
0
    }
1601
1602
0
    auto preset = presetsGraph.ConfigurePresets.find(presetName);
1603
0
    if (preset == presetsGraph.ConfigurePresets.end()) {
1604
0
      cmSystemTools::Error(cmStrCat("No such preset in ",
1605
0
                                    this->GetHomeDirectory(), ": \"",
1606
0
                                    presetName, '"'));
1607
0
      this->PrintPresetList(presetsGraph);
1608
0
      return;
1609
0
    }
1610
0
    if (preset->second.Unexpanded.Hidden) {
1611
0
      cmSystemTools::Error(cmStrCat("Cannot use hidden preset in ",
1612
0
                                    this->GetHomeDirectory(), ": \"",
1613
0
                                    presetName, '"'));
1614
0
      this->PrintPresetList(presetsGraph);
1615
0
      return;
1616
0
    }
1617
0
    auto const& expandedPreset = preset->second.Expanded;
1618
0
    if (!expandedPreset) {
1619
0
      cmSystemTools::Error(cmStrCat("Could not evaluate preset \"",
1620
0
                                    preset->second.Unexpanded.Name,
1621
0
                                    "\": Invalid macro expansion"));
1622
0
      return;
1623
0
    }
1624
0
    if (!expandedPreset->ConditionResult) {
1625
0
      cmSystemTools::Error(cmStrCat("Could not use disabled preset \"",
1626
0
                                    preset->second.Unexpanded.Name, '"'));
1627
0
      return;
1628
0
    }
1629
1630
0
    if (!this->State->IsCacheLoaded() && !haveBArg &&
1631
0
        !expandedPreset->BinaryDir.empty()) {
1632
0
      this->SetHomeOutputDirectory(expandedPreset->BinaryDir);
1633
0
    }
1634
0
    if (!this->GlobalGenerator && !expandedPreset->Generator.empty()) {
1635
0
      if (!this->CreateAndSetGlobalGenerator(expandedPreset->Generator)) {
1636
0
        return;
1637
0
      }
1638
0
    }
1639
0
    this->UnprocessedPresetVariables = expandedPreset->CacheVariables;
1640
0
    this->UnprocessedPresetEnvironment = expandedPreset->Environment;
1641
1642
0
    if (!expandedPreset->InstallDir.empty() &&
1643
0
        !this->State->GetInitializedCacheValue("CMAKE_INSTALL_PREFIX")) {
1644
0
      this->UnprocessedPresetVariables["CMAKE_INSTALL_PREFIX"] = {
1645
0
        "PATH", expandedPreset->InstallDir
1646
0
      };
1647
0
    }
1648
0
    if (!expandedPreset->ToolchainFile.empty() &&
1649
0
        !this->State->GetInitializedCacheValue("CMAKE_TOOLCHAIN_FILE")) {
1650
0
      this->UnprocessedPresetVariables["CMAKE_TOOLCHAIN_FILE"] = {
1651
0
        "FILEPATH", expandedPreset->ToolchainFile
1652
0
      };
1653
0
    }
1654
1655
0
    if (!expandedPreset->ArchitectureStrategy ||
1656
0
        expandedPreset->ArchitectureStrategy ==
1657
0
          cmCMakePresetsGraph::ArchToolsetStrategy::Set) {
1658
0
      if (!this->GeneratorPlatformSet &&
1659
0
          !expandedPreset->Architecture.empty()) {
1660
0
        this->SetGeneratorPlatform(expandedPreset->Architecture);
1661
0
      }
1662
0
    }
1663
0
    if (!expandedPreset->ToolsetStrategy ||
1664
0
        expandedPreset->ToolsetStrategy ==
1665
0
          cmCMakePresetsGraph::ArchToolsetStrategy::Set) {
1666
0
      if (!this->GeneratorToolsetSet && !expandedPreset->Toolset.empty()) {
1667
0
        this->SetGeneratorToolset(expandedPreset->Toolset);
1668
0
      }
1669
0
    }
1670
1671
0
    if (!expandedPreset->GraphVizFile.empty()) {
1672
0
      if (this->GraphVizFile.empty()) {
1673
0
        this->SetGraphVizFile(
1674
0
          cmSystemTools::CollapseFullPath(expandedPreset->GraphVizFile));
1675
0
      }
1676
0
    }
1677
1678
0
    this->SetWarningFromPreset("dev", expandedPreset->WarnDev,
1679
0
                               expandedPreset->ErrorDev);
1680
0
    this->SetWarningFromPreset("deprecated", expandedPreset->WarnDeprecated,
1681
0
                               expandedPreset->ErrorDeprecated);
1682
0
    if (expandedPreset->WarnUninitialized == true) {
1683
0
      this->SetWarnUninitialized(true);
1684
0
    }
1685
0
    if (expandedPreset->WarnUnusedCli == false) {
1686
0
      this->SetWarnUnusedCli(false);
1687
0
    }
1688
0
    if (expandedPreset->WarnSystemVars == true) {
1689
0
      this->SetCheckSystemVars(true);
1690
0
    }
1691
0
    if (expandedPreset->DebugOutput == true) {
1692
0
      this->SetDebugOutputOn(true);
1693
0
    }
1694
0
    if (expandedPreset->DebugTryCompile == true) {
1695
0
      this->DebugTryCompileOn();
1696
0
    }
1697
0
    if (expandedPreset->DebugFind == true) {
1698
0
      this->SetDebugFindOutput(true);
1699
0
    }
1700
0
    if (expandedPreset->TraceMode &&
1701
0
        expandedPreset->TraceMode !=
1702
0
          cmCMakePresetsGraph::TraceEnableMode::Disable) {
1703
0
      this->SetTrace(true);
1704
0
      if (expandedPreset->TraceMode ==
1705
0
          cmCMakePresetsGraph::TraceEnableMode::Expand) {
1706
0
        this->SetTraceExpand(true);
1707
0
      }
1708
0
    }
1709
0
    if (expandedPreset->TraceFormat) {
1710
0
      this->SetTrace(true);
1711
0
      this->SetTraceFormat(*expandedPreset->TraceFormat);
1712
0
    }
1713
0
    if (!expandedPreset->TraceSource.empty()) {
1714
0
      this->SetTrace(true);
1715
0
      for (std::string const& filePaths : expandedPreset->TraceSource) {
1716
0
        this->AddTraceSource(filePaths);
1717
0
      }
1718
0
    }
1719
0
    if (!expandedPreset->TraceRedirect.empty()) {
1720
0
      this->SetTrace(true);
1721
0
      this->SetTraceFile(expandedPreset->TraceRedirect);
1722
0
    }
1723
0
  }
1724
35
#endif
1725
35
}
1726
1727
namespace {
1728
using LevelsPair = std::pair<cm::string_view, Message::LogLevel>;
1729
using LevelsPairArray = std::array<LevelsPair, 7>;
1730
LevelsPairArray const& getStringToLogLevelPairs()
1731
0
{
1732
0
  static LevelsPairArray const levels = {
1733
0
    { { "error", Message::LogLevel::LOG_ERROR },
1734
0
      { "warning", Message::LogLevel::LOG_WARNING },
1735
0
      { "notice", Message::LogLevel::LOG_NOTICE },
1736
0
      { "status", Message::LogLevel::LOG_STATUS },
1737
0
      { "verbose", Message::LogLevel::LOG_VERBOSE },
1738
0
      { "debug", Message::LogLevel::LOG_DEBUG },
1739
0
      { "trace", Message::LogLevel::LOG_TRACE } }
1740
0
  };
1741
0
  return levels;
1742
0
}
1743
} // namespace
1744
1745
Message::LogLevel cmake::StringToLogLevel(cm::string_view levelStr)
1746
0
{
1747
0
  LevelsPairArray const& levels = getStringToLogLevelPairs();
1748
1749
0
  auto const levelStrLowCase =
1750
0
    cmSystemTools::LowerCase(std::string{ levelStr });
1751
1752
  // NOLINTNEXTLINE(readability-qualified-auto)
1753
0
  auto const it = std::find_if(levels.cbegin(), levels.cend(),
1754
0
                               [&levelStrLowCase](LevelsPair const& p) {
1755
0
                                 return p.first == levelStrLowCase;
1756
0
                               });
1757
0
  return (it != levels.cend()) ? it->second : Message::LogLevel::LOG_UNDEFINED;
1758
0
}
1759
1760
std::string cmake::LogLevelToString(Message::LogLevel level)
1761
0
{
1762
0
  LevelsPairArray const& levels = getStringToLogLevelPairs();
1763
1764
  // NOLINTNEXTLINE(readability-qualified-auto)
1765
0
  auto const it =
1766
0
    std::find_if(levels.cbegin(), levels.cend(),
1767
0
                 [&level](LevelsPair const& p) { return p.second == level; });
1768
0
  cm::string_view const levelStrLowerCase =
1769
0
    (it != levels.cend()) ? it->first : "undefined";
1770
0
  std::string levelStrUpperCase =
1771
0
    cmSystemTools::UpperCase(std::string{ levelStrLowerCase });
1772
0
  return levelStrUpperCase;
1773
0
}
1774
1775
cmake::TraceFormat cmake::StringToTraceFormat(std::string const& traceStr)
1776
0
{
1777
0
  using TracePair = std::pair<std::string, TraceFormat>;
1778
0
  static std::vector<TracePair> const levels = {
1779
0
    { "human", TraceFormat::Human },
1780
0
    { "json-v1", TraceFormat::JSONv1 },
1781
0
  };
1782
1783
0
  auto const traceStrLowCase = cmSystemTools::LowerCase(traceStr);
1784
1785
0
  auto const it = std::find_if(levels.cbegin(), levels.cend(),
1786
0
                               [&traceStrLowCase](TracePair const& p) {
1787
0
                                 return p.first == traceStrLowCase;
1788
0
                               });
1789
0
  return (it != levels.cend()) ? it->second : TraceFormat::Undefined;
1790
0
}
1791
1792
bool cmake::PopTraceCmd()
1793
0
{
1794
0
  if (this->cmakeLangTraceCmdStack.empty()) {
1795
    // Nothing to pop! A caller should report an error.
1796
0
    return false;
1797
0
  }
1798
0
  this->cmakeLangTraceCmdStack.pop();
1799
0
  return true;
1800
0
}
1801
1802
void cmake::SetTraceFile(std::string const& file)
1803
0
{
1804
0
  this->TraceFile.close();
1805
0
  this->TraceFile.open(file.c_str());
1806
0
  if (!this->TraceFile) {
1807
0
    cmSystemTools::Error(cmStrCat("Error opening trace file ", file, ": ",
1808
0
                                  cmSystemTools::GetLastSystemError()));
1809
0
    return;
1810
0
  }
1811
0
  std::cout << "Trace will be written to " << file << '\n';
1812
0
}
1813
1814
void cmake::PrintTraceFormatVersion()
1815
0
{
1816
0
  if (!this->GetTrace()) {
1817
0
    return;
1818
0
  }
1819
1820
0
  std::string msg;
1821
1822
0
  switch (this->GetTraceFormat()) {
1823
0
    case TraceFormat::JSONv1: {
1824
0
#ifndef CMAKE_BOOTSTRAP
1825
0
      Json::Value val;
1826
0
      Json::Value version;
1827
0
      Json::StreamWriterBuilder builder;
1828
0
      builder["indentation"] = "";
1829
0
      version["major"] = 1;
1830
0
      version["minor"] = 2;
1831
0
      val["version"] = version;
1832
0
      msg = Json::writeString(builder, val);
1833
0
#endif
1834
0
      break;
1835
0
    }
1836
0
    case TraceFormat::Human:
1837
0
      msg = "";
1838
0
      break;
1839
0
    case TraceFormat::Undefined:
1840
0
      msg = "INTERNAL ERROR: Trace format is Undefined";
1841
0
      break;
1842
0
  }
1843
1844
0
  if (msg.empty()) {
1845
0
    return;
1846
0
  }
1847
1848
0
  auto& f = this->GetTraceFile();
1849
0
  if (f) {
1850
0
    f << msg << '\n';
1851
0
  } else {
1852
0
    cmSystemTools::Message(msg);
1853
0
  }
1854
0
}
1855
1856
void cmake::SetTraceRedirect(cmake* other)
1857
0
{
1858
0
  this->Trace = other->Trace;
1859
0
  this->TraceExpand = other->TraceExpand;
1860
0
  this->TraceFormatVar = other->TraceFormatVar;
1861
0
  this->TraceOnlyThisSources = other->TraceOnlyThisSources;
1862
1863
0
  this->TraceRedirect = other;
1864
0
}
1865
1866
bool cmake::SetDirectoriesFromFile(std::string const& arg)
1867
0
{
1868
  // Check if the argument refers to a CMakeCache.txt or CMakeLists.txt file.
1869
  // Do not check for the custom project filename CMAKE_LIST_FILE_NAME, as it
1870
  // cannot be determined until after reading the CMakeCache.txt
1871
0
  std::string listPath;
1872
0
  std::string cachePath;
1873
0
  bool is_source_dir = false;
1874
0
  bool is_empty_directory = false;
1875
0
  if (cmSystemTools::FileIsDirectory(arg)) {
1876
0
    std::string path = cmSystemTools::ToNormalizedPathOnDisk(arg);
1877
0
    std::string cacheFile = cmStrCat(path, "/CMakeCache.txt");
1878
0
    std::string listFile = this->GetCMakeListFile(path);
1879
1880
0
    is_empty_directory = true;
1881
0
    if (cmSystemTools::FileExists(cacheFile)) {
1882
0
      cachePath = path;
1883
0
      is_empty_directory = false;
1884
0
    }
1885
0
    if (cmSystemTools::FileExists(listFile)) {
1886
0
      listPath = path;
1887
0
      is_empty_directory = false;
1888
0
      is_source_dir = true;
1889
0
    }
1890
0
  } else if (cmSystemTools::FileExists(arg)) {
1891
0
    std::string fullPath = cmSystemTools::ToNormalizedPathOnDisk(arg);
1892
0
    std::string name = cmSystemTools::GetFilenameName(fullPath);
1893
0
    name = cmSystemTools::LowerCase(name);
1894
0
    if (name == "cmakecache.txt"_s) {
1895
0
      cachePath = cmSystemTools::GetFilenamePath(fullPath);
1896
0
    } else if (name == "cmakelists.txt"_s) {
1897
0
      listPath = cmSystemTools::GetFilenamePath(fullPath);
1898
0
    }
1899
0
  } else {
1900
    // Specified file or directory does not exist.  Try to set things
1901
    // up to produce a meaningful error message.
1902
0
    std::string fullPath = cmSystemTools::CollapseFullPath(arg);
1903
0
    std::string name = cmSystemTools::GetFilenameName(fullPath);
1904
0
    name = cmSystemTools::LowerCase(name);
1905
0
    if (name == "cmakecache.txt"_s || name == "cmakelists.txt"_s) {
1906
0
      listPath = cmSystemTools::GetFilenamePath(fullPath);
1907
0
    } else {
1908
0
      listPath = fullPath;
1909
0
    }
1910
0
  }
1911
1912
  // If there is a CMakeCache.txt file, use its settings.
1913
0
  if (!cachePath.empty()) {
1914
0
    if (this->LoadCache(cachePath)) {
1915
0
      cmValue existingValue =
1916
0
        this->State->GetCacheEntryValue("CMAKE_HOME_DIRECTORY");
1917
0
      if (existingValue && !existingValue.IsEmpty()) {
1918
0
        this->SetHomeOutputDirectory(cachePath);
1919
0
        this->SetHomeDirectory(*existingValue);
1920
0
        return true;
1921
0
      }
1922
0
    }
1923
0
  }
1924
1925
0
  bool no_source_tree = this->GetHomeDirectory().empty();
1926
0
  bool no_build_tree = this->GetHomeOutputDirectory().empty();
1927
1928
  // When invoked with a path that points to an existing CMakeCache
1929
  // This function is called multiple times with the same path
1930
0
  bool const passed_same_path = (listPath == this->GetHomeDirectory()) ||
1931
0
    (listPath == this->GetHomeOutputDirectory());
1932
0
  bool used_provided_path =
1933
0
    (passed_same_path || is_source_dir || no_build_tree);
1934
1935
  // If there is a CMakeLists.txt file, use it as the source tree.
1936
0
  if (!listPath.empty()) {
1937
    // When invoked with a path that points to an existing CMakeCache
1938
    // This function is called multiple times with the same path
1939
0
    if (is_source_dir) {
1940
0
      this->SetHomeDirectoryViaCommandLine(listPath);
1941
0
      if (no_build_tree) {
1942
0
        this->SetHomeOutputDirectory(
1943
0
          cmSystemTools::GetLogicalWorkingDirectory());
1944
0
      }
1945
0
    } else if (no_source_tree && no_build_tree) {
1946
0
      this->SetHomeDirectory(listPath);
1947
0
      this->SetHomeOutputDirectory(
1948
0
        cmSystemTools::GetLogicalWorkingDirectory());
1949
0
    } else if (no_build_tree) {
1950
0
      this->SetHomeOutputDirectory(listPath);
1951
0
    }
1952
0
  } else {
1953
0
    if (no_source_tree) {
1954
      // We didn't find a CMakeLists.txt and it wasn't specified
1955
      // with -S. Assume it is the path to the source tree
1956
0
      this->SetHomeDirectory(cmSystemTools::ToNormalizedPathOnDisk(arg));
1957
0
    }
1958
0
    if (no_build_tree && !no_source_tree && is_empty_directory) {
1959
      // passed `-S <path> <build_dir> when build_dir is an empty directory
1960
0
      this->SetHomeOutputDirectory(cmSystemTools::ToNormalizedPathOnDisk(arg));
1961
0
    } else if (no_build_tree) {
1962
      // We didn't find a CMakeCache.txt and it wasn't specified
1963
      // with -B. Assume the current working directory as the build tree.
1964
0
      this->SetHomeOutputDirectory(
1965
0
        cmSystemTools::GetLogicalWorkingDirectory());
1966
0
      used_provided_path = false;
1967
0
    }
1968
0
  }
1969
1970
0
  return used_provided_path;
1971
0
}
1972
1973
// at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
1974
// cache
1975
int cmake::AddCMakePaths()
1976
1
{
1977
  // Save the value in the cache
1978
1
  this->AddCacheEntry("CMAKE_COMMAND", cmSystemTools::GetCMakeCommand(),
1979
1
                      "Path to CMake executable.", cmStateEnums::INTERNAL);
1980
1
#ifndef CMAKE_BOOTSTRAP
1981
1
  this->AddCacheEntry("CMAKE_CTEST_COMMAND", cmSystemTools::GetCTestCommand(),
1982
1
                      "Path to ctest program executable.",
1983
1
                      cmStateEnums::INTERNAL);
1984
1
  this->AddCacheEntry("CMAKE_CPACK_COMMAND", cmSystemTools::GetCPackCommand(),
1985
1
                      "Path to cpack program executable.",
1986
1
                      cmStateEnums::INTERNAL);
1987
1
#endif
1988
1
  if (!cmSystemTools::FileExists(
1989
1
        (cmSystemTools::GetCMakeRoot() + "/Modules/CMake.cmake"))) {
1990
    // couldn't find modules
1991
1
    cmSystemTools::Error(
1992
1
      cmStrCat("Could not find CMAKE_ROOT !!!\n"
1993
1
               "CMake has most likely not been installed correctly.\n"
1994
1
               "Modules directory not found in\n",
1995
1
               cmSystemTools::GetCMakeRoot()));
1996
1
    return 0;
1997
1
  }
1998
0
  this->AddCacheEntry("CMAKE_ROOT", cmSystemTools::GetCMakeRoot(),
1999
0
                      "Path to CMake installation.", cmStateEnums::INTERNAL);
2000
2001
0
  return 1;
2002
1
}
2003
2004
void cmake::AddDefaultExtraGenerators()
2005
35
{
2006
35
#if !defined(CMAKE_BOOTSTRAP)
2007
35
  this->ExtraGenerators.push_back(cmExtraCodeBlocksGenerator::GetFactory());
2008
35
  this->ExtraGenerators.push_back(cmExtraCodeLiteGenerator::GetFactory());
2009
35
  this->ExtraGenerators.push_back(cmExtraEclipseCDT4Generator::GetFactory());
2010
35
  this->ExtraGenerators.push_back(cmExtraKateGenerator::GetFactory());
2011
35
  this->ExtraGenerators.push_back(cmExtraSublimeTextGenerator::GetFactory());
2012
35
#endif
2013
35
}
2014
2015
void cmake::GetRegisteredGenerators(
2016
  std::vector<GeneratorInfo>& generators) const
2017
0
{
2018
0
  for (auto const& gen : this->Generators) {
2019
0
    std::vector<std::string> names = gen->GetGeneratorNames();
2020
2021
0
    for (std::string const& name : names) {
2022
0
      GeneratorInfo info;
2023
0
      info.supportsToolset = gen->SupportsToolset();
2024
0
      info.supportsPlatform = gen->SupportsPlatform();
2025
0
      info.supportedPlatforms = gen->GetKnownPlatforms();
2026
0
      info.defaultPlatform = gen->GetDefaultPlatformName();
2027
0
      info.name = name;
2028
0
      info.baseName = name;
2029
0
      info.isAlias = false;
2030
0
      generators.push_back(std::move(info));
2031
0
    }
2032
0
  }
2033
2034
0
  for (cmExternalMakefileProjectGeneratorFactory* eg : this->ExtraGenerators) {
2035
0
    std::vector<std::string> const genList =
2036
0
      eg->GetSupportedGlobalGenerators();
2037
0
    for (std::string const& gen : genList) {
2038
0
      GeneratorInfo info;
2039
0
      info.name = cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
2040
0
        gen, eg->GetName());
2041
0
      info.baseName = gen;
2042
0
      info.extraName = eg->GetName();
2043
0
      info.supportsPlatform = false;
2044
0
      info.supportsToolset = false;
2045
0
      info.isAlias = false;
2046
0
      generators.push_back(std::move(info));
2047
0
    }
2048
0
    for (std::string const& a : eg->Aliases) {
2049
0
      GeneratorInfo info;
2050
0
      info.name = a;
2051
0
      if (!genList.empty()) {
2052
0
        info.baseName = genList.at(0);
2053
0
      }
2054
0
      info.extraName = eg->GetName();
2055
0
      info.supportsPlatform = false;
2056
0
      info.supportsToolset = false;
2057
0
      info.isAlias = true;
2058
0
      generators.push_back(std::move(info));
2059
0
    }
2060
0
  }
2061
0
}
2062
2063
static std::pair<std::unique_ptr<cmExternalMakefileProjectGenerator>,
2064
                 std::string>
2065
createExtraGenerator(
2066
  std::vector<cmExternalMakefileProjectGeneratorFactory*> const& in,
2067
  std::string const& name)
2068
0
{
2069
0
  for (cmExternalMakefileProjectGeneratorFactory* i : in) {
2070
0
    std::vector<std::string> const generators =
2071
0
      i->GetSupportedGlobalGenerators();
2072
0
    if (i->GetName() == name) { // Match aliases
2073
0
      return { i->CreateExternalMakefileProjectGenerator(), generators.at(0) };
2074
0
    }
2075
0
    for (std::string const& g : generators) {
2076
0
      std::string const fullName =
2077
0
        cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
2078
0
          g, i->GetName());
2079
0
      if (fullName == name) {
2080
0
        return { i->CreateExternalMakefileProjectGenerator(), g };
2081
0
      }
2082
0
    }
2083
0
  }
2084
0
  return { nullptr, name };
2085
0
}
2086
2087
std::unique_ptr<cmGlobalGenerator> cmake::CreateGlobalGenerator(
2088
  std::string const& gname)
2089
0
{
2090
0
  std::pair<std::unique_ptr<cmExternalMakefileProjectGenerator>, std::string>
2091
0
    extra = createExtraGenerator(this->ExtraGenerators, gname);
2092
0
  std::unique_ptr<cmExternalMakefileProjectGenerator>& extraGenerator =
2093
0
    extra.first;
2094
0
  std::string const& name = extra.second;
2095
2096
0
  std::unique_ptr<cmGlobalGenerator> generator;
2097
0
  for (auto const& g : this->Generators) {
2098
0
    generator = g->CreateGlobalGenerator(name, this);
2099
0
    if (generator) {
2100
0
      break;
2101
0
    }
2102
0
  }
2103
2104
0
  if (generator) {
2105
0
    generator->SetExternalMakefileProjectGenerator(std::move(extraGenerator));
2106
0
  }
2107
2108
0
  return generator;
2109
0
}
2110
2111
bool cmake::CreateAndSetGlobalGenerator(std::string const& name)
2112
0
{
2113
0
  auto gen = this->CreateGlobalGenerator(name);
2114
0
  if (!gen) {
2115
0
    std::string kdevError;
2116
0
    std::string vsError;
2117
0
    if (name.find("KDevelop3", 0) != std::string::npos) {
2118
0
      kdevError = "\nThe KDevelop3 generator is not supported anymore.";
2119
0
    }
2120
0
    if (cmHasLiteralPrefix(name, "Visual Studio ") &&
2121
0
        name.length() >= cmStrLen("Visual Studio xx xxxx ")) {
2122
0
      vsError = "\nUsing platforms in Visual Studio generator names is not "
2123
0
                "supported in CMakePresets.json.";
2124
0
    }
2125
2126
0
    cmSystemTools::Error(
2127
0
      cmStrCat("Could not create named generator ", name, kdevError, vsError));
2128
0
    this->PrintGeneratorList();
2129
0
    return false;
2130
0
  }
2131
2132
0
  this->SetGlobalGenerator(std::move(gen));
2133
0
  return true;
2134
0
}
2135
2136
#ifndef CMAKE_BOOTSTRAP
2137
void cmake::PrintPresetList(cmCMakePresetsGraph const& graph) const
2138
0
{
2139
0
  std::vector<GeneratorInfo> generators;
2140
0
  this->GetRegisteredGenerators(generators);
2141
0
  auto filter =
2142
0
    [&generators](cmCMakePresetsGraph::ConfigurePreset const& preset) -> bool {
2143
0
    if (preset.Generator.empty()) {
2144
0
      return true;
2145
0
    }
2146
0
    auto condition = [&preset](GeneratorInfo const& info) -> bool {
2147
0
      return info.name == preset.Generator;
2148
0
    };
2149
0
    auto it = std::find_if(generators.begin(), generators.end(), condition);
2150
0
    return it != generators.end();
2151
0
  };
2152
2153
0
  graph.PrintConfigurePresetList(filter);
2154
0
}
2155
#endif
2156
2157
void cmake::SetHomeDirectoryViaCommandLine(std::string const& path)
2158
0
{
2159
0
  if (path.empty()) {
2160
0
    return;
2161
0
  }
2162
2163
0
  auto prev_path = this->GetHomeDirectory();
2164
0
  if (prev_path != path && !prev_path.empty() &&
2165
0
      this->State->GetRole() == cmState::Role::Project) {
2166
0
    this->IssueMessage(
2167
0
      MessageType::WARNING,
2168
0
      cmStrCat("Ignoring extra path from command line:\n \"", prev_path, '"'));
2169
0
  }
2170
0
  this->SetHomeDirectory(path);
2171
0
}
2172
2173
void cmake::SetHomeDirectory(std::string const& dir)
2174
36
{
2175
36
  assert(!dir.empty());
2176
36
  this->State->SetSourceDirectory(dir);
2177
36
  if (this->CurrentSnapshot.IsValid()) {
2178
36
    this->CurrentSnapshot.SetDefinition("CMAKE_SOURCE_DIR", dir);
2179
36
  }
2180
2181
36
  if (this->State->GetIsTryCompile() == cmState::TryCompile::No) {
2182
36
    this->Messenger->SetTopSource(this->GetHomeDirectory());
2183
36
  } else {
2184
0
    this->Messenger->SetTopSource(cm::nullopt);
2185
0
  }
2186
36
}
2187
2188
std::string const& cmake::GetHomeDirectory() const
2189
72
{
2190
72
  return this->State->GetSourceDirectory();
2191
72
}
2192
2193
void cmake::SetHomeOutputDirectory(std::string const& dir)
2194
36
{
2195
36
  assert(!dir.empty());
2196
36
  this->State->SetBinaryDirectory(dir);
2197
36
  if (this->CurrentSnapshot.IsValid()) {
2198
36
    this->CurrentSnapshot.SetDefinition("CMAKE_BINARY_DIR", dir);
2199
36
  }
2200
36
}
2201
2202
std::string const& cmake::GetHomeOutputDirectory() const
2203
36
{
2204
36
  return this->State->GetBinaryDirectory();
2205
36
}
2206
2207
std::string cmake::FindCacheFile(std::string const& binaryDir)
2208
0
{
2209
0
  std::string cachePath = binaryDir;
2210
0
  cmSystemTools::ConvertToUnixSlashes(cachePath);
2211
0
  std::string cacheFile = cmStrCat(cachePath, "/CMakeCache.txt");
2212
0
  if (!cmSystemTools::FileExists(cacheFile)) {
2213
    // search in parent directories for cache
2214
0
    std::string cmakeFiles = cmStrCat(cachePath, "/CMakeFiles");
2215
0
    if (cmSystemTools::FileExists(cmakeFiles)) {
2216
0
      std::string cachePathFound =
2217
0
        cmSystemTools::FileExistsInParentDirectories("CMakeCache.txt",
2218
0
                                                     cachePath, "/");
2219
0
      if (!cachePathFound.empty()) {
2220
0
        cachePath = cmSystemTools::GetFilenamePath(cachePathFound);
2221
0
      }
2222
0
    }
2223
0
  }
2224
0
  return cachePath;
2225
0
}
2226
2227
void cmake::SetGlobalGenerator(std::unique_ptr<cmGlobalGenerator> gg)
2228
0
{
2229
0
  if (!gg) {
2230
0
    cmSystemTools::Error("Error SetGlobalGenerator called with null");
2231
0
    return;
2232
0
  }
2233
0
  if (this->GlobalGenerator) {
2234
    // restore the original environment variables CXX and CC
2235
0
    std::string env = "CC=";
2236
0
    if (!this->CCEnvironment.empty()) {
2237
0
      env += this->CCEnvironment;
2238
0
      cmSystemTools::PutEnv(env);
2239
0
    } else {
2240
0
      cmSystemTools::UnPutEnv(env);
2241
0
    }
2242
0
    env = "CXX=";
2243
0
    if (!this->CXXEnvironment.empty()) {
2244
0
      env += this->CXXEnvironment;
2245
0
      cmSystemTools::PutEnv(env);
2246
0
    } else {
2247
0
      cmSystemTools::UnPutEnv(env);
2248
0
    }
2249
0
  }
2250
2251
  // set the new
2252
0
  this->GlobalGenerator = std::move(gg);
2253
2254
  // set the global flag for unix style paths on cmSystemTools as soon as
2255
  // the generator is set.  This allows gmake to be used on windows.
2256
0
  cmSystemTools::SetForceUnixPaths(this->GlobalGenerator->GetForceUnixPaths());
2257
2258
  // Save the environment variables CXX and CC
2259
0
  if (!cmSystemTools::GetEnv("CXX", this->CXXEnvironment)) {
2260
0
    this->CXXEnvironment.clear();
2261
0
  }
2262
0
  if (!cmSystemTools::GetEnv("CC", this->CCEnvironment)) {
2263
0
    this->CCEnvironment.clear();
2264
0
  }
2265
0
}
2266
2267
int cmake::DoPreConfigureChecks()
2268
0
{
2269
  // Make sure the Source directory contains a CMakeLists.txt file.
2270
0
  std::string srcList =
2271
0
    cmStrCat(this->GetHomeDirectory(), '/', this->CMakeListName);
2272
0
  if (!cmSystemTools::FileExists(srcList)) {
2273
0
    std::ostringstream err;
2274
0
    if (cmSystemTools::FileIsDirectory(this->GetHomeDirectory())) {
2275
0
      err << "The source directory \"" << this->GetHomeDirectory()
2276
0
          << "\" does not appear to contain " << this->CMakeListName << ".\n";
2277
0
    } else if (cmSystemTools::FileExists(this->GetHomeDirectory())) {
2278
0
      err << "The source directory \"" << this->GetHomeDirectory()
2279
0
          << "\" is a file, not a directory.\n";
2280
0
    } else {
2281
0
      err << "The source directory \"" << this->GetHomeDirectory()
2282
0
          << "\" does not exist.\n";
2283
0
    }
2284
0
    err << "Specify --help for usage, or press the help button on the CMake "
2285
0
           "GUI.";
2286
0
    cmSystemTools::Error(err.str());
2287
0
    return -2;
2288
0
  }
2289
2290
  // do a sanity check on some values
2291
0
  if (cmValue dir =
2292
0
        this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY")) {
2293
0
    std::string cacheStart = cmStrCat(*dir, '/', this->CMakeListName);
2294
0
    if (!cmSystemTools::SameFile(cacheStart, srcList)) {
2295
0
      std::string message =
2296
0
        cmStrCat("The source \"", srcList, "\" does not match the source \"",
2297
0
                 cacheStart,
2298
0
                 "\" used to generate cache.  Re-run cmake with a different "
2299
0
                 "source directory.");
2300
0
      cmSystemTools::Error(message);
2301
0
      return -2;
2302
0
    }
2303
0
  } else {
2304
0
    return 0;
2305
0
  }
2306
0
  return 1;
2307
0
}
2308
struct SaveCacheEntry
2309
{
2310
  std::string key;
2311
  std::string value;
2312
  std::string help;
2313
  cmStateEnums::CacheEntryType type;
2314
};
2315
2316
int cmake::HandleDeleteCacheVariables(std::string const& var)
2317
0
{
2318
0
  cmList argsSplit{ var, cmList::EmptyElements::Yes };
2319
  // erase the property to avoid infinite recursion
2320
0
  this->State->SetGlobalProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", "");
2321
0
  if (this->GetIsInTryCompile()) {
2322
0
    return 0;
2323
0
  }
2324
0
  std::vector<SaveCacheEntry> saved;
2325
0
  std::ostringstream warning;
2326
0
  warning
2327
0
    << "You have changed variables that require your cache to be deleted.\n"
2328
0
       "Configure will be re-run and you may have to reset some variables.\n"
2329
0
       "The following variables have changed:\n";
2330
0
  for (auto i = argsSplit.begin(); i != argsSplit.end(); ++i) {
2331
0
    SaveCacheEntry save;
2332
0
    save.key = *i;
2333
0
    warning << *i << "= ";
2334
0
    i++;
2335
0
    if (i != argsSplit.end()) {
2336
0
      save.value = *i;
2337
0
      warning << *i << '\n';
2338
0
    } else {
2339
0
      warning << '\n';
2340
0
      i -= 1;
2341
0
    }
2342
0
    cmValue existingValue = this->State->GetCacheEntryValue(save.key);
2343
0
    if (existingValue) {
2344
0
      save.type = this->State->GetCacheEntryType(save.key);
2345
0
      if (cmValue help =
2346
0
            this->State->GetCacheEntryProperty(save.key, "HELPSTRING")) {
2347
0
        save.help = *help;
2348
0
      }
2349
0
    } else {
2350
0
      save.type = cmStateEnums::CacheEntryType::UNINITIALIZED;
2351
0
    }
2352
0
    saved.push_back(std::move(save));
2353
0
  }
2354
2355
  // remove the cache
2356
0
  this->DeleteCache(this->GetHomeOutputDirectory());
2357
  // load the empty cache
2358
0
  this->LoadCache();
2359
  // restore the changed compilers
2360
0
  for (SaveCacheEntry const& i : saved) {
2361
0
    this->AddCacheEntry(i.key, i.value, i.help, i.type);
2362
0
  }
2363
0
  cmSystemTools::Message(warning.str());
2364
  // avoid reconfigure if there were errors
2365
0
  if (!cmSystemTools::GetErrorOccurredFlag()) {
2366
    // re-run configure
2367
0
    return this->Configure();
2368
0
  }
2369
0
  return 0;
2370
0
}
2371
2372
int cmake::Configure()
2373
0
{
2374
0
#if !defined(CMAKE_BOOTSTRAP)
2375
0
  auto profilingRAII = this->CreateProfilingEntry("project", "configure");
2376
0
#endif
2377
2378
0
  DiagLevel diagLevel;
2379
2380
0
  if (this->DiagLevels.count("deprecated") == 1) {
2381
2382
0
    diagLevel = this->DiagLevels["deprecated"];
2383
0
    if (diagLevel == DIAG_IGNORE) {
2384
0
      this->SetSuppressDeprecatedWarnings(true);
2385
0
      this->SetDeprecatedWarningsAsErrors(false);
2386
0
    } else if (diagLevel == DIAG_WARN) {
2387
0
      this->SetSuppressDeprecatedWarnings(false);
2388
0
      this->SetDeprecatedWarningsAsErrors(false);
2389
0
    } else if (diagLevel == DIAG_ERROR) {
2390
0
      this->SetSuppressDeprecatedWarnings(false);
2391
0
      this->SetDeprecatedWarningsAsErrors(true);
2392
0
    }
2393
0
  }
2394
2395
0
  if (this->DiagLevels.count("dev") == 1) {
2396
0
    bool setDeprecatedVariables = false;
2397
2398
0
    cmValue cachedWarnDeprecated =
2399
0
      this->State->GetCacheEntryValue("CMAKE_WARN_DEPRECATED");
2400
0
    cmValue cachedErrorDeprecated =
2401
0
      this->State->GetCacheEntryValue("CMAKE_ERROR_DEPRECATED");
2402
2403
    // don't overwrite deprecated warning setting from a previous invocation
2404
0
    if (!cachedWarnDeprecated && !cachedErrorDeprecated) {
2405
0
      setDeprecatedVariables = true;
2406
0
    }
2407
2408
0
    diagLevel = this->DiagLevels["dev"];
2409
0
    if (diagLevel == DIAG_IGNORE) {
2410
0
      this->SetSuppressDevWarnings(true);
2411
0
      this->SetDevWarningsAsErrors(false);
2412
2413
0
      if (setDeprecatedVariables) {
2414
0
        this->SetSuppressDeprecatedWarnings(true);
2415
0
        this->SetDeprecatedWarningsAsErrors(false);
2416
0
      }
2417
0
    } else if (diagLevel == DIAG_WARN) {
2418
0
      this->SetSuppressDevWarnings(false);
2419
0
      this->SetDevWarningsAsErrors(false);
2420
2421
0
      if (setDeprecatedVariables) {
2422
0
        this->SetSuppressDeprecatedWarnings(false);
2423
0
        this->SetDeprecatedWarningsAsErrors(false);
2424
0
      }
2425
0
    } else if (diagLevel == DIAG_ERROR) {
2426
0
      this->SetSuppressDevWarnings(false);
2427
0
      this->SetDevWarningsAsErrors(true);
2428
2429
0
      if (setDeprecatedVariables) {
2430
0
        this->SetSuppressDeprecatedWarnings(false);
2431
0
        this->SetDeprecatedWarningsAsErrors(true);
2432
0
      }
2433
0
    }
2434
0
  }
2435
2436
  // Cache variables may have already been set by a previous invocation,
2437
  // so we cannot rely on command line options alone. Always ensure our
2438
  // messenger is in sync with the cache.
2439
0
  cmValue value = this->State->GetCacheEntryValue("CMAKE_WARN_DEPRECATED");
2440
0
  this->Messenger->SetSuppressDeprecatedWarnings(value && value.IsOff());
2441
2442
0
  value = this->State->GetCacheEntryValue("CMAKE_ERROR_DEPRECATED");
2443
0
  this->Messenger->SetDeprecatedWarningsAsErrors(value.IsOn());
2444
2445
0
  value = this->State->GetCacheEntryValue("CMAKE_SUPPRESS_DEVELOPER_WARNINGS");
2446
0
  this->Messenger->SetSuppressDevWarnings(value.IsOn());
2447
2448
0
  value = this->State->GetCacheEntryValue("CMAKE_SUPPRESS_DEVELOPER_ERRORS");
2449
0
  this->Messenger->SetDevWarningsAsErrors(value && value.IsOff());
2450
2451
0
  int ret = this->ActualConfigure();
2452
0
  cmValue delCacheVars =
2453
0
    this->State->GetGlobalProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
2454
0
  if (delCacheVars && !delCacheVars->empty()) {
2455
0
    return this->HandleDeleteCacheVariables(*delCacheVars);
2456
0
  }
2457
0
  return ret;
2458
0
}
2459
2460
int cmake::ActualConfigure()
2461
0
{
2462
  // Construct right now our path conversion table before it's too late:
2463
0
  this->CleanupCommandsAndMacros();
2464
2465
0
  cmSystemTools::RemoveADirectory(this->GetHomeOutputDirectory() +
2466
0
                                  "/CMakeFiles/CMakeScratch");
2467
2468
0
  std::string cmlNameCache =
2469
0
    this->State->GetInitializedCacheValue("CMAKE_LIST_FILE_NAME");
2470
0
  if (!cmlNameCache.empty() && !this->CMakeListName.empty() &&
2471
0
      cmlNameCache != this->CMakeListName) {
2472
0
    std::string message =
2473
0
      cmStrCat("CMakeLists filename : \"", this->CMakeListName,
2474
0
               "\"\nDoes not match the previous: \"", cmlNameCache,
2475
0
               "\"\nEither remove the CMakeCache.txt file and CMakeFiles "
2476
0
               "directory or choose a different binary directory.");
2477
0
    cmSystemTools::Error(message);
2478
0
    return -2;
2479
0
  }
2480
0
  if (this->CMakeListName.empty()) {
2481
0
    this->CMakeListName =
2482
0
      cmlNameCache.empty() ? "CMakeLists.txt" : cmlNameCache;
2483
0
  }
2484
0
  if (this->CMakeListName != "CMakeLists.txt") {
2485
0
    this->IssueMessage(
2486
0
      MessageType::WARNING,
2487
0
      "This project has been configured with a project file other than "
2488
0
      "CMakeLists.txt. This feature is intended for temporary use during "
2489
0
      "development and not for publication of a final product.");
2490
0
  }
2491
0
  this->AddCacheEntry("CMAKE_LIST_FILE_NAME", this->CMakeListName,
2492
0
                      "Name of CMakeLists files to read",
2493
0
                      cmStateEnums::INTERNAL);
2494
2495
0
  int res = this->DoPreConfigureChecks();
2496
0
  if (res < 0) {
2497
0
    return -2;
2498
0
  }
2499
0
  if (!res) {
2500
0
    this->AddCacheEntry(
2501
0
      "CMAKE_HOME_DIRECTORY", this->GetHomeDirectory(),
2502
0
      "Source directory with the top level CMakeLists.txt file for this "
2503
0
      "project",
2504
0
      cmStateEnums::INTERNAL);
2505
0
  }
2506
2507
  // We want to create the package redirects directory as early as possible,
2508
  // but not before pre-configure checks have passed. This ensures we get
2509
  // errors about inappropriate source/binary directories first.
2510
0
  auto const redirectsDir =
2511
0
    cmStrCat(this->GetHomeOutputDirectory(), "/CMakeFiles/pkgRedirects");
2512
0
  cmSystemTools::RemoveADirectory(redirectsDir);
2513
0
  if (!cmSystemTools::MakeDirectory(redirectsDir)) {
2514
0
    cmSystemTools::Error(
2515
0
      cmStrCat("Unable to (re)create the private pkgRedirects directory:\n  ",
2516
0
               redirectsDir,
2517
0
               "\n"
2518
0
               "This may be caused by not having read/write access to "
2519
0
               "the build directory.\n"
2520
0
               "Try specifying a location with read/write access like:\n"
2521
0
               "  cmake -B build\n"
2522
0
               "If using a CMake presets file, ensure that preset parameter\n"
2523
0
               "'binaryDir' expands to a writable directory.\n"));
2524
0
    return -1;
2525
0
  }
2526
0
  this->AddCacheEntry("CMAKE_FIND_PACKAGE_REDIRECTS_DIR", redirectsDir,
2527
0
                      "Value Computed by CMake.", cmStateEnums::STATIC);
2528
2529
  // no generator specified on the command line
2530
0
  if (!this->GlobalGenerator) {
2531
0
    cmValue genName = this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
2532
0
    cmValue extraGenName =
2533
0
      this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
2534
0
    if (genName) {
2535
0
      std::string fullName =
2536
0
        cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
2537
0
          *genName, extraGenName ? *extraGenName : "");
2538
0
      this->GlobalGenerator = this->CreateGlobalGenerator(fullName);
2539
0
    }
2540
0
    if (this->GlobalGenerator) {
2541
      // set the global flag for unix style paths on cmSystemTools as
2542
      // soon as the generator is set.  This allows gmake to be used
2543
      // on windows.
2544
0
      cmSystemTools::SetForceUnixPaths(
2545
0
        this->GlobalGenerator->GetForceUnixPaths());
2546
0
    } else {
2547
0
      this->CreateDefaultGlobalGenerator();
2548
0
    }
2549
0
    if (!this->GlobalGenerator) {
2550
0
      cmSystemTools::Error("Could not create generator");
2551
0
      return -1;
2552
0
    }
2553
0
  }
2554
2555
0
  cmValue genName = this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
2556
0
  if (genName) {
2557
0
    if (!this->GlobalGenerator->MatchesGeneratorName(*genName)) {
2558
0
      std::string message =
2559
0
        cmStrCat("Error: generator : ", this->GlobalGenerator->GetName(),
2560
0
                 "\n"
2561
0
                 "Does not match the generator used previously: ",
2562
0
                 *genName,
2563
0
                 "\n"
2564
0
                 "Either remove the CMakeCache.txt file and CMakeFiles "
2565
0
                 "directory or choose a different binary directory.");
2566
0
      cmSystemTools::Error(message);
2567
0
      return -2;
2568
0
    }
2569
0
  }
2570
0
  if (!genName) {
2571
0
    this->AddCacheEntry("CMAKE_GENERATOR", this->GlobalGenerator->GetName(),
2572
0
                        "Name of generator.", cmStateEnums::INTERNAL);
2573
0
    this->AddCacheEntry(
2574
0
      "CMAKE_EXTRA_GENERATOR", this->GlobalGenerator->GetExtraGeneratorName(),
2575
0
      "Name of external makefile project generator.", cmStateEnums::INTERNAL);
2576
2577
0
    if (!this->State->GetInitializedCacheValue("CMAKE_TOOLCHAIN_FILE")) {
2578
0
      std::string envToolchain;
2579
0
      if (cmSystemTools::GetEnv("CMAKE_TOOLCHAIN_FILE", envToolchain) &&
2580
0
          !envToolchain.empty()) {
2581
0
        this->AddCacheEntry("CMAKE_TOOLCHAIN_FILE", envToolchain,
2582
0
                            "The CMake toolchain file",
2583
0
                            cmStateEnums::FILEPATH);
2584
0
      }
2585
0
    }
2586
0
  }
2587
2588
0
  if (cmValue instance =
2589
0
        this->State->GetInitializedCacheValue("CMAKE_GENERATOR_INSTANCE")) {
2590
0
    if (this->GeneratorInstanceSet && this->GeneratorInstance != *instance) {
2591
0
      std::string message =
2592
0
        cmStrCat("Error: generator instance: ", this->GeneratorInstance,
2593
0
                 "\n"
2594
0
                 "Does not match the instance used previously: ",
2595
0
                 *instance,
2596
0
                 "\n"
2597
0
                 "Either remove the CMakeCache.txt file and CMakeFiles "
2598
0
                 "directory or choose a different binary directory.");
2599
0
      cmSystemTools::Error(message);
2600
0
      return -2;
2601
0
    }
2602
0
  } else {
2603
0
    this->AddCacheEntry("CMAKE_GENERATOR_INSTANCE", this->GeneratorInstance,
2604
0
                        "Generator instance identifier.",
2605
0
                        cmStateEnums::INTERNAL);
2606
0
  }
2607
2608
0
  if (cmValue platformName =
2609
0
        this->State->GetInitializedCacheValue("CMAKE_GENERATOR_PLATFORM")) {
2610
0
    if (this->GeneratorPlatformSet &&
2611
0
        this->GeneratorPlatform != *platformName) {
2612
0
      std::string message =
2613
0
        cmStrCat("Error: generator platform: ", this->GeneratorPlatform,
2614
0
                 "\n"
2615
0
                 "Does not match the platform used previously: ",
2616
0
                 *platformName,
2617
0
                 "\n"
2618
0
                 "Either remove the CMakeCache.txt file and CMakeFiles "
2619
0
                 "directory or choose a different binary directory.");
2620
0
      cmSystemTools::Error(message);
2621
0
      return -2;
2622
0
    }
2623
0
  } else {
2624
0
    this->AddCacheEntry("CMAKE_GENERATOR_PLATFORM", this->GeneratorPlatform,
2625
0
                        "Name of generator platform.", cmStateEnums::INTERNAL);
2626
0
  }
2627
2628
0
  if (cmValue tsName =
2629
0
        this->State->GetInitializedCacheValue("CMAKE_GENERATOR_TOOLSET")) {
2630
0
    if (this->GeneratorToolsetSet && this->GeneratorToolset != *tsName) {
2631
0
      std::string message =
2632
0
        cmStrCat("Error: generator toolset: ", this->GeneratorToolset,
2633
0
                 "\n"
2634
0
                 "Does not match the toolset used previously: ",
2635
0
                 *tsName,
2636
0
                 "\n"
2637
0
                 "Either remove the CMakeCache.txt file and CMakeFiles "
2638
0
                 "directory or choose a different binary directory.");
2639
0
      cmSystemTools::Error(message);
2640
0
      return -2;
2641
0
    }
2642
0
  } else {
2643
0
    this->AddCacheEntry("CMAKE_GENERATOR_TOOLSET", this->GeneratorToolset,
2644
0
                        "Name of generator toolset.", cmStateEnums::INTERNAL);
2645
0
  }
2646
2647
0
  if (!this->State->GetInitializedCacheValue(
2648
0
        "CMAKE_INTERMEDIATE_DIR_STRATEGY") &&
2649
0
      this->IntermediateDirStrategy) {
2650
0
    this->AddCacheEntry(
2651
0
      "CMAKE_INTERMEDIATE_DIR_STRATEGY", *this->IntermediateDirStrategy,
2652
0
      "Select the intermediate directory strategy", cmStateEnums::INTERNAL);
2653
0
  }
2654
0
  if (!this->State->GetInitializedCacheValue(
2655
0
        "CMAKE_AUTOGEN_INTERMEDIATE_DIR_STRATEGY") &&
2656
0
      this->AutogenIntermediateDirStrategy) {
2657
0
    this->AddCacheEntry(
2658
0
      "CMAKE_AUTOGEN_INTERMEDIATE_DIR_STRATEGY",
2659
0
      *this->AutogenIntermediateDirStrategy,
2660
0
      "Select the intermediate directory strategy for Autogen",
2661
0
      cmStateEnums::INTERNAL);
2662
0
  }
2663
2664
0
  if (!this->State->GetInitializedCacheValue("CMAKE_TEST_LAUNCHER")) {
2665
0
    cm::optional<std::string> testLauncher =
2666
0
      cmSystemTools::GetEnvVar("CMAKE_TEST_LAUNCHER");
2667
0
    if (testLauncher && !testLauncher->empty()) {
2668
0
      std::string message = "Test launcher to run tests executable.";
2669
0
      this->AddCacheEntry("CMAKE_TEST_LAUNCHER", *testLauncher, message,
2670
0
                          cmStateEnums::STRING);
2671
0
    }
2672
0
  }
2673
2674
0
  if (!this->State->GetInitializedCacheValue(
2675
0
        "CMAKE_CROSSCOMPILING_EMULATOR")) {
2676
0
    cm::optional<std::string> emulator =
2677
0
      cmSystemTools::GetEnvVar("CMAKE_CROSSCOMPILING_EMULATOR");
2678
0
    if (emulator && !emulator->empty()) {
2679
0
      std::string message =
2680
0
        "Emulator to run executables and tests when cross compiling.";
2681
0
      this->AddCacheEntry("CMAKE_CROSSCOMPILING_EMULATOR", *emulator, message,
2682
0
                          cmStateEnums::STRING);
2683
0
    }
2684
0
  }
2685
2686
  // reset any system configuration information, except for when we are
2687
  // InTryCompile. With TryCompile the system info is taken from the parent's
2688
  // info to save time
2689
0
  if (!this->GetIsInTryCompile()) {
2690
0
    this->GlobalGenerator->ClearEnabledLanguages();
2691
0
  }
2692
2693
0
#if !defined(CMAKE_BOOTSTRAP)
2694
0
  this->InitializeFileAPI();
2695
0
  this->FileAPI->ReadQueries();
2696
0
  this->InitializeInstrumentation();
2697
2698
0
  if (!this->GetIsInTryCompile()) {
2699
0
    this->TruncateOutputLog("CMakeConfigureLog.yaml");
2700
0
    this->ConfigureLog = cm::make_unique<cmConfigureLog>(
2701
0
      cmStrCat(this->GetHomeOutputDirectory(), "/CMakeFiles"_s),
2702
0
      this->FileAPI->GetConfigureLogVersions());
2703
0
    this->Instrumentation->CheckCDashVariable();
2704
0
  }
2705
0
#endif
2706
2707
  // actually do the configure
2708
0
  auto startTime = std::chrono::steady_clock::now();
2709
0
#if !defined(CMAKE_BOOTSTRAP)
2710
0
  if (this->Instrumentation->HasErrors()) {
2711
0
    return 1;
2712
0
  }
2713
0
  auto doConfigure = [this]() -> int {
2714
0
    this->GlobalGenerator->Configure();
2715
0
    return 0;
2716
0
  };
2717
0
  int ret = this->Instrumentation->InstrumentCommand(
2718
0
    "configure", this->cmdArgs, [doConfigure]() { return doConfigure(); },
2719
0
    cm::nullopt, cm::nullopt,
2720
0
    this->GetIsInTryCompile() ? cmInstrumentation::LoadQueriesAfter::No
2721
0
                              : cmInstrumentation::LoadQueriesAfter::Yes);
2722
0
  if (ret != 0) {
2723
0
    return ret;
2724
0
  }
2725
#else
2726
  this->GlobalGenerator->Configure();
2727
#endif
2728
0
  auto endTime = std::chrono::steady_clock::now();
2729
2730
  // configure result
2731
0
  if (this->State->GetRole() == cmState::Role::Project) {
2732
0
    std::ostringstream msg;
2733
0
    if (cmSystemTools::GetErrorOccurredFlag()) {
2734
0
      msg << "Configuring incomplete, errors occurred!";
2735
0
    } else {
2736
0
      auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
2737
0
        endTime - startTime);
2738
0
      msg << "Configuring done (" << std::fixed << std::setprecision(1)
2739
0
          << ms.count() / 1000.0L << "s)";
2740
0
    }
2741
0
    this->UpdateProgress(msg.str(), -1);
2742
0
  }
2743
2744
0
#if !defined(CMAKE_BOOTSTRAP)
2745
0
  this->ConfigureLog.reset();
2746
0
#endif
2747
2748
  // Before saving the cache
2749
  // if the project did not define one of the entries below, add them now
2750
  // so users can edit the values in the cache:
2751
2752
0
  auto const& mf = this->GlobalGenerator->GetMakefiles()[0];
2753
2754
0
  if (mf->IsOn("CTEST_USE_LAUNCHERS") &&
2755
0
      !this->State->GetGlobalProperty("RULE_LAUNCH_COMPILE")) {
2756
0
    this->IssueMessage(MessageType::FATAL_ERROR,
2757
0
                       "CTEST_USE_LAUNCHERS is enabled, but the "
2758
0
                       "RULE_LAUNCH_COMPILE global property is not defined.\n"
2759
0
                       "Did you forget to include(CTest) in the toplevel "
2760
0
                       "CMakeLists.txt ?");
2761
0
  }
2762
  // Setup launchers for instrumentation
2763
0
#if !defined(CMAKE_BOOTSTRAP)
2764
0
  if (this->Instrumentation->HasQuery()) {
2765
0
    std::string launcher;
2766
0
    if (mf->IsOn("CTEST_USE_LAUNCHERS")) {
2767
0
      launcher = cmStrCat('"', cmSystemTools::GetCTestCommand(),
2768
0
                          "\" --launch "
2769
0
                          "--current-build-dir <CMAKE_CURRENT_BINARY_DIR> "
2770
0
                          "--object-dir <TARGET_SUPPORT_DIR> ");
2771
0
    } else {
2772
0
      launcher =
2773
0
        cmStrCat('"', cmSystemTools::GetCTestCommand(), "\" --instrument ");
2774
0
    }
2775
0
    std::string common_args =
2776
0
      cmStrCat(" --target-name <TARGET_NAME> --config <CONFIG> --build-dir \"",
2777
0
               this->State->GetBinaryDirectory(), "\" ");
2778
0
    this->State->SetGlobalProperty(
2779
0
      "RULE_LAUNCH_COMPILE",
2780
0
      cmStrCat(
2781
0
        launcher, "--command-type compile", common_args,
2782
0
        "--output <OBJECT> --source <SOURCE> --language <LANGUAGE> -- "));
2783
0
    this->State->SetGlobalProperty(
2784
0
      "RULE_LAUNCH_LINK",
2785
0
      cmStrCat(
2786
0
        launcher, "--command-type link", common_args,
2787
0
        "--output <TARGET> --config <CONFIG> --language <LANGUAGE> -- "));
2788
0
    this->State->SetGlobalProperty(
2789
0
      "RULE_LAUNCH_CUSTOM",
2790
0
      cmStrCat(launcher, "--command-type custom", common_args,
2791
0
               "--output \"<OUTPUT>\" --role <ROLE> -- "));
2792
0
  }
2793
0
#endif
2794
2795
0
  this->State->SaveVerificationScript(this->GetHomeOutputDirectory(),
2796
0
                                      this->Messenger.get());
2797
0
  this->SaveCache(this->GetHomeOutputDirectory());
2798
0
  if (cmSystemTools::GetErrorOccurredFlag()) {
2799
0
#if !defined(CMAKE_BOOTSTRAP)
2800
0
    this->FileAPI->WriteReplies(cmFileAPI::IndexFor::FailedConfigure);
2801
0
#endif
2802
0
    return -1;
2803
0
  }
2804
0
  return 0;
2805
0
}
2806
2807
std::unique_ptr<cmGlobalGenerator> cmake::EvaluateDefaultGlobalGenerator()
2808
0
{
2809
0
  if (!this->EnvironmentGenerator.empty()) {
2810
0
    auto gen = this->CreateGlobalGenerator(this->EnvironmentGenerator);
2811
0
    if (!gen) {
2812
0
      cmSystemTools::Error("CMAKE_GENERATOR was set but the specified "
2813
0
                           "generator doesn't exist. Using CMake default.");
2814
0
    } else {
2815
0
      return gen;
2816
0
    }
2817
0
  }
2818
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
2819
  std::string found;
2820
  // Try to find the newest VS installed on the computer and
2821
  // use that as a default if -G is not specified
2822
  std::string const vsregBase = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\";
2823
  static char const* const vsVariants[] = {
2824
    /* clang-format needs this comment to break after the opening brace */
2825
    "VisualStudio\\", "VCExpress\\", "WDExpress\\"
2826
  };
2827
  struct VSVersionedGenerator
2828
  {
2829
    char const* MSVersion;
2830
    char const* GeneratorName;
2831
  };
2832
  static VSVersionedGenerator const vsGenerators[] = {
2833
    { "14.0", "Visual Studio 14 2015" }, //
2834
  };
2835
  static char const* const vsEntries[] = {
2836
    "\\Setup\\VC;ProductDir", //
2837
    ";InstallDir"             //
2838
  };
2839
  if (cmVSSetupAPIHelper(18).IsVSInstalled()) {
2840
    found = "Visual Studio 18 2026";
2841
  } else if (cmVSSetupAPIHelper(17).IsVSInstalled()) {
2842
    found = "Visual Studio 17 2022";
2843
  } else if (cmVSSetupAPIHelper(16).IsVSInstalled()) {
2844
    found = "Visual Studio 16 2019";
2845
  } else if (cmVSSetupAPIHelper(15).IsVSInstalled()) {
2846
    found = "Visual Studio 15 2017";
2847
  } else {
2848
    for (VSVersionedGenerator const* g = cm::cbegin(vsGenerators);
2849
         found.empty() && g != cm::cend(vsGenerators); ++g) {
2850
      for (char const* const* v = cm::cbegin(vsVariants);
2851
           found.empty() && v != cm::cend(vsVariants); ++v) {
2852
        for (char const* const* e = cm::cbegin(vsEntries);
2853
             found.empty() && e != cm::cend(vsEntries); ++e) {
2854
          std::string const reg = vsregBase + *v + g->MSVersion + *e;
2855
          std::string dir;
2856
          if (cmSystemTools::ReadRegistryValue(reg, dir,
2857
                                               cmSystemTools::KeyWOW64_32) &&
2858
              cmSystemTools::PathExists(dir)) {
2859
            found = g->GeneratorName;
2860
          }
2861
        }
2862
      }
2863
    }
2864
  }
2865
  auto gen = this->CreateGlobalGenerator(found);
2866
  if (!gen) {
2867
    gen = cm::make_unique<cmGlobalNMakeMakefileGenerator>(this);
2868
  }
2869
  return std::unique_ptr<cmGlobalGenerator>(std::move(gen));
2870
#elif defined(CMAKE_BOOTSTRAP_NINJA)
2871
  return std::unique_ptr<cmGlobalGenerator>(
2872
    cm::make_unique<cmGlobalNinjaGenerator>(this));
2873
#else
2874
0
  return std::unique_ptr<cmGlobalGenerator>(
2875
0
    cm::make_unique<cmGlobalUnixMakefileGenerator3>(this));
2876
0
#endif
2877
0
}
2878
2879
void cmake::CreateDefaultGlobalGenerator()
2880
0
{
2881
0
  auto gen = this->EvaluateDefaultGlobalGenerator();
2882
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
2883
  // This print could be unified for all platforms
2884
  std::cout << "-- Building for: " << gen->GetName() << '\n';
2885
#endif
2886
0
  this->SetGlobalGenerator(std::move(gen));
2887
0
}
2888
2889
void cmake::PreLoadCMakeFiles()
2890
0
{
2891
0
  std::vector<std::string> args;
2892
0
  std::string pre_load = this->GetHomeDirectory();
2893
0
  if (!pre_load.empty()) {
2894
0
    pre_load += "/PreLoad.cmake";
2895
0
    if (cmSystemTools::FileExists(pre_load)) {
2896
0
      this->ReadListFile(args, pre_load);
2897
0
    }
2898
0
  }
2899
0
  pre_load = this->GetHomeOutputDirectory();
2900
0
  if (!pre_load.empty()) {
2901
0
    pre_load += "/PreLoad.cmake";
2902
0
    if (cmSystemTools::FileExists(pre_load)) {
2903
0
      this->ReadListFile(args, pre_load);
2904
0
    }
2905
0
  }
2906
0
}
2907
2908
#ifdef CMake_ENABLE_DEBUGGER
2909
2910
bool cmake::StartDebuggerIfEnabled()
2911
1
{
2912
1
  if (!this->GetDebuggerOn()) {
2913
1
    return true;
2914
1
  }
2915
2916
0
  if (!DebugAdapter) {
2917
0
    if (this->GetDebuggerPipe().empty()) {
2918
0
      std::cerr
2919
0
        << "Error: --debugger-pipe must be set when debugging is enabled.\n";
2920
0
      return false;
2921
0
    }
2922
2923
0
    try {
2924
0
      DebugAdapter = std::make_shared<cmDebugger::cmDebuggerAdapter>(
2925
0
        std::make_shared<cmDebugger::cmDebuggerPipeConnection>(
2926
0
          this->GetDebuggerPipe()),
2927
0
        this->GetDebuggerDapLogFile());
2928
0
    } catch (std::runtime_error const& error) {
2929
0
      std::cerr << "Error: Failed to create debugger adapter.\n";
2930
0
      std::cerr << error.what() << "\n";
2931
0
      return false;
2932
0
    }
2933
0
    Messenger->SetDebuggerAdapter(DebugAdapter);
2934
0
  }
2935
2936
0
  return true;
2937
0
}
2938
2939
void cmake::StopDebuggerIfNeeded(int exitCode)
2940
0
{
2941
0
  if (!this->GetDebuggerOn()) {
2942
0
    return;
2943
0
  }
2944
2945
  // The debug adapter may have failed to start (e.g. invalid pipe path).
2946
0
  if (DebugAdapter) {
2947
0
    DebugAdapter->ReportExitCode(exitCode);
2948
0
    DebugAdapter.reset();
2949
0
  }
2950
0
}
2951
2952
#endif
2953
2954
void cmake::InitializeFileAPI()
2955
0
{
2956
0
#ifndef CMAKE_BOOTSTRAP
2957
0
  if (!this->FileAPI) {
2958
0
    this->FileAPI = cm::make_unique<cmFileAPI>(this);
2959
0
  }
2960
0
#endif
2961
0
}
2962
2963
void cmake::InitializeInstrumentation()
2964
0
{
2965
0
#ifndef CMAKE_BOOTSTRAP
2966
0
  if (!this->Instrumentation) {
2967
0
    this->Instrumentation = cm::make_unique<cmInstrumentation>(
2968
0
      this->State->GetBinaryDirectory(),
2969
0
      cmInstrumentation::LoadQueriesAfter::No);
2970
0
    this->Instrumentation->ClearGeneratedQueries();
2971
0
  }
2972
0
#endif
2973
0
}
2974
2975
// handle a command line invocation
2976
int cmake::Run(std::vector<std::string> const& args, bool noconfigure)
2977
35
{
2978
  // Process the arguments
2979
35
  this->SetArgs(args);
2980
35
  if (cmSystemTools::GetErrorOccurredFlag()) {
2981
34
    return -1;
2982
34
  }
2983
1
  if (this->State->GetRole() == cmState::Role::Help) {
2984
0
    return 0;
2985
0
  }
2986
2987
1
#ifndef CMAKE_BOOTSTRAP
2988
  // Configure the SARIF log for the current run
2989
1
  cmSarif::LogFileWriter sarifLogFileWriter(
2990
1
    this->GetMessenger()->GetSarifResultsLog());
2991
1
  if (!sarifLogFileWriter.ConfigureForCMakeRun(*this)) {
2992
0
    return -1;
2993
0
  }
2994
1
#endif
2995
2996
  // Log the trace format version to the desired output
2997
1
  if (this->GetTrace()) {
2998
0
    this->PrintTraceFormatVersion();
2999
0
  }
3000
3001
  // If we are given a stamp list file check if it is really out of date.
3002
1
  if (!this->CheckStampList.empty() &&
3003
0
      cmakeCheckStampList(this->CheckStampList)) {
3004
0
    return 0;
3005
0
  }
3006
3007
  // If we are given a stamp file check if it is really out of date.
3008
1
  if (!this->CheckStampFile.empty() &&
3009
0
      cmakeCheckStampFile(this->CheckStampFile)) {
3010
0
    return 0;
3011
0
  }
3012
3013
1
  if (this->State->GetRole() == cmState::Role::Project) {
3014
0
    if (this->FreshCache) {
3015
0
      this->DeleteCache(this->GetHomeOutputDirectory());
3016
0
    }
3017
    // load the cache
3018
0
    if (this->LoadCache() < 0) {
3019
0
      cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
3020
0
      return -1;
3021
0
    }
3022
0
#ifndef CMAKE_BOOTSTRAP
3023
    // If no SARIF file has been explicitly specified, use the default path
3024
0
    if (!this->SarifFileOutput) {
3025
      // If no output file is specified, use the default path
3026
      // Enable parent directory creation for the default path
3027
0
      sarifLogFileWriter.SetPath(cmStrCat(this->GetHomeOutputDirectory(), '/',
3028
0
                                          cmSarif::PROJECT_DEFAULT_SARIF_FILE),
3029
0
                                 true);
3030
0
    }
3031
0
#endif
3032
1
  } else {
3033
1
    if (this->FreshCache) {
3034
0
      cmSystemTools::Error("--fresh allowed only when configuring a project");
3035
0
      return -1;
3036
0
    }
3037
1
    this->AddCMakePaths();
3038
1
  }
3039
3040
1
#ifndef CMAKE_BOOTSTRAP
3041
1
  this->ProcessPresetVariables();
3042
1
  this->ProcessPresetEnvironment();
3043
1
#endif
3044
  // Add any cache args
3045
1
  if (!this->SetCacheArgs(args)) {
3046
0
    cmSystemTools::Error("Run 'cmake --help' for all supported options.");
3047
0
    return -1;
3048
0
  }
3049
1
#ifndef CMAKE_BOOTSTRAP
3050
1
  if (this->GetLogLevel() == Message::LogLevel::LOG_VERBOSE ||
3051
1
      this->GetLogLevel() == Message::LogLevel::LOG_DEBUG ||
3052
1
      this->GetLogLevel() == Message::LogLevel::LOG_TRACE) {
3053
0
    this->PrintPresetVariables();
3054
0
    this->PrintPresetEnvironment();
3055
0
  }
3056
1
#endif
3057
3058
  // In script mode we terminate after running the script.
3059
1
  if (this->State->GetRole() != cmState::Role::Project) {
3060
1
    if (cmSystemTools::GetErrorOccurredFlag()) {
3061
1
      return -1;
3062
1
    }
3063
0
    return this->HasScriptModeExitCode() ? this->GetScriptModeExitCode() : 0;
3064
1
  }
3065
3066
0
#ifndef CMAKE_BOOTSTRAP
3067
  // CMake only responds to the SARIF variable in normal mode
3068
0
  this->MarkCliAsUsed(cmSarif::PROJECT_SARIF_FILE_VARIABLE);
3069
0
#endif
3070
3071
  // If MAKEFLAGS are given in the environment, remove the environment
3072
  // variable.  This will prevent try-compile from succeeding when it
3073
  // should fail (if "-i" is an option).  We cannot simply test
3074
  // whether "-i" is given and remove it because some make programs
3075
  // encode the MAKEFLAGS variable in a strange way.
3076
0
  if (cmSystemTools::HasEnv("MAKEFLAGS")) {
3077
0
    cmSystemTools::PutEnv("MAKEFLAGS=");
3078
0
  }
3079
3080
0
  this->PreLoadCMakeFiles();
3081
3082
0
  if (noconfigure) {
3083
0
    return 0;
3084
0
  }
3085
3086
  // now run the global generate
3087
  // Check the state of the build system to see if we need to regenerate.
3088
0
  if (!this->CheckBuildSystem()) {
3089
0
    return 0;
3090
0
  }
3091
  // After generating fbuild.bff, FastBuild sees rebuild-bff as outdated since
3092
  // it hasn’t built the target yet. To make it a no-op for future runs, we
3093
  // trigger a dummy fbuild invocation that creates this marker file and runs
3094
  // CMake, marking rebuild-bff as up-to-date.
3095
0
  std::string const FBuildRestatFile =
3096
0
    cmStrCat(this->GetHomeOutputDirectory(), '/', FASTBUILD_RESTAT_FILE);
3097
0
  if (cmSystemTools::FileExists(FBuildRestatFile)) {
3098
0
    cmsys::ifstream restat(FBuildRestatFile.c_str(),
3099
0
                           std::ios::in | std::ios::binary);
3100
0
    std::string const file((std::istreambuf_iterator<char>(restat)),
3101
0
                           std::istreambuf_iterator<char>());
3102
    // On Windows can not delete file if it's still opened.
3103
0
    restat.close();
3104
0
    cmSystemTools::Touch(file, true);
3105
0
    cmSystemTools::RemoveFile(FBuildRestatFile);
3106
0
    return 0;
3107
0
  }
3108
3109
0
#ifdef CMake_ENABLE_DEBUGGER
3110
0
  if (!this->StartDebuggerIfEnabled()) {
3111
0
    return -1;
3112
0
  }
3113
0
#endif
3114
3115
0
  int ret = this->Configure();
3116
0
  if (ret) {
3117
#if defined(CMAKE_HAVE_VS_GENERATORS)
3118
    if (!this->VSSolutionFile.empty() && this->GlobalGenerator) {
3119
      // CMake is running to regenerate a Visual Studio build tree
3120
      // during a build from the VS IDE.  The build files cannot be
3121
      // regenerated, so we should stop the build.
3122
      cmSystemTools::Message("CMake Configure step failed.  "
3123
                             "Build files cannot be regenerated correctly.  "
3124
                             "Attempting to stop IDE build.");
3125
      cmGlobalVisualStudioGenerator& gg =
3126
        cm::static_reference_cast<cmGlobalVisualStudioGenerator>(
3127
          this->GlobalGenerator);
3128
      gg.CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
3129
                               this->VSSolutionFile);
3130
    }
3131
#endif
3132
0
    return ret;
3133
0
  }
3134
0
  ret = this->Generate();
3135
0
  if (ret) {
3136
0
    cmSystemTools::Message("CMake Generate step failed.  "
3137
0
                           "Build files cannot be regenerated correctly.");
3138
0
    return ret;
3139
0
  }
3140
0
  std::string message = cmStrCat("Build files have been written to: ",
3141
0
                                 this->GetHomeOutputDirectory());
3142
0
  this->UpdateProgress(message, -1);
3143
0
  return ret;
3144
0
}
3145
3146
int cmake::Generate()
3147
0
{
3148
0
  if (!this->GlobalGenerator) {
3149
0
    return -1;
3150
0
  }
3151
3152
0
  auto startTime = std::chrono::steady_clock::now();
3153
0
#if !defined(CMAKE_BOOTSTRAP)
3154
0
  auto profilingRAII = this->CreateProfilingEntry("project", "generate");
3155
0
  auto doGenerate = [this]() -> int {
3156
0
    if (!this->GlobalGenerator->Compute()) {
3157
0
      this->FileAPI->WriteReplies(cmFileAPI::IndexFor::FailedCompute);
3158
0
      return -1;
3159
0
    }
3160
0
    this->GlobalGenerator->Generate();
3161
0
    if (this->Instrumentation->HasQuery()) {
3162
0
      this->Instrumentation->WriteCMakeContent(this->GlobalGenerator);
3163
0
    }
3164
0
    return 0;
3165
0
  };
3166
3167
0
  int ret = this->Instrumentation->InstrumentCommand(
3168
0
    "generate", this->cmdArgs, [doGenerate]() { return doGenerate(); });
3169
0
  if (ret != 0) {
3170
0
    return ret;
3171
0
  }
3172
#else
3173
  if (!this->GlobalGenerator->Compute()) {
3174
    return -1;
3175
  }
3176
  this->GlobalGenerator->Generate();
3177
#endif
3178
0
  auto endTime = std::chrono::steady_clock::now();
3179
0
  {
3180
0
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(endTime -
3181
0
                                                                    startTime);
3182
0
    std::ostringstream msg;
3183
0
    msg << "Generating done (" << std::fixed << std::setprecision(1)
3184
0
        << ms.count() / 1000.0L << "s)";
3185
0
    this->UpdateProgress(msg.str(), -1);
3186
0
  }
3187
0
  if (!this->GraphVizFile.empty()) {
3188
0
    std::cout << "Generate graphviz: " << this->GraphVizFile << '\n';
3189
0
    this->GenerateGraphViz(this->GraphVizFile);
3190
0
  }
3191
0
  if (this->WarnUnusedCli) {
3192
0
    this->RunCheckForUnusedVariables();
3193
0
  }
3194
0
  if (cmSystemTools::GetErrorOccurredFlag()) {
3195
0
#if !defined(CMAKE_BOOTSTRAP)
3196
0
    this->FileAPI->WriteReplies(cmFileAPI::IndexFor::FailedGenerate);
3197
0
#endif
3198
0
    return -1;
3199
0
  }
3200
  // Save the cache again after a successful Generate so that any internal
3201
  // variables created during Generate are saved. (Specifically target GUIDs
3202
  // for the Visual Studio and Xcode generators.)
3203
0
  this->SaveCache(this->GetHomeOutputDirectory());
3204
3205
0
#if !defined(CMAKE_BOOTSTRAP)
3206
0
  this->GlobalGenerator->WriteInstallJson();
3207
0
  this->FileAPI->WriteReplies(cmFileAPI::IndexFor::Success);
3208
0
  this->Instrumentation->CollectTimingData(
3209
0
    cmInstrumentationQuery::Hook::PostGenerate);
3210
0
#endif
3211
3212
0
  return 0;
3213
0
}
3214
3215
void cmake::AddCacheEntry(std::string const& key, cmValue value,
3216
                          cmValue helpString, int type)
3217
3
{
3218
3
  this->State->AddCacheEntry(key, value, helpString,
3219
3
                             static_cast<cmStateEnums::CacheEntryType>(type));
3220
3
  this->UnwatchUnusedCli(key);
3221
3222
3
  if (key == "CMAKE_WARN_DEPRECATED"_s) {
3223
0
    this->Messenger->SetSuppressDeprecatedWarnings(value && value.IsOff());
3224
3
  } else if (key == "CMAKE_ERROR_DEPRECATED"_s) {
3225
0
    this->Messenger->SetDeprecatedWarningsAsErrors(value.IsOn());
3226
3
  } else if (key == "CMAKE_SUPPRESS_DEVELOPER_WARNINGS"_s) {
3227
0
    this->Messenger->SetSuppressDevWarnings(value.IsOn());
3228
3
  } else if (key == "CMAKE_SUPPRESS_DEVELOPER_ERRORS"_s) {
3229
0
    this->Messenger->SetDevWarningsAsErrors(value && value.IsOff());
3230
0
  }
3231
3
}
3232
3233
bool cmake::DoWriteGlobVerifyTarget() const
3234
0
{
3235
0
  return this->State->DoWriteGlobVerifyTarget();
3236
0
}
3237
3238
std::string const& cmake::GetGlobVerifyScript() const
3239
0
{
3240
0
  return this->State->GetGlobVerifyScript();
3241
0
}
3242
3243
std::string const& cmake::GetGlobVerifyStamp() const
3244
0
{
3245
0
  return this->State->GetGlobVerifyStamp();
3246
0
}
3247
3248
void cmake::AddGlobCacheEntry(cmGlobCacheEntry const& entry,
3249
                              std::string const& variable,
3250
                              cmListFileBacktrace const& backtrace)
3251
0
{
3252
0
  this->State->AddGlobCacheEntry(entry, variable, backtrace,
3253
0
                                 this->Messenger.get());
3254
0
}
3255
3256
std::vector<cmGlobCacheEntry> cmake::GetGlobCacheEntries() const
3257
0
{
3258
0
  return this->State->GetGlobCacheEntries();
3259
0
}
3260
3261
std::vector<std::string> cmake::GetAllExtensions() const
3262
0
{
3263
0
  std::vector<std::string> allExt = this->CLikeSourceFileExtensions.ordered;
3264
0
  allExt.insert(allExt.end(), this->HeaderFileExtensions.ordered.begin(),
3265
0
                this->HeaderFileExtensions.ordered.end());
3266
  // cuda extensions are also in SourceFileExtensions so we ignore it here
3267
0
  allExt.insert(allExt.end(), this->FortranFileExtensions.ordered.begin(),
3268
0
                this->FortranFileExtensions.ordered.end());
3269
0
  allExt.insert(allExt.end(), this->HipFileExtensions.ordered.begin(),
3270
0
                this->HipFileExtensions.ordered.end());
3271
0
  allExt.insert(allExt.end(), this->ISPCFileExtensions.ordered.begin(),
3272
0
                this->ISPCFileExtensions.ordered.end());
3273
0
  return allExt;
3274
0
}
3275
3276
std::string cmake::StripExtension(std::string const& file) const
3277
0
{
3278
0
  auto dotpos = file.rfind('.');
3279
0
  if (dotpos != std::string::npos) {
3280
#if defined(_WIN32) || defined(__APPLE__)
3281
    auto ext = cmSystemTools::LowerCase(file.substr(dotpos + 1));
3282
#else
3283
0
    auto ext = cm::string_view(file).substr(dotpos + 1);
3284
0
#endif
3285
0
    if (this->IsAKnownExtension(ext)) {
3286
0
      return file.substr(0, dotpos);
3287
0
    }
3288
0
  }
3289
0
  return file;
3290
0
}
3291
3292
cmValue cmake::GetCacheDefinition(std::string const& name) const
3293
0
{
3294
0
  return this->State->GetInitializedCacheValue(name);
3295
0
}
3296
3297
void cmake::AddScriptingCommands() const
3298
35
{
3299
35
  GetScriptingCommands(this->GetState());
3300
35
}
3301
3302
void cmake::AddProjectCommands() const
3303
0
{
3304
0
  GetProjectCommands(this->GetState());
3305
0
}
3306
3307
void cmake::AddDefaultGenerators()
3308
35
{
3309
#if defined(_WIN32) && !defined(__CYGWIN__)
3310
#  if !defined(CMAKE_BOOT_MINGW)
3311
  this->Generators.push_back(
3312
    cmGlobalVisualStudioVersionedGenerator::NewFactory18());
3313
  this->Generators.push_back(
3314
    cmGlobalVisualStudioVersionedGenerator::NewFactory17());
3315
  this->Generators.push_back(
3316
    cmGlobalVisualStudioVersionedGenerator::NewFactory16());
3317
  this->Generators.push_back(
3318
    cmGlobalVisualStudioVersionedGenerator::NewFactory15());
3319
  this->Generators.push_back(cmGlobalVisualStudio14Generator::NewFactory());
3320
  this->Generators.push_back(cmGlobalBorlandMakefileGenerator::NewFactory());
3321
  this->Generators.push_back(cmGlobalNMakeMakefileGenerator::NewFactory());
3322
  this->Generators.push_back(cmGlobalJOMMakefileGenerator::NewFactory());
3323
#  endif
3324
  this->Generators.push_back(cmGlobalMSYSMakefileGenerator::NewFactory());
3325
  this->Generators.push_back(cmGlobalMinGWMakefileGenerator::NewFactory());
3326
#endif
3327
35
#if !defined(CMAKE_BOOTSTRAP)
3328
35
#  if (defined(__linux__) && !defined(__ANDROID__)) || defined(_WIN32)
3329
35
  this->Generators.push_back(cmGlobalGhsMultiGenerator::NewFactory());
3330
35
#  endif
3331
35
  this->Generators.push_back(cmGlobalUnixMakefileGenerator3::NewFactory());
3332
35
  this->Generators.push_back(cmGlobalNinjaGenerator::NewFactory());
3333
35
  this->Generators.push_back(cmGlobalNinjaMultiGenerator::NewFactory());
3334
35
  this->Generators.push_back(cmGlobalFastbuildGenerator::NewFactory());
3335
#elif defined(CMAKE_BOOTSTRAP_NINJA)
3336
  this->Generators.push_back(cmGlobalNinjaGenerator::NewFactory());
3337
#elif defined(CMAKE_BOOTSTRAP_MAKEFILES)
3338
  this->Generators.push_back(cmGlobalUnixMakefileGenerator3::NewFactory());
3339
#endif
3340
35
#if defined(CMAKE_USE_WMAKE)
3341
35
  this->Generators.push_back(cmGlobalWatcomWMakeGenerator::NewFactory());
3342
35
#endif
3343
#ifdef CMAKE_USE_XCODE
3344
  this->Generators.push_back(cmGlobalXCodeGenerator::NewFactory());
3345
#endif
3346
35
}
3347
3348
bool cmake::ParseCacheEntry(std::string const& entry, std::string& var,
3349
                            std::string& value,
3350
                            cmStateEnums::CacheEntryType& type)
3351
0
{
3352
0
  return cmState::ParseCacheEntry(entry, var, value, type);
3353
0
}
3354
3355
int cmake::LoadCache()
3356
0
{
3357
  // could we not read the cache
3358
0
  if (!this->LoadCache(this->GetHomeOutputDirectory())) {
3359
    // if it does exist, but isn't readable then warn the user
3360
0
    std::string cacheFile =
3361
0
      cmStrCat(this->GetHomeOutputDirectory(), "/CMakeCache.txt");
3362
0
    if (cmSystemTools::FileExists(cacheFile)) {
3363
0
      cmSystemTools::Error(
3364
0
        "There is a CMakeCache.txt file for the current binary tree but "
3365
0
        "cmake does not have permission to read it. Please check the "
3366
0
        "permissions of the directory you are trying to run CMake on.");
3367
0
      return -1;
3368
0
    }
3369
0
  }
3370
3371
  // setup CMAKE_ROOT and CMAKE_COMMAND
3372
0
  if (!this->AddCMakePaths()) {
3373
0
    return -3;
3374
0
  }
3375
0
  return 0;
3376
0
}
3377
3378
bool cmake::LoadCache(std::string const& path)
3379
0
{
3380
0
  std::set<std::string> emptySet;
3381
0
  return this->LoadCache(path, true, emptySet, emptySet);
3382
0
}
3383
3384
bool cmake::LoadCache(std::string const& path, bool internal,
3385
                      std::set<std::string>& excludes,
3386
                      std::set<std::string>& includes)
3387
0
{
3388
0
  bool result = this->State->LoadCache(path, internal, excludes, includes);
3389
0
  static auto const entries = { "CMAKE_CACHE_MAJOR_VERSION",
3390
0
                                "CMAKE_CACHE_MINOR_VERSION" };
3391
0
  for (auto const& entry : entries) {
3392
0
    this->UnwatchUnusedCli(entry);
3393
0
  }
3394
0
  return result;
3395
0
}
3396
3397
bool cmake::SaveCache(std::string const& path)
3398
0
{
3399
0
  bool result = this->State->SaveCache(path, this->GetMessenger());
3400
0
  static auto const entries = { "CMAKE_CACHE_MAJOR_VERSION",
3401
0
                                "CMAKE_CACHE_MINOR_VERSION",
3402
0
                                "CMAKE_CACHE_PATCH_VERSION",
3403
0
                                "CMAKE_CACHEFILE_DIR" };
3404
0
  for (auto const& entry : entries) {
3405
0
    this->UnwatchUnusedCli(entry);
3406
0
  }
3407
0
  return result;
3408
0
}
3409
3410
bool cmake::DeleteCache(std::string const& path)
3411
0
{
3412
0
  return this->State->DeleteCache(path);
3413
0
}
3414
3415
void cmake::SetProgressCallback(ProgressCallbackType f)
3416
0
{
3417
0
  this->ProgressCallback = std::move(f);
3418
0
}
3419
3420
void cmake::UpdateProgress(std::string const& msg, float prog)
3421
0
{
3422
0
  if (this->ProgressCallback && !this->GetIsInTryCompile()) {
3423
0
    this->ProgressCallback(msg, prog);
3424
0
  }
3425
0
}
3426
3427
bool cmake::GetIsInTryCompile() const
3428
0
{
3429
0
  return this->State->GetIsTryCompile() == cmState::TryCompile::Yes;
3430
0
}
3431
3432
void cmake::AppendGlobalGeneratorsDocumentation(
3433
  std::vector<cmDocumentationEntry>& v)
3434
0
{
3435
0
  auto const defaultGenerator = this->EvaluateDefaultGlobalGenerator();
3436
0
  auto const defaultName = defaultGenerator->GetName();
3437
0
  auto foundDefaultOne = false;
3438
3439
0
  for (auto const& g : this->Generators) {
3440
0
    v.emplace_back(g->GetDocumentation());
3441
0
    if (!foundDefaultOne && cmHasPrefix(v.back().Name, defaultName)) {
3442
0
      v.back().CustomNamePrefix = '*';
3443
0
      foundDefaultOne = true;
3444
0
    }
3445
0
  }
3446
0
}
3447
3448
void cmake::AppendExtraGeneratorsDocumentation(
3449
  std::vector<cmDocumentationEntry>& v)
3450
0
{
3451
0
  for (cmExternalMakefileProjectGeneratorFactory* eg : this->ExtraGenerators) {
3452
0
    std::string const doc = eg->GetDocumentation();
3453
0
    std::string const name = eg->GetName();
3454
3455
    // Aliases:
3456
0
    for (std::string const& a : eg->Aliases) {
3457
0
      v.emplace_back(cmDocumentationEntry{ a, doc });
3458
0
    }
3459
3460
    // Full names:
3461
0
    for (std::string const& g : eg->GetSupportedGlobalGenerators()) {
3462
0
      v.emplace_back(cmDocumentationEntry{
3463
0
        cmExternalMakefileProjectGenerator::CreateFullGeneratorName(g, name),
3464
0
        doc });
3465
0
    }
3466
0
  }
3467
0
}
3468
3469
std::vector<cmDocumentationEntry> cmake::GetGeneratorsDocumentation()
3470
0
{
3471
0
  std::vector<cmDocumentationEntry> v;
3472
0
  this->AppendGlobalGeneratorsDocumentation(v);
3473
0
  this->AppendExtraGeneratorsDocumentation(v);
3474
0
  return v;
3475
0
}
3476
3477
void cmake::PrintGeneratorList()
3478
0
{
3479
0
#ifndef CMAKE_BOOTSTRAP
3480
0
  cmDocumentation doc;
3481
0
  auto generators = this->GetGeneratorsDocumentation();
3482
0
  doc.AppendSection("Generators", generators);
3483
0
  std::cerr << '\n';
3484
0
  doc.PrintDocumentation(cmDocumentation::ListGenerators, std::cerr);
3485
0
#endif
3486
0
}
3487
3488
int cmake::CheckBuildSystem()
3489
0
{
3490
  // We do not need to rerun CMake.  Check dependency integrity.
3491
0
  bool const verbose = isCMakeVerbose();
3492
3493
  // This method will check the integrity of the build system if the
3494
  // option was given on the command line.  It reads the given file to
3495
  // determine whether CMake should rerun.
3496
3497
  // If no file is provided for the check, we have to rerun.
3498
0
  if (this->CheckBuildSystemArgument.empty()) {
3499
0
    if (verbose) {
3500
0
      cmSystemTools::Stdout("Re-run cmake no build system arguments\n");
3501
0
    }
3502
0
    return 1;
3503
0
  }
3504
3505
  // If the file provided does not exist, we have to rerun.
3506
0
  if (!cmSystemTools::FileExists(this->CheckBuildSystemArgument)) {
3507
0
    if (verbose) {
3508
0
      std::ostringstream msg;
3509
0
      msg << "Re-run cmake missing file: " << this->CheckBuildSystemArgument
3510
0
          << '\n';
3511
0
      cmSystemTools::Stdout(msg.str());
3512
0
    }
3513
0
    return 1;
3514
0
  }
3515
3516
  // Read the rerun check file and use it to decide whether to do the
3517
  // global generate.
3518
  // Actually, all we need is the `set` command.
3519
0
  cmake cm(cmState::Role::Script);
3520
0
  cm.GetCurrentSnapshot().SetDefaultDefinitions();
3521
0
  cmGlobalGenerator gg(&cm);
3522
0
  cmMakefile mf(&gg, cm.GetCurrentSnapshot());
3523
0
  if (!mf.ReadListFile(this->CheckBuildSystemArgument) ||
3524
0
      cmSystemTools::GetErrorOccurredFlag()) {
3525
0
    if (verbose) {
3526
0
      std::ostringstream msg;
3527
0
      msg << "Re-run cmake error reading : " << this->CheckBuildSystemArgument
3528
0
          << '\n';
3529
0
      cmSystemTools::Stdout(msg.str());
3530
0
    }
3531
    // There was an error reading the file.  Just rerun.
3532
0
    return 1;
3533
0
  }
3534
3535
0
  if (this->ClearBuildSystem) {
3536
    // Get the generator used for this build system.
3537
0
    std::string genName = mf.GetSafeDefinition("CMAKE_DEPENDS_GENERATOR");
3538
0
    if (!cmNonempty(genName)) {
3539
0
      genName = "Unix Makefiles";
3540
0
    }
3541
3542
    // Create the generator and use it to clear the dependencies.
3543
0
    std::unique_ptr<cmGlobalGenerator> ggd =
3544
0
      this->CreateGlobalGenerator(genName);
3545
0
    if (ggd) {
3546
0
      cm.GetCurrentSnapshot().SetDefaultDefinitions();
3547
0
      cmMakefile mfd(ggd.get(), cm.GetCurrentSnapshot());
3548
0
      auto lgd = ggd->CreateLocalGenerator(&mfd);
3549
0
      lgd->ClearDependencies(&mfd, verbose);
3550
0
    }
3551
0
  }
3552
3553
  // If any byproduct of makefile generation is missing we must re-run.
3554
0
  cmList products{ mf.GetDefinition("CMAKE_MAKEFILE_PRODUCTS") };
3555
0
  for (auto const& p : products) {
3556
0
    if (!cmSystemTools::PathExists(p)) {
3557
0
      if (verbose) {
3558
0
        cmSystemTools::Stdout(
3559
0
          cmStrCat("Re-run cmake, missing byproduct: ", p, '\n'));
3560
0
      }
3561
0
      return 1;
3562
0
    }
3563
0
  }
3564
3565
  // Get the set of dependencies and outputs.
3566
0
  cmList depends{ mf.GetDefinition("CMAKE_MAKEFILE_DEPENDS") };
3567
0
  cmList outputs;
3568
0
  if (!depends.empty()) {
3569
0
    outputs.assign(mf.GetDefinition("CMAKE_MAKEFILE_OUTPUTS"));
3570
0
  }
3571
0
  if (depends.empty() || outputs.empty()) {
3572
    // Not enough information was provided to do the test.  Just rerun.
3573
0
    if (verbose) {
3574
0
      cmSystemTools::Stdout("Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
3575
0
                            "or CMAKE_MAKEFILE_OUTPUTS :\n");
3576
0
    }
3577
0
    return 1;
3578
0
  }
3579
3580
  // Find the newest dependency.
3581
0
  auto dep = depends.begin();
3582
0
  std::string dep_newest = *dep++;
3583
0
  for (; dep != depends.end(); ++dep) {
3584
0
    int result = 0;
3585
0
    if (this->FileTimeCache->Compare(dep_newest, *dep, &result)) {
3586
0
      if (result < 0) {
3587
0
        dep_newest = *dep;
3588
0
      }
3589
0
    } else {
3590
0
      if (verbose) {
3591
0
        cmSystemTools::Stdout(
3592
0
          "Re-run cmake: build system dependency is missing\n");
3593
0
      }
3594
0
      return 1;
3595
0
    }
3596
0
  }
3597
3598
  // Find the oldest output.
3599
0
  auto out = outputs.begin();
3600
0
  std::string out_oldest = *out++;
3601
0
  for (; out != outputs.end(); ++out) {
3602
0
    int result = 0;
3603
0
    if (this->FileTimeCache->Compare(out_oldest, *out, &result)) {
3604
0
      if (result > 0) {
3605
0
        out_oldest = *out;
3606
0
      }
3607
0
    } else {
3608
0
      if (verbose) {
3609
0
        cmSystemTools::Stdout(
3610
0
          "Re-run cmake: build system output is missing\n");
3611
0
      }
3612
0
      return 1;
3613
0
    }
3614
0
  }
3615
3616
  // If any output is older than any dependency then rerun.
3617
0
  {
3618
0
    int result = 0;
3619
0
    if (!this->FileTimeCache->Compare(out_oldest, dep_newest, &result) ||
3620
0
        result < 0) {
3621
0
      if (verbose) {
3622
0
        std::ostringstream msg;
3623
0
        msg << "Re-run cmake file: " << out_oldest
3624
0
            << " older than: " << dep_newest << '\n';
3625
0
        cmSystemTools::Stdout(msg.str());
3626
0
      }
3627
0
      return 1;
3628
0
    }
3629
0
  }
3630
3631
  // No need to rerun.
3632
0
  return 0;
3633
0
}
3634
3635
void cmake::TruncateOutputLog(char const* fname)
3636
0
{
3637
0
  std::string fullPath = cmStrCat(this->GetHomeOutputDirectory(), '/', fname);
3638
0
  struct stat st;
3639
0
  if (::stat(fullPath.c_str(), &st)) {
3640
0
    return;
3641
0
  }
3642
0
  if (!this->State->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR")) {
3643
0
    cmSystemTools::RemoveFile(fullPath);
3644
0
    return;
3645
0
  }
3646
0
  off_t fsize = st.st_size;
3647
0
  off_t const maxFileSize = 50 * 1024;
3648
0
  if (fsize < maxFileSize) {
3649
    // TODO: truncate file
3650
0
    return;
3651
0
  }
3652
0
}
3653
3654
void cmake::MarkCliAsUsed(std::string const& variable)
3655
0
{
3656
0
  this->UsedCliVariables[variable] = true;
3657
0
}
3658
3659
void cmake::GenerateGraphViz(std::string const& fileName) const
3660
0
{
3661
0
#ifndef CMAKE_BOOTSTRAP
3662
0
  cmGraphVizWriter gvWriter(fileName, this->GetGlobalGenerator());
3663
3664
0
  std::string settingsFile =
3665
0
    cmStrCat(this->GetHomeOutputDirectory(), "/CMakeGraphVizOptions.cmake");
3666
0
  std::string fallbackSettingsFile =
3667
0
    cmStrCat(this->GetHomeDirectory(), "/CMakeGraphVizOptions.cmake");
3668
3669
0
  gvWriter.ReadSettings(settingsFile, fallbackSettingsFile);
3670
3671
0
  gvWriter.Write();
3672
3673
0
#endif
3674
0
}
3675
3676
void cmake::SetProperty(std::string const& prop, cmValue value)
3677
0
{
3678
0
  this->State->SetGlobalProperty(prop, value);
3679
0
}
3680
3681
void cmake::AppendProperty(std::string const& prop, std::string const& value,
3682
                           bool asString)
3683
0
{
3684
0
  this->State->AppendGlobalProperty(prop, value, asString);
3685
0
}
3686
3687
cmValue cmake::GetProperty(std::string const& prop)
3688
0
{
3689
0
  return this->State->GetGlobalProperty(prop);
3690
0
}
3691
3692
bool cmake::GetPropertyAsBool(std::string const& prop)
3693
0
{
3694
0
  return this->State->GetGlobalPropertyAsBool(prop);
3695
0
}
3696
3697
cmInstalledFile* cmake::GetOrCreateInstalledFile(cmMakefile* mf,
3698
                                                 std::string const& name)
3699
0
{
3700
0
  auto i = this->InstalledFiles.find(name);
3701
3702
0
  if (i != this->InstalledFiles.end()) {
3703
0
    cmInstalledFile& file = i->second;
3704
0
    return &file;
3705
0
  }
3706
0
  cmInstalledFile& file = this->InstalledFiles[name];
3707
0
  file.SetName(mf, name);
3708
0
  return &file;
3709
0
}
3710
3711
cmInstalledFile const* cmake::GetInstalledFile(std::string const& name) const
3712
0
{
3713
0
  auto i = this->InstalledFiles.find(name);
3714
3715
0
  if (i != this->InstalledFiles.end()) {
3716
0
    cmInstalledFile const& file = i->second;
3717
0
    return &file;
3718
0
  }
3719
0
  return nullptr;
3720
0
}
3721
3722
int cmake::GetSystemInformation(std::vector<std::string>& args)
3723
0
{
3724
  // so create the directory
3725
0
  std::string resultFile;
3726
0
  std::string cwd = cmSystemTools::GetLogicalWorkingDirectory();
3727
0
  std::string destPath = cwd + "/__cmake_systeminformation";
3728
0
  cmSystemTools::RemoveADirectory(destPath);
3729
0
  if (!cmSystemTools::MakeDirectory(destPath)) {
3730
0
    std::cerr << "Error: --system-information must be run from a "
3731
0
                 "writable directory!\n";
3732
0
    return 1;
3733
0
  }
3734
3735
  // process the arguments
3736
0
  bool writeToStdout = true;
3737
0
  for (unsigned int i = 1; i < args.size(); ++i) {
3738
0
    std::string const& arg = args[i];
3739
0
    if (cmHasLiteralPrefix(arg, "-G")) {
3740
0
      std::string value = arg.substr(2);
3741
0
      if (value.empty()) {
3742
0
        ++i;
3743
0
        if (i >= args.size()) {
3744
0
          cmSystemTools::Error("No generator specified for -G");
3745
0
          this->PrintGeneratorList();
3746
0
          return -1;
3747
0
        }
3748
0
        value = args[i];
3749
0
      }
3750
0
      auto gen = this->CreateGlobalGenerator(value);
3751
0
      if (!gen) {
3752
0
        cmSystemTools::Error("Could not create named generator " + value);
3753
0
        this->PrintGeneratorList();
3754
0
      } else {
3755
0
        this->SetGlobalGenerator(std::move(gen));
3756
0
      }
3757
0
    }
3758
    // no option assume it is the output file
3759
0
    else {
3760
0
      if (!cmSystemTools::FileIsFullPath(arg)) {
3761
0
        resultFile = cmStrCat(cwd, '/');
3762
0
      }
3763
0
      resultFile += arg;
3764
0
      writeToStdout = false;
3765
0
    }
3766
0
  }
3767
3768
  // we have to find the module directory, so we can copy the files
3769
0
  this->AddCMakePaths();
3770
0
  std::string modulesPath =
3771
0
    cmStrCat(cmSystemTools::GetCMakeRoot(), "/Modules");
3772
0
  std::string inFile = cmStrCat(modulesPath, "/SystemInformation.cmake");
3773
0
  std::string outFile = cmStrCat(destPath, "/CMakeLists.txt");
3774
3775
  // Copy file
3776
0
  if (!cmsys::SystemTools::CopyFileAlways(inFile, outFile)) {
3777
0
    std::cerr << "Error copying file \"" << inFile << "\" to \"" << outFile
3778
0
              << "\".\n";
3779
0
    return 1;
3780
0
  }
3781
3782
  // do we write to a file or to stdout?
3783
0
  if (resultFile.empty()) {
3784
0
    resultFile = cmStrCat(cwd, "/__cmake_systeminformation/results.txt");
3785
0
  }
3786
3787
0
  {
3788
    // now run cmake on the CMakeLists file
3789
0
    cmWorkingDirectory workdir(destPath);
3790
0
    if (workdir.Failed()) {
3791
      // We created the directory and we were able to copy the CMakeLists.txt
3792
      // file to it, so we wouldn't expect to get here unless the default
3793
      // permissions are questionable or some other process has deleted the
3794
      // directory
3795
0
      std::cerr << workdir.GetError() << '\n';
3796
0
      return 1;
3797
0
    }
3798
0
    std::vector<std::string> args2;
3799
0
    args2.push_back(args[0]);
3800
0
    args2.push_back(destPath);
3801
0
    args2.push_back("-DRESULT_FILE=" + resultFile);
3802
0
    int res = this->Run(args2, false);
3803
3804
0
    if (res != 0) {
3805
0
      std::cerr << "Error: --system-information failed on internal CMake!\n";
3806
0
      return res;
3807
0
    }
3808
0
  }
3809
3810
  // echo results to stdout if needed
3811
0
  if (writeToStdout) {
3812
0
    FILE* fin = cmsys::SystemTools::Fopen(resultFile, "r");
3813
0
    if (fin) {
3814
0
      int const bufferSize = 4096;
3815
0
      char buffer[bufferSize];
3816
0
      size_t n;
3817
0
      while ((n = fread(buffer, 1, bufferSize, fin)) > 0) {
3818
0
        for (char* c = buffer; c < buffer + n; ++c) {
3819
0
          putc(*c, stdout);
3820
0
        }
3821
0
        fflush(stdout);
3822
0
      }
3823
0
      fclose(fin);
3824
0
    }
3825
0
  }
3826
3827
  // clean up the directory
3828
0
  cmSystemTools::RemoveADirectory(destPath);
3829
0
  return 0;
3830
0
}
3831
3832
void cmake::IssueMessage(MessageType t, std::string const& text,
3833
                         cmListFileBacktrace const& backtrace) const
3834
0
{
3835
0
  this->Messenger->IssueMessage(t, text, backtrace);
3836
0
}
3837
3838
std::vector<std::string> cmake::GetDebugConfigs()
3839
0
{
3840
0
  cmList configs;
3841
0
  if (cmValue config_list =
3842
0
        this->State->GetGlobalProperty("DEBUG_CONFIGURATIONS")) {
3843
    // Expand the specified list and convert to upper-case.
3844
0
    configs.assign(*config_list);
3845
0
    configs.transform(cmList::TransformAction::TOUPPER);
3846
0
  }
3847
  // If no configurations were specified, use a default list.
3848
0
  if (configs.empty()) {
3849
0
    configs.emplace_back("DEBUG");
3850
0
  }
3851
0
  return std::move(configs.data());
3852
0
}
3853
3854
int cmake::Build(cmBuildArgs buildArgs, std::vector<std::string> targets,
3855
                 std::vector<std::string> nativeOptions,
3856
                 cmBuildOptions& buildOptions, std::string const& presetName,
3857
                 bool listPresets, std::vector<std::string> const& args)
3858
0
{
3859
0
  buildArgs.timeout = cmDuration::zero();
3860
3861
0
#if !defined(CMAKE_BOOTSTRAP)
3862
0
  if (!presetName.empty() || listPresets) {
3863
0
    this->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
3864
0
    this->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
3865
3866
0
    cmCMakePresetsGraph settingsFile;
3867
0
    auto result = settingsFile.ReadProjectPresets(this->GetHomeDirectory());
3868
0
    if (result != true) {
3869
0
      cmSystemTools::Error(
3870
0
        cmStrCat("Could not read presets from ", this->GetHomeDirectory(),
3871
0
                 ":\n", settingsFile.parseState.GetErrorMessage()));
3872
0
      return 1;
3873
0
    }
3874
3875
0
    if (listPresets) {
3876
0
      settingsFile.PrintBuildPresetList();
3877
0
      return 0;
3878
0
    }
3879
3880
0
    auto presetPair = settingsFile.BuildPresets.find(presetName);
3881
0
    if (presetPair == settingsFile.BuildPresets.end()) {
3882
0
      cmSystemTools::Error(cmStrCat("No such build preset in ",
3883
0
                                    this->GetHomeDirectory(), ": \"",
3884
0
                                    presetName, '"'));
3885
0
      settingsFile.PrintBuildPresetList();
3886
0
      return 1;
3887
0
    }
3888
3889
0
    if (presetPair->second.Unexpanded.Hidden) {
3890
0
      cmSystemTools::Error(cmStrCat("Cannot use hidden build preset in ",
3891
0
                                    this->GetHomeDirectory(), ": \"",
3892
0
                                    presetName, '"'));
3893
0
      settingsFile.PrintBuildPresetList();
3894
0
      return 1;
3895
0
    }
3896
3897
0
    auto const& expandedPreset = presetPair->second.Expanded;
3898
0
    if (!expandedPreset) {
3899
0
      cmSystemTools::Error(cmStrCat("Could not evaluate build preset \"",
3900
0
                                    presetName,
3901
0
                                    "\": Invalid macro expansion"));
3902
0
      settingsFile.PrintBuildPresetList();
3903
0
      return 1;
3904
0
    }
3905
3906
0
    if (!expandedPreset->ConditionResult) {
3907
0
      cmSystemTools::Error(cmStrCat("Cannot use disabled build preset in ",
3908
0
                                    this->GetHomeDirectory(), ": \"",
3909
0
                                    presetName, '"'));
3910
0
      settingsFile.PrintBuildPresetList();
3911
0
      return 1;
3912
0
    }
3913
3914
0
    auto configurePresetPair =
3915
0
      settingsFile.ConfigurePresets.find(expandedPreset->ConfigurePreset);
3916
0
    if (configurePresetPair == settingsFile.ConfigurePresets.end()) {
3917
0
      cmSystemTools::Error(cmStrCat("No such configure preset in ",
3918
0
                                    this->GetHomeDirectory(), ": \"",
3919
0
                                    expandedPreset->ConfigurePreset, '"'));
3920
0
      this->PrintPresetList(settingsFile);
3921
0
      return 1;
3922
0
    }
3923
3924
0
    if (configurePresetPair->second.Unexpanded.Hidden) {
3925
0
      cmSystemTools::Error(cmStrCat("Cannot use hidden configure preset in ",
3926
0
                                    this->GetHomeDirectory(), ": \"",
3927
0
                                    expandedPreset->ConfigurePreset, '"'));
3928
0
      this->PrintPresetList(settingsFile);
3929
0
      return 1;
3930
0
    }
3931
3932
0
    auto const& expandedConfigurePreset = configurePresetPair->second.Expanded;
3933
0
    if (!expandedConfigurePreset) {
3934
0
      cmSystemTools::Error(cmStrCat("Could not evaluate configure preset \"",
3935
0
                                    expandedPreset->ConfigurePreset,
3936
0
                                    "\": Invalid macro expansion"));
3937
0
      return 1;
3938
0
    }
3939
3940
0
    if (buildArgs.binaryDir.empty() &&
3941
0
        !expandedConfigurePreset->BinaryDir.empty()) {
3942
0
      buildArgs.binaryDir = expandedConfigurePreset->BinaryDir;
3943
0
    }
3944
3945
0
    this->UnprocessedPresetEnvironment = expandedPreset->Environment;
3946
0
    this->ProcessPresetEnvironment();
3947
3948
0
    if ((buildArgs.jobs == cmake::DEFAULT_BUILD_PARALLEL_LEVEL ||
3949
0
         buildArgs.jobs == cmake::NO_BUILD_PARALLEL_LEVEL) &&
3950
0
        expandedPreset->Jobs) {
3951
0
      if (*expandedPreset->Jobs > static_cast<unsigned int>(INT_MAX)) {
3952
0
        cmSystemTools::Error(
3953
0
          "The build preset \"jobs\" value is too large.\n");
3954
0
        return 1;
3955
0
      }
3956
0
      buildArgs.jobs = *expandedPreset->Jobs;
3957
0
    }
3958
3959
0
    if (targets.empty()) {
3960
0
      targets.insert(targets.begin(), expandedPreset->Targets.begin(),
3961
0
                     expandedPreset->Targets.end());
3962
0
    }
3963
3964
0
    if (buildArgs.config.empty()) {
3965
0
      buildArgs.config = expandedPreset->Configuration;
3966
0
    }
3967
3968
0
    if (!buildOptions.Clean && expandedPreset->CleanFirst) {
3969
0
      buildOptions.Clean = *expandedPreset->CleanFirst;
3970
0
    }
3971
3972
0
    if (buildOptions.ResolveMode == PackageResolveMode::Default &&
3973
0
        expandedPreset->ResolvePackageReferences) {
3974
0
      buildOptions.ResolveMode = *expandedPreset->ResolvePackageReferences;
3975
0
    }
3976
3977
0
    if (!buildArgs.verbose && expandedPreset->Verbose) {
3978
0
      buildArgs.verbose = *expandedPreset->Verbose;
3979
0
    }
3980
3981
0
    if (nativeOptions.empty()) {
3982
0
      nativeOptions.insert(nativeOptions.begin(),
3983
0
                           expandedPreset->NativeToolOptions.begin(),
3984
0
                           expandedPreset->NativeToolOptions.end());
3985
0
    }
3986
0
  }
3987
0
#endif
3988
3989
0
  if (!cmSystemTools::FileIsDirectory(buildArgs.binaryDir)) {
3990
0
    std::cerr << "Error: " << buildArgs.binaryDir << " is not a directory\n";
3991
0
    return 1;
3992
0
  }
3993
3994
0
  std::string cachePath = FindCacheFile(buildArgs.binaryDir);
3995
0
  if (!this->LoadCache(cachePath)) {
3996
0
    std::cerr
3997
0
      << "Error: not a CMake build directory (missing CMakeCache.txt)\n";
3998
0
    return 1;
3999
0
  }
4000
0
  cmValue cachedGenerator = this->State->GetCacheEntryValue("CMAKE_GENERATOR");
4001
0
  if (!cachedGenerator) {
4002
0
    std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
4003
0
    return 1;
4004
0
  }
4005
0
  auto gen = this->CreateGlobalGenerator(*cachedGenerator);
4006
0
  if (!gen) {
4007
0
    std::cerr << "Error: could not create CMAKE_GENERATOR \""
4008
0
              << *cachedGenerator << "\"\n";
4009
0
    return 1;
4010
0
  }
4011
0
  this->SetGlobalGenerator(std::move(gen));
4012
0
  cmValue cachedGeneratorInstance =
4013
0
    this->State->GetCacheEntryValue("CMAKE_GENERATOR_INSTANCE");
4014
0
  if (cachedGeneratorInstance) {
4015
0
    cmMakefile mf(this->GetGlobalGenerator(), this->GetCurrentSnapshot());
4016
0
    if (!this->GlobalGenerator->SetGeneratorInstance(*cachedGeneratorInstance,
4017
0
                                                     &mf)) {
4018
0
      return 1;
4019
0
    }
4020
0
  }
4021
0
  cmValue cachedGeneratorPlatform =
4022
0
    this->State->GetCacheEntryValue("CMAKE_GENERATOR_PLATFORM");
4023
0
  if (cachedGeneratorPlatform) {
4024
0
    cmMakefile mf(this->GetGlobalGenerator(), this->GetCurrentSnapshot());
4025
0
    if (!this->GlobalGenerator->SetGeneratorPlatform(*cachedGeneratorPlatform,
4026
0
                                                     &mf)) {
4027
0
      return 1;
4028
0
    }
4029
0
  }
4030
0
  cmValue cachedGeneratorToolset =
4031
0
    this->State->GetCacheEntryValue("CMAKE_GENERATOR_TOOLSET");
4032
0
  if (cachedGeneratorToolset) {
4033
0
    cmMakefile mf(this->GetGlobalGenerator(), this->GetCurrentSnapshot());
4034
0
    if (!this->GlobalGenerator->SetGeneratorToolset(*cachedGeneratorToolset,
4035
0
                                                    true, &mf)) {
4036
0
      return 1;
4037
0
    }
4038
0
  }
4039
0
  cmValue cachedProjectName =
4040
0
    this->State->GetCacheEntryValue("CMAKE_PROJECT_NAME");
4041
0
  if (!cachedProjectName) {
4042
0
    std::cerr << "Error: could not find CMAKE_PROJECT_NAME in Cache\n";
4043
0
    return 1;
4044
0
  }
4045
0
  buildArgs.projectName = *cachedProjectName;
4046
4047
0
  if (this->State->GetCacheEntryValue("CMAKE_VERBOSE_MAKEFILE").IsOn()) {
4048
0
    buildArgs.verbose = true;
4049
0
  }
4050
4051
#ifdef CMAKE_HAVE_VS_GENERATORS
4052
  // For VS generators, explicitly check if regeneration is necessary before
4053
  // actually starting the build. If not done separately from the build
4054
  // itself, there is the risk of building an out-of-date solution file due
4055
  // to limitations of the underlying build system.
4056
  std::string const stampList =
4057
    cmStrCat(cachePath, "/CMakeFiles/",
4058
             cmGlobalVisualStudio14Generator::GetGenerateStampList());
4059
4060
  // Note that the stampList file only exists for VS generators.
4061
  if (cmSystemTools::FileExists(stampList) &&
4062
      !cmakeCheckStampList(stampList)) {
4063
    // Upgrade cmake role from --build to reconfigure the project.
4064
    this->State->SetRoleToProjectForCMakeBuildVsReconfigure();
4065
    this->AddScriptingCommands();
4066
    this->AddProjectCommands();
4067
4068
    // Correctly initialize the home (=source) and home output (=binary)
4069
    // directories, which is required for running the generation step.
4070
    this->SetDirectoriesFromFile(cachePath);
4071
4072
    int ret = this->Configure();
4073
    if (ret) {
4074
      cmSystemTools::Message("CMake Configure step failed.  "
4075
                             "Build files cannot be regenerated correctly.");
4076
      return ret;
4077
    }
4078
    ret = this->Generate();
4079
    if (ret) {
4080
      cmSystemTools::Message("CMake Generate step failed.  "
4081
                             "Build files cannot be regenerated correctly.");
4082
      return ret;
4083
    }
4084
    std::string message = cmStrCat("Build files have been written to: ",
4085
                                   this->GetHomeOutputDirectory());
4086
    this->UpdateProgress(message, -1);
4087
  }
4088
#endif
4089
4090
0
  if (!this->GlobalGenerator->ReadCacheEntriesForBuild(*this->State)) {
4091
0
    return 1;
4092
0
  }
4093
4094
0
#if !defined(CMAKE_BOOTSTRAP)
4095
0
  cmInstrumentation instrumentation(buildArgs.binaryDir);
4096
0
  if (instrumentation.HasErrors()) {
4097
0
    return 1;
4098
0
  }
4099
0
  instrumentation.CollectTimingData(
4100
0
    cmInstrumentationQuery::Hook::PreCMakeBuild);
4101
0
#endif
4102
4103
0
  this->GlobalGenerator->PrintBuildCommandAdvice(std::cerr, buildArgs.jobs);
4104
0
  std::stringstream ostr;
4105
  // `cmGlobalGenerator::Build` logs metadata about what directory and commands
4106
  // are being executed to the `output` parameter. If CMake is verbose, print
4107
  // this out.
4108
0
  std::ostream& verbose_ostr = buildArgs.verbose ? std::cout : ostr;
4109
0
  auto doBuild = [this, targets, &verbose_ostr, buildOptions, buildArgs,
4110
0
                  nativeOptions]() -> int {
4111
0
    return this->GlobalGenerator->Build(
4112
0
      buildArgs, targets, verbose_ostr, "", buildArgs.config, buildOptions,
4113
0
      buildArgs.timeout, cmSystemTools::OUTPUT_PASSTHROUGH, nativeOptions);
4114
0
  };
4115
4116
0
#if !defined(CMAKE_BOOTSTRAP)
4117
  // Block the instrumentation build daemon from spawning during this build.
4118
  // This lock will be released when the process exits at the end of the build.
4119
0
  instrumentation.LockBuildDaemon();
4120
0
  int buildresult =
4121
0
    instrumentation.InstrumentCommand("cmakeBuild", args, doBuild);
4122
0
  instrumentation.CollectTimingData(
4123
0
    cmInstrumentationQuery::Hook::PostCMakeBuild);
4124
#else
4125
  int buildresult = doBuild();
4126
#endif
4127
4128
0
  return buildresult;
4129
0
}
4130
4131
bool cmake::Open(std::string const& dir, DryRun dryRun)
4132
0
{
4133
0
  if (!cmSystemTools::FileIsDirectory(dir)) {
4134
0
    if (dryRun == DryRun::No) {
4135
0
      std::cerr << "Error: " << dir << " is not a directory\n";
4136
0
    }
4137
0
    return false;
4138
0
  }
4139
4140
0
  std::string cachePath = FindCacheFile(dir);
4141
0
  if (!this->LoadCache(cachePath)) {
4142
0
    std::cerr
4143
0
      << "Error: not a CMake build directory (missing CMakeCache.txt)\n";
4144
0
    return false;
4145
0
  }
4146
0
  cmValue genName = this->State->GetCacheEntryValue("CMAKE_GENERATOR");
4147
0
  if (!genName) {
4148
0
    std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
4149
0
    return false;
4150
0
  }
4151
0
  cmValue extraGenName =
4152
0
    this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
4153
0
  std::string fullName =
4154
0
    cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
4155
0
      *genName, extraGenName ? *extraGenName : "");
4156
4157
0
  std::unique_ptr<cmGlobalGenerator> gen =
4158
0
    this->CreateGlobalGenerator(fullName);
4159
0
  if (!gen) {
4160
0
    std::cerr << "Error: could not create CMAKE_GENERATOR \"" << fullName
4161
0
              << "\"\n";
4162
0
    return false;
4163
0
  }
4164
4165
0
  cmValue cachedProjectName =
4166
0
    this->State->GetCacheEntryValue("CMAKE_PROJECT_NAME");
4167
0
  if (!cachedProjectName) {
4168
0
    std::cerr << "Error: could not find CMAKE_PROJECT_NAME in Cache\n";
4169
0
    return false;
4170
0
  }
4171
4172
0
  return gen->Open(dir, *cachedProjectName, dryRun == DryRun::Yes);
4173
0
}
4174
4175
#if !defined(CMAKE_BOOTSTRAP)
4176
template <typename T>
4177
T const* cmake::FindPresetForWorkflow(
4178
  cm::static_string_view type,
4179
  std::map<std::string, cmCMakePresetsGraph::PresetPair<T>> const& presets,
4180
  cmCMakePresetsGraph::WorkflowPreset::WorkflowStep const& step)
4181
0
{
4182
0
  auto it = presets.find(step.PresetName);
4183
0
  if (it == presets.end()) {
4184
0
    cmSystemTools::Error(cmStrCat("No such ", type, " preset in ",
4185
0
                                  this->GetHomeDirectory(), ": \"",
4186
0
                                  step.PresetName, '"'));
4187
0
    return nullptr;
4188
0
  }
4189
4190
0
  if (it->second.Unexpanded.Hidden) {
4191
0
    cmSystemTools::Error(cmStrCat("Cannot use hidden ", type, " preset in ",
4192
0
                                  this->GetHomeDirectory(), ": \"",
4193
0
                                  step.PresetName, '"'));
4194
0
    return nullptr;
4195
0
  }
4196
4197
0
  if (!it->second.Expanded) {
4198
0
    cmSystemTools::Error(cmStrCat("Could not evaluate ", type, " preset \"",
4199
0
                                  step.PresetName,
4200
0
                                  "\": Invalid macro expansion"));
4201
0
    return nullptr;
4202
0
  }
4203
4204
0
  if (!it->second.Expanded->ConditionResult) {
4205
0
    cmSystemTools::Error(cmStrCat("Cannot use disabled ", type, " preset in ",
4206
0
                                  this->GetHomeDirectory(), ": \"",
4207
0
                                  step.PresetName, '"'));
4208
0
    return nullptr;
4209
0
  }
4210
4211
0
  return &*it->second.Expanded;
4212
0
}
Unexecuted instantiation: cmCMakePresetsGraph::ConfigurePreset const* cmake::FindPresetForWorkflow<cmCMakePresetsGraph::ConfigurePreset>(cm::static_string_view, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, cmCMakePresetsGraph::PresetPair<cmCMakePresetsGraph::ConfigurePreset>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, cmCMakePresetsGraph::PresetPair<cmCMakePresetsGraph::ConfigurePreset> > > > const&, cmCMakePresetsGraph::WorkflowPreset::WorkflowStep const&)
Unexecuted instantiation: cmCMakePresetsGraph::BuildPreset const* cmake::FindPresetForWorkflow<cmCMakePresetsGraph::BuildPreset>(cm::static_string_view, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, cmCMakePresetsGraph::PresetPair<cmCMakePresetsGraph::BuildPreset>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, cmCMakePresetsGraph::PresetPair<cmCMakePresetsGraph::BuildPreset> > > > const&, cmCMakePresetsGraph::WorkflowPreset::WorkflowStep const&)
Unexecuted instantiation: cmCMakePresetsGraph::TestPreset const* cmake::FindPresetForWorkflow<cmCMakePresetsGraph::TestPreset>(cm::static_string_view, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, cmCMakePresetsGraph::PresetPair<cmCMakePresetsGraph::TestPreset>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, cmCMakePresetsGraph::PresetPair<cmCMakePresetsGraph::TestPreset> > > > const&, cmCMakePresetsGraph::WorkflowPreset::WorkflowStep const&)
Unexecuted instantiation: cmCMakePresetsGraph::PackagePreset const* cmake::FindPresetForWorkflow<cmCMakePresetsGraph::PackagePreset>(cm::static_string_view, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, cmCMakePresetsGraph::PresetPair<cmCMakePresetsGraph::PackagePreset>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, cmCMakePresetsGraph::PresetPair<cmCMakePresetsGraph::PackagePreset> > > > const&, cmCMakePresetsGraph::WorkflowPreset::WorkflowStep const&)
4213
4214
namespace {
4215
4216
std::function<cmUVProcessChain::Status()> buildWorkflowStep(
4217
  std::vector<std::string> const& args)
4218
0
{
4219
0
  cmUVProcessChainBuilder builder;
4220
0
  builder.AddCommand(args)
4221
0
    .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, stdout)
4222
0
    .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, stderr);
4223
0
  return [builder]() -> cmUVProcessChain::Status {
4224
0
    auto chain = builder.Start();
4225
0
    chain.Wait();
4226
0
    return chain.GetStatus(0);
4227
0
  };
4228
0
}
4229
4230
}
4231
#endif
4232
4233
int cmake::Workflow(std::string const& presetName,
4234
                    WorkflowListPresets listPresets, WorkflowFresh fresh)
4235
0
{
4236
0
  int exitStatus = 0;
4237
0
#ifndef CMAKE_BOOTSTRAP
4238
0
  this->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
4239
0
  this->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
4240
4241
0
  cmCMakePresetsGraph settingsFile;
4242
0
  auto result = settingsFile.ReadProjectPresets(this->GetHomeDirectory());
4243
0
  if (result != true) {
4244
0
    cmSystemTools::Error(cmStrCat("Could not read presets from ",
4245
0
                                  this->GetHomeDirectory(), ":\n",
4246
0
                                  settingsFile.parseState.GetErrorMessage()));
4247
0
    return 1;
4248
0
  }
4249
4250
0
  if (listPresets == WorkflowListPresets::Yes) {
4251
0
    settingsFile.PrintWorkflowPresetList();
4252
0
    return 0;
4253
0
  }
4254
4255
0
  auto presetPair = settingsFile.WorkflowPresets.find(presetName);
4256
0
  if (presetPair == settingsFile.WorkflowPresets.end()) {
4257
0
    cmSystemTools::Error(cmStrCat("No such workflow preset in ",
4258
0
                                  this->GetHomeDirectory(), ": \"", presetName,
4259
0
                                  '"'));
4260
0
    settingsFile.PrintWorkflowPresetList();
4261
0
    return 1;
4262
0
  }
4263
4264
0
  if (presetPair->second.Unexpanded.Hidden) {
4265
0
    cmSystemTools::Error(cmStrCat("Cannot use hidden workflow preset in ",
4266
0
                                  this->GetHomeDirectory(), ": \"", presetName,
4267
0
                                  '"'));
4268
0
    settingsFile.PrintWorkflowPresetList();
4269
0
    return 1;
4270
0
  }
4271
4272
0
  auto const& expandedPreset = presetPair->second.Expanded;
4273
0
  if (!expandedPreset) {
4274
0
    cmSystemTools::Error(cmStrCat("Could not evaluate workflow preset \"",
4275
0
                                  presetName, "\": Invalid macro expansion"));
4276
0
    settingsFile.PrintWorkflowPresetList();
4277
0
    return 1;
4278
0
  }
4279
4280
0
  if (!expandedPreset->ConditionResult) {
4281
0
    cmSystemTools::Error(cmStrCat("Cannot use disabled workflow preset in ",
4282
0
                                  this->GetHomeDirectory(), ": \"", presetName,
4283
0
                                  '"'));
4284
0
    settingsFile.PrintWorkflowPresetList();
4285
0
    return 1;
4286
0
  }
4287
4288
0
  struct CalculatedStep
4289
0
  {
4290
0
    int StepNumber;
4291
0
    cm::static_string_view Type;
4292
0
    std::string Name;
4293
0
    std::function<cmUVProcessChain::Status()> Action;
4294
4295
0
    CalculatedStep(int stepNumber, cm::static_string_view type,
4296
0
                   std::string name,
4297
0
                   std::function<cmUVProcessChain::Status()> action)
4298
0
      : StepNumber(stepNumber)
4299
0
      , Type(type)
4300
0
      , Name(std::move(name))
4301
0
      , Action(std::move(action))
4302
0
    {
4303
0
    }
4304
0
  };
4305
4306
0
  std::vector<CalculatedStep> steps;
4307
0
  steps.reserve(expandedPreset->Steps.size());
4308
0
  int stepNumber = 1;
4309
0
  cmCMakePresetsGraph::ConfigurePreset const* configurePreset = {};
4310
0
  for (auto const& step : expandedPreset->Steps) {
4311
0
    switch (step.PresetType) {
4312
0
      case cmCMakePresetsGraph::WorkflowPreset::WorkflowStep::Type::
4313
0
        Configure: {
4314
0
        configurePreset = this->FindPresetForWorkflow(
4315
0
          "configure"_s, settingsFile.ConfigurePresets, step);
4316
0
        if (!configurePreset) {
4317
0
          return 1;
4318
0
        }
4319
0
        std::vector<std::string> args{ cmSystemTools::GetCMakeCommand(),
4320
0
                                       "--preset", step.PresetName };
4321
0
        if (fresh == WorkflowFresh::Yes) {
4322
0
          args.emplace_back("--fresh");
4323
0
        }
4324
0
        steps.emplace_back(stepNumber, "configure"_s, step.PresetName,
4325
0
                           buildWorkflowStep(args));
4326
0
      } break;
4327
0
      case cmCMakePresetsGraph::WorkflowPreset::WorkflowStep::Type::Build: {
4328
0
        auto const* buildPreset = this->FindPresetForWorkflow(
4329
0
          "build"_s, settingsFile.BuildPresets, step);
4330
0
        if (!buildPreset) {
4331
0
          return 1;
4332
0
        }
4333
0
        steps.emplace_back(
4334
0
          stepNumber, "build"_s, step.PresetName,
4335
0
          buildWorkflowStep({ cmSystemTools::GetCMakeCommand(), "--build",
4336
0
                              "--preset", step.PresetName }));
4337
0
      } break;
4338
0
      case cmCMakePresetsGraph::WorkflowPreset::WorkflowStep::Type::Test: {
4339
0
        auto const* testPreset = this->FindPresetForWorkflow(
4340
0
          "test"_s, settingsFile.TestPresets, step);
4341
0
        if (!testPreset) {
4342
0
          return 1;
4343
0
        }
4344
0
        steps.emplace_back(
4345
0
          stepNumber, "test"_s, step.PresetName,
4346
0
          buildWorkflowStep({ cmSystemTools::GetCTestCommand(), "--preset",
4347
0
                              step.PresetName }));
4348
0
      } break;
4349
0
      case cmCMakePresetsGraph::WorkflowPreset::WorkflowStep::Type::Package: {
4350
0
        auto const* packagePreset = this->FindPresetForWorkflow(
4351
0
          "package"_s, settingsFile.PackagePresets, step);
4352
0
        if (!packagePreset) {
4353
0
          return 1;
4354
0
        }
4355
0
        steps.emplace_back(
4356
0
          stepNumber, "package"_s, step.PresetName,
4357
0
          buildWorkflowStep({ cmSystemTools::GetCPackCommand(), "--preset",
4358
0
                              step.PresetName }));
4359
0
      } break;
4360
0
    }
4361
0
    stepNumber++;
4362
0
  }
4363
4364
0
  bool first = true;
4365
0
  for (auto const& step : steps) {
4366
0
    if (!first) {
4367
0
      std::cout << "\n";
4368
0
    }
4369
0
    std::cout << "Executing workflow step " << step.StepNumber << " of "
4370
0
              << steps.size() << ": " << step.Type << " preset \"" << step.Name
4371
0
              << "\"\n\n"
4372
0
              << std::flush;
4373
0
    cmUVProcessChain::Status const status = step.Action();
4374
0
    if (status.ExitStatus != 0) {
4375
0
      exitStatus = static_cast<int>(status.ExitStatus);
4376
0
      break;
4377
0
    }
4378
0
    auto const codeReasonPair = status.GetException();
4379
0
    if (codeReasonPair.first != cmUVProcessChain::ExceptionCode::None) {
4380
0
      std::cout << "Step command ended abnormally: " << codeReasonPair.second
4381
0
                << std::endl;
4382
0
      exitStatus =
4383
0
        status.SpawnResult != 0 ? status.SpawnResult : status.TermSignal;
4384
0
      break;
4385
0
    }
4386
0
    first = false;
4387
0
  }
4388
0
  if (configurePreset) {
4389
0
    cmInstrumentation instrumentation(configurePreset->BinaryDir);
4390
0
    instrumentation.CollectTimingData(
4391
0
      cmInstrumentationQuery::Hook::PostCMakeWorkflow);
4392
0
  }
4393
0
#endif
4394
4395
0
  return exitStatus;
4396
0
}
4397
4398
void cmake::WatchUnusedCli(std::string const& var)
4399
0
{
4400
0
#ifndef CMAKE_BOOTSTRAP
4401
0
  this->VariableWatch->AddWatch(var, cmWarnUnusedCliWarning, this);
4402
0
  if (!cm::contains(this->UsedCliVariables, var)) {
4403
0
    this->UsedCliVariables[var] = false;
4404
0
  }
4405
0
#endif
4406
0
}
4407
4408
void cmake::UnwatchUnusedCli(std::string const& var)
4409
3
{
4410
3
#ifndef CMAKE_BOOTSTRAP
4411
3
  this->VariableWatch->RemoveWatch(var, cmWarnUnusedCliWarning);
4412
3
  this->UsedCliVariables.erase(var);
4413
3
#endif
4414
3
}
4415
4416
void cmake::RunCheckForUnusedVariables()
4417
0
{
4418
0
#ifndef CMAKE_BOOTSTRAP
4419
0
  bool haveUnused = false;
4420
0
  std::ostringstream msg;
4421
0
  msg << "Manually-specified variables were not used by the project:";
4422
0
  for (auto const& it : this->UsedCliVariables) {
4423
0
    if (!it.second) {
4424
0
      haveUnused = true;
4425
0
      msg << "\n  " << it.first;
4426
0
    }
4427
0
  }
4428
0
  if (haveUnused) {
4429
0
    this->IssueMessage(MessageType::WARNING, msg.str());
4430
0
  }
4431
0
#endif
4432
0
}
4433
4434
bool cmake::GetSuppressDevWarnings() const
4435
0
{
4436
0
  return this->Messenger->GetSuppressDevWarnings();
4437
0
}
4438
4439
void cmake::SetSuppressDevWarnings(bool b)
4440
0
{
4441
0
  std::string value;
4442
4443
  // equivalent to -Wno-dev
4444
0
  if (b) {
4445
0
    value = "TRUE";
4446
0
  }
4447
  // equivalent to -Wdev
4448
0
  else {
4449
0
    value = "FALSE";
4450
0
  }
4451
4452
0
  this->AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", value,
4453
0
                      "Suppress Warnings that are meant for"
4454
0
                      " the author of the CMakeLists.txt files.",
4455
0
                      cmStateEnums::INTERNAL);
4456
0
}
4457
4458
bool cmake::GetSuppressDeprecatedWarnings() const
4459
0
{
4460
0
  return this->Messenger->GetSuppressDeprecatedWarnings();
4461
0
}
4462
4463
void cmake::SetSuppressDeprecatedWarnings(bool b)
4464
0
{
4465
0
  std::string value;
4466
4467
  // equivalent to -Wno-deprecated
4468
0
  if (b) {
4469
0
    value = "FALSE";
4470
0
  }
4471
  // equivalent to -Wdeprecated
4472
0
  else {
4473
0
    value = "TRUE";
4474
0
  }
4475
4476
0
  this->AddCacheEntry("CMAKE_WARN_DEPRECATED", value,
4477
0
                      "Whether to issue warnings for deprecated "
4478
0
                      "functionality.",
4479
0
                      cmStateEnums::INTERNAL);
4480
0
}
4481
4482
bool cmake::GetDevWarningsAsErrors() const
4483
0
{
4484
0
  return this->Messenger->GetDevWarningsAsErrors();
4485
0
}
4486
4487
void cmake::SetDevWarningsAsErrors(bool b)
4488
0
{
4489
0
  std::string value;
4490
4491
  // equivalent to -Werror=dev
4492
0
  if (b) {
4493
0
    value = "FALSE";
4494
0
  }
4495
  // equivalent to -Wno-error=dev
4496
0
  else {
4497
0
    value = "TRUE";
4498
0
  }
4499
4500
0
  this->AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_ERRORS", value,
4501
0
                      "Suppress errors that are meant for"
4502
0
                      " the author of the CMakeLists.txt files.",
4503
0
                      cmStateEnums::INTERNAL);
4504
0
}
4505
4506
bool cmake::GetDeprecatedWarningsAsErrors() const
4507
0
{
4508
0
  return this->Messenger->GetDeprecatedWarningsAsErrors();
4509
0
}
4510
4511
void cmake::SetDeprecatedWarningsAsErrors(bool b)
4512
0
{
4513
0
  std::string value;
4514
4515
  // equivalent to -Werror=deprecated
4516
0
  if (b) {
4517
0
    value = "TRUE";
4518
0
  }
4519
  // equivalent to -Wno-error=deprecated
4520
0
  else {
4521
0
    value = "FALSE";
4522
0
  }
4523
4524
0
  this->AddCacheEntry("CMAKE_ERROR_DEPRECATED", value,
4525
0
                      "Whether to issue deprecation errors for macros"
4526
0
                      " and functions.",
4527
0
                      cmStateEnums::INTERNAL);
4528
0
}
4529
4530
void cmake::SetDebugFindOutputPkgs(std::string const& args)
4531
0
{
4532
0
  this->DebugFindPkgs.emplace(args);
4533
0
}
4534
4535
void cmake::SetDebugFindOutputVars(std::string const& args)
4536
0
{
4537
0
  this->DebugFindVars.emplace(args);
4538
0
}
4539
4540
bool cmake::GetDebugFindOutput(std::string const& var) const
4541
0
{
4542
0
  return this->DebugFindVars.count(var);
4543
0
}
4544
4545
bool cmake::GetDebugFindPkgOutput(std::string const& pkg) const
4546
0
{
4547
0
  return this->DebugFindPkgs.count(pkg);
4548
0
}
4549
4550
void cmake::SetCMakeListName(std::string const& name)
4551
0
{
4552
0
  this->CMakeListName = name;
4553
0
}
4554
4555
std::string cmake::GetCMakeListFile(std::string const& dir) const
4556
0
{
4557
0
  assert(!dir.empty());
4558
0
  cm::string_view const slash = dir.back() != '/' ? "/"_s : ""_s;
4559
0
  std::string listFile;
4560
0
  if (!this->CMakeListName.empty()) {
4561
0
    listFile = cmStrCat(dir, slash, this->CMakeListName);
4562
0
  }
4563
0
  if (listFile.empty() || !cmSystemTools::FileExists(listFile, true)) {
4564
0
    listFile = cmStrCat(dir, slash, "CMakeLists.txt");
4565
0
  }
4566
0
  return listFile;
4567
0
}
4568
4569
#if !defined(CMAKE_BOOTSTRAP)
4570
cmMakefileProfilingData& cmake::GetProfilingOutput()
4571
0
{
4572
0
  return *(this->ProfilingOutput);
4573
0
}
4574
4575
bool cmake::IsProfilingEnabled() const
4576
0
{
4577
0
  return static_cast<bool>(this->ProfilingOutput);
4578
0
}
4579
#endif