cmCMakeHostSystemInformationCommand.cxx 6.8 KB

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