2016-10-30 18:24:19 +01:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2008-10-12 18:41:06 +02:00
|
|
|
#include "cmSiteNameCommand.h"
|
|
|
|
|
2017-07-20 19:35:53 +02:00
|
|
|
#include "cmsys/RegularExpression.hxx"
|
2008-10-12 18:41:06 +02:00
|
|
|
|
2020-02-01 23:06:01 +01:00
|
|
|
#include "cmExecutionStatus.h"
|
2017-04-14 19:02:05 +02:00
|
|
|
#include "cmMakefile.h"
|
|
|
|
#include "cmStateTypes.h"
|
|
|
|
#include "cmSystemTools.h"
|
2021-11-20 13:41:27 +01:00
|
|
|
#include "cmValue.h"
|
2017-04-14 19:02:05 +02:00
|
|
|
|
2008-10-12 18:41:06 +02:00
|
|
|
// cmSiteNameCommand
|
2020-02-01 23:06:01 +01:00
|
|
|
bool cmSiteNameCommand(std::vector<std::string> const& args,
|
|
|
|
cmExecutionStatus& status)
|
2008-10-12 18:41:06 +02:00
|
|
|
{
|
2016-07-09 11:21:54 +02:00
|
|
|
if (args.size() != 1) {
|
2020-02-01 23:06:01 +01:00
|
|
|
status.SetError("called with incorrect number of arguments");
|
2008-10-12 18:41:06 +02:00
|
|
|
return false;
|
2016-07-09 11:21:54 +02:00
|
|
|
}
|
2008-10-12 18:41:06 +02:00
|
|
|
std::vector<std::string> paths;
|
2019-11-11 23:01:05 +01:00
|
|
|
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");
|
2013-03-16 19:13:01 +02:00
|
|
|
|
2021-11-20 13:41:27 +01:00
|
|
|
cmValue cacheValue = status.GetMakefile().GetDefinition(args[0]);
|
2016-07-09 11:21:54 +02:00
|
|
|
if (cacheValue) {
|
2008-10-12 18:41:06 +02:00
|
|
|
return true;
|
2016-07-09 11:21:54 +02:00
|
|
|
}
|
2013-03-16 19:13:01 +02:00
|
|
|
|
2021-11-20 13:41:27 +01:00
|
|
|
cmValue temp = status.GetMakefile().GetDefinition("HOSTNAME");
|
2008-10-12 18:41:06 +02:00
|
|
|
std::string hostname_cmd;
|
2016-07-09 11:21:54 +02:00
|
|
|
if (temp) {
|
2021-09-14 00:13:48 +02:00
|
|
|
hostname_cmd = *temp;
|
2016-07-09 11:21:54 +02:00
|
|
|
} else {
|
2008-10-12 18:41:06 +02:00
|
|
|
hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
|
2016-07-09 11:21:54 +02:00
|
|
|
}
|
2013-03-16 19:13:01 +02:00
|
|
|
|
2008-10-12 18:41:06 +02:00
|
|
|
std::string siteName = "unknown";
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
std::string host;
|
2016-07-09 11:21:54 +02:00
|
|
|
if (cmSystemTools::ReadRegistryValue(
|
|
|
|
"HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\"
|
|
|
|
"Control\\ComputerName\\ComputerName;ComputerName",
|
|
|
|
host)) {
|
2008-10-12 18:41:06 +02:00
|
|
|
siteName = host;
|
2016-07-09 11:21:54 +02:00
|
|
|
}
|
2008-10-12 18:41:06 +02:00
|
|
|
#else
|
|
|
|
// try to find the hostname for this computer
|
2020-02-01 23:06:01 +01:00
|
|
|
if (!cmIsOff(hostname_cmd)) {
|
2008-10-12 18:41:06 +02:00
|
|
|
std::string host;
|
2019-11-11 23:01:05 +01:00
|
|
|
cmSystemTools::RunSingleCommand(hostname_cmd, &host, nullptr, nullptr,
|
|
|
|
nullptr, cmSystemTools::OUTPUT_NONE);
|
2013-03-16 19:13:01 +02:00
|
|
|
|
2008-10-12 18:41:06 +02:00
|
|
|
// got the hostname
|
2016-07-09 11:21:54 +02:00
|
|
|
if (!host.empty()) {
|
2008-10-12 18:41:06 +02:00
|
|
|
// remove any white space from the host name
|
|
|
|
std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
|
2016-07-09 11:21:54 +02:00
|
|
|
cmsys::RegularExpression hostReg(hostRegExp.c_str());
|
|
|
|
if (hostReg.find(host.c_str())) {
|
2008-10-12 18:41:06 +02:00
|
|
|
// strip whitespace
|
|
|
|
host = hostReg.match(1);
|
2016-07-09 11:21:54 +02:00
|
|
|
}
|
2008-10-12 18:41:06 +02:00
|
|
|
|
2016-07-09 11:21:54 +02:00
|
|
|
if (!host.empty()) {
|
2008-10-12 18:41:06 +02:00
|
|
|
siteName = host;
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 11:21:54 +02:00
|
|
|
}
|
2008-10-12 18:41:06 +02:00
|
|
|
#endif
|
2020-02-01 23:06:01 +01:00
|
|
|
status.GetMakefile().AddCacheDefinition(
|
2020-08-30 11:54:41 +02:00
|
|
|
args[0], siteName, "Name of the computer/site where compile is being run",
|
2017-04-14 19:02:05 +02:00
|
|
|
cmStateEnums::STRING);
|
2008-10-12 18:41:06 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|