cmCMakeHostSystemInformationCommand.cxx 3.2 KB

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