/src/CMake/Source/cmScriptGenerator.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmScriptGenerator.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <cstddef> |
7 | | #include <sstream> |
8 | | #include <utility> |
9 | | |
10 | | #include "cmStringAlgorithms.h" |
11 | | #include "cmSystemTools.h" |
12 | | |
13 | | namespace { |
14 | | |
15 | | int RequiredBracketLength(cm::string_view s) |
16 | 0 | { |
17 | 0 | for (int n = 0;; ++n) { |
18 | 0 | std::string const needle = "]" + std::string(n, '='); |
19 | |
|
20 | 0 | bool ok = true; |
21 | 0 | std::size_t pos = s.find(needle); |
22 | 0 | while (pos != cm::string_view::npos) { |
23 | 0 | std::size_t const next = pos + needle.size(); |
24 | 0 | if (next == s.size() || s[next] == ']') { |
25 | 0 | ok = false; |
26 | 0 | break; |
27 | 0 | } |
28 | 0 | pos = s.find(needle, pos + 1); |
29 | 0 | } |
30 | |
|
31 | 0 | if (ok) { |
32 | 0 | return n; |
33 | 0 | } |
34 | 0 | } |
35 | 0 | } |
36 | | |
37 | | } // namespace |
38 | | |
39 | | std::ostream& operator<<(std::ostream& os, cmScriptGeneratorQuoted const& self) |
40 | 0 | { |
41 | 0 | if (self.BracketLength == -1) { |
42 | 0 | return os << '"' << self.Value << '"'; |
43 | 0 | } |
44 | 0 | auto const sep = std::string(self.BracketLength, '='); |
45 | 0 | return os << '[' << sep << '[' << self.Value << ']' << sep << ']'; |
46 | 0 | } |
47 | | |
48 | | std::string cmScriptGeneratorQuoted::str() const |
49 | 0 | { |
50 | 0 | std::stringstream out; |
51 | 0 | out << *this; |
52 | 0 | return out.str(); |
53 | 0 | } |
54 | | |
55 | | cmScriptGenerator::cmScriptGenerator(std::string config_var, |
56 | | std::vector<std::string> configurations) |
57 | 0 | : RuntimeConfigVariable(std::move(config_var)) |
58 | 0 | , Configurations(std::move(configurations)) |
59 | 0 | { |
60 | 0 | } |
61 | | |
62 | 0 | cmScriptGenerator::~cmScriptGenerator() = default; |
63 | | |
64 | | void cmScriptGenerator::Generate( |
65 | | std::ostream& os, std::string const& config, |
66 | | std::vector<std::string> const& configurationTypes) |
67 | 0 | { |
68 | 0 | this->ConfigurationName = config; |
69 | 0 | this->ConfigurationTypes = &configurationTypes; |
70 | 0 | this->GenerateScript(os); |
71 | 0 | this->ConfigurationName.clear(); |
72 | 0 | this->ConfigurationTypes = nullptr; |
73 | 0 | } |
74 | | |
75 | | cmScriptGeneratorQuoted cmScriptGenerator::Quote(cm::string_view value) |
76 | 0 | { |
77 | 0 | if (value.find_first_of("\"$\\") == std::string::npos) { |
78 | 0 | return cmScriptGeneratorQuoted(value, -1); |
79 | 0 | } |
80 | 0 | return cmScriptGeneratorQuoted(value, RequiredBracketLength(value)); |
81 | 0 | } |
82 | | |
83 | | static void cmScriptGeneratorEncodeConfig(std::string const& config, |
84 | | std::string& result) |
85 | 0 | { |
86 | 0 | for (char const* c = config.c_str(); *c; ++c) { |
87 | 0 | if (*c >= 'a' && *c <= 'z') { |
88 | 0 | result += "["; |
89 | 0 | result += static_cast<char>(*c + 'A' - 'a'); |
90 | 0 | result += *c; |
91 | 0 | result += "]"; |
92 | 0 | } else if (*c >= 'A' && *c <= 'Z') { |
93 | 0 | result += "["; |
94 | 0 | result += *c; |
95 | 0 | result += static_cast<char>(*c + 'a' - 'A'); |
96 | 0 | result += "]"; |
97 | 0 | } else { |
98 | 0 | result += *c; |
99 | 0 | } |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | | std::string cmScriptGenerator::CreateConfigTest(std::string const& configVar, |
104 | | std::string const& config) |
105 | 0 | { |
106 | 0 | std::string result = cmStrCat(configVar, " MATCHES \"^("); |
107 | 0 | if (!config.empty()) { |
108 | 0 | cmScriptGeneratorEncodeConfig(config, result); |
109 | 0 | } |
110 | 0 | result += ")$\""; |
111 | 0 | return result; |
112 | 0 | } |
113 | | |
114 | | std::string cmScriptGenerator::CreateConfigTest( |
115 | | std::string const& configVar, std::vector<std::string> const& configs) |
116 | 0 | { |
117 | 0 | std::string result = cmStrCat(configVar, " MATCHES \"^("); |
118 | 0 | char const* sep = ""; |
119 | 0 | for (std::string const& config : configs) { |
120 | 0 | result += sep; |
121 | 0 | sep = "|"; |
122 | 0 | cmScriptGeneratorEncodeConfig(config, result); |
123 | 0 | } |
124 | 0 | result += ")$\""; |
125 | 0 | return result; |
126 | 0 | } |
127 | | |
128 | | std::string cmScriptGenerator::CreateConfigTest(std::string const& config) |
129 | 0 | { |
130 | 0 | return cmScriptGenerator::CreateConfigTest(this->RuntimeConfigVariable, |
131 | 0 | config); |
132 | 0 | } |
133 | | |
134 | | std::string cmScriptGenerator::CreateConfigTest( |
135 | | std::vector<std::string> const& configs) |
136 | 0 | { |
137 | 0 | return cmScriptGenerator::CreateConfigTest(this->RuntimeConfigVariable, |
138 | 0 | configs); |
139 | 0 | } |
140 | | |
141 | | void cmScriptGenerator::GenerateScript(std::ostream& os) |
142 | 0 | { |
143 | | // Track indentation. |
144 | 0 | Indent indent; |
145 | | |
146 | | // Generate the script possibly with per-configuration code. |
147 | 0 | this->GenerateScriptConfigs(os, indent); |
148 | 0 | } |
149 | | |
150 | | void cmScriptGenerator::GenerateScriptConfigs(std::ostream& os, Indent indent) |
151 | 0 | { |
152 | 0 | if (this->ActionsPerConfig) { |
153 | 0 | this->GenerateScriptActionsPerConfig(os, indent); |
154 | 0 | } else { |
155 | 0 | this->GenerateScriptActionsOnce(os, indent); |
156 | 0 | } |
157 | 0 | } |
158 | | |
159 | | void cmScriptGenerator::GenerateScriptActions(std::ostream& os, Indent indent) |
160 | 0 | { |
161 | 0 | if (this->ActionsPerConfig) { |
162 | | // This is reached for single-configuration build generators in a |
163 | | // per-config script generator. |
164 | 0 | this->GenerateScriptForConfig(os, this->ConfigurationName, indent); |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | | void cmScriptGenerator::GenerateScriptForConfig(std::ostream& /*unused*/, |
169 | | std::string const& /*unused*/, |
170 | | Indent /*unused*/) |
171 | 0 | { |
172 | | // No actions for this generator. |
173 | 0 | } |
174 | | |
175 | | bool cmScriptGenerator::GeneratesForConfig(std::string const& config) |
176 | 0 | { |
177 | | // If this is not a configuration-specific rule then we install. |
178 | 0 | if (this->Configurations.empty()) { |
179 | 0 | return true; |
180 | 0 | } |
181 | | |
182 | | // This is a configuration-specific rule. Check if the config |
183 | | // matches this rule. |
184 | 0 | std::string config_upper = cmSystemTools::UpperCase(config); |
185 | 0 | return std::any_of(this->Configurations.begin(), this->Configurations.end(), |
186 | 0 | [&config_upper](std::string const& cfg) -> bool { |
187 | 0 | return cmSystemTools::UpperCase(cfg) == config_upper; |
188 | 0 | }); |
189 | 0 | } |
190 | | |
191 | | void cmScriptGenerator::GenerateScriptActionsOnce(std::ostream& os, |
192 | | Indent indent) |
193 | 0 | { |
194 | 0 | if (this->Configurations.empty()) { |
195 | | // This rule is for all configurations. |
196 | 0 | this->GenerateScriptActions(os, indent); |
197 | 0 | } else { |
198 | | // Generate a per-configuration block. |
199 | 0 | std::string config_test = this->CreateConfigTest(this->Configurations); |
200 | 0 | os << indent << "if(" << config_test << ")\n"; |
201 | 0 | this->GenerateScriptActions(os, indent.Next()); |
202 | 0 | os << indent << "endif()\n"; |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | void cmScriptGenerator::GenerateScriptActionsPerConfig(std::ostream& os, |
207 | | Indent indent) |
208 | 0 | { |
209 | 0 | if (this->ConfigurationTypes->empty()) { |
210 | | // In a single-configuration generator there is only one action |
211 | | // and it applies if the runtime-requested configuration is among |
212 | | // the rule's allowed configurations. The configuration built in |
213 | | // the tree does not matter for this decision but will be used to |
214 | | // generate proper target file names into the code. |
215 | 0 | this->GenerateScriptActionsOnce(os, indent); |
216 | 0 | } else { |
217 | | // In a multi-configuration generator we produce a separate rule |
218 | | // in a block for each configuration that is built. We restrict |
219 | | // the list of configurations to those to which this rule applies. |
220 | 0 | bool first = true; |
221 | 0 | for (std::string const& cfgType : *this->ConfigurationTypes) { |
222 | 0 | if (this->GeneratesForConfig(cfgType)) { |
223 | | // Generate a per-configuration block. |
224 | 0 | std::string config_test = this->CreateConfigTest(cfgType); |
225 | 0 | os << indent << (first ? "if(" : "elseif(") << config_test << ")\n"; |
226 | 0 | this->GenerateScriptForConfig(os, cfgType, indent.Next()); |
227 | 0 | first = false; |
228 | 0 | } |
229 | 0 | } |
230 | 0 | if (!first) { |
231 | 0 | if (this->NeedsScriptNoConfig()) { |
232 | 0 | os << indent << "else()\n"; |
233 | 0 | this->GenerateScriptNoConfig(os, indent.Next()); |
234 | 0 | } |
235 | 0 | os << indent << "endif()\n"; |
236 | 0 | } |
237 | 0 | } |
238 | 0 | } |