Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmCMakeHostSystemInformationCommand.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 "cmCMakeHostSystemInformationCommand.h"
4
5
#include <algorithm>
6
#include <cassert>
7
#include <cctype>
8
#include <cstddef>
9
#include <initializer_list>
10
#include <map>
11
#include <string>
12
#include <utility>
13
14
#include <cm/optional>
15
#include <cm/string_view>
16
#include <cm/type_traits>
17
#include <cmext/string_view>
18
19
#include "cmsys/FStream.hxx"
20
#include "cmsys/Glob.hxx"
21
#include "cmsys/SystemInformation.hxx"
22
23
#include "cmArgumentParser.h"
24
#include "cmExecutionStatus.h"
25
#include "cmList.h"
26
#include "cmMakefile.h"
27
#include "cmRange.h"
28
#include "cmStringAlgorithms.h"
29
#include "cmSystemTools.h"
30
#include "cmValue.h"
31
#include "cmWindowsRegistry.h"
32
33
#ifdef _WIN32
34
#  include "cmAlgorithms.h"
35
#  include "cmGlobalGenerator.h"
36
#  include "cmGlobalVisualStudio10Generator.h"
37
#  include "cmGlobalVisualStudioVersionedGenerator.h"
38
#  include "cmVSSetupHelper.h"
39
#endif
40
41
namespace {
42
std::string const DELIM[2] = { {}, ";" };
43
44
// BEGIN Private functions
45
template <typename T>
46
cm::enable_if_t<std::is_arithmetic<T>::value, std::string> ValueToString(
47
  T const& value)
48
0
{
49
0
  return std::to_string(value);
50
0
}
Unexecuted instantiation: cmCMakeHostSystemInformationCommand.cxx:_ZN12_GLOBAL__N_113ValueToStringIjEENSt3__19enable_ifIXsr3std13is_arithmeticIT_EE5valueENS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEE4typeERKS3_
Unexecuted instantiation: cmCMakeHostSystemInformationCommand.cxx:_ZN12_GLOBAL__N_113ValueToStringImEENSt3__19enable_ifIXsr3std13is_arithmeticIT_EE5valueENS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEE4typeERKS3_
Unexecuted instantiation: cmCMakeHostSystemInformationCommand.cxx:_ZN12_GLOBAL__N_113ValueToStringIbEENSt3__19enable_ifIXsr3std13is_arithmeticIT_EE5valueENS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEE4typeERKS3_
Unexecuted instantiation: cmCMakeHostSystemInformationCommand.cxx:_ZN12_GLOBAL__N_113ValueToStringIiEENSt3__19enable_ifIXsr3std13is_arithmeticIT_EE5valueENS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEE4typeERKS3_
Unexecuted instantiation: cmCMakeHostSystemInformationCommand.cxx:_ZN12_GLOBAL__N_113ValueToStringIfEENSt3__19enable_ifIXsr3std13is_arithmeticIT_EE5valueENS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEE4typeERKS3_
51
52
std::string ValueToString(char const* const value)
53
0
{
54
0
  return value ? value : std::string{};
55
0
}
56
57
std::string ValueToString(std::string const& value)
58
0
{
59
0
  return value;
60
0
}
61
62
cm::optional<std::string> GetValue(cmsys::SystemInformation& info,
63
                                   std::string const& key)
64
0
{
65
0
  if (key == "NUMBER_OF_LOGICAL_CORES"_s) {
66
0
    return ValueToString(info.GetNumberOfLogicalCPU());
67
0
  }
68
0
  if (key == "NUMBER_OF_PHYSICAL_CORES"_s) {
69
0
    return ValueToString(info.GetNumberOfPhysicalCPU());
70
0
  }
71
0
  if (key == "HOSTNAME"_s) {
72
0
    return ValueToString(info.GetHostname());
73
0
  }
74
0
  if (key == "FQDN"_s) {
75
0
    return ValueToString(info.GetFullyQualifiedDomainName());
76
0
  }
77
0
  if (key == "TOTAL_VIRTUAL_MEMORY"_s) {
78
0
    return ValueToString(info.GetTotalVirtualMemory());
79
0
  }
80
0
  if (key == "AVAILABLE_VIRTUAL_MEMORY"_s) {
81
0
    return ValueToString(info.GetAvailableVirtualMemory());
82
0
  }
83
0
  if (key == "TOTAL_PHYSICAL_MEMORY"_s) {
84
0
    return ValueToString(info.GetTotalPhysicalMemory());
85
0
  }
86
0
  if (key == "AVAILABLE_PHYSICAL_MEMORY"_s) {
87
0
    return ValueToString(info.GetAvailablePhysicalMemory());
88
0
  }
89
0
  if (key == "IS_64BIT"_s) {
90
0
    return ValueToString(info.Is64Bits());
91
0
  }
92
0
  if (key == "HAS_FPU"_s) {
93
0
    return ValueToString(
94
0
      info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_FPU));
95
0
  }
96
0
  if (key == "HAS_MMX"_s) {
97
0
    return ValueToString(
98
0
      info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_MMX));
99
0
  }
