packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmSiteNameCommand.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmSiteNameCommand.h"
13
14 #include <cmsys/RegularExpression.hxx>
15
16 // cmSiteNameCommand
17 bool cmSiteNameCommand
18 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
19 {
20   if(args.size() != 1 )
21     {
22     this->SetError("called with incorrect number of arguments");
23     return false;
24     }
25   std::vector<std::string> paths;
26   paths.push_back("/usr/bsd");
27   paths.push_back("/usr/sbin");
28   paths.push_back("/usr/bin");
29   paths.push_back("/bin");
30   paths.push_back("/sbin");
31   paths.push_back("/usr/local/bin");
32
33   const char* cacheValue
34     = this->Makefile->GetDefinition(args[0].c_str());
35   if(cacheValue)
36     {
37     return true;
38     }
39
40   const char *temp = this->Makefile->GetDefinition("HOSTNAME");
41   std::string hostname_cmd;
42   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";
52 #if defined(_WIN32) && !defined(__CYGWIN__)
53   std::string host;
54   if(cmSystemTools::ReadRegistryValue
55      ("HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\"
56       "Control\\ComputerName\\ComputerName;ComputerName", host))
57     {
58     siteName = host;
59     }
60 #else
61   // try to find the hostname for this computer
62   if (!cmSystemTools::IsOff(hostname_cmd.c_str()))
63     {
64     std::string host;
65     cmSystemTools::RunSingleCommand(hostname_cmd.c_str(),
66       &host, 0, 0, cmSystemTools::OUTPUT_NONE);
67
68     // got the hostname
69     if (host.length())
70       {
71       // remove any white space from the host name
72       std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
73       cmsys::RegularExpression hostReg (hostRegExp.c_str());
74       if (hostReg.find(host.c_str()))
75         {
76         // strip whitespace
77         host = hostReg.match(1);
78         }
79
80       if(host.length())
81         {
82         siteName = host;
83         }
84       }
85     }
86 #endif
87   this->Makefile->
88     AddCacheDefinition(args[0].c_str(),
89                        siteName.c_str(),
90                        "Name of the computer/site where compile is being run",
91                        cmCacheManager::STRING);
92
93   return true;
94 }
95