cmCMakeHostSystemInformationCommand.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "cmCMakeHostSystemInformationCommand.h"
  4. #include <sstream>
  5. #include "cmMakefile.h"
  6. #include "cmsys/SystemInformation.hxx"
  7. #if defined(_WIN32)
  8. #include "cmSystemTools.h"
  9. #include "cmVSSetupHelper.h"
  10. #define HAVE_VS_SETUP_HELPER
  11. #endif
  12. class cmExecutionStatus;
  13. // cmCMakeHostSystemInformation
  14. bool cmCMakeHostSystemInformationCommand::InitialPass(
  15. std::vector<std::string> const& args, cmExecutionStatus&)
  16. {
  17. size_t current_index = 0;
  18. if (args.size() < (current_index + 2) || args[current_index] != "RESULT") {
  19. this->SetError("missing RESULT specification.");
  20. return false;
  21. }
  22. std::string const& variable = args[current_index + 1];
  23. current_index += 2;
  24. if (args.size() < (current_index + 2) || args[current_index] != "QUERY") {
  25. this->SetError("missing QUERY specification");
  26. return false;
  27. }
  28. cmsys::SystemInformation info;
  29. info.RunCPUCheck();
  30. info.RunOSCheck();
  31. info.RunMemoryCheck();
  32. std::string result_list;
  33. for (size_t i = current_index + 1; i < args.size(); ++i) {
  34. std::string const& key = args[i];
  35. if (i != current_index + 1) {
  36. result_list += ";";
  37. }
  38. std::string value;
  39. if (!this->GetValue(info, key, value)) {
  40. return false;
  41. }
  42. result_list += value;
  43. }
  44. this->Makefile->AddDefinition(variable, result_list.c_str());
  45. return true;
  46. }
  47. bool cmCMakeHostSystemInformationCommand::GetValue(
  48. cmsys::SystemInformation& info, std::string const& key, std::string& value)
  49. {
  50. if (key == "NUMBER_OF_LOGICAL_CORES") {
  51. value = this->ValueToString(info.GetNumberOfLogicalCPU());
  52. } else if (key == "NUMBER_OF_PHYSICAL_CORES") {
  53. value = this->ValueToString(info.GetNumberOfPhysicalCPU());
  54. } else if (key == "HOSTNAME") {
  55. value = this->ValueToString(info.GetHostname());
  56. } else if (key == "FQDN") {
  57. value = this->ValueToString(info.GetFullyQualifiedDomainName());
  58. } else if (key == "TOTAL_VIRTUAL_MEMORY") {
  59. value = this->ValueToString(info.GetTotalVirtualMemory());
  60. } else if (key == "AVAILABLE_VIRTUAL_MEMORY") {
  61. value = this->ValueToString(info.GetAvailableVirtualMemory());
  62. } else if (key == "TOTAL_PHYSICAL_MEMORY") {
  63. value = this->ValueToString(info.GetTotalPhysicalMemory());
  64. } else if (key == "AVAILABLE_PHYSICAL_MEMORY") {
  65. value = this->ValueToString(info.GetAvailablePhysicalMemory());
  66. #ifdef HAVE_VS_SETUP_HELPER
  67. } else if (key == "VS_15_DIR") {
  68. cmVSSetupAPIHelper vsSetupAPIHelper;
  69. if (vsSetupAPIHelper.GetVSInstanceInfo(value)) {
  70. cmSystemTools::ConvertToUnixSlashes(value);
  71. }
  72. #endif
  73. } else {
  74. std::string e = "does not recognize <key> " + key;
  75. this->SetError(e);
  76. return false;
  77. }
  78. return true;
  79. }
  80. std::string cmCMakeHostSystemInformationCommand::ValueToString(
  81. size_t value) const
  82. {
  83. std::ostringstream tmp;
  84. tmp << value;
  85. return tmp.str();
  86. }
  87. std::string cmCMakeHostSystemInformationCommand::ValueToString(
  88. const char* value) const
  89. {
  90. std::string safe_string = value ? value : "";
  91. return safe_string;
  92. }
  93. std::string cmCMakeHostSystemInformationCommand::ValueToString(
  94. std::string const& value) const
  95. {
  96. return value;
  97. }