100
0
  if (key == "HAS_MMX_PLUS"_s) {
101
0
    return ValueToString(info.DoesCPUSupportFeature(
102
0
      cmsys::SystemInformation::CPU_FEATURE_MMX_PLUS));
103
0
  }
104
0
  if (key == "HAS_SSE"_s) {
105
0
    return ValueToString(
106
0
      info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_SSE));
107
0
  }
108
0
  if (key == "HAS_SSE2"_s) {
109
0
    return ValueToString(
110
0
      info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_SSE2));
111
0
  }
112
0
  if (key == "HAS_SSE_FP"_s) {
113
0
    return ValueToString(info.DoesCPUSupportFeature(
114
0
      cmsys::SystemInformation::CPU_FEATURE_SSE_FP));
115
0
  }
116
0
  if (key == "HAS_SSE_MMX"_s) {
117
0
    return ValueToString(info.DoesCPUSupportFeature(
118
0
      cmsys::SystemInformation::CPU_FEATURE_SSE_MMX));
119
0
  }
120
0
  if (key == "HAS_AMD_3DNOW"_s) {
121
0
    return ValueToString(info.DoesCPUSupportFeature(
122
0
      cmsys::SystemInformation::CPU_FEATURE_AMD_3DNOW));
123
0
  }
124
0
  if (key == "HAS_AMD_3DNOW_PLUS"_s) {
125
0
    return ValueToString(info.DoesCPUSupportFeature(
126
0
      cmsys::SystemInformation::CPU_FEATURE_AMD_3DNOW_PLUS));
127
0
  }
128
0
  if (key == "HAS_IA64"_s) {
129
0
    return ValueToString(
130
0
      info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_IA64));
131
0
  }
132
0
  if (key == "HAS_SERIAL_NUMBER"_s) {
133
0
    return ValueToString(info.DoesCPUSupportFeature(
134
0
      cmsys::SystemInformation::CPU_FEATURE_SERIALNUMBER));
135
0
  }
136
0
  if (key == "HAS_APIC"_s) {
137
0
    return ValueToString(
138
0
      info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_APIC));
139
0
  }
140
0
  if (key == "HAS_L1_CACHE"_s) {
141
0
    return ValueToString(info.DoesCPUSupportFeature(
142
0
      cmsys::SystemInformation::CPU_FEATURE_L1CACHE));
143
0
  }
144
0
  if (key == "PROCESSOR_NAME"_s) {
145
0
    return ValueToString(info.GetExtendedProcessorName());
146
0
  }
147
0
  if (key == "PROCESSOR_DESCRIPTION"_s) {
148
0
    return info.GetCPUDescription();
149
0
  }
150
0
  if (key == "PROCESSOR_SERIAL_NUMBER"_s) {
151
0
    return ValueToString(info.GetProcessorSerialNumber());
152
0
  }
153
0
  if (key == "OS_NAME"_s) {
154
0
    return ValueToString(info.GetOSName());
155
0
  }
156
0
  if (key == "OS_RELEASE"_s) {
157
0
    return ValueToString(info.GetOSRelease());
158
0
  }
159
0
  if (key == "OS_VERSION"_s) {
160
0
    return ValueToString(info.GetOSVersion());
161
0
  }
