cmake/Source/cmCMakeHostSystemInformationCommand.cxx

115 lines
3.2 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. */
2013-11-03 12:27:13 +02:00
#include "cmCMakeHostSystemInformationCommand.h"
2017-04-14 19:02:05 +02:00
#include <sstream>
#include "cmMakefile.h"
#include "cmsys/SystemInformation.hxx"
#if defined(_WIN32)
#include "cmSystemTools.h"
#include "cmVSSetupHelper.h"
#define HAVE_VS_SETUP_HELPER
#endif
class cmExecutionStatus;
2013-11-03 12:27:13 +02:00
// cmCMakeHostSystemInformation
2016-07-09 11:21:54 +02:00
bool cmCMakeHostSystemInformationCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
2013-11-03 12:27:13 +02:00
{
size_t current_index = 0;
2016-07-09 11:21:54 +02:00
if (args.size() < (current_index + 2) || args[current_index] != "RESULT") {
2013-11-03 12:27:13 +02:00
this->SetError("missing RESULT specification.");
return false;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
2017-07-20 19:35:53 +02:00
std::string const& variable = args[current_index + 1];
2013-11-03 12:27:13 +02:00
current_index += 2;
2016-07-09 11:21:54 +02:00
if (args.size() < (current_index + 2) || args[current_index] != "QUERY") {
2013-11-03 12:27:13 +02:00
this->SetError("missing QUERY specification");
return false;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
cmsys::SystemInformation info;
info.RunCPUCheck();
info.RunOSCheck();
info.RunMemoryCheck();
std::string result_list;
2016-07-09 11:21:54 +02:00
for (size_t i = current_index + 1; i < args.size(); ++i) {
2017-07-20 19:35:53 +02:00
std::string const& key = args[i];
2016-07-09 11:21:54 +02:00
if (i != current_index + 1) {
2013-11-03 12:27:13 +02:00
result_list += ";";
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
std::string value;
2016-10-30 18:24:19 +01:00
if (!this->GetValue(info, key, value)) {
2016-07-09 11:21:54 +02:00
return false;
2016-10-30 18:24:19 +01:00
}
2013-11-03 12:27:13 +02:00
result_list += value;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(variable, result_list.c_str());
2013-11-03 12:27:13 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
bool cmCMakeHostSystemInformationCommand::GetValue(
cmsys::SystemInformation& info, std::string const& key, std::string& value)
2013-11-03 12:27:13 +02:00
{
2016-07-09 11:21:54 +02:00
if (key == "NUMBER_OF_LOGICAL_CORES") {
2013-11-03 12:27:13 +02:00
value = this->ValueToString(info.GetNumberOfLogicalCPU());
2016-07-09 11:21:54 +02:00
} else if (key == "NUMBER_OF_PHYSICAL_CORES") {
2013-11-03 12:27:13 +02:00
value = this->ValueToString(info.GetNumberOfPhysicalCPU());
2016-07-09 11:21:54 +02:00
} else if (key == "HOSTNAME") {
2013-11-03 12:27:13 +02:00
value = this->ValueToString(info.GetHostname());
2016-07-09 11:21:54 +02:00
} else if (key == "FQDN") {
2013-11-03 12:27:13 +02:00
value = this->ValueToString(info.GetFullyQualifiedDomainName());
2016-07-09 11:21:54 +02:00
} else if (key == "TOTAL_VIRTUAL_MEMORY") {
2013-11-03 12:27:13 +02:00
value = this->ValueToString(info.GetTotalVirtualMemory());
2016-07-09 11:21:54 +02:00
} else if (key == "AVAILABLE_VIRTUAL_MEMORY") {
2013-11-03 12:27:13 +02:00
value = this->ValueToString(info.GetAvailableVirtualMemory());
2016-07-09 11:21:54 +02:00
} else if (key == "TOTAL_PHYSICAL_MEMORY") {
2013-11-03 12:27:13 +02:00
value = this->ValueToString(info.GetTotalPhysicalMemory());
2016-07-09 11:21:54 +02:00
} else if (key == "AVAILABLE_PHYSICAL_MEMORY") {
2013-11-03 12:27:13 +02:00
value = this->ValueToString(info.GetAvailablePhysicalMemory());
2017-04-14 19:02:05 +02:00
#ifdef HAVE_VS_SETUP_HELPER
} else if (key == "VS_15_DIR") {
cmVSSetupAPIHelper vsSetupAPIHelper;
if (vsSetupAPIHelper.GetVSInstanceInfo(value)) {
cmSystemTools::ConvertToUnixSlashes(value);
}
#endif
2016-07-09 11:21:54 +02:00
} else {
2013-11-03 12:27:13 +02:00
std::string e = "does not recognize <key> " + key;
2015-04-27 22:25:09 +02:00
this->SetError(e);
2013-11-03 12:27:13 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
std::string cmCMakeHostSystemInformationCommand::ValueToString(
size_t value) const
2013-11-03 12:27:13 +02:00
{
2016-10-30 18:24:19 +01:00
std::ostringstream tmp;
2013-11-03 12:27:13 +02:00
tmp << value;
return tmp.str();
}
2016-07-09 11:21:54 +02:00
std::string cmCMakeHostSystemInformationCommand::ValueToString(
const char* value) const
2013-11-03 12:27:13 +02:00
{
std::string safe_string = value ? value : "";
return safe_string;
}
2016-07-09 11:21:54 +02:00
std::string cmCMakeHostSystemInformationCommand::ValueToString(
std::string const& value) const
2013-11-03 12:27:13 +02:00
{
return value;
}