cmake/Source/cmCMakeHostSystemInformationCommand.cxx

200 lines
6.8 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"
2020-02-01 23:06:01 +01:00
#include <cstddef>
2017-04-14 19:02:05 +02:00
#include "cmsys/SystemInformation.hxx"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
2017-04-14 19:02:05 +02:00
#if defined(_WIN32)
2018-08-09 18:06:22 +02:00
# include "cmAlgorithms.h"
# include "cmGlobalGenerator.h"
2019-11-11 23:01:05 +01:00
# include "cmGlobalVisualStudioVersionedGenerator.h"
2018-08-09 18:06:22 +02:00
# include "cmSystemTools.h"
# include "cmVSSetupHelper.h"
# define HAVE_VS_SETUP_HELPER
2017-04-14 19:02:05 +02:00
#endif
2020-02-01 23:06:01 +01:00
namespace {
bool GetValue(cmExecutionStatus& status, cmsys::SystemInformation& info,
std::string const& key, std::string& value);
std::string ValueToString(size_t value);
std::string ValueToString(const char* value);
std::string ValueToString(std::string const& value);
}
2017-04-14 19:02:05 +02:00
2013-11-03 12:27:13 +02:00
// cmCMakeHostSystemInformation
2020-02-01 23:06:01 +01:00
bool cmCMakeHostSystemInformationCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
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") {
2020-02-01 23:06:01 +01:00
status.SetError("missing RESULT specification.");
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
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") {
2020-02-01 23:06:01 +01:00
status.SetError("missing QUERY specification");
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
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;
2020-02-01 23:06:01 +01:00
if (!GetValue(status, 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
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(variable, result_list);
2013-11-03 12:27:13 +02:00
return true;
}
2020-02-01 23:06:01 +01:00
namespace {
bool GetValue(cmExecutionStatus& status, 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") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetNumberOfLogicalCPU());
2016-07-09 11:21:54 +02:00
} else if (key == "NUMBER_OF_PHYSICAL_CORES") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetNumberOfPhysicalCPU());
2016-07-09 11:21:54 +02:00
} else if (key == "HOSTNAME") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetHostname());
2016-07-09 11:21:54 +02:00
} else if (key == "FQDN") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetFullyQualifiedDomainName());
2016-07-09 11:21:54 +02:00
} else if (key == "TOTAL_VIRTUAL_MEMORY") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetTotalVirtualMemory());
2016-07-09 11:21:54 +02:00
} else if (key == "AVAILABLE_VIRTUAL_MEMORY") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetAvailableVirtualMemory());
2016-07-09 11:21:54 +02:00
} else if (key == "TOTAL_PHYSICAL_MEMORY") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetTotalPhysicalMemory());
2016-07-09 11:21:54 +02:00
} else if (key == "AVAILABLE_PHYSICAL_MEMORY") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetAvailablePhysicalMemory());
2018-01-26 17:06:56 +01:00
} else if (key == "IS_64BIT") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.Is64Bits());
2018-01-26 17:06:56 +01:00
} else if (key == "HAS_FPU") {
2020-02-01 23:06:01 +01:00
value = ValueToString(
2018-01-26 17:06:56 +01:00
info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_FPU));
} else if (key == "HAS_MMX") {
2020-02-01 23:06:01 +01:00
value = ValueToString(
2018-01-26 17:06:56 +01:00
info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_MMX));
} else if (key == "HAS_MMX_PLUS") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.DoesCPUSupportFeature(
2018-01-26 17:06:56 +01:00
cmsys::SystemInformation::CPU_FEATURE_MMX_PLUS));
} else if (key == "HAS_SSE") {
2020-02-01 23:06:01 +01:00
value = ValueToString(
2018-01-26 17:06:56 +01:00
info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_SSE));
} else if (key == "HAS_SSE2") {
2020-02-01 23:06:01 +01:00
value = ValueToString(
2018-01-26 17:06:56 +01:00
info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_SSE2));
} else if (key == "HAS_SSE_FP") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.DoesCPUSupportFeature(
2018-01-26 17:06:56 +01:00
cmsys::SystemInformation::CPU_FEATURE_SSE_FP));
} else if (key == "HAS_SSE_MMX") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.DoesCPUSupportFeature(
2018-01-26 17:06:56 +01:00
cmsys::SystemInformation::CPU_FEATURE_SSE_MMX));
} else if (key == "HAS_AMD_3DNOW") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.DoesCPUSupportFeature(
2018-01-26 17:06:56 +01:00
cmsys::SystemInformation::CPU_FEATURE_AMD_3DNOW));
} else if (key == "HAS_AMD_3DNOW_PLUS") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.DoesCPUSupportFeature(
2018-01-26 17:06:56 +01:00
cmsys::SystemInformation::CPU_FEATURE_AMD_3DNOW_PLUS));
} else if (key == "HAS_IA64") {
2020-02-01 23:06:01 +01:00
value = ValueToString(
2018-01-26 17:06:56 +01:00
info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_IA64));
} else if (key == "HAS_SERIAL_NUMBER") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.DoesCPUSupportFeature(
2018-01-26 17:06:56 +01:00
cmsys::SystemInformation::CPU_FEATURE_SERIALNUMBER));
} else if (key == "PROCESSOR_NAME") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetExtendedProcessorName());
2018-01-26 17:06:56 +01:00
} else if (key == "PROCESSOR_DESCRIPTION") {
value = info.GetCPUDescription();
} else if (key == "PROCESSOR_SERIAL_NUMBER") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetProcessorSerialNumber());
2018-01-26 17:06:56 +01:00
} else if (key == "OS_NAME") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetOSName());
2018-01-26 17:06:56 +01:00
} else if (key == "OS_RELEASE") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetOSRelease());
2018-01-26 17:06:56 +01:00
} else if (key == "OS_VERSION") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetOSVersion());
2018-01-26 17:06:56 +01:00
} else if (key == "OS_PLATFORM") {
2020-02-01 23:06:01 +01:00
value = ValueToString(info.GetOSPlatform());
2017-04-14 19:02:05 +02:00
#ifdef HAVE_VS_SETUP_HELPER
} else if (key == "VS_15_DIR") {
2018-04-23 21:13:27 +02:00
// If generating for the VS 15 IDE, use the same instance.
2020-02-01 23:06:01 +01:00
cmGlobalGenerator* gg = status.GetMakefile().GetGlobalGenerator();
2018-04-23 21:13:27 +02:00
if (cmHasLiteralPrefix(gg->GetName(), "Visual Studio 15 ")) {
2019-11-11 23:01:05 +01:00
cmGlobalVisualStudioVersionedGenerator* vs15gen =
static_cast<cmGlobalVisualStudioVersionedGenerator*>(gg);
2018-04-23 21:13:27 +02:00
if (vs15gen->GetVSInstance(value)) {
return true;
}
}
// Otherwise, find a VS 15 instance ourselves.
2019-11-11 23:01:05 +01:00
cmVSSetupAPIHelper vsSetupAPIHelper(15);
if (vsSetupAPIHelper.GetVSInstanceInfo(value)) {
cmSystemTools::ConvertToUnixSlashes(value);
}
} else if (key == "VS_16_DIR") {
// If generating for the VS 16 IDE, use the same instance.
2020-02-01 23:06:01 +01:00
cmGlobalGenerator* gg = status.GetMakefile().GetGlobalGenerator();
2019-11-11 23:01:05 +01:00
if (cmHasLiteralPrefix(gg->GetName(), "Visual Studio 16 ")) {
cmGlobalVisualStudioVersionedGenerator* vs16gen =
static_cast<cmGlobalVisualStudioVersionedGenerator*>(gg);
if (vs16gen->GetVSInstance(value)) {
return true;
}
}
// Otherwise, find a VS 16 instance ourselves.
cmVSSetupAPIHelper vsSetupAPIHelper(16);
2017-04-14 19:02:05 +02:00
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;
2020-02-01 23:06:01 +01:00
status.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;
}
2020-02-01 23:06:01 +01:00
std::string ValueToString(size_t value)
2013-11-03 12:27:13 +02:00
{
2020-02-01 23:06:01 +01:00
return std::to_string(value);
2013-11-03 12:27:13 +02:00
}
2020-02-01 23:06:01 +01:00
std::string ValueToString(const char* value)
2013-11-03 12:27:13 +02:00
{
std::string safe_string = value ? value : "";
return safe_string;
}
2020-02-01 23:06:01 +01:00
std::string ValueToString(std::string const& value)
2013-11-03 12:27:13 +02:00
{
return value;
}
2020-02-01 23:06:01 +01:00
}