Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmScriptGenerator.cxx
blob1df18bb1ada4062903eb354b755b5be788caf9d2
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmScriptGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2009-03-16 14:39:51 $
7 Version: $Revision: 1.1 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmScriptGenerator.h"
19 #include "cmSystemTools.h"
21 //----------------------------------------------------------------------------
22 cmScriptGenerator
23 ::cmScriptGenerator(const char* config_var,
24 std::vector<std::string> const& configurations):
25 RuntimeConfigVariable(config_var),
26 Configurations(configurations),
27 ConfigurationName(0),
28 ConfigurationTypes(0),
29 ActionsPerConfig(false)
33 //----------------------------------------------------------------------------
34 cmScriptGenerator
35 ::~cmScriptGenerator()
39 //----------------------------------------------------------------------------
40 void
41 cmScriptGenerator
42 ::Generate(std::ostream& os, const char* config,
43 std::vector<std::string> const& configurationTypes)
45 this->ConfigurationName = config;
46 this->ConfigurationTypes = &configurationTypes;
47 this->GenerateScript(os);
48 this->ConfigurationName = 0;
49 this->ConfigurationTypes = 0;
52 //----------------------------------------------------------------------------
53 static void cmScriptGeneratorEncodeConfig(const char* config,
54 std::string& result)
56 for(const char* c = config; *c; ++c)
58 if(*c >= 'a' && *c <= 'z')
60 result += "[";
61 result += *c + ('A' - 'a');
62 result += *c;
63 result += "]";
65 else if(*c >= 'A' && *c <= 'Z')
67 result += "[";
68 result += *c;
69 result += *c + ('a' - 'A');
70 result += "]";
72 else
74 result += *c;
79 //----------------------------------------------------------------------------
80 std::string
81 cmScriptGenerator::CreateConfigTest(const char* config)
83 std::string result = "\"${";
84 result += this->RuntimeConfigVariable;
85 result += "}\" MATCHES \"^(";
86 if(config && *config)
88 cmScriptGeneratorEncodeConfig(config, result);
90 result += ")$\"";
91 return result;
94 //----------------------------------------------------------------------------
95 std::string
96 cmScriptGenerator::CreateConfigTest(std::vector<std::string> const& configs)
98 std::string result = "\"${";
99 result += this->RuntimeConfigVariable;
100 result += "}\" MATCHES \"^(";
101 const char* sep = "";
102 for(std::vector<std::string>::const_iterator ci = configs.begin();
103 ci != configs.end(); ++ci)
105 result += sep;
106 sep = "|";
107 cmScriptGeneratorEncodeConfig(ci->c_str(), result);
109 result += ")$\"";
110 return result;
113 //----------------------------------------------------------------------------
114 void cmScriptGenerator::GenerateScript(std::ostream& os)
116 // Track indentation.
117 Indent indent;
119 // Generate the script possibly with per-configuration code.
120 this->GenerateScriptConfigs(os, indent);
123 //----------------------------------------------------------------------------
124 void cmScriptGenerator::GenerateScriptConfigs(std::ostream& os,
125 Indent const& indent)
127 if(this->ActionsPerConfig)
129 this->GenerateScriptActionsPerConfig(os, indent);
131 else
133 this->GenerateScriptActionsOnce(os, indent);
137 //----------------------------------------------------------------------------
138 void cmScriptGenerator::GenerateScriptActions(std::ostream& os,
139 Indent const& indent)
141 if(this->ActionsPerConfig)
143 // This is reached for single-configuration build generators in a
144 // per-config script generator.
145 this->GenerateScriptForConfig(os, this->ConfigurationName, indent);
149 //----------------------------------------------------------------------------
150 void cmScriptGenerator::GenerateScriptForConfig(std::ostream&, const char*,
151 Indent const&)
153 // No actions for this generator.
156 //----------------------------------------------------------------------------
157 bool cmScriptGenerator::GeneratesForConfig(const char* config)
159 // If this is not a configuration-specific rule then we install.
160 if(this->Configurations.empty())
162 return true;
165 // This is a configuration-specific rule. Check if the config
166 // matches this rule.
167 std::string config_upper = cmSystemTools::UpperCase(config?config:"");
168 for(std::vector<std::string>::const_iterator i =
169 this->Configurations.begin();
170 i != this->Configurations.end(); ++i)
172 if(cmSystemTools::UpperCase(*i) == config_upper)
174 return true;
177 return false;
180 //----------------------------------------------------------------------------
181 void cmScriptGenerator::GenerateScriptActionsOnce(std::ostream& os,
182 Indent const& indent)
184 if(this->Configurations.empty())
186 // This rule is for all configurations.
187 this->GenerateScriptActions(os, indent);
189 else
191 // Generate a per-configuration block.
192 std::string config_test = this->CreateConfigTest(this->Configurations);
193 os << indent << "IF(" << config_test << ")\n";
194 this->GenerateScriptActions(os, indent.Next());
195 os << indent << "ENDIF(" << config_test << ")\n";
199 //----------------------------------------------------------------------------
200 void cmScriptGenerator::GenerateScriptActionsPerConfig(std::ostream& os,
201 Indent const& indent)
203 if(this->ConfigurationTypes->empty())
205 // In a single-configuration generator there is only one action
206 // and it applies if the runtime-requested configuration is among
207 // the rule's allowed configurations. The configuration built in
208 // the tree does not matter for this decision but will be used to
209 // generate proper target file names into the code.
210 this->GenerateScriptActionsOnce(os, indent);
212 else
214 // In a multi-configuration generator we produce a separate rule
215 // in a block for each configuration that is built. We restrict
216 // the list of configurations to those to which this rule applies.
217 for(std::vector<std::string>::const_iterator i =
218 this->ConfigurationTypes->begin();
219 i != this->ConfigurationTypes->end(); ++i)
221 const char* config = i->c_str();
222 if(this->GeneratesForConfig(config))
224 // Generate a per-configuration block.
225 std::string config_test = this->CreateConfigTest(config);
226 os << indent << "IF(" << config_test << ")\n";
227 this->GenerateScriptForConfig(os, config, indent.Next());
228 os << indent << "ENDIF(" << config_test << ")\n";