Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmCMakePolicyCommand.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 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 "cmCMakePolicyCommand.h"
13
14 #include "cmVersion.h"
15
16 // cmCMakePolicyCommand
17 bool cmCMakePolicyCommand
18 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
19 {
20   if(args.size() < 1)
21     {
22     this->SetError("requires at least one argument.");
23     return false;
24     }
25
26   if(args[0] == "SET")
27     {
28     return this->HandleSetMode(args);
29     }
30   else if(args[0] == "GET")
31     {
32     return this->HandleGetMode(args);
33     }
34   else if(args[0] == "PUSH")
35     {
36     if(args.size() > 1)
37       {
38       this->SetError("PUSH may not be given additional arguments.");
39       return false;
40       }
41     this->Makefile->PushPolicy();
42     return true;
43     }
44   else if(args[0] == "POP")
45     {
46     if(args.size() > 1)
47       {
48       this->SetError("POP may not be given additional arguments.");
49       return false;
50       }
51     this->Makefile->PopPolicy();
52     return true;
53     }
54   else if(args[0] == "VERSION")
55     {
56     return this->HandleVersionMode(args);
57     }
58
59   cmOStringStream e;
60   e << "given unknown first argument \"" << args[0] << "\"";
61   this->SetError(e.str().c_str());
62   return false;
63 }
64
65 //----------------------------------------------------------------------------
66 bool cmCMakePolicyCommand::HandleSetMode(std::vector<std::string> const& args)
67 {
68   if(args.size() != 3)
69     {
70     this->SetError("SET must be given exactly 2 additional arguments.");
71     return false;
72     }
73
74   cmPolicies::PolicyStatus status;
75   if(args[2] == "OLD")
76     {
77     status = cmPolicies::OLD;
78     }
79   else if(args[2] == "NEW")
80     {
81     status = cmPolicies::NEW;
82     }
83   else
84     {
85     cmOStringStream e;
86     e << "SET given unrecognized policy status \"" << args[2] << "\"";
87     this->SetError(e.str().c_str());
88     return false;
89     }
90
91   if(!this->Makefile->SetPolicy(args[1].c_str(), status))
92     {
93     this->SetError("SET failed to set policy.");
94     return false;
95     }
96   return true;
97 }
98
99 //----------------------------------------------------------------------------
100 bool cmCMakePolicyCommand::HandleGetMode(std::vector<std::string> const& args)
101 {
102   if(args.size() != 3)
103     {
104     this->SetError("GET must be given exactly 2 additional arguments.");
105     return false;
106     }
107
108   // Get arguments.
109   std::string const& id = args[1];
110   std::string const& var = args[2];
111
112   // Lookup the policy number.
113   cmPolicies::PolicyID pid;
114   if(!this->Makefile->GetPolicies()->GetPolicyID(id.c_str(), pid))
115     {
116     cmOStringStream e;
117     e << "GET given policy \"" << id << "\" which is not known to this "
118       << "version of CMake.";
119     this->SetError(e.str().c_str());
120     return false;
121     }
122
123   // Lookup the policy setting.
124   cmPolicies::PolicyStatus status = this->Makefile->GetPolicyStatus(pid);
125   switch (status)
126     {
127     case cmPolicies::OLD:
128       // Report that the policy is set to OLD.
129       this->Makefile->AddDefinition(var.c_str(), "OLD");
130       break;
131     case cmPolicies::WARN:
132       // Report that the policy is not set.
133       this->Makefile->AddDefinition(var.c_str(), "");
134       break;
135     case cmPolicies::NEW:
136       // Report that the policy is set to NEW.
137       this->Makefile->AddDefinition(var.c_str(), "NEW");
138       break;
139     case cmPolicies::REQUIRED_IF_USED:
140     case cmPolicies::REQUIRED_ALWAYS:
141       // The policy is required to be set before anything needs it.
142       {
143       cmOStringStream e;
144       e << this->Makefile->GetPolicies()->GetRequiredPolicyError(pid)
145         << "\n"
146         << "The call to cmake_policy(GET " << id << " ...) at which this "
147         << "error appears requests the policy, and this version of CMake "
148         << "requires that the policy be set to NEW before it is checked.";
149       this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
150       }
151     }
152
153   return true;
154 }
155
156 //----------------------------------------------------------------------------
157 bool
158 cmCMakePolicyCommand::HandleVersionMode(std::vector<std::string> const& args)
159 {
160   if(args.size() <= 1)
161     {
162     this->SetError("VERSION not given an argument");
163     return false;
164     }
165   else if(args.size() >= 3)
166     {
167     this->SetError("VERSION given too many arguments");
168     return false;
169     }
170   this->Makefile->SetPolicyVersion(args[1].c_str());
171   return true;
172 }