Coverage Report

Created: 2026-06-15 07:03

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