cmSiteNameCommand.cxx 2.28 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
#include "cmStringAlgorithms.h"
10 11
#include "cmSystemTools.h"

12
class cmExecutionStatus;
13

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

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

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

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

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

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

80 81
  return true;
}