162
0
  if (key == "OS_PLATFORM"_s) {
163
0
    return ValueToString(info.GetOSPlatform());
164
0
  }
165
0
  if (key == "FAMILY_ID"_s) {
166
0
    return ValueToString(info.GetFamilyID());
167
0
  }
168
0
  if (key == "MODEL_ID"_s) {
169
0
    return ValueToString(info.GetModelID());
170
0
  }
171
0
  if (key == "MODEL_NAME"_s) {
172
0
    return ValueToString(info.GetModelName());
173
0
  }
174
0
  if (key == "PROCESSOR_APIC_ID"_s) {
175
0
    int apicId = info.GetProcessorAPICID();
176
0
    if (apicId) {
177
0
      return ValueToString(apicId);
178
0
    }
179
0
    return "";
180
0
  }
181
0
  if (key == "PROCESSOR_CACHE_SIZE"_s) {
182
0
    int cacheSize = info.GetProcessorCacheSize();
183
0
    if (cacheSize > 0) {
184
0
      return ValueToString(cacheSize);
185
0
    }
186
0
    return "";
187
0
  }
188
0
  if (key == "PROCESSOR_CLOCK_FREQUENCY"_s) {
189
0
    return ValueToString(info.GetProcessorClockFrequency());
190
0
  }
191
0
  if (key == "VENDOR_ID"_s) {
192
0
    return ValueToString(info.GetVendorID());
193
0
  }
194
0
  if (key == "VENDOR_STRING"_s) {
195
0
    return ValueToString(info.GetVendorString());
196
0
  }
197
0
  return {};
198
0
}
199
200
cm::optional<std::pair<std::string, std::string>> ParseOSReleaseLine(
201
  std::string const& line)
202
0
{
203
0
  std::string key;
204
0
  std::string value;
205
206
0
  char prev = 0;
207
0
  enum ParserState
208
0
  {
209
0
    PARSE_KEY_1ST,
210
0
    PARSE_KEY,
211
0
    FOUND_EQ,
212
0
    PARSE_SINGLE_QUOTE_VALUE,
213
0
    PARSE_DBL_QUOTE_VALUE,
214
0
    PARSE_VALUE,
215
0
    IGNORE_REST
216
0
  } state = PARSE_KEY_1ST;
217
218
0
  for (auto ch : line) {
219
0
    switch (state) {
220
0
      case PARSE_KEY_1ST:
221
0
        if (std::isalpha(ch) || ch == '_') {
222
0
          key += ch;
223
0
          state = PARSE_KEY;
224
0
        } else if (!cmIsSpace(ch)) {
225
0
          state = IGNORE_REST;
226
0
        }
227
0
        break;
228
229
0
      case PARSE_KEY:
230
0
        if (ch == '=') {
231
0
          state = FOUND_EQ;
232
0
        } else if (std::isalnum(ch) || ch == '_') {
233
0
          key += ch;
234
0
        } else {
235
0
          state = IGNORE_REST;
236
0
        }
237
0
        break;
238
239
0
      case FOUND_EQ:
240
0
        switch (ch) {
241
0
          case '\'':
242
0
            state = PARSE_SINGLE_QUOTE_VALUE;
243
0
            break;
244
0
          case '"':
245
0
            state = PARSE_DBL_QUOTE_VALUE;
246
0
            break;
247
0
          case '#':
248
0
          case '\\':
249
0
            state = IGNORE_REST;
250
0
            break;
251
0
          default:
252
0
            value += ch;
253
0
            state = PARSE_VALUE;
254
0
        }
255
0
        break;
256
257
0
      case PARSE_SINGLE_QUOTE_VALUE:
258
0
        if (ch == '\'') {
259
0
          if (prev != '\\') {
260
0
            state = IGNORE_REST;
261
0
          } else {
262
0
            assert(!value.empty());
263
0
            value[value.size() - 1] = ch;
264
0
          }
265
0
        } else {
266
0
          value += ch;
267
0
        }
268
0
        break;
269
270
0
      case PARSE_DBL_QUOTE_VALUE:
271
0
        if (ch == '"') {
272
0
          if (prev != '\\') {
273
0
            state = IGNORE_REST;
274
0
          } else {
275
0
            assert(!value.empty());
276
0
            value[value.size() - 1] = ch;
277
0
          }
278
0
        } else {
279
0
          value += ch;
280
0
        }
281
0
        break;
282
283
0
      case PARSE_VALUE:
284
0
        if (ch == '#' || cmIsSpace(ch)) {
285
0
          state = IGNORE_REST;
286
0
        } else {
287
0
          value += ch;
288
0
        }
289
0
        break;
290
291
0
      default:
292
        // Unexpected os-release parser state!
293
0
        state = IGNORE_REST;
294
0
        break;
295
0
    }
296
297
0
    if (state == IGNORE_REST) {
298
0
      break;
299
0
    }
300
0
    prev = ch;
301
0
  }
302
0
  if (!(key.empty() || value.empty())) {
303
0
    return std::make_pair(key, value);
304
0
  }
305
0
  return {};
306
0
}
307
308
std::map<std::string, std::string> GetOSReleaseVariables(
309
  cmExecutionStatus& status)
