cmCMakeHostSystemInformationCommand.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "cmAlgorithms.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmGlobalVisualStudio15Generator.h"
  11. #include "cmSystemTools.h"
  12. #include "cmVSSetupHelper.h"
  13. #define HAVE_VS_SETUP_HELPER
  14. #endif
  15. class cmExecutionStatus;
  16. // cmCMakeHostSystemInformation
  17. bool cmCMakeHostSystemInformationCommand::InitialPass(
  18. std::vector<std::string> const& args, cmExecutionStatus&)
  19. {
  20. size_t current_index = 0;
  21. if (args.size() < (current_index + 2) || args[current_index] != "RESULT") {
  22. this->SetError("missing RESULT specification.");
  23. return false;
  24. }
  25. std::string const& variable = args[current_index + 1];
  26. current_index += 2;
  27. if (args.size() < (current_index + 2) || args[current_index] != "QUERY") {
  28. this->SetError("missing QUERY specification");
  29. return false;
  30. }
  31. cmsys::SystemInformation info;
  32. info.RunCPUCheck();
  33. info.RunOSCheck();
  34. info.RunMemoryCheck();
  35. std::string result_list;
  36. for (size_t i = current_index + 1; i < args.size(); ++i) {
  37. std::string const& key = args[i];
  38. if (i != current_index + 1) {
  39. result_list += ";";
  40. }
  41. std::string value;
  42. if (!this->GetValue(info, key, value)) {
  43. return false;
  44. }
  45. result_list += value;
  46. }
  47. this->Makefile->AddDefinition(variable, result_list.c_str());
  48. return true;
  49. }
  50. bool cmCMakeHostSystemInformationCommand::GetValue(
  51. cmsys::SystemInformation& info, std::string const& key, std::string& value)
  52. {
  53. if (key == "NUMBER_OF_LOGICAL_CORES") {
  54. value = this->ValueToString(info.GetNumberOfLogicalCPU());
  55. } else if (key == "NUMBER_OF_PHYSICAL_CORES") {
  56. value = this->ValueToString(info.GetNumberOfPhysicalCPU());
  57. } else if (key == "HOSTNAME") {
  58. value = this->ValueToString(info.GetHostname());
  59. } else if (key == "FQDN") {
  60. value = this->ValueToString(info.GetFullyQualifiedDomainName());
  61. } else if (key == "TOTAL_VIRTUAL_MEMORY") {
  62. value = this->ValueToString(info.GetTotalVirtualMemory());
  63. } else if (key == "AVAILABLE_VIRTUAL_MEMORY") {
  64. value = this->ValueToString(info.GetAvailableVirtualMemory());
  65. } else if (key == "TOTAL_PHYSICAL_MEMORY") {
  66. value = this->ValueToString(info.GetTotalPhysicalMemory());
  67. } else if (key == "AVAILABLE_PHYSICAL_MEMORY") {
  68. value = this->ValueToString(info.GetAvailablePhysicalMemory());
  69. } else if (key == "IS_64BIT") {
  70. value = this->ValueToString(info.Is64Bits());
  71. } else if (key == "HAS_FPU") {
  72. value = this->ValueToString(
  73. info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_FPU));
  74. } else if (key == "HAS_MMX") {
  75. value = this->ValueToString(
  76. info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_MMX));
  77. } else if (key == "HAS_MMX_PLUS") {
  78. value = this->ValueToString(info.DoesCPUSupportFeature(
  79. cmsys::SystemInformation::CPU_FEATURE_MMX_PLUS));
  80. } else if (key == "HAS_SSE") {
  81. value = this->ValueToString(
  82. info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_SSE));
  83. } else if (key == "HAS_SSE2") {
  84. value = this->ValueToString(
  85. info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_SSE2));
  86. } else if (key == "HAS_SSE_FP") {
  87. value = this->ValueToString(info.DoesCPUSupportFeature(
  88. cmsys::SystemInformation::CPU_FEATURE_SSE_FP));
  89. } else if (key == "HAS_SSE_MMX") {
  90. value = this->ValueToString(info.DoesCPUSupportFeature(
  91. cmsys::SystemInformation::CPU_FEATURE_SSE_MMX));
  92. } else if (key == "HAS_AMD_3DNOW") {
  93. value = this->ValueToString(info.DoesCPUSupportFeature(
  94. cmsys::SystemInformation::CPU_FEATURE_AMD_3DNOW));
  95. } else if (key == "HAS_AMD_3DNOW_PLUS") {
  96. value = this->ValueToString(info.DoesCPUSupportFeature(
  97. cmsys::SystemInformation::CPU_FEATURE_AMD_3DNOW_PLUS));
  98. } else if (key == "HAS_IA64") {
  99. value = this->ValueToString(
  100. info.DoesCPUSupportFeature(cmsys::SystemInformation::CPU_FEATURE_IA64));
  101. } else if (key == "HAS_SERIAL_NUMBER") {
  102. value = this->ValueToString(info.DoesCPUSupportFeature(
  103. cmsys::SystemInformation::CPU_FEATURE_SERIALNUMBER));
  104. } else if (key == "PROCESSOR_NAME") {
  105. value = this->ValueToString(info.GetExtendedProcessorName());
  106. } else if (key == "PROCESSOR_DESCRIPTION") {
  107. value = info.GetCPUDescription();
  108. } else if (key == "PROCESSOR_SERIAL_NUMBER") {
  109. value = this->ValueToString(info.GetProcessorSerialNumber());
  110. } else if (key == "OS_NAME") {
  111. value = this->ValueToString(info.GetOSName());
  112. } else if (key == "OS_RELEASE") {
  113. value = this->ValueToString(info.GetOSRelease());
  114. } else if (key == "OS_VERSION") {
  115. value = this->ValueToString(info.GetOSVersion());
  116. } else if (key == "OS_PLATFORM") {
  117. value = this->ValueToString(info.GetOSPlatform());
  118. #ifdef HAVE_VS_SETUP_HELPER
  119. } else if (key == "VS_15_DIR") {
  120. // If generating for the VS 15 IDE, use the same instance.
  121. cmGlobalGenerator* gg = this->Makefile->GetGlobalGenerator();
  122. if (cmHasLiteralPrefix(gg->GetName(), "Visual Studio 15 ")) {
  123. cmGlobalVisualStudio15Generator* vs15gen =
  124. static_cast<cmGlobalVisualStudio15Generator*>(gg);
  125. if (vs15gen->GetVSInstance(value)) {
  126. return true;
  127. }
  128. }
  129. // Otherwise, find a VS 15 instance ourselves.
  130. cmVSSetupAPIHelper vsSetupAPIHelper;
  131. if (vsSetupAPIHelper.GetVSInstanceInfo(value)) {
  132. cmSystemTools::ConvertToUnixSlashes(value);
  133. }
  134. #endif
  135. } else {
  136. std::string e = "does not recognize <key> " + key;
  137. this->SetError(e);
  138. return false;
  139. }
  140. return true;
  141. }
  142. std::string cmCMakeHostSystemInformationCommand::ValueToString(
  143. size_t value) const
  144. {
  145. std::ostringstream tmp;
  146. tmp << value;
  147. return tmp.str();
  148. }
  149. std::string cmCMakeHostSystemInformationCommand::ValueToString(
  150. const char* value) const
  151. {
  152. std::string safe_string = value ? value : "";
  153. return safe_string;
  154. }
  155. std::string cmCMakeHostSystemInformationCommand::ValueToString(
  156. std::string const& value) const
  157. {
  158. return value;
  159. }