Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmSiteNameCommand.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 "cmSiteNameCommand.h"
4
5
#include "cmsys/RegularExpression.hxx"
6
7
#include "cmExecutionStatus.h"
8
#include "cmMakefile.h"
9
#include "cmStateTypes.h"
10
#include "cmSystemTools.h"
11
#include "cmValue.h"
12
13
// cmSiteNameCommand
14
bool cmSiteNameCommand(std::vector<std::string> const& args,
15
                       cmExecutionStatus& status)
16
0
{
17
0
  if (args.size() != 1) {
18
0
    status.SetError("called with incorrect number of arguments");
19
0
    return false;
20
0
  }
21
0
  std::vector<std::string> paths;
22
0
  paths.emplace_back("/usr/bsd");
23
0
  paths.emplace_back("/usr/sbin");
24
0
  paths.emplace_back("/usr/bin");
25
0
  paths.emplace_back("/bin");
26
0
  paths.emplace_back("/sbin");
27
0
  paths.emplace_back("/usr/local/bin");
28
29
0
  cmValue cacheValue = status.GetMakefile().GetDefinition(args[0]);
30
0
  if (cacheValue) {
31
0
    return true;
32
0
  }
33
34
0
  cmValue temp = status.GetMakefile().GetDefinition("HOSTNAME");
35
0
  std::string hostname_cmd;
36
0
  if (temp) {
37
0
    hostname_cmd = *temp;
38
0
  } else {
39
0
    hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
40
0
  }
41
42
0
  std::string siteName = "unknown";
43
#if defined(_WIN32) && !defined(__CYGWIN__)
44
  std::string host;
45
  if (cmSystemTools::ReadRegistryValue(
46
        "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\"
47
        "Control\\ComputerName\\ComputerName;ComputerName",
48
        host)) {
49
    siteName = host;
50
  }
51
#else
52
  // try to find the hostname for this computer
53
0
  if (!cmIsOff(hostname_cmd)) {
54
0
    std::string host;
55
0
    cmSystemTools::RunSingleCommand(hostname_cmd, &host, nullptr, nullptr,
56
0
                                    nullptr, cmSystemTools::OUTPUT_NONE);
57
58
    // got the hostname
59
0
    if (!host.empty()) {
60
      // remove any white space from the host name
61
0
      std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
62
0
      cmsys::RegularExpression hostReg(hostRegExp.c_str());
63
0
      if (hostReg.find(host.c_str())) {
64
        // strip whitespace
65
0
        host = hostReg.match(1);
66
0
      }
67
68
0
      if (!host.empty()) {
69
0
        siteName = host;
70
0
      }
71
0
    }
72
0
  }
73
0
#endif
74
0
  status.GetMakefile().AddCacheDefinition(
75
0
    args[0], siteName, "Name of the computer/site where compile is being run",
76
0
    cmStateEnums::STRING);
77
78
0
  return true;
79
0
}