310
0
{
311
0
  auto& makefile = status.GetMakefile();
312
0
  auto const& sysroot = makefile.GetSafeDefinition("CMAKE_SYSROOT");
313
314
0
  std::map<std::string, std::string> data;
315
  // Based on
316
  // https://www.freedesktop.org/software/systemd/man/latest/os-release.html
317
0
  for (auto name : { "/etc/os-release"_s, "/usr/lib/os-release"_s }) {
318
0
    auto const& filename = cmStrCat(sysroot, name);
319
0
    if (cmSystemTools::FileExists(filename)) {
320
0
      cmsys::ifstream fin(filename.c_str());
321
0
      for (std::string line; !std::getline(fin, line).fail();) {
322
0
        auto kv = ParseOSReleaseLine(line);
323
0
        if (kv.has_value()) {
324
0
          data.emplace(kv.value());
325
0
        }
326
0
      }
327
0
      break;
328
0
    }
329
0
  }
330
  // Got smth?
331
0
  if (!data.empty()) {
332
0
    return data;
333
0
  }
334
335
  // Ugh, it could be some pre-os-release distro.
336
  // Lets try some fallback getters.
337
  // See also:
338
  //  - http://linuxmafia.com/faq/Admin/release-files.html
339
340
  // 1. CMake provided
341
0
  cmsys::Glob gl;
342
0
  std::vector<std::string> scripts;
343
0
  auto const findExpr = cmStrCat(cmSystemTools::GetCMakeRoot(),
344
0
                                 "/Modules/Internal/OSRelease/*.cmake");
345
0
  if (gl.FindFiles(findExpr)) {
346
0
    scripts = gl.GetFiles();
347
0
  }
348
349
  // 2. User provided (append to the CMake provided)
350
0
  cmList::append(
351
0
    scripts, makefile.GetDefinition("CMAKE_GET_OS_RELEASE_FALLBACK_SCRIPTS"));
352
353
  // Filter out files that are not in format `NNN-name.cmake`
354
0
  auto checkName = [](std::string const& filepath) -> bool {
355
0
    auto const& filename = cmSystemTools::GetFilenameName(filepath);
356
    // NOTE Minimum filename length expected:
357
    //   NNN-<at-least-one-char-name>.cmake  --> 11
358
0
    return (filename.size() < 11) || !std::isdigit(filename[0]) ||
359
0
      !std::isdigit(filename[1]) || !std::isdigit(filename[2]) ||
360
0
      filename[3] != '-';
361
0
  };
362
0
  scripts.erase(std::remove_if(scripts.begin(), scripts.end(), checkName),
363
0
                scripts.end());
364
365
  // Make sure scripts are running in desired order
366
0
  std::sort(scripts.begin(), scripts.end(),
367
0
            [](std::string const& lhs, std::string const& rhs) -> bool {
368
0
              long lhs_order;
369
0
              cmStrToLong(cmSystemTools::GetFilenameName(lhs).substr(0u, 3u),
370
0
                          &lhs_order);
371
0
              long rhs_order;
372
0
              cmStrToLong(cmSystemTools::GetFilenameName(rhs).substr(0u, 3u),
373
0
                          &rhs_order);
374
0
              return lhs_order < rhs_order;
375
0
            });
376
377
  // Name of the variable to put the results
378
0
  std::string const result_variable{ "CMAKE_GET_OS_RELEASE_FALLBACK_RESULT" };
379
380
0
  for (auto const& script : scripts) {
381
    // Unset the result variable
382
0
    makefile.RemoveDefinition(result_variable);
383
384
    // include FATAL_ERROR and ERROR in the return status
385
0
    if (!makefile.ReadListFile(script) ||
386
0
        cmSystemTools::GetErrorOccurredFlag()) {
387
      // Ok, no worries... go try the next script.
388
0
      continue;
389
0
    }
390
391
0
    cmList variables{ makefile.GetDefinition(result_variable) };
392
0
    if (variables.empty()) {
393
      // Heh, this script didn't found anything... go try the next one.
394
0
      continue;
395
0
    }
396
397
0
    for (auto const& variable : variables) {
398
0
      auto value = makefile.GetSafeDefinition(variable);
399
0
      makefile.RemoveDefinition(variable);
400
401
0
      if (!cmHasPrefix(variable, cmStrCat(result_variable, '_'))) {
402
        // Ignore unknown variable set by the script
403
0
        continue;
404
0
      }
405
406
0
      auto key = variable.substr(result_variable.size() + 1,
407
0
                                 variable.size() - result_variable.size() - 1);
408
0
      data.emplace(std::move(key), std::move(value));
409
0
    }
410
411
    // Try 'till some script can get anything
412
0
    if (!data.empty()) {
413
0
      data.emplace("USED_FALLBACK_SCRIPT", script);
414
0
      break;
415
0
    }
416
0
  }
417
418
0
  makefile.RemoveDefinition(result_variable);
419
420
0
  return data;
421
0
}
422
423
cm::optional<std::string> GetDistribValue(cmExecutionStatus& status,
424
                                          std::string const& key,
425
                                          std::string const& variable)
