packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmScriptGenerator.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 "cmScriptGenerator.h"
13
14 #include "cmSystemTools.h"
15
16 //----------------------------------------------------------------------------
17 cmScriptGenerator
18 ::cmScriptGenerator(const char* config_var,
19                     std::vector<std::string> const& configurations):
20   RuntimeConfigVariable(config_var),
21   Configurations(configurations),
22   ConfigurationName(0),
23   ConfigurationTypes(0),
24   ActionsPerConfig(false)
25 {
26 }
27
28 //----------------------------------------------------------------------------
29 cmScriptGenerator
30 ::~cmScriptGenerator()
31 {
32 }
33
34 //----------------------------------------------------------------------------
35 void
36 cmScriptGenerator
37 ::Generate(std::ostream& os, const char* config,
38            std::vector<std::string> const& configurationTypes)
39 {
40   this->ConfigurationName = config;
41   this->ConfigurationTypes = &configurationTypes;
42   this->GenerateScript(os);
43   this->ConfigurationName = 0;
44   this->ConfigurationTypes = 0;
45 }
46
47 //----------------------------------------------------------------------------
48 static void cmScriptGeneratorEncodeConfig(const char* config,
49                                           std::string& result)
50 {
51   for(const char* c = config; *c; ++c)
52     {
53     if(*c >= 'a' && *c <= 'z')
54       {
55       result += "[";
56       result += static_cast<char>(*c + 'A' - 'a');
57       result += *c;
58       result += "]";
59       }
60     else if(*c >= 'A' && *c <= 'Z')
61       {
62       result += "[";
63       result += *c;
64       result += static_cast<char>(*c + 'a' - 'A');
65       result += "]";
66       }
67     else
68       {
69       result += *c;
70       }
71     }
72 }
73
74 //----------------------------------------------------------------------------
75 std::string
76 cmScriptGenerator::CreateConfigTest(const char* config)
77 {
78   std::string result = "\"${";
79   result += this->RuntimeConfigVariable;
80   result += "}\" MATCHES \"^(";
81   if(config && *config)
82     {
83     cmScriptGeneratorEncodeConfig(config, result);
84     }
85   result += ")$\"";
86   return result;
87 }
88
89 //----------------------------------------------------------------------------
90 std::string
91 cmScriptGenerator::CreateConfigTest(std::vector<std::string> const& configs)
92 {
93   std::string result = "\"${";
94   result += this->RuntimeConfigVariable;
95   result += "}\" MATCHES \"^(";
96   const char* sep = "";
97   for(std::vector<std::string>::const_iterator ci = configs.begin();
98       ci != configs.end(); ++ci)
99     {
100     result += sep;
101     sep = "|";
102     cmScriptGeneratorEncodeConfig(ci->c_str(), result);
103     }
104   result += ")$\"";
105   return result;
106 }
107
108 //----------------------------------------------------------------------------
109 void cmScriptGenerator::GenerateScript(std::ostream& os)
110 {
111   // Track indentation.
112   Indent indent;
113
114   // Generate the script possibly with per-configuration code.
115   this->GenerateScriptConfigs(os, indent);
116 }
117
118 //----------------------------------------------------------------------------
119 void cmScriptGenerator::GenerateScriptConfigs(std::ostream& os,
120                                               Indent const& indent)
121 {
122   if(this->ActionsPerConfig)
123     {
124     this->GenerateScriptActionsPerConfig(os, indent);
125     }
126   else
127     {
128     this->GenerateScriptActionsOnce(os, indent);
129     }
130 }
131
132 //----------------------------------------------------------------------------
133 void cmScriptGenerator::GenerateScriptActions(std::ostream& os,
134                                               Indent const& indent)
135 {
136   if(this->ActionsPerConfig)
137     {
138     // This is reached for single-configuration build generators in a
139     // per-config script generator.
140     this->GenerateScriptForConfig(os, this->ConfigurationName, indent);
141     }
142 }
143
144 //----------------------------------------------------------------------------
145 void cmScriptGenerator::GenerateScriptForConfig(std::ostream&, const char*,
146                                                 Indent const&)
147 {
148   // No actions for this generator.
149 }
150
151 //----------------------------------------------------------------------------
152 bool cmScriptGenerator::GeneratesForConfig(const char* config)
153 {
154   // If this is not a configuration-specific rule then we install.
155   if(this->Configurations.empty())
156     {
157     return true;
158     }
159
160   // This is a configuration-specific rule.  Check if the config
161   // matches this rule.
162   std::string config_upper = cmSystemTools::UpperCase(config?config:"");
163   for(std::vector<std::string>::const_iterator i =
164         this->Configurations.begin();
165       i != this->Configurations.end(); ++i)
166     {
167     if(cmSystemTools::UpperCase(*i) == config_upper)
168       {
169       return true;
170       }
171     }
172   return false;
173 }
174
175 //----------------------------------------------------------------------------
176 void cmScriptGenerator::GenerateScriptActionsOnce(std::ostream& os,
177                                                   Indent const& indent)
178 {
179   if(this->Configurations.empty())
180     {
181     // This rule is for all configurations.
182     this->GenerateScriptActions(os, indent);
183     }
184   else
185     {
186     // Generate a per-configuration block.
187     std::string config_test = this->CreateConfigTest(this->Configurations);
188     os << indent << "IF(" << config_test << ")\n";
189     this->GenerateScriptActions(os, indent.Next());
190     os << indent << "ENDIF(" << config_test << ")\n";
191     }
192 }
193
194 //----------------------------------------------------------------------------
195 void cmScriptGenerator::GenerateScriptActionsPerConfig(std::ostream& os,
196                                                        Indent const& indent)
197 {
198   if(this->ConfigurationTypes->empty())
199     {
200     // In a single-configuration generator there is only one action
201     // and it applies if the runtime-requested configuration is among
202     // the rule's allowed configurations.  The configuration built in
203     // the tree does not matter for this decision but will be used to
204     // generate proper target file names into the code.
205     this->GenerateScriptActionsOnce(os, indent);
206     }
207   else
208     {
209     // In a multi-configuration generator we produce a separate rule
210     // in a block for each configuration that is built.  We restrict
211     // the list of configurations to those to which this rule applies.
212     bool first = true;
213     for(std::vector<std::string>::const_iterator i =
214           this->ConfigurationTypes->begin();
215         i != this->ConfigurationTypes->end(); ++i)
216       {
217       const char* config = i->c_str();
218       if(this->GeneratesForConfig(config))
219         {
220         // Generate a per-configuration block.
221         std::string config_test = this->CreateConfigTest(config);
222         os << indent << (first? "IF(" : "ELSEIF(") << config_test << ")\n";
223         this->GenerateScriptForConfig(os, config, indent.Next());
224         first = false;
225         }
226       }
227     if(!first)
228       {
229       if(this->NeedsScriptNoConfig())
230         {
231         os << indent << "ELSE()\n";
232         this->GenerateScriptNoConfig(os, indent.Next());
233         }
234       os << indent << "ENDIF()\n";
235       }
236     }
237 }