Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmCMakeMinimumRequired.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 "cmCMakeMinimumRequired.h"
13
14 #include "cmVersion.h"
15
16 // cmCMakeMinimumRequired
17 bool cmCMakeMinimumRequired
18 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
19 {
20   // Process arguments.
21   std::string version_string;
22   bool doing_version = false;
23   for(unsigned int i=0; i < args.size(); ++i)
24     {
25     if(args[i] == "VERSION")
26       {
27       doing_version = true;
28       }
29     else if(args[i] == "FATAL_ERROR")
30       {
31       if(doing_version)
32         {
33         this->SetError("called with no value for VERSION.");
34         return false;
35         }
36       doing_version = false;
37       }
38     else if(doing_version)
39       {
40       doing_version = false;
41       version_string = args[i];
42       }
43     else
44       {
45       this->UnknownArguments.push_back(args[i]);
46       }
47     }
48   if(doing_version)
49     {
50     this->SetError("called with no value for VERSION.");
51     return false;
52     }
53
54   // Make sure there was a version to check.
55   if(version_string.empty())
56     {
57     return this->EnforceUnknownArguments();
58     }
59
60   // Save the required version string.
61   this->Makefile->AddDefinition("CMAKE_MINIMUM_REQUIRED_VERSION",
62                                 version_string.c_str());
63
64
65   // Get the current version number.
66   int current_major = cmVersion::GetMajorVersion();
67   int current_minor = cmVersion::GetMinorVersion();
68   int current_patch = cmVersion::GetPatchVersion();
69   int current_tweak = cmVersion::GetTweakVersion();
70
71   // Parse at least two components of the version number.
72   // Use zero for those not specified.
73   int required_major = 0;
74   int required_minor = 0;
75   int required_patch = 0;
76   int required_tweak = 0;
77   if(sscanf(version_string.c_str(), "%u.%u.%u.%u",
78             &required_major, &required_minor,
79             &required_patch, &required_tweak) < 2)
80     {
81     cmOStringStream e;
82     e << "could not parse VERSION \"" << version_string.c_str() << "\".";
83     this->SetError(e.str().c_str());
84     return false;
85     }
86
87   // Compare the version numbers.
88   if((current_major < required_major) ||
89      (current_major == required_major &&
90       current_minor < required_minor) ||
91      (current_major == required_major &&
92       current_minor == required_minor &&
93       current_patch < required_patch) ||
94      (current_major == required_major &&
95       current_minor == required_minor &&
96       current_patch == required_patch &&
97       current_tweak < required_tweak))
98     {
99     // The current version is too low.
100     cmOStringStream e;
101     e << "CMake " << version_string.c_str()
102       << " or higher is required.  You are running version "
103       << cmVersion::GetCMakeVersion();
104     this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
105     cmSystemTools::SetFatalErrorOccured();
106     return true;
107     }
108
109   // The version is not from the future, so enforce unknown arguments.
110   if(!this->EnforceUnknownArguments())
111     {
112     return false;
113     }
114
115   if (required_major < 2 || (required_major == 2 && required_minor < 4))
116   {
117     this->Makefile->SetPolicyVersion("2.4");
118   }
119   else
120   {
121     this->Makefile->SetPolicyVersion(version_string.c_str());
122   }
123
124   return true;
125 }
126
127 //----------------------------------------------------------------------------
128 bool cmCMakeMinimumRequired::EnforceUnknownArguments()
129 {
130   if(!this->UnknownArguments.empty())
131     {
132     cmOStringStream e;
133     e << "called with unknown argument \""
134       << this->UnknownArguments[0] << "\".";
135     this->SetError(e.str().c_str());
136     return false;
137     }
138   return true;
139 }