426
0
{
427
0
  auto const prefix = "DISTRIB_"_s;
428
0
  if (!cmHasPrefix(key, prefix)) {
429
0
    return {};
430
0
  }
431
432
0
  static std::map<std::string, std::string> const s_os_release =
433
0
    GetOSReleaseVariables(status);
434
435
0
  auto& makefile = status.GetMakefile();
436
437
0
  std::string const subkey =
438
0
    key.substr(prefix.size(), key.size() - prefix.size());
439
0
  if (subkey == "INFO"_s) {
440
0
    std::string vars;
441
0
    for (auto const& kv : s_os_release) {
442
0
      auto cmake_var_name = cmStrCat(variable, '_', kv.first);
443
0
      vars += DELIM[!vars.empty()] + cmake_var_name;
444
0
      makefile.AddDefinition(cmake_var_name, kv.second);
445
0
    }
446
0
    return cm::optional<std::string>(std::move(vars));
447
0
  }
448
449
  // Query individual variable
450
0
  auto const it = s_os_release.find(subkey);
451
0
  if (it != s_os_release.cend()) {
452
0
    return it->second;
453
0
  }
454
455
  // NOTE Empty string means requested variable not set
456
0
  return std::string{};
457
0
}
458
459
#ifdef _WIN32
460
std::string FindMSYSTEM_PREFIX(std::vector<std::string> prefixes)
461
{
462
  for (std::string const& prefix : prefixes) {
463
    std::string out;
464
    std::string err;
465
    int ret;
466
    // In a modern MSYSTEM environment we expect cygpath to be in PATH.
467
    std::vector<std::string> cygpath_cmd{ "cygpath", "-w", prefix };
468
    if (cmSystemTools::RunSingleCommand(cygpath_cmd, &out, &err, &ret, nullptr,
469
                                        cmSystemTools::OUTPUT_NONE)) {
470
      if (ret == 0) {
471
        out = cmTrimWhitespace(out);
472
        cmSystemTools::ConvertToUnixSlashes(out);
473
        if (cmSystemTools::FileIsDirectory(out)) {
474
          return out;
475
        }
476
      }
477
    } else {
478
      // In a legacy MSYSTEM environment (MinGW/MSYS 1.0) there is no
479
      // cygpath but we expect 'sh' to be in PATH.
480
      std::vector<std::string> sh_cmd{
481
        "sh", "-c", cmStrCat("cd \"", prefix, "\" && cmd //c cd")
482
      };
483
      if (cmSystemTools::RunSingleCommand(sh_cmd, &out, &err, &ret, nullptr,
484
                                          cmSystemTools::OUTPUT_NONE)) {
485
        if (ret == 0) {
486
          out = cmTrimWhitespace(out);
487
          cmSystemTools::ConvertToUnixSlashes(out);
488
          if (cmSystemTools::FileIsDirectory(out)) {
489
            return out;
490
          }
491
        }
492
      }
493
    }
494
  }
495
  return {};
496
}
497
498
std::string FallbackMSYSTEM_PREFIX(cm::string_view msystem)
499
{
500
  // These layouts are used by distributions such as
501
  // * MSYS2: https://www.msys2.org/docs/environments/
502
  // * MinGW/MSYS 1.0: http://mingw.osdn.io/
503
  if (msystem == "MSYS"_s) {
504
    static std::string const msystem_msys = FindMSYSTEM_PREFIX({ "/usr" });
505
    return msystem_msys;
506
  }
507
  if (msystem == "MINGW32"_s) {
508
    static std::string const msystem_mingw32 =
509
      FindMSYSTEM_PREFIX({ "/mingw32", "/mingw" });
510
    return msystem_mingw32;
511
  }
512
  if (msystem == "MINGW64"_s) {
513
    static std::string const msystem_mingw64 =
514
      FindMSYSTEM_PREFIX({ "/mingw64" });
515
    return msystem_mingw64;
516
  }
517
  if (msystem == "UCRT64"_s) {
518
    static std::string const msystem_ucrt64 =
519
      FindMSYSTEM_PREFIX({ "/ucrt64" });
520
    return msystem_ucrt64;
521
  }
522
  if (msystem == "CLANG32"_s) {
523
    static std::string const msystem_clang32 =
524
      FindMSYSTEM_PREFIX({ "/clang32" });
525
    return msystem_clang32;
526
  }
527
  if (msystem == "CLANG64"_s) {
528
    static std::string const msystem_clang64 =
529
      FindMSYSTEM_PREFIX({ "/clang64" });
530
    return msystem_clang64;
531
  }
532
  if (msystem == "CLANGARM64"_s) {
533
    static std::string const msystem_clangarm64 =
534
      FindMSYSTEM_PREFIX({ "/clangarm64" });
535
    return msystem_clangarm64;
536
  }
537
  return {};
538
}
539
540
cm::optional<std::string> GetWindowsValue(cmExecutionStatus& status,
541
                                          std::string const& key)
