cmCMakeHostSystemInformationCommand.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2013 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCMakeHostSystemInformationCommand.h"
  11. // cmCMakeHostSystemInformation
  12. bool cmCMakeHostSystemInformationCommand::InitialPass(
  13. std::vector<std::string> const& args, cmExecutionStatus&)
  14. {
  15. size_t current_index = 0;
  16. if (args.size() < (current_index + 2) || args[current_index] != "RESULT") {
  17. this->SetError("missing RESULT specification.");
  18. return false;
  19. }
  20. std::string variable = args[current_index + 1];
  21. current_index += 2;
  22. if (args.size() < (current_index + 2) || args[current_index] != "QUERY") {
  23. this->SetError("missing QUERY specification");
  24. return false;
  25. }
  26. cmsys::SystemInformation info;
  27. info.RunCPUCheck();
  28. info.RunOSCheck();
  29. info.RunMemoryCheck();
  30. std::string result_list;
  31. for (size_t i = current_index + 1; i < args.size(); ++i) {
  32. std::string key = args[i];
  33. if (i != current_index + 1) {
  34. result_list += ";";
  35. }
  36. std::string value;
  37. if (!this->GetValue(info, key, value)) {
  38. return false;
  39. }
  40. result_list += value;
  41. }
  42. this->Makefile->AddDefinition(variable, result_list.c_str());
  43. return true;
  44. }
  45. bool cmCMakeHostSystemInformationCommand::GetValue(
  46. cmsys::SystemInformation& info, std::string const& key, std::string& value)
  47. {
  48. if (key == "NUMBER_OF_LOGICAL_CORES") {
  49. value = this->ValueToString(info.GetNumberOfLogicalCPU());
  50. } else if (key == "NUMBER_OF_PHYSICAL_CORES") {
  51. value = this->ValueToString(info.GetNumberOfPhysicalCPU());
  52. } else if (key == "HOSTNAME") {
  53. value = this->ValueToString(info.GetHostname());
  54. } else if (key == "FQDN") {
  55. value = this->ValueToString(info.GetFullyQualifiedDomainName());
  56. } else if (key == "TOTAL_VIRTUAL_MEMORY") {
  57. value = this->ValueToString(info.GetTotalVirtualMemory());
  58. } else if (key == "AVAILABLE_VIRTUAL_MEMORY") {
  59. value = this->ValueToString(info.GetAvailableVirtualMemory());
  60. } else if (key == "TOTAL_PHYSICAL_MEMORY") {
  61. value = this->ValueToString(info.GetTotalPhysicalMemory());
  62. } else if (key == "AVAILABLE_PHYSICAL_MEMORY") {
  63. value = this->ValueToString(info.GetAvailablePhysicalMemory());
  64. } else {
  65. std::string e = "does not recognize <key> " + key;
  66. this->SetError(e);
  67. return false;
  68. }
  69. return true;
  70. }
  71. std::string cmCMakeHostSystemInformationCommand::ValueToString(
  72. size_t value) const
  73. {
  74. std::ostringstream tmp;
  75. tmp << value;
  76. return tmp.str();
  77. }
  78. std::string cmCMakeHostSystemInformationCommand::ValueToString(
  79. const char* value) const
  80. {
  81. std::string safe_string = value ? value : "";
  82. return safe_string;
  83. }
  84. std::string cmCMakeHostSystemInformationCommand::ValueToString(
  85. std::string const& value) const
  86. {
  87. return value;
  88. }