cmSiteNameCommand.cxx 2.22 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 6
#include "cmSystemTools.h"

7 8
#include <cmsys/RegularExpression.hxx>

9
// cmSiteNameCommand
10 11
bool cmSiteNameCommand::InitialPass(std::vector<std::string> const& args,
                                    cmExecutionStatus&)
12
{
13
  if (args.size() != 1) {
14 15
    this->SetError("called with incorrect number of arguments");
    return false;
16
  }
17 18 19 20 21 22 23
  std::vector<std::string> paths;
  paths.push_back("/usr/bsd");
  paths.push_back("/usr/sbin");
  paths.push_back("/usr/bin");
  paths.push_back("/bin");
  paths.push_back("/sbin");
  paths.push_back("/usr/local/bin");
24

25 26
  const char* cacheValue = this->Makefile->GetDefinition(args[0]);
  if (cacheValue) {
27
    return true;
28
  }
29

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

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

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

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

76 77
  return true;
}