cmCMakeHostSystemInformationCommand.cxx 3.3 KB

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