/src/CMake/Source/cmTestGenerator.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 "cmTestGenerator.h" |
4 | | |
5 | | #include <cstddef> // IWYU pragma: keep |
6 | | #include <memory> |
7 | | #include <ostream> |
8 | | #include <set> |
9 | | #include <string> |
10 | | #include <utility> |
11 | | #include <vector> |
12 | | |
13 | | #include "cmGeneratorExpression.h" |
14 | | #include "cmGeneratorTarget.h" |
15 | | #include "cmGlobalGenerator.h" |
16 | | #include "cmList.h" |
17 | | #include "cmListFileCache.h" |
18 | | #include "cmLocalGenerator.h" |
19 | | #include "cmMakefile.h" |
20 | | #include "cmMessageType.h" |
21 | | #include "cmPolicies.h" |
22 | | #include "cmPropertyMap.h" |
23 | | #include "cmRange.h" |
24 | | #include "cmScriptGenerator.h" |
25 | | #include "cmStateTypes.h" |
26 | | #include "cmStringAlgorithms.h" |
27 | | #include "cmSystemTools.h" |
28 | | #include "cmTest.h" |
29 | | #include "cmValue.h" |
30 | | |
31 | | namespace /* anonymous */ |
32 | | { |
33 | | |
34 | | bool needToQuoteTestName(cmMakefile const& mf, std::string const& name) |
35 | 0 | { |
36 | | // Determine if policy CMP0110 is set to NEW. |
37 | 0 | switch (mf.GetPolicyStatus(cmPolicies::CMP0110)) { |
38 | 0 | case cmPolicies::WARN: |
39 | | // Only warn if a forbidden character is used in the name. |
40 | 0 | if (name.find_first_of("$[] #;\t\n\"\\") != std::string::npos) { |
41 | 0 | mf.IssuePolicyWarning( |
42 | 0 | cmPolicies::CMP0110, {}, |
43 | 0 | cmStrCat("The following name given to add_test() is invalid if " |
44 | 0 | "CMP0110 is not set or set to OLD:\n `", |
45 | 0 | name, "ยด\n")); |
46 | 0 | } |
47 | 0 | CM_FALLTHROUGH; |
48 | 0 | case cmPolicies::OLD: |
49 | | // OLD behavior is to not quote the test's name. |
50 | 0 | return false; |
51 | 0 | case cmPolicies::NEW: |
52 | 0 | default: |
53 | | // NEW behavior is to quote the test's name. |
54 | 0 | return true; |
55 | 0 | } |
56 | 0 | } |
57 | | |
58 | | std::string TestName(cmTest* test) |
59 | 0 | { |
60 | 0 | std::string name = test->GetName(); |
61 | 0 | if (needToQuoteTestName(*test->GetMakefile(), name)) { |
62 | 0 | name = cmScriptGenerator::Quote(name); |
63 | 0 | } |
64 | 0 | return name; |
65 | 0 | } |
66 | | |
67 | | } // End: anonymous namespace |
68 | | |
69 | | cmTestGenerator::cmTestGenerator( |
70 | | cmTest* test, std::vector<std::string> const& configurations) |
71 | 0 | : cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations) |
72 | 0 | , Test(test) |
73 | 0 | { |
74 | 0 | this->ActionsPerConfig = test == nullptr || !test->GetOldStyle(); |
75 | 0 | this->TestGenerated = false; |
76 | 0 | this->LG = nullptr; |
77 | 0 | } |
78 | | |
79 | 0 | cmTestGenerator::~cmTestGenerator() = default; |
80 | | |
81 | | void cmTestGenerator::Compute(cmLocalGenerator* lg) |
82 | 0 | { |
83 | 0 | this->LG = lg; |
84 | 0 | } |
85 | | |
86 | | bool cmTestGenerator::TestsForConfig(std::string const& config) |
87 | 0 | { |
88 | 0 | return this->Test != nullptr && this->GeneratesForConfig(config); |
89 | 0 | } |
90 | | |
91 | | cmTest* cmTestGenerator::GetTest() const |
92 | 0 | { |
93 | 0 | return this->Test; |
94 | 0 | } |
95 | | |
96 | | bool cmTestGenerator::GetBuildDependencies(cmLocalGenerator* lg, |
97 | | BuildDependencies& info) |
98 | 0 | { |
99 | 0 | if (this->Test == nullptr || |
100 | 0 | !cmGeneratorExpression::IsValidTargetName(this->Test->GetName()) || |
101 | 0 | cmGlobalGenerator::IsReservedTarget(this->Test->GetName())) { |
102 | 0 | return false; |
103 | 0 | } |
104 | | |
105 | 0 | std::set<cmGeneratorTarget*> dependencies; |
106 | | |
107 | | // Get dependencies from generator expressions |
108 | 0 | cmGeneratorExpression ge(*this->Test->GetMakefile()->GetCMakeInstance(), |
109 | 0 | this->Test->GetBacktrace()); |
110 | 0 | std::string const config; |
111 | 0 | for (std::string const& arg : this->Test->GetCommand()) { |
112 | 0 | auto parsed = ge.Parse(arg); |
113 | 0 | parsed->Evaluate(lg, config); |
114 | 0 | for (cmGeneratorTarget* dep : parsed->GetTargets()) { |
115 | 0 | if (dep && !dep->IsImported()) { |
116 | 0 | dependencies.insert(dep); |
117 | 0 | } |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | // Add target executed by test |
122 | 0 | if (!this->Test->GetCommand().empty()) { |
123 | 0 | std::string exe = this->Test->GetCommand().front(); |
124 | 0 | cmGeneratorTarget* target = lg->FindGeneratorTargetToUse(exe); |
125 | 0 | if (target && target->GetType() == cmStateEnums::EXECUTABLE && |
126 | 0 | !target->IsImported()) { |
127 | 0 | dependencies.insert(target); |
128 | 0 | } |
129 | 0 | } |
130 | | |
131 | | // Add dependencies from BUILD_DEPENDS keyword |
132 | 0 | for (auto const& depName : this->Test->GetDependencies()) { |
133 | 0 | if (depName.empty()) { |
134 | 0 | continue; |
135 | 0 | } |
136 | 0 | cmGeneratorTarget* depTarget = lg->FindGeneratorTargetToUse(depName); |
137 | 0 | if (!depTarget) { |
138 | 0 | info.Files.push_back(depName); |
139 | 0 | continue; |
140 | 0 | } |
141 | 0 | if (depTarget->IsImported()) { |
142 | 0 | lg->GetMakefile()->IssueMessage( |
143 | 0 | MessageType::FATAL_ERROR, |
144 | 0 | cmStrCat("Test \"", this->Test->GetName(), "\" DEPENDS target \"", |
145 | 0 | depName, "\" which is imported and cannot be built."), |
146 | 0 | this->Test->GetBacktrace()); |
147 | 0 | return false; |
148 | 0 | } |
149 | 0 | dependencies.insert(depTarget); |
150 | 0 | } |
151 | | |
152 | 0 | info.Targets.insert(info.Targets.end(), dependencies.begin(), |
153 | 0 | dependencies.end()); |
154 | 0 | return true; |
155 | 0 | } |
156 | | |
157 | | void cmTestGenerator::GenerateScriptActions(std::ostream& os, Indent indent) |
158 | 0 | { |
159 | 0 | if (this->ActionsPerConfig) { |
160 | | // This is the per-config generation in a single-configuration |
161 | | // build generator case. The superclass will call our per-config |
162 | | // method. |
163 | 0 | this->cmScriptGenerator::GenerateScriptActions(os, indent); |
164 | 0 | } else { |
165 | | // This is an old-style test, so there is only one config. |
166 | | // assert(this->Test->GetOldStyle()); |
167 | 0 | this->GenerateOldStyle(os, indent); |
168 | 0 | } |
169 | 0 | } |
170 | | |
171 | | void cmTestGenerator::GenerateCommand(std::ostream& os, |
172 | | std::vector<std::string> const& command, |
173 | | std::string const& config, bool expand, |
174 | | cmGeneratorExpression& ge, |
175 | | cmPolicies::PolicyStatus cmp0158, |
176 | | cmPolicies::PolicyStatus cmp0178) |
177 | 0 | { |
178 | | // Evaluate command line arguments |
179 | 0 | cmList argv{ |
180 | 0 | this->EvaluateCommandLineArguments(command, ge, config), |
181 | | // Expand arguments if COMMAND_EXPAND_LISTS is set |
182 | 0 | expand ? cmList::ExpandElements::Yes : cmList::ExpandElements::No, |
183 | 0 | cmList::EmptyElements::Yes, |
184 | 0 | }; |
185 | | // Expanding lists on an empty command may have left it empty |
186 | 0 | if (argv.empty()) { |
187 | 0 | argv.emplace_back(); |
188 | 0 | } |
189 | | |
190 | | // Check whether the command executable is a target whose name is to |
191 | | // be translated. |
192 | 0 | std::string exe = argv[0]; |
193 | 0 | cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe); |
194 | 0 | if (target && target->GetType() == cmStateEnums::EXECUTABLE) { |
195 | | // Use the target file on disk. |
196 | 0 | exe = target->GetFullPath(config); |
197 | |
|
198 | 0 | auto addLauncher = [&](std::string const& propertyName) { |
199 | 0 | cmValue launcher = target->GetProperty(propertyName); |
200 | 0 | if (!cmNonempty(launcher)) { |
201 | 0 | return; |
202 | 0 | } |
203 | 0 | auto const propVal = ge.Parse(*launcher)->Evaluate(this->LG, config); |
204 | 0 | cmList launcherWithArgs(propVal, cmList::ExpandElements::Yes, |
205 | 0 | cmp0178 == cmPolicies::NEW |
206 | 0 | ? cmList::EmptyElements::Yes |
207 | 0 | : cmList::EmptyElements::No); |
208 | 0 | if (!launcherWithArgs.empty() && !launcherWithArgs[0].empty()) { |
209 | 0 | if (cmp0178 == cmPolicies::WARN) { |
210 | 0 | cmList argsWithEmptyValuesPreserved( |
211 | 0 | propVal, cmList::ExpandElements::Yes, cmList::EmptyElements::Yes); |
212 | 0 | if (launcherWithArgs != argsWithEmptyValuesPreserved) { |
213 | 0 | this->LG->GetMakefile()->IssuePolicyWarning( |
214 | 0 | cmPolicies::CMP0178, |
215 | 0 | cmStrCat("The ", propertyName, " property of target '", |
216 | 0 | target->GetName(), |
217 | 0 | "' contains empty list items. Those empty items are " |
218 | 0 | "being silently discarded to preserve backward " |
219 | 0 | "compatibility.")); |
220 | 0 | } |
221 | 0 | } |
222 | 0 | std::string launcherExe(launcherWithArgs[0]); |
223 | 0 | cmSystemTools::ConvertToUnixSlashes(launcherExe); |
224 | 0 | os << cmScriptGenerator::Quote(launcherExe) << " "; |
225 | 0 | for (std::string const& arg : |
226 | 0 | cmMakeRange(launcherWithArgs).advance(1)) { |
227 | 0 | os << cmScriptGenerator::Quote(arg) << " "; |
228 | 0 | } |
229 | 0 | } |
230 | 0 | }; |
231 | | |
232 | | // Prepend with the test launcher if specified. |
233 | 0 | addLauncher("TEST_LAUNCHER"); |
234 | | |
235 | | // Prepend with the emulator when cross compiling if required. |
236 | 0 | if (cmp0158 != cmPolicies::NEW || |
237 | 0 | this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) { |
238 | 0 | addLauncher("CROSSCOMPILING_EMULATOR"); |
239 | 0 | } |
240 | 0 | } else { |
241 | | // Use the command name given. |
242 | 0 | cmSystemTools::ConvertToUnixSlashes(exe); |
243 | 0 | } |
244 | | |
245 | | // Generate the command line with full escapes. |
246 | 0 | os << cmScriptGenerator::Quote(exe); |
247 | |
|
248 | 0 | for (auto const& arg : cmMakeRange(argv).advance(1)) { |
249 | 0 | os << " " << cmScriptGenerator::Quote(arg); |
250 | 0 | } |
251 | 0 | } |
252 | | |
253 | | void cmTestGenerator::GenerateScriptForConfig(std::ostream& os, |
254 | | std::string const& config, |
255 | | Indent indent) |
256 | 0 | { |
257 | 0 | this->TestGenerated = true; |
258 | | |
259 | | // Set up generator expression evaluation context. |
260 | 0 | cmGeneratorExpression ge(*this->Test->GetMakefile()->GetCMakeInstance(), |
261 | 0 | this->Test->GetBacktrace()); |
262 | |
|
263 | 0 | auto const test_name = TestName(this->Test); |
264 | 0 | os << indent << "add_test(" << test_name << ' '; |
265 | 0 | this->GenerateCommand( |
266 | 0 | os, this->Test->GetCommand(), config, this->Test->GetCommandExpandLists(), |
267 | 0 | ge, this->GetTest()->GetCMP0158(), this->Test->GetCMP0178()); |
268 | 0 | os << ")\n"; |
269 | | |
270 | | // Output properties for the test. |
271 | 0 | os << indent << "set_tests_properties(" << test_name << " PROPERTIES "; |
272 | 0 | for (auto const& i : this->Test->GetProperties().GetList()) { |
273 | 0 | os << " " << i.first << " " |
274 | 0 | << cmScriptGenerator::Quote( |
275 | 0 | ge.Parse(i.second)->Evaluate(this->LG, config)); |
276 | 0 | } |
277 | 0 | os << ' '; |
278 | 0 | this->GenerateBacktrace(os, this->Test->GetBacktrace()); |
279 | 0 | os << ")\n"; |
280 | 0 | } |
281 | | |
282 | | void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent) |
283 | 0 | { |
284 | 0 | os << indent << "add_test(" << TestName(this->Test) << " NOT_AVAILABLE)\n"; |
285 | 0 | } |
286 | | |
287 | | bool cmTestGenerator::NeedsScriptNoConfig() const |
288 | 0 | { |
289 | 0 | return (this->TestGenerated && // test generated for at least one config |
290 | 0 | this->ActionsPerConfig && // test is config-aware |
291 | 0 | this->Configurations.empty() && // test runs in all configs |
292 | 0 | !this->ConfigurationTypes->empty()); // config-dependent command |
293 | 0 | } |
294 | | |
295 | | void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent) |
296 | 0 | { |
297 | 0 | this->TestGenerated = true; |
298 | |
|
299 | 0 | auto const test_name = TestName(this->Test); |
300 | | |
301 | | // Get the test command line to be executed. |
302 | 0 | std::vector<std::string> const& command = this->Test->GetCommand(); |
303 | |
|
304 | 0 | std::string exe = command[0]; |
305 | 0 | cmSystemTools::ConvertToUnixSlashes(exe); |
306 | 0 | fout << indent << "add_test(" << test_name << " \"" << exe << "\""; |
307 | |
|
308 | 0 | for (std::string const& arg : cmMakeRange(command).advance(1)) { |
309 | | // Just double-quote all arguments so they are re-parsed |
310 | | // correctly by the test system. |
311 | 0 | fout << " \""; |
312 | 0 | for (char c : arg) { |
313 | | // Escape quotes within arguments. We should escape |
314 | | // backslashes too but we cannot because it makes the result |
315 | | // inconsistent with previous behavior of this command. |
316 | 0 | if (c == '"') { |
317 | 0 | fout << '\\'; |
318 | 0 | } |
319 | 0 | fout << c; |
320 | 0 | } |
321 | 0 | fout << '"'; |
322 | 0 | } |
323 | 0 | fout << ")\n"; |
324 | | |
325 | | // Output properties for the test. |
326 | 0 | fout << indent << "set_tests_properties(" << test_name << " PROPERTIES "; |
327 | 0 | for (auto const& i : this->Test->GetProperties().GetList()) { |
328 | 0 | fout << " " << i.first << " " << cmScriptGenerator::Quote(i.second); |
329 | 0 | } |
330 | 0 | fout << ' '; |
331 | 0 | this->GenerateBacktrace(fout, this->Test->GetBacktrace()); |
332 | 0 | fout << ")\n"; |
333 | 0 | } |
334 | | |
335 | | void cmTestGenerator::GenerateBacktrace(std::ostream& os, |
336 | | cmListFileBacktrace bt) |
337 | 0 | { |
338 | 0 | if (bt.Empty()) { |
339 | 0 | return; |
340 | 0 | } |
341 | | |
342 | 0 | os << "_BACKTRACE_TRIPLES \""; |
343 | |
|
344 | 0 | bool prependTripleSeparator = false; |
345 | 0 | while (!bt.Empty()) { |
346 | 0 | auto const& entry = bt.Top(); |
347 | 0 | if (prependTripleSeparator) { |
348 | 0 | os << ";"; |
349 | 0 | } |
350 | 0 | os << entry.FilePath << ";" << entry.Line << ";" << entry.Name; |
351 | 0 | bt = bt.Pop(); |
352 | 0 | prependTripleSeparator = true; |
353 | 0 | } |
354 | |
|
355 | 0 | os << '"'; |
356 | 0 | } |
357 | | |
358 | | std::vector<std::string> cmTestGenerator::EvaluateCommandLineArguments( |
359 | | std::vector<std::string> const& argv, cmGeneratorExpression& ge, |
360 | | std::string const& config) const |
361 | 0 | { |
362 | | // Evaluate executable name and arguments |
363 | 0 | auto evaluatedRange = |
364 | 0 | cmMakeRange(argv).transform([&](std::string const& arg) { |
365 | 0 | return ge.Parse(arg)->Evaluate(this->LG, config); |
366 | 0 | }); |
367 | |
|
368 | 0 | return { evaluatedRange.begin(), evaluatedRange.end() }; |
369 | 0 | } |