/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 "cmPolicies.h" |
21 | | #include "cmPropertyMap.h" |
22 | | #include "cmRange.h" |
23 | | #include "cmScriptGenerator.h" |
24 | | #include "cmStringAlgorithms.h" |
25 | | #include "cmSystemTools.h" |
26 | | #include "cmTargetTypes.h" |
27 | | #include "cmTest.h" |
28 | | #include "cmValue.h" |
29 | | |
30 | | namespace /* anonymous */ |
31 | | { |
32 | | |
33 | | bool needToQuoteTestName(cmMakefile const& mf, std::string const& name) |
34 | 0 | { |
35 | | // Determine if policy CMP0110 is set to NEW. |
36 | 0 | switch (mf.GetPolicyStatus(cmPolicies::CMP0110)) { |
37 | 0 | case cmPolicies::WARN: |
38 | | // Only warn if a forbidden character is used in the name. |
39 | 0 | if (name.find_first_of("$[] #;\t\n\"\\") != std::string::npos) { |
40 | 0 | mf.IssuePolicyWarning( |
41 | 0 | cmPolicies::CMP0110, {}, |
42 | 0 | cmStrCat("The following name given to add_test() is invalid if " |
43 | 0 | "CMP0110 is not set or set to OLD:\n `", |
44 | 0 | name, "ยด\n")); |
45 | 0 | } |
46 | 0 | CM_FALLTHROUGH; |
47 | 0 | case cmPolicies::OLD: |
48 | | // OLD behavior is to not quote the test's name. |
49 | 0 | return false; |
50 | 0 | case cmPolicies::NEW: |
51 | 0 | default: |
52 | | // NEW behavior is to quote the test's name. |
53 | 0 | return true; |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | std::string TestName(cmTest* test) |
58 | 0 | { |
59 | 0 | std::string name = test->GetName(); |
60 | 0 | if (needToQuoteTestName(*test->GetMakefile(), name)) { |
61 | 0 | name = cmScriptGenerator::Quote(name); |
62 | 0 | } |
63 | 0 | return name; |
64 | 0 | } |
65 | | |
66 | | } // End: anonymous namespace |
67 | | |
68 | | cmTestGenerator::cmTestGenerator( |
69 | | cmTest* test, std::vector<std::string> const& configurations) |
70 | 0 | : cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations) |
71 | 0 | , Test(test) |
72 | 0 | { |
73 | 0 | this->ActionsPerConfig = test == nullptr || !test->GetOldStyle(); |
74 | 0 | this->TestGenerated = false; |
75 | 0 | this->LG = nullptr; |
76 | 0 | } |
77 | | |
78 | 0 | cmTestGenerator::~cmTestGenerator() = default; |
79 | | |
80 | | void cmTestGenerator::Compute(cmLocalGenerator* lg) |
81 | 0 | { |
82 | 0 | this->LG = lg; |
83 | 0 | } |
84 | | |
85 | | bool cmTestGenerator::TestsForConfig(std::string const& config) |
86 | 0 | { |
87 | 0 | return this->Test != nullptr && this->GeneratesForConfig(config); |
88 | 0 | } |
89 | | |
90 | | cmTest* cmTestGenerator::GetTest() const |
91 | 0 | { |
92 | 0 | return this->Test; |
93 | 0 | } |
94 | | |
95 | | bool cmTestGenerator::EvaluateBuildDependencies( |
96 | | cmLocalGenerator* lg, std::string const& config, |
97 | | cmListFileBacktrace const& backtrace, std::string const& testName, |
98 | | std::vector<std::string> const& command, |
99 | | std::vector<std::string> const& buildDepends, BuildDependencies& info, |
100 | | cmMakefile* mf) |
101 | 0 | { |
102 | 0 | if (!cmGeneratorExpression::IsValidTargetName(testName) || |
103 | 0 | cmGlobalGenerator::IsReservedTarget(testName)) { |
104 | 0 | return false; |
105 | 0 | } |
106 | | |
107 | 0 | std::set<cmGeneratorTarget*> dependencies; |
108 | | |
109 | | // Get dependencies from generator expressions |
110 | 0 | cmGeneratorExpression ge(*mf->GetCMakeInstance(), backtrace); |
111 | 0 | std::string exe; |
112 | 0 | for (std::string const& arg : command) { |
113 | 0 | auto parsed = ge.Parse(arg); |
114 | 0 | std::string const& evaluated = parsed->Evaluate(lg, config); |
115 | 0 | if (exe.empty()) { |
116 | 0 | exe = evaluated; |
117 | 0 | } |
118 | 0 | for (cmGeneratorTarget* dep : parsed->GetTargets()) { |
119 | 0 | if (dep && !dep->IsImported()) { |
120 | 0 | dependencies.insert(dep); |
121 | 0 | } |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | | // Add target executed by test |
126 | 0 | if (!exe.empty()) { |
127 | 0 | cmGeneratorTarget* target = lg->FindGeneratorTargetToUse(exe); |
128 | 0 | if (target && target->GetType() == cm::TargetType::EXECUTABLE && |
129 | 0 | !target->IsImported()) { |
130 | 0 | dependencies.insert(target); |
131 | 0 | } |
132 | 0 | } |
133 | | |
134 | | // Add dependencies from BUILD_DEPENDS keyword |
135 | 0 | for (std::string const& depName : buildDepends) { |
136 | 0 | if (depName.empty()) { |
137 | 0 | continue; |
138 | 0 | } |
139 | 0 | cmGeneratorTarget* depTarget = lg->FindGeneratorTargetToUse(depName); |
140 | 0 | if (!depTarget) { |
141 | 0 | cmGlobalGenerator* gg = lg->GetGlobalGenerator(); |
142 | 0 | BuildDependencies::FileDependency file; |
143 | 0 | file.Path = lg->GetRealFileDependency(depName); |
144 | 0 | file.Owner = gg->FindOutputOwningTarget(file.Path); |
145 | 0 | file.Generated = gg->IsGeneratedByBuild(file.Path); |
146 | 0 | info.Files.push_back(std::move(file)); |
147 | 0 | continue; |
148 | 0 | } |
149 | 0 | dependencies.insert(depTarget); |
150 | 0 | } |
151 | |
|
152 | 0 | for (cmGeneratorTarget* gt : dependencies) { |
153 | 0 | if (gt->IsInBuildSystem()) { |
154 | 0 | info.Targets.push_back(gt); |
155 | 0 | } |
156 | 0 | } |
157 | 0 | return true; |
158 | 0 | } |
159 | | |
160 | | bool cmTestGenerator::GetBuildDependencies(cmLocalGenerator* lg, |
161 | | std::string const& config, |
162 | | BuildDependencies& info) |
163 | 0 | { |
164 | 0 | if (this->Test == nullptr) { |
165 | 0 | return false; |
166 | 0 | } |
167 | 0 | return EvaluateBuildDependencies( |
168 | 0 | lg, config, this->Test->GetBacktrace(), this->Test->GetName(), |
169 | 0 | this->Test->GetCommand(), this->Test->GetDependencies(), info, |
170 | 0 | this->Test->GetMakefile()); |
171 | 0 | } |
172 | | |
173 | | void cmTestGenerator::GenerateScriptActions(std::ostream& os, Indent indent) |
174 | 0 | { |
175 | 0 | if (this->ActionsPerConfig) { |
176 | | // This is the per-config generation in a single-configuration |
177 | | // build generator case. The superclass will call our per-config |
178 | | // method. |
179 | 0 | this->cmScriptGenerator::GenerateScriptActions(os, indent); |
180 | 0 | } else { |
181 | | // This is an old-style test, so there is only one config. |
182 | | // assert(this->Test->GetOldStyle()); |
183 | 0 | this->GenerateOldStyle(os, indent); |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | void cmTestGenerator::GenerateCommand(std::ostream& os, |
188 | | std::vector<std::string> const& command, |
189 | | std::string const& config, bool expand, |
190 | | cmGeneratorExpression& ge, |
191 | | cmPolicies::PolicyStatus cmp0158, |
192 | | cmPolicies::PolicyStatus cmp0178) |
193 | 0 | { |
194 | | // Evaluate command line arguments |
195 | 0 | cmList argv{ |
196 | 0 | this->EvaluateCommandLineArguments(command, ge, config), |
197 | | // Expand arguments if COMMAND_EXPAND_LISTS is set |
198 | 0 | expand ? cmList::ExpandElements::Yes : cmList::ExpandElements::No, |
199 | 0 | cmList::EmptyElements::Yes, |
200 | 0 | }; |
201 | | // Expanding lists on an empty command may have left it empty |
202 | 0 | if (argv.empty()) { |
203 | 0 | argv.emplace_back(); |
204 | 0 | } |
205 | | |
206 | | // Check whether the command executable is a target whose name is to |
207 | | // be translated. |
208 | 0 | std::string exe = argv[0]; |
209 | 0 | cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe); |
210 | 0 | if (target && target->GetType() == cm::TargetType::EXECUTABLE) { |
211 | | // Use the target file on disk. |
212 | 0 | exe = target->GetFullPath(config); |
213 | |
|
214 | 0 | auto addLauncher = [&](std::string const& propertyName) { |
215 | 0 | cmValue launcher = target->GetProperty(propertyName); |
216 | 0 | if (!cmNonempty(launcher)) { |
217 | 0 | return; |
218 | 0 | } |
219 | 0 | auto const propVal = ge.Parse(*launcher)->Evaluate(this->LG, config); |
220 | 0 | cmList launcherWithArgs(propVal, cmList::ExpandElements::Yes, |
221 | 0 | cmp0178 == cmPolicies::NEW |
222 | 0 | ? cmList::EmptyElements::Yes |
223 | 0 | : cmList::EmptyElements::No); |
224 | 0 | if (!launcherWithArgs.empty() && !launcherWithArgs[0].empty()) { |
225 | 0 | if (cmp0178 == cmPolicies::WARN) { |
226 | 0 | cmList argsWithEmptyValuesPreserved( |
227 | 0 | propVal, cmList::ExpandElements::Yes, cmList::EmptyElements::Yes); |
228 | 0 | if (launcherWithArgs != argsWithEmptyValuesPreserved) { |
229 | 0 | this->LG->GetMakefile()->IssuePolicyWarning( |
230 | 0 | cmPolicies::CMP0178, |
231 | 0 | cmStrCat("The ", propertyName, " property of target '", |
232 | 0 | target->GetName(), |
233 | 0 | "' contains empty list items. Those empty items are " |
234 | 0 | "being silently discarded to preserve backward " |
235 | 0 | "compatibility.")); |
236 | 0 | } |
237 | 0 | } |
238 | 0 | std::string launcherExe(launcherWithArgs[0]); |
239 | 0 | cmSystemTools::ConvertToUnixSlashes(launcherExe); |
240 | 0 | os << cmScriptGenerator::Quote(launcherExe) << " "; |
241 | 0 | for (std::string const& arg : |
242 | 0 | cmMakeRange(launcherWithArgs).advance(1)) { |
243 | 0 | os << cmScriptGenerator::Quote(arg) << " "; |
244 | 0 | } |
245 | 0 | } |
246 | 0 | }; |
247 | | |
248 | | // Prepend with the test launcher if specified. |
249 | 0 | addLauncher("TEST_LAUNCHER"); |
250 | | |
251 | | // Prepend with the emulator when cross compiling if required. |
252 | 0 | if (cmp0158 != cmPolicies::NEW || |
253 | 0 | this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) { |
254 | 0 | addLauncher("CROSSCOMPILING_EMULATOR"); |
255 | 0 | } |
256 | 0 | } else { |
257 | | // Use the command name given. |
258 | 0 | cmSystemTools::ConvertToUnixSlashes(exe); |
259 | 0 | } |
260 | | |
261 | | // Generate the command line with full escapes. |
262 | 0 | os << cmScriptGenerator::Quote(exe); |
263 | |
|
264 | 0 | for (auto const& arg : cmMakeRange(argv).advance(1)) { |
265 | 0 | os << " " << cmScriptGenerator::Quote(arg); |
266 | 0 | } |
267 | 0 | } |
268 | | |
269 | | void cmTestGenerator::GenerateScriptForConfig(std::ostream& os, |
270 | | std::string const& config, |
271 | | Indent indent) |
272 | 0 | { |
273 | 0 | this->TestGenerated = true; |
274 | | |
275 | | // Set up generator expression evaluation context. |
276 | 0 | cmGeneratorExpression ge(*this->Test->GetMakefile()->GetCMakeInstance(), |
277 | 0 | this->Test->GetBacktrace()); |
278 | |
|
279 | 0 | auto const test_name = TestName(this->Test); |
280 | 0 | os << indent << "add_test(" << test_name << ' '; |
281 | 0 | this->GenerateCommand( |
282 | 0 | os, this->Test->GetCommand(), config, this->Test->GetCommandExpandLists(), |
283 | 0 | ge, this->GetTest()->GetCMP0158(), this->Test->GetCMP0178()); |
284 | 0 | os << ")\n"; |
285 | | |
286 | | // Output properties for the test. |
287 | 0 | os << indent << "set_tests_properties(" << test_name << " PROPERTIES "; |
288 | 0 | for (auto const& i : this->Test->GetProperties().GetList()) { |
289 | 0 | os << " " << i.first << " " |
290 | 0 | << cmScriptGenerator::Quote( |
291 | 0 | ge.Parse(i.second)->Evaluate(this->LG, config)); |
292 | 0 | } |
293 | 0 | BuildDependencies deps; |
294 | 0 | if (this->GetBuildDependencies(this->LG, config, deps)) { |
295 | 0 | cmList depList; |
296 | 0 | for (std::string const& dep : |
297 | 0 | this->LG->GetGlobalGenerator()->GetTestBuildDependencyPaths(config, |
298 | 0 | deps)) { |
299 | 0 | depList.append(dep); |
300 | 0 | } |
301 | 0 | os << " _CMAKE_TEST_BUILD_DEPENDS " |
302 | 0 | << cmScriptGenerator::Quote(depList.to_string()); |
303 | 0 | } |
304 | 0 | this->GenerateDefaultFixtureRepeatMode(os); |
305 | 0 | os << ' '; |
306 | 0 | this->GenerateBacktrace(os, this->Test->GetBacktrace()); |
307 | 0 | os << ")\n"; |
308 | 0 | } |
309 | | |
310 | | void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent) |
311 | 0 | { |
312 | 0 | os << indent << "add_test(" << TestName(this->Test) << " NOT_AVAILABLE)\n"; |
313 | 0 | } |
314 | | |
315 | | bool cmTestGenerator::NeedsScriptNoConfig() const |
316 | 0 | { |
317 | 0 | return (this->TestGenerated && // test generated for at least one config |
318 | 0 | this->ActionsPerConfig && // test is config-aware |
319 | 0 | this->Configurations.empty() && // test runs in all configs |
320 | 0 | !this->ConfigurationTypes->empty()); // config-dependent command |
321 | 0 | } |
322 | | |
323 | | void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent) |
324 | 0 | { |
325 | 0 | this->TestGenerated = true; |
326 | |
|
327 | 0 | auto const test_name = TestName(this->Test); |
328 | | |
329 | | // Get the test command line to be executed. |
330 | 0 | std::vector<std::string> const& command = this->Test->GetCommand(); |
331 | |
|
332 | 0 | std::string exe = command[0]; |
333 | 0 | cmSystemTools::ConvertToUnixSlashes(exe); |
334 | 0 | fout << indent << "add_test(" << test_name << " \"" << exe << "\""; |
335 | |
|
336 | 0 | for (std::string const& arg : cmMakeRange(command).advance(1)) { |
337 | | // Just double-quote all arguments so they are re-parsed |
338 | | // correctly by the test system. |
339 | 0 | fout << " \""; |
340 | 0 | for (char c : arg) { |
341 | | // Escape quotes within arguments. We should escape |
342 | | // backslashes too but we cannot because it makes the result |
343 | | // inconsistent with previous behavior of this command. |
344 | 0 | if (c == '"') { |
345 | 0 | fout << '\\'; |
346 | 0 | } |
347 | 0 | fout << c; |
348 | 0 | } |
349 | 0 | fout << '"'; |
350 | 0 | } |
351 | 0 | fout << ")\n"; |
352 | | |
353 | | // Output properties for the test. |
354 | 0 | fout << indent << "set_tests_properties(" << test_name << " PROPERTIES "; |
355 | 0 | for (auto const& i : this->Test->GetProperties().GetList()) { |
356 | 0 | fout << " " << i.first << " " << cmScriptGenerator::Quote(i.second); |
357 | 0 | } |
358 | 0 | this->GenerateDefaultFixtureRepeatMode(fout); |
359 | 0 | fout << ' '; |
360 | 0 | this->GenerateBacktrace(fout, this->Test->GetBacktrace()); |
361 | 0 | fout << ")\n"; |
362 | 0 | } |
363 | | |
364 | | void cmTestGenerator::GenerateDefaultFixtureRepeatMode(std::ostream& os) |
365 | 0 | { |
366 | | // Nothing to choose for a test that is not part of a fixture, or that |
367 | | // names a mode itself. |
368 | 0 | if (this->Test->GetProperty("FIXTURE_REPEAT_MODE") || |
369 | 0 | (!this->Test->GetProperty("FIXTURES_SETUP") && |
370 | 0 | !this->Test->GetProperty("FIXTURES_CLEANUP"))) { |
371 | 0 | return; |
372 | 0 | } |
373 | | |
374 | | // Write the mode the policy chose into the test file, so that ctest reads |
375 | | // a mode rather than the policy settings behind it. Only NEW needs |
376 | | // writing: with nothing written, ctest already uses the |
377 | | // EACH_TEST_SEPARATELY behavior of CMake 4.4 and below. |
378 | 0 | switch (this->Test->GetCMP0224()) { |
379 | 0 | case cmPolicies::WARN: |
380 | | // Warn only on request. Fixtures are common, and the choice of mode |
381 | | // matters only to those who run ctest --repeat. Collect the tests |
382 | | // rather than warning about each: a project that sets its fixtures up |
383 | | // in an add_test() wrapper would fill the console. |
384 | 0 | if (this->Test->GetMakefile()->PolicyOptionalWarningEnabled( |
385 | 0 | "CMAKE_POLICY_WARNING_CMP0224")) { |
386 | 0 | this->LG->GetGlobalGenerator()->AddCMP0224WarnTest( |
387 | 0 | this->Test->GetName()); |
388 | 0 | } |
389 | 0 | CM_FALLTHROUGH; |
390 | 0 | case cmPolicies::OLD: |
391 | 0 | break; |
392 | 0 | case cmPolicies::NEW: |
393 | 0 | os << " _CMAKE_DEFAULT_FIXTURE_REPEAT_MODE AROUND_EACH_REPEAT"; |
394 | 0 | break; |
395 | 0 | } |
396 | 0 | } |
397 | | |
398 | | void cmTestGenerator::GenerateBacktrace(std::ostream& os, |
399 | | cmListFileBacktrace bt) |
400 | 0 | { |
401 | 0 | if (bt.Empty()) { |
402 | 0 | return; |
403 | 0 | } |
404 | | |
405 | 0 | os << "_BACKTRACE_TRIPLES \""; |
406 | |
|
407 | 0 | bool prependTripleSeparator = false; |
408 | 0 | while (!bt.Empty()) { |
409 | 0 | auto const& entry = bt.Top(); |
410 | 0 | if (prependTripleSeparator) { |
411 | 0 | os << ";"; |
412 | 0 | } |
413 | 0 | os << entry.FilePath << ";" << entry.Line << ";" << entry.Name; |
414 | 0 | bt = bt.Pop(); |
415 | 0 | prependTripleSeparator = true; |
416 | 0 | } |
417 | |
|
418 | 0 | os << '"'; |
419 | 0 | } |
420 | | |
421 | | std::vector<std::string> cmTestGenerator::EvaluateCommandLineArguments( |
422 | | std::vector<std::string> const& argv, cmGeneratorExpression& ge, |
423 | | std::string const& config) const |
424 | 0 | { |
425 | | // Evaluate executable name and arguments |
426 | 0 | auto evaluatedRange = |
427 | 0 | cmMakeRange(argv).transform([&](std::string const& arg) { |
428 | 0 | return ge.Parse(arg)->Evaluate(this->LG, config); |
429 | 0 | }); |
430 | |
|
431 | 0 | return { evaluatedRange.begin(), evaluatedRange.end() }; |
432 | 0 | } |