2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2009-03-16 17:39:56 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2009-03-16 17:39:56 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2009-03-16 17:39:56 +03:00
|
|
|
#include "cmScriptGenerator.h"
|
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmScriptGenerator::cmScriptGenerator(
|
|
|
|
const std::string& config_var,
|
|
|
|
std::vector<std::string> const& configurations)
|
|
|
|
: RuntimeConfigVariable(config_var)
|
|
|
|
, Configurations(configurations)
|
|
|
|
, ConfigurationName("")
|
2016-06-27 23:44:16 +03:00
|
|
|
, ConfigurationTypes(CM_NULLPTR)
|
2016-05-16 17:34:04 +03:00
|
|
|
, ActionsPerConfig(false)
|
2009-03-16 17:39:56 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmScriptGenerator::~cmScriptGenerator()
|
2009-03-16 17:39:56 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmScriptGenerator::Generate(
|
|
|
|
std::ostream& os, const std::string& config,
|
|
|
|
std::vector<std::string> const& configurationTypes)
|
2009-03-16 17:39:56 +03:00
|
|
|
{
|
|
|
|
this->ConfigurationName = config;
|
|
|
|
this->ConfigurationTypes = &configurationTypes;
|
|
|
|
this->GenerateScript(os);
|
2014-02-10 07:48:34 +04:00
|
|
|
this->ConfigurationName = "";
|
2016-06-27 23:44:16 +03:00
|
|
|
this->ConfigurationTypes = CM_NULLPTR;
|
2009-03-16 17:39:56 +03:00
|
|
|
}
|
|
|
|
|
2014-02-10 07:48:34 +04:00
|
|
|
static void cmScriptGeneratorEncodeConfig(const std::string& config,
|
2009-03-16 17:39:56 +03:00
|
|
|
std::string& result)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
for (const char* c = config.c_str(); *c; ++c) {
|
|
|
|
if (*c >= 'a' && *c <= 'z') {
|
2009-03-16 17:39:56 +03:00
|
|
|
result += "[";
|
2009-10-02 00:47:08 +04:00
|
|
|
result += static_cast<char>(*c + 'A' - 'a');
|
2009-03-16 17:39:56 +03:00
|
|
|
result += *c;
|
|
|
|
result += "]";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*c >= 'A' && *c <= 'Z') {
|
2009-03-16 17:39:56 +03:00
|
|
|
result += "[";
|
|
|
|
result += *c;
|
2009-10-02 00:47:08 +04:00
|
|
|
result += static_cast<char>(*c + 'a' - 'A');
|
2009-03-16 17:39:56 +03:00
|
|
|
result += "]";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2009-03-16 17:39:56 +03:00
|
|
|
result += *c;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-03-16 17:39:56 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string cmScriptGenerator::CreateConfigTest(const std::string& config)
|
2009-03-16 17:39:56 +03:00
|
|
|
{
|
|
|
|
std::string result = "\"${";
|
|
|
|
result += this->RuntimeConfigVariable;
|
|
|
|
result += "}\" MATCHES \"^(";
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!config.empty()) {
|
2009-03-16 17:39:56 +03:00
|
|
|
cmScriptGeneratorEncodeConfig(config, result);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-03-16 17:39:56 +03:00
|
|
|
result += ")$\"";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string cmScriptGenerator::CreateConfigTest(
|
|
|
|
std::vector<std::string> const& configs)
|
2009-03-16 17:39:56 +03:00
|
|
|
{
|
|
|
|
std::string result = "\"${";
|
|
|
|
result += this->RuntimeConfigVariable;
|
|
|
|
result += "}\" MATCHES \"^(";
|
|
|
|
const char* sep = "";
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator ci = configs.begin();
|
|
|
|
ci != configs.end(); ++ci) {
|
2009-03-16 17:39:56 +03:00
|
|
|
result += sep;
|
|
|
|
sep = "|";
|
2014-03-11 03:04:11 +04:00
|
|
|
cmScriptGeneratorEncodeConfig(*ci, result);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-03-16 17:39:56 +03:00
|
|
|
result += ")$\"";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmScriptGenerator::GenerateScript(std::ostream& os)
|
|
|
|
{
|
|
|
|
// Track indentation.
|
|
|
|
Indent indent;
|
|
|
|
|
|
|
|
// Generate the script possibly with per-configuration code.
|
|
|
|
this->GenerateScriptConfigs(os, indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmScriptGenerator::GenerateScriptConfigs(std::ostream& os,
|
|
|
|
Indent const& indent)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->ActionsPerConfig) {
|
2009-03-16 17:39:56 +03:00
|
|
|
this->GenerateScriptActionsPerConfig(os, indent);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2009-03-16 17:39:56 +03:00
|
|
|
this->GenerateScriptActionsOnce(os, indent);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-03-16 17:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmScriptGenerator::GenerateScriptActions(std::ostream& os,
|
|
|
|
Indent const& indent)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->ActionsPerConfig) {
|
2009-03-16 17:39:56 +03:00
|
|
|
// This is reached for single-configuration build generators in a
|
|
|
|
// per-config script generator.
|
|
|
|
this->GenerateScriptForConfig(os, this->ConfigurationName, indent);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-03-16 17:39:56 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:49:57 +03:00
|
|
|
void cmScriptGenerator::GenerateScriptForConfig(std::ostream& /*unused*/,
|
|
|
|
const std::string& /*unused*/,
|
|
|
|
Indent const& /*unused*/)
|
2009-03-16 17:39:56 +03:00
|
|
|
{
|
|
|
|
// No actions for this generator.
|
|
|
|
}
|
|
|
|
|
2014-02-10 07:48:34 +04:00
|
|
|
bool cmScriptGenerator::GeneratesForConfig(const std::string& config)
|
2009-03-16 17:39:56 +03:00
|
|
|
{
|
|
|
|
// If this is not a configuration-specific rule then we install.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Configurations.empty()) {
|
2009-03-16 17:39:56 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-03-16 17:39:56 +03:00
|
|
|
|
|
|
|
// This is a configuration-specific rule. Check if the config
|
|
|
|
// matches this rule.
|
2014-02-10 07:48:34 +04:00
|
|
|
std::string config_upper = cmSystemTools::UpperCase(config);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator i =
|
|
|
|
this->Configurations.begin();
|
|
|
|
i != this->Configurations.end(); ++i) {
|
|
|
|
if (cmSystemTools::UpperCase(*i) == config_upper) {
|
2009-03-16 17:39:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-03-16 17:39:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmScriptGenerator::GenerateScriptActionsOnce(std::ostream& os,
|
|
|
|
Indent const& indent)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Configurations.empty()) {
|
2009-03-16 17:39:56 +03:00
|
|
|
// This rule is for all configurations.
|
|
|
|
this->GenerateScriptActions(os, indent);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2009-03-16 17:39:56 +03:00
|
|
|
// Generate a per-configuration block.
|
|
|
|
std::string config_test = this->CreateConfigTest(this->Configurations);
|
2013-08-22 13:30:19 +04:00
|
|
|
os << indent << "if(" << config_test << ")\n";
|
2009-03-16 17:39:56 +03:00
|
|
|
this->GenerateScriptActions(os, indent.Next());
|
2013-08-22 13:30:19 +04:00
|
|
|
os << indent << "endif(" << config_test << ")\n";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-03-16 17:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmScriptGenerator::GenerateScriptActionsPerConfig(std::ostream& os,
|
|
|
|
Indent const& indent)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->ConfigurationTypes->empty()) {
|
2009-03-16 17:39:56 +03:00
|
|
|
// In a single-configuration generator there is only one action
|
|
|
|
// and it applies if the runtime-requested configuration is among
|
|
|
|
// the rule's allowed configurations. The configuration built in
|
|
|
|
// the tree does not matter for this decision but will be used to
|
|
|
|
// generate proper target file names into the code.
|
|
|
|
this->GenerateScriptActionsOnce(os, indent);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2009-03-16 17:39:56 +03:00
|
|
|
// In a multi-configuration generator we produce a separate rule
|
|
|
|
// in a block for each configuration that is built. We restrict
|
|
|
|
// the list of configurations to those to which this rule applies.
|
2011-06-10 16:48:20 +04:00
|
|
|
bool first = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator i =
|
|
|
|
this->ConfigurationTypes->begin();
|
|
|
|
i != this->ConfigurationTypes->end(); ++i) {
|
2009-03-16 17:39:56 +03:00
|
|
|
const char* config = i->c_str();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->GeneratesForConfig(config)) {
|
2009-03-16 17:39:56 +03:00
|
|
|
// Generate a per-configuration block.
|
|
|
|
std::string config_test = this->CreateConfigTest(config);
|
2016-05-16 17:34:04 +03:00
|
|
|
os << indent << (first ? "if(" : "elseif(") << config_test << ")\n";
|
2009-03-16 17:39:56 +03:00
|
|
|
this->GenerateScriptForConfig(os, config, indent.Next());
|
2011-06-10 16:48:20 +04:00
|
|
|
first = false;
|
2009-03-16 17:39:56 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!first) {
|
|
|
|
if (this->NeedsScriptNoConfig()) {
|
2013-08-22 13:30:19 +04:00
|
|
|
os << indent << "else()\n";
|
2011-06-10 16:48:20 +04:00
|
|
|
this->GenerateScriptNoConfig(os, indent.Next());
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
os << indent << "endif()\n";
|
2009-03-16 17:39:56 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-03-16 17:39:56 +03:00
|
|
|
}
|