cmake/Source/cmSiteNameCommand.cxx

82 lines
2.3 KiB
C++
Raw Normal View History

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. */
#include "cmSiteNameCommand.h"
2017-07-20 19:35:53 +02:00
#include "cmsys/RegularExpression.hxx"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
// cmSiteNameCommand
2016-07-09 11:21:54 +02:00
bool cmSiteNameCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
2016-07-09 11:21:54 +02:00
if (args.size() != 1) {
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
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");
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
const char* cacheValue = this->Makefile->GetDefinition(args[0]);
if (cacheValue) {
return true;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
const char* temp = this->Makefile->GetDefinition("HOSTNAME");
std::string hostname_cmd;
2016-07-09 11:21:54 +02:00
if (temp) {
hostname_cmd = temp;
2016-07-09 11:21:54 +02:00
} else {
hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +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)) {
siteName = host;
2016-07-09 11:21:54 +02:00
}
#else
// try to find the hostname for this computer
2018-10-28 12:09:07 +01:00
if (!cmSystemTools::IsOff(hostname_cmd)) {
std::string host;
2018-01-26 17:06:56 +01:00
cmSystemTools::RunSingleCommand(hostname_cmd.c_str(), &host, nullptr,
nullptr, nullptr,
2016-07-09 11:21:54 +02:00
cmSystemTools::OUTPUT_NONE);
2013-03-16 19:13:01 +02:00
// got the hostname
2016-07-09 11:21:54 +02:00
if (!host.empty()) {
// 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())) {
// strip whitespace
host = hostReg.match(1);
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (!host.empty()) {
siteName = host;
}
}
2016-07-09 11:21:54 +02:00
}
#endif
2016-07-09 11:21:54 +02:00
this->Makefile->AddCacheDefinition(
args[0], siteName.c_str(),
2017-04-14 19:02:05 +02:00
"Name of the computer/site where compile is being run",
cmStateEnums::STRING);
return true;
}