542
{
543
  auto* const gg = status.GetMakefile().GetGlobalGenerator();
544
  for (auto vs : { 15, 16, 17, 18 }) {
545
    if (key == cmStrCat("VS_"_s, vs, "_DIR"_s)) {
546
      std::string value;
547
      // If generating for the VS nn IDE, use the same instance.
548
549
      if (cmHasPrefix(gg->GetName(), cmStrCat("Visual Studio "_s, vs, ' '))) {
550
        cmGlobalVisualStudioVersionedGenerator* vsNNgen =
551
          static_cast<cmGlobalVisualStudioVersionedGenerator*>(gg);
552
        if (vsNNgen->GetVSInstance(value)) {
553
          return value;
554
        }
555
      }
556
557
      // Otherwise, find a VS nn instance ourselves.
558
      cmVSSetupAPIHelper vsSetupAPIHelper(vs);
559
      if (vsSetupAPIHelper.GetVSInstanceInfo(value)) {
560
        cmSystemTools::ConvertToUnixSlashes(value);
561
      }
562
      return value;
563
    }
564
  }
565
566
  if (key == "VS_MSBUILD_COMMAND"_s && gg->IsVisualStudioAtLeast10()) {
567
    cmGlobalVisualStudio10Generator* vs10gen =
568
      static_cast<cmGlobalVisualStudio10Generator*>(gg);
569
    return vs10gen->FindMSBuildCommandEarly(&status.GetMakefile());
570
  }
571
572
  if (key == "MSYSTEM_PREFIX") {
573
    // MSYSTEM_PREFIX is meaningful only under a MSYSTEM environment.
574
    cm::optional<std::string> ms = cmSystemTools::GetEnvVar("MSYSTEM");
575
    if (!ms || ms->empty()) {
576
      return std::string();
577
    }
578
    // Prefer the MSYSTEM_PREFIX environment variable.
579
    if (cm::optional<std::string> msp =
580
          cmSystemTools::GetEnvVar("MSYSTEM_PREFIX")) {
581
      cmSystemTools::ConvertToUnixSlashes(*msp);
582
      if (cmSystemTools::FileIsDirectory(*msp)) {
583
        return msp;
584
      }
585
    }
586
    // Fall back to known distribution layouts.
587
    return FallbackMSYSTEM_PREFIX(*ms);
588
  }
589
  return {};
590
}
591
#endif
592
593
cm::optional<std::string> GetValueChained()
594
0
{
595
0
  return {};
596
0
}
597
598
template <typename GetterFn, typename... Next>
599
cm::optional<std::string> GetValueChained(GetterFn current, Next... chain)
600
0
{
601
0
  auto value = current();
602
0
  if (value.has_value()) {
603
0
    return value;
604
0
  }
605
0
  return GetValueChained(chain...);
606
0
}
Unexecuted instantiation: cmCMakeHostSystemInformationCommand.cxx:std::__1::optional<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > (anonymous namespace)::GetValueChained<cmCMakeHostSystemInformationCommand(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, cmExecutionStatus&)::$_0, cmCMakeHostSystemInformationCommand(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, cmExecutionStatus&)::$_1>(cmCMakeHostSystemInformationCommand(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, cmExecutionStatus&)::$_0, cmCMakeHostSystemInformationCommand(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, cmExecutionStatus&)::$_1)
Unexecuted instantiation: cmCMakeHostSystemInformationCommand.cxx:std::__1::optional<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > (anonymous namespace)::GetValueChained<cmCMakeHostSystemInformationCommand(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, cmExecutionStatus&)::$_1>(cmCMakeHostSystemInformationCommand(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, cmExecutionStatus&)::$_1)
607
608
template <typename Range>
609
bool QueryWindowsRegistry(Range args, cmExecutionStatus& status,
610
                          std::string const& variable)
