1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
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"
14 #include "cmSystemTools.h"
16 //----------------------------------------------------------------------------
18 ::cmScriptGenerator(const char* config_var,
19 std::vector<std::string> const& configurations):
20 RuntimeConfigVariable(config_var),
21 Configurations(configurations),
23 ConfigurationTypes(0),
24 ActionsPerConfig(false)
28 //----------------------------------------------------------------------------
30 ::~cmScriptGenerator()
34 //----------------------------------------------------------------------------
37 ::Generate(std::ostream& os, const char* config,
38 std::vector<std::string> const& configurationTypes)
40 this->ConfigurationName = config;
41 this->ConfigurationTypes = &configurationTypes;
42 this->GenerateScript(os);
43 this->ConfigurationName = 0;
44 this->ConfigurationTypes = 0;
47 //----------------------------------------------------------------------------
48 static void cmScriptGeneratorEncodeConfig(const char* config,
51 for(const char* c = config; *c; ++c)
53 if(*c >= 'a' && *c <= 'z')
56 result += static_cast<char>(*c + 'A' - 'a');
60 else if(*c >= 'A' && *c <= 'Z')
64 result += static_cast<char>(*c + 'a' - 'A');
74 //----------------------------------------------------------------------------
76 cmScriptGenerator::CreateConfigTest(const char* config)
78 std::string result = "\"${";
79 result += this->RuntimeConfigVariable;
80 result += "}\" MATCHES \"^(";
83 cmScriptGeneratorEncodeConfig(config, result);
89 //----------------------------------------------------------------------------
91 cmScriptGenerator::CreateConfigTest(std::vector<std::string> const& configs)
93 std::string result = "\"${";
94 result += this->RuntimeConfigVariable;
95 result += "}\" MATCHES \"^(";
97 for(std::vector<std::string>::const_iterator ci = configs.begin();
98 ci != configs.end(); ++ci)
102 cmScriptGeneratorEncodeConfig(ci->c_str(), result);
108 //----------------------------------------------------------------------------
109 void cmScriptGenerator::GenerateScript(std::ostream& os)
111 // Track indentation.
114 // Generate the script possibly with per-configuration code.
115 this->GenerateScriptConfigs(os, indent);
118 //----------------------------------------------------------------------------
119 void cmScriptGenerator::GenerateScriptConfigs(std::ostream& os,
120 Indent const& indent)
122 if(this->ActionsPerConfig)
124 this->GenerateScriptActionsPerConfig(os, indent);
128 this->GenerateScriptActionsOnce(os, indent);
132 //----------------------------------------------------------------------------
133 void cmScriptGenerator::GenerateScriptActions(std::ostream& os,
134 Indent const& indent)
136 if(this->ActionsPerConfig)
138 // This is reached for single-configuration build generators in a
139 // per-config script generator.
140 this->GenerateScriptForConfig(os, this->ConfigurationName, indent);
144 //----------------------------------------------------------------------------
145 void cmScriptGenerator::GenerateScriptForConfig(std::ostream&, const char*,
148 // No actions for this generator.
151 //----------------------------------------------------------------------------
152 bool cmScriptGenerator::GeneratesForConfig(const char* config)
154 // If this is not a configuration-specific rule then we install.
155 if(this->Configurations.empty())
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)
167 if(cmSystemTools::UpperCase(*i) == config_upper)
175 //----------------------------------------------------------------------------
176 void cmScriptGenerator::GenerateScriptActionsOnce(std::ostream& os,
177 Indent const& indent)
179 if(this->Configurations.empty())
181 // This rule is for all configurations.
182 this->GenerateScriptActions(os, indent);
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";
194 //----------------------------------------------------------------------------
195 void cmScriptGenerator::GenerateScriptActionsPerConfig(std::ostream& os,
196 Indent const& indent)
198 if(this->ConfigurationTypes->empty())
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);
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.
213 for(std::vector<std::string>::const_iterator i =
214 this->ConfigurationTypes->begin();
215 i != this->ConfigurationTypes->end(); ++i)
217 const char* config = i->c_str();
218 if(this->GeneratesForConfig(config))
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());
229 if(this->NeedsScriptNoConfig())
231 os << indent << "ELSE()\n";
232 this->GenerateScriptNoConfig(os, indent.Next());
234 os << indent << "ENDIF()\n";