1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmScriptGenerator.h"
#include <algorithm>
#include <utility>
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
cmScriptGenerator::cmScriptGenerator(std::string config_var,
std::vector<std::string> configurations)
: RuntimeConfigVariable(std::move(config_var))
, Configurations(std::move(configurations))
{
}
cmScriptGenerator::~cmScriptGenerator() = default;
void cmScriptGenerator::Generate(
std::ostream& os, const std::string& config,
std::vector<std::string> const& configurationTypes)
{
this->ConfigurationName = config;
this->ConfigurationTypes = &configurationTypes;
this->GenerateScript(os);
this->ConfigurationName.clear();
this->ConfigurationTypes = nullptr;
}
static void cmScriptGeneratorEncodeConfig(const std::string& config,
std::string& result)
{
for (const char* c = config.c_str(); *c; ++c) {
if (*c >= 'a' && *c <= 'z') {
result += "[";
result += static_cast<char>(*c + 'A' - 'a');
result += *c;
result += "]";
} else if (*c >= 'A' && *c <= 'Z') {
result += "[";
result += *c;
result += static_cast<char>(*c + 'a' - 'A');
result += "]";
} else {
result += *c;
}
}
}
std::string cmScriptGenerator::CreateConfigTest(const std::string& config)
{
std::string result = cmStrCat(this->RuntimeConfigVariable, " MATCHES \"^(");
if (!config.empty()) {
cmScriptGeneratorEncodeConfig(config, result);
}
result += ")$\"";
return result;
}
std::string cmScriptGenerator::CreateConfigTest(
std::vector<std::string> const& configs)
{
std::string result = cmStrCat(this->RuntimeConfigVariable, " MATCHES \"^(");
const char* sep = "";
for (std::string const& config : configs) {
result += sep;
sep = "|";
cmScriptGeneratorEncodeConfig(config, result);
}
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 indent)
{
if (this->ActionsPerConfig) {
this->GenerateScriptActionsPerConfig(os, indent);
} else {
this->GenerateScriptActionsOnce(os, indent);
}
}
void cmScriptGenerator::GenerateScriptActions(std::ostream& os, Indent indent)
{
if (this->ActionsPerConfig) {
// This is reached for single-configuration build generators in a
// per-config script generator.
this->GenerateScriptForConfig(os, this->ConfigurationName, indent);
}
}
void cmScriptGenerator::GenerateScriptForConfig(std::ostream& /*unused*/,
const std::string& /*unused*/,
Indent /*unused*/)
{
// No actions for this generator.
}
bool cmScriptGenerator::GeneratesForConfig(const std::string& config)
{
// If this is not a configuration-specific rule then we install.
if (this->Configurations.empty()) {
return true;
}
// This is a configuration-specific rule. Check if the config
// matches this rule.
std::string config_upper = cmSystemTools::UpperCase(config);
return std::any_of(this->Configurations.begin(), this->Configurations.end(),
[&config_upper](std::string const& cfg) -> bool {
return cmSystemTools::UpperCase(cfg) == config_upper;
});
}
void cmScriptGenerator::GenerateScriptActionsOnce(std::ostream& os,
Indent indent)
{
if (this->Configurations.empty()) {
// This rule is for all configurations.
this->GenerateScriptActions(os, indent);
} else {
// Generate a per-configuration block.
std::string config_test = this->CreateConfigTest(this->Configurations);
os << indent << "if(" << config_test << ")\n";
this->GenerateScriptActions(os, indent.Next());
os << indent << "endif()\n";
}
}
void cmScriptGenerator::GenerateScriptActionsPerConfig(std::ostream& os,
Indent indent)
{
if (this->ConfigurationTypes->empty()) {
// 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);
} else {
// 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.
bool first = true;
for (std::string const& cfgType : *this->ConfigurationTypes) {
if (this->GeneratesForConfig(cfgType)) {
// Generate a per-configuration block.
std::string config_test = this->CreateConfigTest(cfgType);
os << indent << (first ? "if(" : "elseif(") << config_test << ")\n";
this->GenerateScriptForConfig(os, cfgType, indent.Next());
first = false;
}
}
if (!first) {
if (this->NeedsScriptNoConfig()) {
os << indent << "else()\n";
this->GenerateScriptNoConfig(os, indent.Next());
}
os << indent << "endif()\n";
}
}
}
|