611
0
{
612
0
  using View = cmWindowsRegistry::View;
613
0
  if (args.empty()) {
614
0
    status.SetError("missing <key> specification.");
615
0
    return false;
616
0
  }
617
0
  std::string const& key = *args.begin();
618
619
0
  struct Arguments : public ArgumentParser::ParseResult
620
0
  {
621
0
    std::string ValueName;
622
0
    bool ValueNames = false;
623
0
    bool SubKeys = false;
624
0
    std::string View;
625
0
    std::string Separator;
626
0
    std::string ErrorVariable;
627
0
  };
628
0
  cmArgumentParser<Arguments> parser;
629
0
  parser.Bind("VALUE"_s, &Arguments::ValueName)
630
0
    .Bind("VALUE_NAMES"_s, &Arguments::ValueNames)
631
0
    .Bind("SUBKEYS"_s, &Arguments::SubKeys)
632
0
    .Bind("VIEW"_s, &Arguments::View)
633
0
    .Bind("SEPARATOR"_s, &Arguments::Separator)
634
0
    .Bind("ERROR_VARIABLE"_s, &Arguments::ErrorVariable);
635
0
  std::vector<std::string> invalidArgs;
636
637
0
  Arguments const arguments = parser.Parse(args.advance(1), &invalidArgs);
638
0
  if (!invalidArgs.empty()) {
639
0
    status.SetError(cmStrCat("given invalid argument(s) \"",
640
0
                             cmJoin(invalidArgs, ", "_s), "\"."));
641
0
    return false;
642
0
  }
643
0
  if (arguments.MaybeReportError(status.GetMakefile())) {
644
0
    return true;
645
0
  }
646
0
  if ((!arguments.ValueName.empty() &&
647
0
       (arguments.ValueNames || arguments.SubKeys)) ||
648
0
      (arguments.ValueName.empty() && arguments.ValueNames &&
649
0
       arguments.SubKeys)) {
650
0
    status.SetError("given mutually exclusive sub-options VALUE, "
651
0
                    "VALUE_NAMES or SUBKEYS.");
652
0
    return false;
653
0
  }
654
655
0
  if (!arguments.View.empty() && !cmWindowsRegistry::ToView(arguments.View)) {
656
0
    status.SetError(
657
0
      cmStrCat("given invalid value for VIEW: ", arguments.View, '.'));
658
0
    return false;
659
0
  }
660
661
0
  auto& makefile = status.GetMakefile();
662
663
0
  makefile.AddDefinition(variable, ""_s);
664
665
0
  auto view = arguments.View.empty()
666
0
    ? View::Both
667
0
    : *cmWindowsRegistry::ToView(arguments.View);
668
0
  cmWindowsRegistry registry(makefile);
669
0
  if (arguments.ValueNames) {
670
0
    auto result = registry.GetValueNames(key, view);
671
0
    if (result) {
672
0
      makefile.AddDefinition(variable, cmList::to_string(*result));
673
0
    }
674
0
  } else if (arguments.SubKeys) {
675
0
    auto result = registry.GetSubKeys(key, view);
676
0
    if (result) {
677
0
      makefile.AddDefinition(variable, cmList::to_string(*result));
678
0
    }
679
0
  } else {
680
0
    auto result =
681
0
      registry.ReadValue(key, arguments.ValueName, view, arguments.Separator);
682
0
    if (result) {
683
0
      makefile.AddDefinition(variable, *result);
684
0
    }
685
0
  }
686
687
  // return error message if requested
688
0
  if (!arguments.ErrorVariable.empty()) {
689
0
    makefile.AddDefinition(arguments.ErrorVariable, registry.GetLastError());
690
0
  }
691
692
0
  return true;
693
0
}
694
695
// END Private functions
696
} // anonymous namespace
697
698
// cmCMakeHostSystemInformation
699
bool cmCMakeHostSystemInformationCommand(std::vector<std::string> const& args,
700
                                         cmExecutionStatus& status)
