cmSiteNameCommand.cxx 2.26 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3 4
#include "cmSiteNameCommand.h"

5
#include "cmsys/RegularExpression.hxx"
6 7 8

#include "cmMakefile.h"
#include "cmStateTypes.h"
9 10
#include "cmSystemTools.h"

11
class cmExecutionStatus;
12

13
// cmSiteNameCommand
14 15
bool cmSiteNameCommand::InitialPass(std::vector<std::string> const& args,
                                    cmExecutionStatus&)
16
{
17
  if (args.size() != 1) {
18 19
    this->SetError("called with incorrect number of arguments");
    return false;
20
  }
21
  std::vector<std::string> paths;
wahikihiki's avatar
wahikihiki committed
22 23 24 25 26 27
  paths.emplace_back("/usr/bsd");
  paths.emplace_back("/usr/sbin");
  paths.emplace_back("/usr/bin");
  paths.emplace_back("/bin");
  paths.emplace_back("/sbin");
  paths.emplace_back("/usr/local/bin");
28

29 30
  const char* cacheValue = this->Makefile->GetDefinition(args[0]);
  if (cacheValue) {
31
    return true;
32
  }
33

34
  const char* temp = this->Makefile->GetDefinition("HOSTNAME");
35
  std::string hostname_cmd;
36
  if (temp) {
37
    hostname_cmd = temp;
38
  } else {
39
    hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
40
  }
41

42
  std::string siteName = "unknown";
Bill Hoffman's avatar
Bill Hoffman committed
43 44
#if defined(_WIN32) && !defined(__CYGWIN__)
  std::string host;
45 46 47 48
  if (cmSystemTools::ReadRegistryValue(
        "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\"
        "Control\\ComputerName\\ComputerName;ComputerName",
        host)) {
Bill Hoffman's avatar
Bill Hoffman committed
49
    siteName = host;
50
  }
Bill Hoffman's avatar
Bill Hoffman committed
51
#else
52
  // try to find the hostname for this computer
53
  if (!cmSystemTools::IsOff(hostname_cmd)) {
54
    std::string host;
55 56
    cmSystemTools::RunSingleCommand(hostname_cmd, &host, nullptr, nullptr,
                                    nullptr, cmSystemTools::OUTPUT_NONE);
57

58
    // got the hostname
59
    if (!host.empty()) {
60 61
      // remove any white space from the host name
      std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
62 63
      cmsys::RegularExpression hostReg(hostRegExp.c_str());
      if (hostReg.find(host.c_str())) {
64 65
        // strip whitespace
        host = hostReg.match(1);
66
      }
67

68
      if (!host.empty()) {
69
        siteName = host;
70 71
      }
    }
72
  }
Bill Hoffman's avatar
Bill Hoffman committed
73
#endif
74 75
  this->Makefile->AddCacheDefinition(
    args[0], siteName.c_str(),
76 77
    "Name of the computer/site where compile is being run",
    cmStateEnums::STRING);
78

79 80
  return true;
}