cmake/Source/cmScriptGenerator.cxx

173 lines
5.5 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2009-10-04 10:30:41 +03:00
#include "cmScriptGenerator.h"
2021-09-14 00:13:48 +02:00
#include <algorithm>
2019-11-11 23:01:05 +01:00
#include <utility>
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
2019-11-11 23:01:05 +01:00
cmScriptGenerator::cmScriptGenerator(std::string config_var,
std::vector<std::string> configurations)
: RuntimeConfigVariable(std::move(config_var))
, Configurations(std::move(configurations))
2009-10-04 10:30:41 +03:00
{
}
2019-11-11 23:01:05 +01:00
cmScriptGenerator::~cmScriptGenerator() = default;
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
void cmScriptGenerator::Generate(
std::ostream& os, const std::string& config,
std::vector<std::string> const& configurationTypes)
2009-10-04 10:30:41 +03:00
{
this->ConfigurationName = config;
this->ConfigurationTypes = &configurationTypes;
this->GenerateScript(os);
2018-01-26 17:06:56 +01:00
this->ConfigurationName.clear();
this->ConfigurationTypes = nullptr;
2009-10-04 10:30:41 +03:00
}
2015-04-27 22:25:09 +02:00
static void cmScriptGeneratorEncodeConfig(const std::string& config,
2009-10-04 10:30:41 +03:00
std::string& result)
{
2016-07-09 11:21:54 +02:00
for (const char* c = config.c_str(); *c; ++c) {
if (*c >= 'a' && *c <= 'z') {
2009-10-04 10:30:41 +03:00
result += "[";
2009-10-11 10:55:36 +03:00
result += static_cast<char>(*c + 'A' - 'a');
2009-10-04 10:30:41 +03:00
result += *c;
result += "]";
2016-07-09 11:21:54 +02:00
} else if (*c >= 'A' && *c <= 'Z') {
2009-10-04 10:30:41 +03:00
result += "[";
result += *c;
2009-10-11 10:55:36 +03:00
result += static_cast<char>(*c + 'a' - 'A');
2009-10-04 10:30:41 +03:00
result += "]";
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
result += *c;
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
std::string cmScriptGenerator::CreateConfigTest(const std::string& config)
2009-10-04 10:30:41 +03:00
{
2022-08-04 22:12:04 +02:00
std::string result = cmStrCat(this->RuntimeConfigVariable, " MATCHES \"^(");
2016-07-09 11:21:54 +02:00
if (!config.empty()) {
2009-10-04 10:30:41 +03:00
cmScriptGeneratorEncodeConfig(config, result);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
result += ")$\"";
return result;
}
2016-07-09 11:21:54 +02:00
std::string cmScriptGenerator::CreateConfigTest(
std::vector<std::string> const& configs)
2009-10-04 10:30:41 +03:00
{
2022-08-04 22:12:04 +02:00
std::string result = cmStrCat(this->RuntimeConfigVariable, " MATCHES \"^(");
2009-10-04 10:30:41 +03:00
const char* sep = "";
2018-01-26 17:06:56 +01:00
for (std::string const& config : configs) {
2009-10-04 10:30:41 +03:00
result += sep;
sep = "|";
2018-01-26 17:06:56 +01:00
cmScriptGeneratorEncodeConfig(config, result);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +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);
}
2017-07-20 19:35:53 +02:00
void cmScriptGenerator::GenerateScriptConfigs(std::ostream& os, Indent indent)
2009-10-04 10:30:41 +03:00
{
2016-07-09 11:21:54 +02:00
if (this->ActionsPerConfig) {
2009-10-04 10:30:41 +03:00
this->GenerateScriptActionsPerConfig(os, indent);
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
this->GenerateScriptActionsOnce(os, indent);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
2017-07-20 19:35:53 +02:00
void cmScriptGenerator::GenerateScriptActions(std::ostream& os, Indent indent)
2009-10-04 10:30:41 +03:00
{
2016-07-09 11:21:54 +02:00
if (this->ActionsPerConfig) {
2009-10-04 10:30:41 +03:00
// This is reached for single-configuration build generators in a
// per-config script generator.
this->GenerateScriptForConfig(os, this->ConfigurationName, indent);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
2016-10-30 18:24:19 +01:00
void cmScriptGenerator::GenerateScriptForConfig(std::ostream& /*unused*/,
const std::string& /*unused*/,
2017-07-20 19:35:53 +02:00
Indent /*unused*/)
2009-10-04 10:30:41 +03:00
{
// No actions for this generator.
}
2015-04-27 22:25:09 +02:00
bool cmScriptGenerator::GeneratesForConfig(const std::string& config)
2009-10-04 10:30:41 +03:00
{
// If this is not a configuration-specific rule then we install.
2016-07-09 11:21:54 +02:00
if (this->Configurations.empty()) {
2009-10-04 10:30:41 +03:00
return true;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// This is a configuration-specific rule. Check if the config
// matches this rule.
2015-04-27 22:25:09 +02:00
std::string config_upper = cmSystemTools::UpperCase(config);
2021-09-14 00:13:48 +02:00
return std::any_of(this->Configurations.begin(), this->Configurations.end(),
[&config_upper](std::string const& cfg) -> bool {
return cmSystemTools::UpperCase(cfg) == config_upper;
});
2009-10-04 10:30:41 +03:00
}
void cmScriptGenerator::GenerateScriptActionsOnce(std::ostream& os,
2017-07-20 19:35:53 +02:00
Indent indent)
2009-10-04 10:30:41 +03:00
{
2016-07-09 11:21:54 +02:00
if (this->Configurations.empty()) {
2009-10-04 10:30:41 +03:00
// This rule is for all configurations.
this->GenerateScriptActions(os, indent);
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
// Generate a per-configuration block.
std::string config_test = this->CreateConfigTest(this->Configurations);
2014-08-03 19:52:23 +02:00
os << indent << "if(" << config_test << ")\n";
2009-10-04 10:30:41 +03:00
this->GenerateScriptActions(os, indent.Next());
2022-11-16 20:14:03 +01:00
os << indent << "endif()\n";
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
void cmScriptGenerator::GenerateScriptActionsPerConfig(std::ostream& os,
2017-07-20 19:35:53 +02:00
Indent indent)
2009-10-04 10:30:41 +03:00
{
2016-07-09 11:21:54 +02:00
if (this->ConfigurationTypes->empty()) {
2009-10-04 10:30:41 +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-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +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-19 15:41:06 +03:00
bool first = true;
2018-01-26 17:06:56 +01:00
for (std::string const& cfgType : *this->ConfigurationTypes) {
if (this->GeneratesForConfig(cfgType)) {
2009-10-04 10:30:41 +03:00
// Generate a per-configuration block.
2018-01-26 17:06:56 +01:00
std::string config_test = this->CreateConfigTest(cfgType);
2016-07-09 11:21:54 +02:00
os << indent << (first ? "if(" : "elseif(") << config_test << ")\n";
2018-01-26 17:06:56 +01:00
this->GenerateScriptForConfig(os, cfgType, indent.Next());
2011-06-19 15:41:06 +03:00
first = false;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
if (!first) {
if (this->NeedsScriptNoConfig()) {
2014-08-03 19:52:23 +02:00
os << indent << "else()\n";
2011-06-19 15:41:06 +03:00
this->GenerateScriptNoConfig(os, indent.Next());
}
2016-07-09 11:21:54 +02:00
os << indent << "endif()\n";
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}