Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmCMakeHostSystemInformationCommand.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2013 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmCMakeHostSystemInformationCommand.h"
13
14 #include <cmsys/ios/sstream>
15
16 // cmCMakeHostSystemInformation
17 bool cmCMakeHostSystemInformationCommand
18 ::InitialPass(std::vector<std::string> const &args, cmExecutionStatus &)
19 {
20   size_t current_index = 0;
21
22   if(args.size() < (current_index + 2) || args[current_index] != "RESULT")
23     {
24     this->SetError("missing RESULT specification.");
25     return false;
26     }
27
28   std::string variable = args[current_index + 1];
29   current_index += 2;
30
31   if(args.size() < (current_index + 2) || args[current_index] != "QUERY")
32     {
33     this->SetError("missing QUERY specification");
34     return false;
35     }
36
37   cmsys::SystemInformation info;
38   info.RunCPUCheck();
39   info.RunOSCheck();
40   info.RunMemoryCheck();
41
42   std::string result_list;
43   for(size_t i = current_index + 1; i < args.size(); ++i)
44     {
45     std::string key = args[i];
46     if(i != current_index + 1)
47       {
48       result_list += ";";
49       }
50     std::string value;
51     if(!this->GetValue(info, key, value)) return false;
52
53     result_list += value;
54     }
55
56   this->Makefile->AddDefinition(variable.c_str(), result_list.c_str());
57
58   return true;
59 }
60
61 bool cmCMakeHostSystemInformationCommand
62 ::GetValue(cmsys::SystemInformation &info,
63            std::string const& key, std::string &value)
64 {
65   if(key == "NUMBER_OF_LOGICAL_CORES")
66     {
67     value = this->ValueToString(info.GetNumberOfLogicalCPU());
68     }
69   else if(key == "NUMBER_OF_PHYSICAL_CORES")
70     {
71     value = this->ValueToString(info.GetNumberOfPhysicalCPU());
72     }
73   else if(key == "HOSTNAME")
74     {
75     value = this->ValueToString(info.GetHostname());
76     }
77   else if(key == "FQDN")
78     {
79     value = this->ValueToString(info.GetFullyQualifiedDomainName());
80     }
81   else if(key == "TOTAL_VIRTUAL_MEMORY")
82     {
83     value = this->ValueToString(info.GetTotalVirtualMemory());
84     }
85   else if(key == "AVAILABLE_VIRTUAL_MEMORY")
86     {
87     value = this->ValueToString(info.GetAvailableVirtualMemory());
88     }
89   else if(key == "TOTAL_PHYSICAL_MEMORY")
90     {
91     value = this->ValueToString(info.GetTotalPhysicalMemory());
92     }
93   else if(key == "AVAILABLE_PHYSICAL_MEMORY")
94     {
95     value = this->ValueToString(info.GetAvailablePhysicalMemory());
96     }
97   else
98     {
99     std::string e = "does not recognize <key> " + key;
100     this->SetError(e.c_str());
101     return false;
102     }
103
104   return true;
105 }
106
107 std::string cmCMakeHostSystemInformationCommand
108 ::ValueToString(size_t value) const
109 {
110   cmsys_ios::stringstream tmp;
111   tmp << value;
112   return tmp.str();
113 }
114
115 std::string cmCMakeHostSystemInformationCommand
116 ::ValueToString(const char *value) const
117 {
118   std::string safe_string = value ? value : "";
119   return safe_string;
120 }
121
122 std::string cmCMakeHostSystemInformationCommand
123 ::ValueToString(std::string const& value) const
124 {
125   return value;
126 }