resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmSiteNameCommand.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmSiteNameCommand.h"
4
5 #include "cmsys/RegularExpression.hxx"
6
7 #include "cmExecutionStatus.h"
8 #include "cmMakefile.h"
9 #include "cmStateTypes.h"
10 #include "cmSystemTools.h"
11 #include "cmValue.h"
12
13 // cmSiteNameCommand
14 bool cmSiteNameCommand(std::vector<std::string> const& args,
15                        cmExecutionStatus& status)
16 {
17   if (args.size() != 1) {
18     status.SetError("called with incorrect number of arguments");
19     return false;
20   }
21   std::vector<std::string> paths;
22   paths.emplace_back("/usr/bsd");
23   paths.emplace_back("/usr/sbin");
24   paths.emplace_back("/usr/bin");
25   paths.emplace_back("/bin");
26   paths.emplace_back("/sbin");
27   paths.emplace_back("/usr/local/bin");
28
29   cmValue cacheValue = status.GetMakefile().GetDefinition(args[0]);
30   if (cacheValue) {
31     return true;
32   }
33
34   cmValue temp = status.GetMakefile().GetDefinition("HOSTNAME");
35   std::string hostname_cmd;
36   if (temp) {
37     hostname_cmd = *temp;
38   } else {
39     hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
40   }
41
42   std::string siteName = "unknown";
43 #if defined(_WIN32) && !defined(__CYGWIN__)
44   std::string host;
45   if (cmSystemTools::ReadRegistryValue(
46         "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\"
47         "Control\\ComputerName\\ComputerName;ComputerName",
48         host)) {
49     siteName = host;
50   }
51 #else
52   // try to find the hostname for this computer
53   if (!cmIsOff(hostname_cmd)) {
54     std::string host;
55     cmSystemTools::RunSingleCommand(hostname_cmd, &host, nullptr, nullptr,
56                                     nullptr, cmSystemTools::OUTPUT_NONE);
57
58     // got the hostname
59     if (!host.empty()) {
60       // remove any white space from the host name
61       std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
62       cmsys::RegularExpression hostReg(hostRegExp.c_str());
63       if (hostReg.find(host.c_str())) {
64         // strip whitespace
65         host = hostReg.match(1);
66       }
67
68       if (!host.empty()) {
69         siteName = host;
70       }
71     }
72   }
73 #endif
74   status.GetMakefile().AddCacheDefinition(
75     args[0], siteName, "Name of the computer/site where compile is being run",
76     cmStateEnums::STRING);
77
78   return true;
79 }