cmCMakeHostSystemInformationCommand.cxx 7.4 KB

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