cmSiteNameCommand.cxx 2.63 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 23 24
    {
    this->SetError("called with incorrect number of arguments");
    return false;
    }
25 26 27 28 29 30 31 32
  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");
  
33
  const char* cacheValue
34
    = this->Makefile->GetDefinition(args[0].c_str());
35 36 37 38 39
  if(cacheValue)
    {
    return true;
    }
  
40
  const char *temp = this->Makefile->GetDefinition("HOSTNAME");
41 42
  std::string hostname_cmd;
  if(temp)
43
    {
44
    hostname_cmd = temp;
45
    }
46
  else 
47
    {
48
    hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
49
    }
50 51
  
  std::string siteName = "unknown";
Bill Hoffman's avatar
Bill Hoffman committed
52 53
#if defined(_WIN32) && !defined(__CYGWIN__)
  std::string host;
Ken Martin's avatar
Ken Martin committed
54 55 56
  if(cmSystemTools::ReadRegistryValue
     ("HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\"
      "Control\\ComputerName\\ComputerName;ComputerName", host))
Bill Hoffman's avatar
Bill Hoffman committed
57 58 59 60
    {
    siteName = host;
    }
#else
61
  // try to find the hostname for this computer
Bill Hoffman's avatar
Bill Hoffman committed
62
  if (!cmSystemTools::IsOff(hostname_cmd.c_str()))
63
    {
64
    std::string host;
Andy Cedilnik's avatar
Andy Cedilnik committed
65
    cmSystemTools::RunSingleCommand(hostname_cmd.c_str(),
66
      &host, 0, 0, cmSystemTools::OUTPUT_NONE);
67 68 69
    
    // got the hostname
    if (host.length())
70
      {
71 72
      // remove any white space from the host name
      std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
73
      cmsys::RegularExpression hostReg (hostRegExp.c_str());
74 75 76 77 78
      if (hostReg.find(host.c_str()))
        {
        // strip whitespace
        host = hostReg.match(1);
        }
79

80 81 82 83
      if(host.length())
        {
        siteName = host;
        }
84 85
      }
    }
Bill Hoffman's avatar
Bill Hoffman committed
86
#endif
87
  this->Makefile->
88
    AddCacheDefinition(args[0].c_str(),
89 90 91
                       siteName.c_str(),
                       "Name of the computer/site where compile is being run",
                       cmCacheManager::STRING);
92

93 94 95
  return true;
}