701
0
{
702
0
  std::size_t current_index = 0;
703
704
0
  if (args.size() < (current_index + 2) || args[current_index] != "RESULT"_s) {
705
0
    status.SetError("missing RESULT specification.");
706
0
    return false;
707
0
  }
708
709
0
  auto const& variable = args[current_index + 1];
710
0
  current_index += 2;
711
712
0
  if (args.size() < (current_index + 2) || args[current_index] != "QUERY"_s) {
713
0
    status.SetError("missing QUERY specification");
714
0
    return false;
715
0
  }
716
717
0
  if (args[current_index + 1] == "WINDOWS_REGISTRY"_s) {
718
0
    return QueryWindowsRegistry(cmMakeRange(args).advance(current_index + 2),
719
0
                                status, variable);
720
0
  }
721
722
0
  static cmsys::SystemInformation info;
723
0
  static auto initialized = false;
724
0
  if (!initialized) {
725
0
    info.RunCPUCheck();
726
0
    info.RunOSCheck();
727
0
    info.RunMemoryCheck();
728
0
    initialized = true;
729
0
  }
730
731
0
  std::string result_list;
732
0
  for (auto i = current_index + 1; i < args.size(); ++i) {
733
0
    result_list += DELIM[!result_list.empty()];
734
735
0
    auto const& key = args[i];
736
    // clang-format off
737
0
    auto value =
738
0
      GetValueChained(
739
0
          [&]() { return GetValue(info, key); }
740
0
        , [&]() { return GetDistribValue(status, key, variable); }
741
#ifdef _WIN32
742
        , [&]() { return GetWindowsValue(status, key); }
743
#endif
744
0
        );
745
    // clang-format on
746
0
    if (!value) {
747
0
      status.SetError("does not recognize <key> " + key);
748
0
      return false;
749
0
    }
750
0
    result_list += value.value();
751
0
  }
752
753
0
  status.GetMakefile().AddDefinition(variable, result_list);
754
755
0
  return true;
756
0
}