cmSiteNameCommand.cxx 2.6 KB
Newer Older
1 2 3
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4

5 6
  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.
7

8 9 10 11
  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
12 13
#include "cmSiteNameCommand.h"

14 15
#include <cmsys/RegularExpression.hxx>

16
// cmSiteNameCommand
17 18
bool cmSiteNameCommand::InitialPass(std::vector<std::string> const& args,
                                    cmExecutionStatus&)
19
{
20
  if (args.size() != 1) {
21 22
    this->SetError("called with incorrect number of arguments");
    return false;
23
  }
24 25 26 27 28 29 30
  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");
31

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

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

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

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

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

82 83
  return true;
}